diff --git a/internal/app/wwctl/overlay/list/main.go b/internal/app/wwctl/overlay/list/main.go index ac02a619..daab2a93 100644 --- a/internal/app/wwctl/overlay/list/main.go +++ b/internal/app/wwctl/overlay/list/main.go @@ -1,7 +1,6 @@ package list import ( - "fmt" "os" "syscall" @@ -26,9 +25,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if ListLong { - fmt.Printf("%-10s %5s %-5s %-18s %s\n", "PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH") + wwlog.Info("%-10s %5s %-5s %-18s %s\n", "PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH") } else { - fmt.Printf("%-30s %-12s\n", "OVERLAY NAME", "FILES/DIRS") + wwlog.Info("%-30s %-12s\n", "OVERLAY NAME", "FILES/DIRS") } for o := range overlays { @@ -51,19 +50,19 @@ func CobraRunE(cmd *cobra.Command, args []string) error { sys := s.Sys() - fmt.Printf("%v %5d %-5d %-18s /%s\n", perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, overlays[o], files[file]) + wwlog.Info("%v %5d %-5d %-18s /%s\n", perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, overlays[o], files[file]) } } else if ListContents { var fileCount int for file := range files { - fmt.Printf("%-30s /%-12s\n", name, files[file]) + wwlog.Info("%-30s /%-12s\n", name, files[file]) fileCount++ } if fileCount == 0 { - fmt.Printf("%-30s %-12d\n", name, 0) + wwlog.Info("%-30s %-12d\n", name, 0) } } else { - fmt.Printf("%-30s %-12d\n", name, len(files)) + wwlog.Info("%-30s %-12d\n", name, len(files)) } } else { diff --git a/internal/app/wwctl/overlay/list/main_test.go b/internal/app/wwctl/overlay/list/main_test.go new file mode 100644 index 00000000..55e9d34e --- /dev/null +++ b/internal/app/wwctl/overlay/list/main_test.go @@ -0,0 +1,45 @@ +package list + +import ( + "bytes" + "path" + "testing" + + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/stretchr/testify/assert" + + "github.com/hpcng/warewulf/internal/pkg/testenv" + "github.com/hpcng/warewulf/internal/pkg/warewulfd" +) + +func Test_Overlay_List(t *testing.T) { + env := testenv.New(t) + wwconf := warewulfconf.Get() + env.WriteFileAbs(t, path.Join(wwconf.Paths.WWOverlaydir, "testoverlay/email.ww"), ` +{{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }} +`) + defer env.RemoveAll(t) + warewulfd.SetNoDaemon() + t.Run("overlay list", func(t *testing.T) { + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "testoverlay") + }) + t.Run("overlay list all", func(t *testing.T) { + baseCmd.SetArgs([]string{"-a"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "email.ww") + }) +} diff --git a/internal/app/wwctl/overlay/show/main.go b/internal/app/wwctl/overlay/show/main.go index 8475c85e..f201e548 100644 --- a/internal/app/wwctl/overlay/show/main.go +++ b/internal/app/wwctl/overlay/show/main.go @@ -3,7 +3,6 @@ package show import ( "bufio" "bytes" - "fmt" "os" "path" "path/filepath" @@ -43,7 +42,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { os.Exit(1) } - fmt.Print(string(f)) + wwlog.Info(string(f)) } else { if !util.IsFile(overlayFile) { wwlog.Debug("%s is not a file", overlayFile) @@ -105,7 +104,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Info("backupFile: %v\nwriteFile: %v", backupFile, writeFile) wwlog.Info("Filename: %s\n", destFileName) } - fmt.Print(outBuffer.String()) + wwlog.Info(outBuffer.String()) } return nil } diff --git a/internal/app/wwctl/overlay/show/main_test.go b/internal/app/wwctl/overlay/show/main_test.go new file mode 100644 index 00000000..3bd63a84 --- /dev/null +++ b/internal/app/wwctl/overlay/show/main_test.go @@ -0,0 +1,89 @@ +package show + +import ( + "bytes" + "path" + "testing" + + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/stretchr/testify/assert" + + "github.com/hpcng/warewulf/internal/pkg/testenv" + "github.com/hpcng/warewulf/internal/pkg/warewulfd" +) + +var ( + overlayCont = ` +{{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }} +` +) + +func Test_Overlay_List(t *testing.T) { + env := testenv.New(t) + wwconf := warewulfconf.Get() + env.WriteFile(t, "etc/warewulf/nodes.conf", + `WW_INTERNAL: 43 +nodeprofiles: + default: + tags: + email: admin@localhost + empty: {} +nodes: + node1: + tags: + email: admin@node1 + node2: {} + node3: + profiles: + - empty +`) + + env.WriteFileAbs(t, path.Join(wwconf.Paths.WWOverlaydir, "testoverlay/email.ww"), overlayCont) + defer env.RemoveAll(t) + warewulfd.SetNoDaemon() + t.Run("overlay show raw", func(t *testing.T) { + baseCmd.SetArgs([]string{"testoverlay", "email.ww"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), overlayCont) + }) + t.Run("overlay show rendered node tag", func(t *testing.T) { + baseCmd.SetArgs([]string{"-r", "node1", "testoverlay", "email.ww"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "admin@node1") + }) + t.Run("overlay show rendered profile tag", func(t *testing.T) { + baseCmd.SetArgs([]string{"-r", "node2", "testoverlay", "email.ww"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "admin@localhost") + }) + t.Run("overlay show no tag", func(t *testing.T) { + baseCmd.SetArgs([]string{"-r", "node3", "testoverlay", "email.ww"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "noMail") + }) +} diff --git a/internal/pkg/testenv/testenv.go b/internal/pkg/testenv/testenv.go index 15ea117a..9d86eb40 100644 --- a/internal/pkg/testenv/testenv.go +++ b/internal/pkg/testenv/testenv.go @@ -119,6 +119,18 @@ func (env *TestEnv) WriteFile(t *testing.T, fileName string, content string) { assert.NoError(t, err) } +// WriteFileAbs uses an absloute path in opposite to WriteFile +func (env *TestEnv) WriteFileAbs(t *testing.T, fileName string, content string) { + dirName := filepath.Dir(fileName) + err := os.MkdirAll(dirName, 0755) + assert.NoError(t, err) + f, err := os.Create(fileName) + assert.NoError(t, err) + defer f.Close() + _, err = f.WriteString(content) + assert.NoError(t, err) +} + // ReadFile returns the content of fileName as converted to a // string. //