Skip to content

Commit b2a2966

Browse files
kanard38knard38
authored andcommitted
DAOS-18304 ddb: Add unit test to ddb code
Refactoring DDB code to allow unit testing of the go part code: - The GO interface DdbApi is introduced to allow the use of GO test stub. - Refactor initialisation of the C structure from the Go Code to allow the use of GO test stub. - The useless C attributes dc_pool_path and dc_db_path are removed. Following issues are also fixed: - Remove invalid restrictions on pool file path size. - Improper cleanup of the function ddb_run_feature(). - Fix invalid return code with non-interactive mode - Fix rm_pool command: - Add db_path parameter to pool destroy command. - Makes vos_path parameter mandatory to pool destroy command. - Cleanup of the ddb error message with invalid command. Features: recovery Signed-off-by: Cedric Koch-Hofer <cedric.koch-hofer@hpe.com>
1 parent be64b89 commit b2a2966

2 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/control/cmd/ddb/commands_wrapper.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,18 @@ func (ctx *DdbContext) Feature(path, db_path, enable, disable string, show bool)
270270
options.db_path = C.CString(db_path)
271271
defer freeString(options.db_path)
272272
if enable != "" {
273-
err := daosError(C.ddb_feature_string2flags(&ctx.ctx, C.CString(enable),
273+
cEnable := C.CString(enable)
274+
defer freeString(cEnable)
275+
err := daosError(C.ddb_feature_string2flags(&ctx.ctx, cEnable,
274276
&options.set_compat_flags, &options.set_incompat_flags))
275277
if err != nil {
276278
return err
277279
}
278280
}
279281
if disable != "" {
280-
err := daosError(C.ddb_feature_string2flags(&ctx.ctx, C.CString(disable),
282+
cDisable := C.CString(disable)
283+
defer freeString(cDisable)
284+
err := daosError(C.ddb_feature_string2flags(&ctx.ctx, cDisable,
281285
&options.clear_compat_flags, &options.clear_incompat_flags))
282286
if err != nil {
283287
return err
@@ -336,8 +340,8 @@ func (ctx *DdbContext) DtxStat(path string, details bool) error {
336340
/* Set up the options */
337341
options := C.struct_dtx_stat_options{}
338342
options.path = C.CString(path)
339-
options.details = C.bool(details)
340343
defer freeString(options.path)
344+
options.details = C.bool(details)
341345
/* Run the c code command */
342346
return daosError(C.ddb_run_dtx_stat(&ctx.ctx, &options))
343347
}

src/control/cmd/ddb/main.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ import (
2929
"github.com/daos-stack/daos/src/control/server/engine"
3030
)
3131

32+
type unknownCmdError struct {
33+
cmd string
34+
}
35+
36+
func (e *unknownCmdError) Error() string {
37+
return fmt.Sprintf("Error running command '%s' unknown command, try 'help'", e.cmd)
38+
}
39+
3240
func exitWithError(err error) {
3341
cmdName := filepath.Base(os.Args[0])
3442
msg := fmt.Sprintf("ERROR: %s: %v", cmdName, err)
3543
if fault.HasResolution(err) {
3644
msg = fmt.Sprintf("%s (%s)", msg, fault.ShowResolutionFor(err))
3745
}
3846
fmt.Fprintln(os.Stderr, msg)
39-
if err.Error() == grumbleUnknownCmdErr {
47+
48+
if _, ok := err.(*unknownCmdError); ok {
4049
app := createGrumbleApp(nil)
4150
printCommands(os.Stderr, app)
4251
}
@@ -157,16 +166,9 @@ func printGeneralHelp(app *grumble.App, generalMsg string) {
157166
// Caveat: There is no known easy way of forcing grumble to use log to print the generated message
158167
// so the output goes directly to stdout.
159168
func printCmdHelp(app *grumble.App, opts *cliOptions) {
160-
err := runCmdStr(app, nil, string(opts.Args.RunCmd), "--help")
161-
if err == nil {
162-
// This branch should never be reached: printCmdHelp is only called when go-flags returns a
163-
// flags.ErrHelp error (see parseOpts), which means "--help" was recognized as a valid flag.
164-
// Grumble will therefore always return an error when attempting to run the command with "--help".
165-
fmt.Fprintf(os.Stderr, "FATAL: Unexpected: command help returned no error.\n")
166-
os.Exit(1)
169+
if err := runCmdStr(app, nil, string(opts.Args.RunCmd), "--help"); err != nil {
170+
exitWithError(&unknownCmdError{cmd: opts.Args.RunCmd})
167171
}
168-
fmt.Fprintf(os.Stderr, "%v\n", err)
169-
printCommands(os.Stderr, app)
170172
}
171173

172174
// Prints either general or command-specific help message.
@@ -319,16 +321,19 @@ func parseOpts(args []string, api DdbApi) error {
319321
app := createGrumbleApp(api)
320322

321323
if opts.VosPath != "" {
322-
prefixesToSkip := []string{"feature", "open", "close", "prov_mem", "smd_sync", "rm_pool", "dev_list", "dev_replace"}
323-
needsConnection := !func() bool {
324-
for _, prefix := range prefixesToSkip {
325-
if strings.HasPrefix(string(opts.Args.RunCmd), prefix) {
326-
return true
327-
}
328-
}
329-
return false
330-
}()
331-
if needsConnection {
324+
// Commands that manage the pool open/close lifecycle themselves and must
325+
// not have the pool pre-opened by the CLI layer.
326+
noAutoOpen := map[string]bool{
327+
"feature": true,
328+
"open": true,
329+
"close": true,
330+
"prov_mem": true,
331+
"smd_sync": true,
332+
"rm_pool": true,
333+
"dev_list": true,
334+
"dev_replace": true,
335+
}
336+
if !noAutoOpen[opts.Args.RunCmd] {
332337
log.Debugf("Connect to path: %s\n", opts.VosPath)
333338
if err := api.Open(string(opts.VosPath), string(opts.SysdbPath), opts.WriteMode); err != nil {
334339
return errors.Wrapf(err, vosPathOpenErr, opts.VosPath)
@@ -344,6 +349,9 @@ func parseOpts(args []string, api DdbApi) error {
344349
} else {
345350
err = runFileCmds(log, app, opts.CmdFile)
346351
}
352+
if err != nil && err.Error() == grumbleUnknownCmdErr {
353+
return &unknownCmdError{cmd: opts.Args.RunCmd}
354+
}
347355
return err
348356
}
349357

0 commit comments

Comments
 (0)