diff --git a/internal/app/wwctl/profile/list/main.go b/internal/app/wwctl/profile/list/main.go index 34791034..3a984baf 100644 --- a/internal/app/wwctl/profile/list/main.go +++ b/internal/app/wwctl/profile/list/main.go @@ -10,22 +10,24 @@ import ( "github.com/spf13/cobra" ) -func CobraRunE(cmd *cobra.Command, args []string) (err error) { - req := wwapiv1.GetProfileList{ - ShowAll: ShowAll, - Profiles: args, - } - profileInfo, err := apiprofile.ProfileList(&req) - if err != nil { +func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) { + return func(cmd *cobra.Command, args []string) (err error) { + req := wwapiv1.GetProfileList{ + ShowAll: vars.showAll, + Profiles: args, + } + profileInfo, err := apiprofile.ProfileList(&req) + if err != nil { + return + } + + if len(profileInfo.Output) > 0 { + ph := helper.NewPrintHelper(strings.Split(profileInfo.Output[0], "=")) + for _, val := range profileInfo.Output[1:] { + ph.Append(strings.Split(val, "=")) + } + ph.Render() + } return } - - if len(profileInfo.Output) > 0 { - ph := helper.NewPrintHelper(strings.Split(profileInfo.Output[0], "=")) - for _, val := range profileInfo.Output[1:] { - ph.Append(strings.Split(val, "=")) - } - ph.Render() - } - return } diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go new file mode 100644 index 00000000..39e9f187 --- /dev/null +++ b/internal/app/wwctl/profile/list/main_test.go @@ -0,0 +1,82 @@ +package list + +import ( + "bytes" + "io" + "os" + "strings" + "testing" + + "github.com/hpcng/warewulf/internal/pkg/node" + "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + "github.com/hpcng/warewulf/internal/pkg/warewulfd" + "github.com/stretchr/testify/assert" +) + +func Test_List(t *testing.T) { + tests := []struct { + name string + args []string + stdout string + inDb string + }{ + { + name: "profile list test", + args: []string{}, + stdout: `PROFILE NAME COMMENT/DESCRIPTION + default --`, + inDb: `WW_INTERNAL: 43 +nodeprofiles: + default: {} +nodes: + n01: + profiles: + - default +`, + }, + } + + conf_yml := ` +WW_INTERNAL: 0 + ` + + conf := warewulfconf.New() + err := conf.Read([]byte(conf_yml)) + assert.NoError(t, err) + warewulfd.SetNoDaemon() + for _, tt := range tests { + _, err = node.TestNew([]byte(tt.inDb)) + assert.NoError(t, err) + t.Logf("Running test: %s\n", tt.name) + t.Run(tt.name, func(t *testing.T) { + baseCmd := GetCommand() + baseCmd.SetArgs(tt.args) + baseCmd.SetOut(nil) + baseCmd.SetErr(nil) + stdoutR, stdoutW, _ := os.Pipe() + os.Stdout = stdoutW + err = baseCmd.Execute() + if err != nil { + t.Errorf("Received error when running command, err: %v", err) + t.FailNow() + } + stdoutC := make(chan string) + go func() { + var buf bytes.Buffer + _, _ = io.Copy(&buf, stdoutR) + stdoutC <- buf.String() + }() + stdoutW.Close() + + stdout := <-stdoutC + stdout = strings.TrimSpace(stdout) + stdout = strings.ReplaceAll(stdout, " ", "") + assert.NotEmpty(t, stdout, "os.stdout should not be empty") + tt.stdout = strings.ReplaceAll(strings.TrimSpace(tt.stdout), " ", "") + if stdout != strings.ReplaceAll(strings.TrimSpace(tt.stdout), " ", "") { + t.Errorf("Got wrong output, got:\n '%s'\n, but want:\n '%s'\n", stdout, tt.stdout) + t.FailNow() + } + }) + } +} diff --git a/internal/app/wwctl/profile/list/root.go b/internal/app/wwctl/profile/list/root.go index 1144d5c2..14689314 100644 --- a/internal/app/wwctl/profile/list/root.go +++ b/internal/app/wwctl/profile/list/root.go @@ -2,23 +2,22 @@ package list import "github.com/spf13/cobra" -var ( - baseCmd = &cobra.Command{ - DisableFlagsInUseLine: true, - Use: "list [OPTIONS] [PROFILE ...]", - Short: "List profiles and configurations", - Long: "This command will display configurations for PROFILE.", - RunE: CobraRunE, - Aliases: []string{"ls"}, - } - ShowAll bool -) - -func init() { - baseCmd.PersistentFlags().BoolVarP(&ShowAll, "all", "a", false, "Show all node configurations") +type variables struct { + showAll bool } // GetRootCommand returns the root cobra.Command for the application. func GetCommand() *cobra.Command { + vars := variables{} + baseCmd := &cobra.Command{ + DisableFlagsInUseLine: true, + Use: "list [OPTIONS] [PROFILE ...]", + Short: "List profiles and configurations", + Long: "This command will display configurations for PROFILE.", + RunE: CobraRunE(&vars), + Aliases: []string{"ls"}, + } + baseCmd.PersistentFlags().BoolVarP(&vars.showAll, "all", "a", false, "Show all node configurations") + return baseCmd }