From c28404ea6cfed8a8766a7b59de3af62397a29f9c Mon Sep 17 00:00:00 2001 From: xu yang Date: Wed, 12 Jun 2024 02:12:48 +0000 Subject: [PATCH] Add test cases for container exec Signed-off-by: xu yang --- .../app/wwctl/container/exec/main_test.go | 76 +++++++++++++++---- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/internal/app/wwctl/container/exec/main_test.go b/internal/app/wwctl/container/exec/main_test.go index 1b6a22ae..d17c4651 100644 --- a/internal/app/wwctl/container/exec/main_test.go +++ b/internal/app/wwctl/container/exec/main_test.go @@ -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 { @@ -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() - 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()) + 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'`, + }, + } + + 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() + } + }) + } }