add test case for container list
Signed-off-by: jason yang <jasonyangshadow@gmail.com>
This commit is contained in:
@@ -11,26 +11,30 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
containerInfo, err := container.ContainerList()
|
||||
if err != nil {
|
||||
wwlog.Error("%s", err)
|
||||
var containerList = container.ContainerList
|
||||
|
||||
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
|
||||
return func(cmd *cobra.Command, args []string) (err error) {
|
||||
containerInfo, err := containerList()
|
||||
if err != nil {
|
||||
wwlog.Error("%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
ph := helper.NewPrintHelper([]string{"CONTAINER NAME", "NODES", "KERNEL VERSION", "CREATION TIME", "MODIFICATION TIME", "SIZE"})
|
||||
for i := 0; i < len(containerInfo); i++ {
|
||||
createTime := time.Unix(int64(containerInfo[i].CreateDate), 0)
|
||||
modTime := time.Unix(int64(containerInfo[i].ModDate), 0)
|
||||
ph.Append([]string{
|
||||
containerInfo[i].Name,
|
||||
strconv.FormatUint(uint64(containerInfo[i].NodeCount), 10),
|
||||
containerInfo[i].KernelVersion,
|
||||
createTime.Format(time.RFC822),
|
||||
modTime.Format(time.RFC822),
|
||||
util.ByteToString(int64(containerInfo[i].Size)),
|
||||
})
|
||||
}
|
||||
ph.Render()
|
||||
return
|
||||
}
|
||||
|
||||
ph := helper.NewPrintHelper([]string{"CONTAINER NAME", "NODES", "KERNEL VERSION", "CREATION TIME", "MODIFICATION TIME", "SIZE"})
|
||||
for i := 0; i < len(containerInfo); i++ {
|
||||
createTime := time.Unix(int64(containerInfo[i].CreateDate), 0)
|
||||
modTime := time.Unix(int64(containerInfo[i].ModDate), 0)
|
||||
ph.Append([]string{
|
||||
containerInfo[i].Name,
|
||||
strconv.FormatUint(uint64(containerInfo[i].NodeCount), 10),
|
||||
containerInfo[i].KernelVersion,
|
||||
createTime.Format(time.RFC822),
|
||||
modTime.Format(time.RFC822),
|
||||
util.ByteToString(int64(containerInfo[i].Size)),
|
||||
})
|
||||
}
|
||||
ph.Render()
|
||||
return
|
||||
}
|
||||
|
||||
100
internal/app/wwctl/container/list/main_test.go
Normal file
100
internal/app/wwctl/container/list/main_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"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
|
||||
mockFunc func()
|
||||
}{
|
||||
{
|
||||
name: "profile list test",
|
||||
args: []string{},
|
||||
stdout: `CONTAINERNAMENODESKERNELVERSIONCREATIONTIMEMODIFICATIONTIMESIZE
|
||||
test1kernel01Jan7000:00UTC01Jan7000:00UTC1B
|
||||
`,
|
||||
inDb: `WW_INTERNAL: 43
|
||||
nodeprofiles:
|
||||
default: {}
|
||||
nodes:
|
||||
n01:
|
||||
profiles:
|
||||
- default
|
||||
`,
|
||||
mockFunc: func() {
|
||||
containerList = func() (containerInfo []*wwapiv1.ContainerInfo, err error) {
|
||||
containerInfo = append(containerInfo, &wwapiv1.ContainerInfo{
|
||||
Name: "test",
|
||||
NodeCount: 1,
|
||||
KernelVersion: "kernel",
|
||||
CreateDate: uint64(time.Unix(0, 0).Unix()),
|
||||
ModDate: uint64(time.Unix(0, 0).Unix()),
|
||||
Size: uint64(1),
|
||||
})
|
||||
return
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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) {
|
||||
tt.mockFunc()
|
||||
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()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,18 @@ package list
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var (
|
||||
baseCmd = &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "list [OPTIONS]",
|
||||
Short: "List imported Warewulf containers",
|
||||
Long: "This command will show you the containers that are imported into Warewulf.",
|
||||
RunE: CobraRunE,
|
||||
Aliases: []string{"ls"},
|
||||
}
|
||||
)
|
||||
type variables struct{}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
func GetCommand() *cobra.Command {
|
||||
vars := variables{}
|
||||
baseCmd := &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "list [OPTIONS]",
|
||||
Short: "List imported Warewulf containers",
|
||||
Long: "This command will show you the containers that are imported into Warewulf.",
|
||||
RunE: CobraRunE(&vars),
|
||||
Aliases: []string{"ls"},
|
||||
}
|
||||
return baseCmd
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ WW_INTERNAL: 0
|
||||
baseCmd.SetOut(nil)
|
||||
baseCmd.SetErr(nil)
|
||||
stdoutR, stdoutW, _ := os.Pipe()
|
||||
oriout := os.Stdout
|
||||
os.Stdout = stdoutW
|
||||
err = baseCmd.Execute()
|
||||
if err != nil {
|
||||
@@ -67,6 +68,7 @@ WW_INTERNAL: 0
|
||||
stdoutC <- buf.String()
|
||||
}()
|
||||
stdoutW.Close()
|
||||
os.Stdout = oriout
|
||||
|
||||
stdout := <-stdoutC
|
||||
stdout = strings.TrimSpace(stdout)
|
||||
|
||||
Reference in New Issue
Block a user