added a test for wwctl node add

Signed-off-by: Christian Goll <cgoll@suse.de>
This commit is contained in:
Christian Goll
2023-03-07 08:58:29 +01:00
parent a422a1617c
commit 3367b67b9f
8 changed files with 177 additions and 5 deletions

View File

@@ -0,0 +1,89 @@
package add
import (
"bytes"
"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_Add(t *testing.T) {
t.Helper()
conf_yml := `
WW_INTERNAL: 0
`
nodes_yml := `
WW_INTERNAL: 43
`
conf := warewulfconf.New()
err := conf.Read([]byte(conf_yml))
assert.NoError(t, err)
db, err := node.TestNew([]byte(nodes_yml))
assert.NoError(t, err)
warewulfd.SetNoDaemon()
buf := new(bytes.Buffer)
baseCmd.SetOut(buf)
baseCmd.SetErr(buf)
tests := []struct {
name string
args []string
wantErr bool
stdout string
outDb string
}{
{name: "single node add",
args: []string{"n01"},
wantErr: false,
stdout: "",
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes:
n01:
profiles:
- default
network devices:
default: {}
`},
{name: "double node add",
args: []string{"n0[1-2]"},
wantErr: false,
stdout: "",
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes:
n01:
profiles:
- default
network devices:
default: {}
n02:
profiles:
- default
network devices:
default: {}
`},
}
for _, tt := range tests {
db, err = node.TestNew([]byte(nodes_yml))
assert.NoError(t, err)
t.Run(tt.name, func(t *testing.T) {
baseCmd.SetArgs(tt.args)
err = baseCmd.Execute()
if (err != nil) != tt.wantErr {
t.Errorf("Got unwanted error: %s", err)
return
}
dump := string(db.DBDump())
if dump != tt.outDb {
t.Errorf("DB dump is wrong, got:'%s'\nwant:'%s'", dump, tt.outDb)
return
}
if buf.String() != tt.stdout {
t.Errorf("Got wrong output, got:'%s'\nwant:'%s'", buf.String(), tt.stdout)
}
})
}
}

View File

@@ -77,7 +77,7 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) {
wwlog.SetLogLevel(LogLevel)
}
conf := warewulfconf.New()
if !AllowEmptyConf {
if !AllowEmptyConf && !conf.Initialized() {
if WarewulfConfArg != "" {
err = conf.ReadConf(WarewulfConfArg)
} else if os.Getenv("WAREWULFCONF") != "" {
@@ -86,7 +86,7 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) {
err = conf.ReadConf(warewulfconf.ConfigFile)
}
} else {
conf.SetDynamicDefaults()
err = conf.SetDynamicDefaults()
}
return
}