added test for node list
Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
@@ -9,27 +9,29 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
req := wwapiv1.GetNodeList{
|
||||
Nodes: args,
|
||||
Type: wwapiv1.GetNodeList_Simple,
|
||||
}
|
||||
if ShowAll {
|
||||
req.Type = wwapiv1.GetNodeList_All
|
||||
} else if ShowIpmi {
|
||||
req.Type = wwapiv1.GetNodeList_Ipmi
|
||||
} else if ShowNet {
|
||||
req.Type = wwapiv1.GetNodeList_Network
|
||||
} else if ShowLong {
|
||||
req.Type = wwapiv1.GetNodeList_Long
|
||||
}
|
||||
nodeInfo, err := apinode.NodeList(&req)
|
||||
if len(nodeInfo.Output) > 0 {
|
||||
ph := helper.NewPrintHelper(strings.Split(nodeInfo.Output[0], "="))
|
||||
for _, val := range nodeInfo.Output[1:] {
|
||||
ph.Append(strings.Split(val, "="))
|
||||
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
|
||||
return func(cmd *cobra.Command, args []string) (err error) {
|
||||
req := wwapiv1.GetNodeList{
|
||||
Nodes: args,
|
||||
Type: wwapiv1.GetNodeList_Simple,
|
||||
}
|
||||
ph.Render()
|
||||
if vars.showAll {
|
||||
req.Type = wwapiv1.GetNodeList_All
|
||||
} else if vars.showIpmi {
|
||||
req.Type = wwapiv1.GetNodeList_Ipmi
|
||||
} else if vars.showNet {
|
||||
req.Type = wwapiv1.GetNodeList_Network
|
||||
} else if vars.showLong {
|
||||
req.Type = wwapiv1.GetNodeList_Long
|
||||
}
|
||||
nodeInfo, err := apinode.NodeList(&req)
|
||||
if len(nodeInfo.Output) > 0 {
|
||||
ph := helper.NewPrintHelper(strings.Split(nodeInfo.Output[0], "="))
|
||||
for _, val := range nodeInfo.Output[1:] {
|
||||
ph.Append(strings.Split(val, "="))
|
||||
}
|
||||
ph.Render()
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
80
internal/app/wwctl/node/list/main_test.go
Normal file
80
internal/app/wwctl/node/list/main_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"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
|
||||
wantErr bool
|
||||
stdout string
|
||||
inDb string
|
||||
}{
|
||||
{name: "single node list",
|
||||
args: []string{},
|
||||
wantErr: false,
|
||||
stdout: ` NODE NAME PROFILES NETWORK
|
||||
n01 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)
|
||||
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)
|
||||
buf := new(bytes.Buffer)
|
||||
baseCmd.SetOut(buf)
|
||||
baseCmd.SetErr(buf)
|
||||
old := os.Stdout // keep backup of the real stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
err = baseCmd.Execute()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Got unwanted error: %s", err)
|
||||
t.FailNow()
|
||||
}
|
||||
outC := make(chan string)
|
||||
go func() {
|
||||
var buf bytes.Buffer
|
||||
_, _ = io.Copy(&buf, r)
|
||||
outC <- buf.String()
|
||||
}()
|
||||
// back to normal state
|
||||
w.Close()
|
||||
os.Stdout = old // restoring the real stdout
|
||||
out := <-outC
|
||||
if out != tt.stdout {
|
||||
t.Errorf("Got wrong output, got:'%s'\nwant:'%s'", out, tt.stdout)
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,22 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
baseCmd = &cobra.Command{
|
||||
type variables struct {
|
||||
showNet bool
|
||||
showIpmi bool
|
||||
showAll bool
|
||||
showLong bool
|
||||
}
|
||||
|
||||
func GetCommand() *cobra.Command {
|
||||
vars := variables{}
|
||||
baseCmd := &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "list [OPTIONS] [PATTERN]",
|
||||
Short: "List nodes",
|
||||
Long: "This command lists all configured nodes. Optionally, it will list only\n" +
|
||||
"nodes matching a glob PATTERN.",
|
||||
RunE: CobraRunE,
|
||||
RunE: CobraRunE(&vars),
|
||||
Aliases: []string{"ls"},
|
||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if len(args) != 0 {
|
||||
@@ -28,20 +36,10 @@ var (
|
||||
return node_names, cobra.ShellCompDirectiveNoFileComp
|
||||
},
|
||||
}
|
||||
ShowNet bool
|
||||
ShowIpmi bool
|
||||
ShowAll bool
|
||||
ShowLong bool
|
||||
)
|
||||
baseCmd.PersistentFlags().BoolVarP(&vars.showNet, "net", "n", false, "Show node network configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&vars.showIpmi, "ipmi", "i", false, "Show node IPMI configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&vars.showAll, "all", "a", false, "Show all node configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&vars.showLong, "long", "l", false, "Show long or wide format")
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&ShowNet, "net", "n", false, "Show node network configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&ShowIpmi, "ipmi", "i", false, "Show node IPMI configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&ShowAll, "all", "a", false, "Show all node configurations")
|
||||
baseCmd.PersistentFlags().BoolVarP(&ShowLong, "long", "l", false, "Show long or wide format")
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
func GetCommand() *cobra.Command {
|
||||
return baseCmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user