From 2be806d1391d8ff685b328b53b68c2d27a00b26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 22 Oct 2020 11:58:31 -0500 Subject: [PATCH] libcontainer/configs: improve CommandHook unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test that CommandHook actually executes a new process with the given env variables, parameters and json state. This commit also solves an issue with the previous approach that was calling 'os.Exit(0)' failing to signal test failures. Signed-off-by: Mauricio Vásquez --- libcontainer/configs/config_test.go | 63 ++++++++++++++++++----------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/libcontainer/configs/config_test.go b/libcontainer/configs/config_test.go index 7d66575db..97286740f 100644 --- a/libcontainer/configs/config_test.go +++ b/libcontainer/configs/config_test.go @@ -3,6 +3,7 @@ package configs_test import ( "encoding/json" "fmt" + "io/ioutil" "os" "reflect" "testing" @@ -160,18 +161,45 @@ func TestCommandHookRun(t *testing.T) { Pid: 1, Bundle: "/bundle", } - timeout := time.Second + + stateJson, err := json.Marshal(state) + if err != nil { + t.Fatal(err) + } + + verifyCommandTemplate := `#!/bin/sh +if [ "$1" != "testarg" ]; then + echo "Bad value for $1. Expected 'testarg', found '$1'" + exit 1 +fi +if [ -z "$FOO" ] || [ "$FOO" != BAR ]; then + echo "Bad value for FOO. Expected 'BAR', found '$FOO'" + exit 1 +fi +expectedJson=%q +read JSON +if [ "$JSON" != "$expectedJson" ]; then + echo "Bad JSON received. Expected '$expectedJson', found '$JSON'" + exit 1 +fi +exit 0 + ` + verifyCommand := fmt.Sprintf(verifyCommandTemplate, stateJson) + filename := "/tmp/runc-hooktest.sh" + os.Remove(filename) + if err := ioutil.WriteFile(filename, []byte(verifyCommand), 0700); err != nil { + t.Fatalf("Failed to create tmp file: %v", err) + } + defer os.Remove(filename) cmdHook := configs.NewCommandHook(configs.Command{ - Path: os.Args[0], - Args: []string{os.Args[0], "-test.run=TestHelperProcess"}, - Env: []string{"FOO=BAR"}, - Dir: "/", - Timeout: &timeout, + Path: filename, + Args: []string{filename, "testarg"}, + Env: []string{"FOO=BAR"}, + Dir: "/", }) - err := cmdHook.Run(state) - if err != nil { + if err := cmdHook.Run(state); err != nil { t.Errorf(fmt.Sprintf("Expected error to not occur but it was %+v", err)) } } @@ -184,26 +212,15 @@ func TestCommandHookRunTimeout(t *testing.T) { Pid: 1, Bundle: "/bundle", } - timeout := (10 * time.Millisecond) + timeout := 100 * time.Millisecond cmdHook := configs.NewCommandHook(configs.Command{ - Path: os.Args[0], - Args: []string{os.Args[0], "-test.run=TestHelperProcessWithTimeout"}, - Env: []string{"FOO=BAR"}, - Dir: "/", + Path: "/bin/sleep", + Args: []string{"/bin/sleep", "1"}, Timeout: &timeout, }) - err := cmdHook.Run(state) - if err == nil { + if err := cmdHook.Run(state); err == nil { t.Error("Expected error to occur but it was nil") } } - -func TestHelperProcess(*testing.T) { - fmt.Println("Helper Process") - os.Exit(0) -} -func TestHelperProcessWithTimeout(*testing.T) { - time.Sleep(time.Second) -}