added explicit tests for overlay commands
Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
committed by
Jonathon Anderson
parent
7f502c1f8a
commit
5d25b3a4a6
@@ -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 {
|
||||
|
||||
45
internal/app/wwctl/overlay/list/main_test.go
Normal file
45
internal/app/wwctl/overlay/list/main_test.go
Normal file
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
89
internal/app/wwctl/overlay/show/main_test.go
Normal file
89
internal/app/wwctl/overlay/show/main_test.go
Normal file
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user