Skip to content

Commit

Permalink
Add test cases for container exec
Browse files Browse the repository at this point in the history
Signed-off-by: xu yang <[email protected]>
  • Loading branch information
JasonYangShadow authored and anderbubble committed Jun 12, 2024
1 parent 5bb13c8 commit c28404e
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions internal/app/wwctl/container/exec/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/warewulf/warewulf/internal/pkg/testenv"
"path"
"strings"

"github.com/warewulf/warewulf/internal/pkg/warewulfd"
)

func mockChildCmd(cmd *cobra.Command, args []string) error {
Expand All @@ -18,24 +22,70 @@ func mockChildCmd(cmd *cobra.Command, args []string) error {
return child.Run()
}

func Test_Exec_with_bind(t *testing.T) {
func Test_Exec(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll(t)
env.MkdirAll(t, "var/lib/warewulf/chroots/alpine/rootfs")
env.MkdirAll(t, path.Join(testenv.WWChrootdir, "test/rootfs"))
childCommandFunc = mockChildCmd
defer func() {
childCommandFunc = runChildCmd
}()
warewulfd.SetNoDaemon()

tests := []struct {
name string
args []string
stdout string
}{
{
name: "plain test",
args: []string{"test", "/bin/true"},
stdout: `--loglevel 20 container exec __child test /bin/true`,
},
{
name: "test with --bind",
args: []string{"test", "--bind", "/tmp", "/bin/true"},
stdout: `--loglevel 20 container exec __child test --bind /tmp /bin/true`,
},
{
name: "test with --node",
args: []string{"test", "--node", "node1", "/bin/true"},
stdout: `--loglevel 20 container exec __child test --node node1 /bin/true`,
},
{
name: "test with --node and --bind",
args: []string{"test", "--bind", "/tmp", "--node", "node1", "/bin/true"},
stdout: `--loglevel 20 container exec __child test --bind /tmp --node node1 /bin/true`,
},
{
name: "test with complex command",
args: []string{"test", "/bin/bash", "echo 'hello'"},
stdout: `--loglevel 20 container exec __child test /bin/bash echo 'hello'`,
},
}

cmd := GetCommand()
out := bytes.NewBufferString("")
err := bytes.NewBufferString("")
cmd.SetOut(out)
cmd.SetErr(err)
cmd.SetArgs([]string{"--bind", "/mnt:/mnt", "alpine", "--", "/bin/echo", "Hello, world!"})
assert.NoError(t, cmd.Execute())
assert.Contains(t, out.String(), "alpine")
assert.Contains(t, out.String(), "/bin/echo Hello, world!")
assert.Contains(t, out.String(), "--bind /mnt:/mnt")
assert.Empty(t, err.String())
for _, tt := range tests {
t.Logf("Running test: %s\n", tt.name)
t.Run(tt.name, func(t *testing.T) {
defer func() {
binds = []string{}
nodeName = ""
}()
cmd := GetCommand()
cmd.SetArgs(tt.args)
out := bytes.NewBufferString("")
err := bytes.NewBufferString("")
cmd.SetOut(out)
cmd.SetErr(err)
if err := cmd.Execute(); err != nil {
t.Errorf("Received error when running command, err: %v", err)
t.FailNow()
}
assert.NotEmpty(t, out.String(), "os.stdout should not be empty")
if !strings.Contains(out.String(), tt.stdout) {
t.Errorf("Got wrong output, got:\n '%s'\n, but want:\n '%s'\n", out.String(), tt.stdout)
t.FailNow()
}
})
}
}

0 comments on commit c28404e

Please sign in to comment.