From 0f69323623e8f1126fe81809f82fc1ac41828ff9 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 23 Aug 2020 03:14:51 +0530 Subject: [PATCH] Update all the e2e tests to use the new helpers --- e2e/config_test.go | 53 ++++----- e2e/cp_test.go | 258 ++++++++++++++++------------------------- e2e/image_test.go | 48 ++++---- e2e/run_test.go | 96 ++++++--------- e2e/run_volume_test.go | 137 +++++++++------------- e2e/vm_exec_test.go | 59 ++++------ 6 files changed, 259 insertions(+), 392 deletions(-) diff --git a/e2e/config_test.go b/e2e/config_test.go index 883ddbbbc..561e6b9a2 100644 --- a/e2e/config_test.go +++ b/e2e/config_test.go @@ -4,12 +4,12 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "strings" "testing" "gotest.tools/assert" + "github.com/weaveworks/ignite/e2e/util" "github.com/weaveworks/ignite/pkg/constants" ) @@ -169,41 +169,31 @@ spec: vmName := "e2e_test_ignite_config_file" + igniteCmd := util.NewCommand(t, igniteBin) + // Create a VM with the ignite config file. // NOTE: Set a sandbox-image to have deterministic results. - runArgs := []string{ - "run", "--name=" + vmName, - "weaveworks/ignite-ubuntu", - "--ignite-config=" + file.Name(), - "--sandbox-image=weaveworks/ignite:dev", - } + vmRun := igniteCmd.New(). + With("run", "--name="+vmName). + With("--sandbox-image=weaveworks/ignite:dev"). + With("--ignite-config=" + file.Name()). + With(util.DefaultVMImage) // Append VM config if provided. if vmConfigFileName != "" { - runArgs = append(runArgs, "--config="+vmConfigFileName) + vmRun = vmRun.With("--config=" + vmConfigFileName) } // Append the args to the run args for override flags. - runArgs = append(runArgs, rt.args...) - runCmd := exec.Command( - igniteBin, - runArgs..., - ) - _, err = runCmd.CombinedOutput() + vmRun = vmRun.With(rt.args...) + _, err = vmRun.Cmd.CombinedOutput() if err == nil { // Delete the VM only when the creation succeeds, with the // config file. - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - "--ignite-config="+file.Name(), - ) - - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() // Check if run failure was expected. if (err != nil) != rt.err { @@ -213,14 +203,15 @@ spec: if !rt.err { // Query VM properties. - psCmd := exec.Command( - igniteBin, - "ps", - "--filter={{.ObjectMeta.Name}}="+vmName, + psArgs := []string{ + "--filter={{.ObjectMeta.Name}}=" + vmName, "--template='{{.Spec.Memory}} {{.Spec.CPUs}} {{.Spec.DiskSize}} {{.Spec.Image.OCI}} {{.Spec.Sandbox.OCI}} {{.Spec.Kernel.OCI}} {{.Spec.SSH}}'", - ) - psOut, psErr := psCmd.CombinedOutput() - assert.Check(t, psErr, fmt.Sprintf("ps: \n%q\n%s", psCmd.Args, psOut)) + } + psCmd := igniteCmd.New(). + With("ps"). + With(psArgs...) + psOut, psErr := psCmd.Cmd.CombinedOutput() + assert.Check(t, psErr, fmt.Sprintf("ps: \n%q\n%s", psCmd.Cmd, psOut)) got := strings.TrimSpace(string(psOut)) assert.Equal(t, got, rt.wantVMProperties, fmt.Sprintf("unexpected VM properties:\n\t(WNT): %q\n\t(GOT): %q", rt.wantVMProperties, got)) } diff --git a/e2e/cp_test.go b/e2e/cp_test.go index 2bb08280c..1f21a40cf 100644 --- a/e2e/cp_test.go +++ b/e2e/cp_test.go @@ -5,58 +5,49 @@ import ( "io/ioutil" "math/rand" "os" - "os/exec" "path/filepath" "strings" "testing" "time" - "github.com/weaveworks/ignite/cmd/ignite/run" "gotest.tools/assert" + + "github.com/weaveworks/ignite/cmd/ignite/run" + "github.com/weaveworks/ignite/e2e/util" ) func runCopyFilesToVM(t *testing.T, vmName, source, destination, wantFileContent string) { assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set") - runCmd := exec.Command( - igniteBin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } + igniteCmd := util.NewCommand(t, igniteBin) - copyCmd := exec.Command( - igniteBin, - "cp", source, destination, - ) - copyOut, copyErr := copyCmd.CombinedOutput() - assert.Check(t, copyErr, fmt.Sprintf("copy: \n%q\n%s", copyCmd.Args, copyOut)) + // Clean-up the following VM. + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() + + // Run a VM. + igniteCmd.New(). + With("run"). + With("--name=" + vmName). + With("--ssh"). + With(util.DefaultVMImage). + Run() + + igniteCmd.New(). + With("cp", source, destination). + Run() // When copying to a VM, the file path succeeds the file path separator. // Split the destination to obtain VM destination file path. dest := strings.Split(destination, run.VMFilePathSeparator) - catCmd := exec.Command( - igniteBin, - "exec", vmName, - "cat", dest[1], - ) - catOut, catErr := catCmd.CombinedOutput() - assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Args, catOut)) + + catCmd := igniteCmd.New(). + With("exec", vmName). + With("cat", dest[1]) + + catOut, catErr := catCmd.Cmd.CombinedOutput() + assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Cmd, catOut)) assert.Equal(t, string(catOut), wantFileContent, fmt.Sprintf("unexpected copied file content:\n\t(WNT): %q\n\t(GOT): %q", wantFileContent, string(catOut))) } @@ -144,48 +135,35 @@ func TestCopyFileFromVMToHost(t *testing.T) { vmName := "e2e_test_copy_file_from_vm_to_host" - runCmd := exec.Command( - igniteBin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } + igniteCmd := util.NewCommand(t, igniteBin) + + // Clean-up the following VM. + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() + + // Run a VM. + igniteCmd.New(). + With("run"). + With("--name=" + vmName). + With("--ssh"). + With(util.DefaultVMImage). + Run() // File to be copied from VM. vmFilePath := "/proc/version" - catCmd := exec.Command( - igniteBin, - "exec", vmName, - "cat", vmFilePath, - ) - catOut, catErr := catCmd.CombinedOutput() - assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Args, catOut)) + catCmd := igniteCmd.New(). + With("exec", vmName). + With("cat", vmFilePath) + catOut, catErr := catCmd.Cmd.CombinedOutput() + assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Cmd, catOut)) // Host file path. hostFilePath := "/tmp/ignite-os-version" - copyCmd := exec.Command( - igniteBin, - "cp", - fmt.Sprintf("%s:%s", vmName, vmFilePath), - hostFilePath, - ) - copyOut, copyErr := copyCmd.CombinedOutput() - assert.Check(t, copyErr, fmt.Sprintf("copy: \n%q\n%s", copyCmd.Args, copyOut)) + vmSource := fmt.Sprintf("%s:%s", vmName, vmFilePath) + igniteCmd.New(). + With("cp", vmSource, hostFilePath). + Run() defer os.Remove(hostFilePath) hostContent, err := ioutil.ReadFile(hostFilePath) @@ -228,58 +206,42 @@ func TestCopyDirectoryFromHostToVM(t *testing.T) { source := dir dest := fmt.Sprintf("%s:%s", vmName, source) + igniteCmd := util.NewCommand(t, igniteBin) + + // Clean-up the following VM. + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() + // Run a VM. - runCmd := exec.Command( - igniteBin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } + igniteCmd.New(). + With("run"). + With("--name=" + vmName). + With("--ssh"). + With(util.DefaultVMImage). + Run() - // Copy dir to VM. - copyCmd := exec.Command( - igniteBin, - "cp", source, dest, - ) - copyOut, copyErr := copyCmd.CombinedOutput() - assert.Check(t, copyErr, fmt.Sprintf("copy: \n%q\n%s", copyCmd.Args, copyOut)) + igniteCmd.New(). + With("cp", source, dest). + Run() // Check if the directory exists in the VM. dirFind := fmt.Sprintf("find %s -type d -name %s", filepath.Dir(source), filepath.Base(source)) - dirFindCmd := exec.Command( - igniteBin, - "exec", vmName, - dirFind, - ) - dirFindOut, dirFindErr := dirFindCmd.CombinedOutput() - assert.Check(t, dirFindErr, fmt.Sprintf("find: \n%q\n%s", dirFindCmd.Args, dirFindOut)) + dirFindCmd := igniteCmd.New(). + With("exec", vmName). + With(dirFind) + dirFindOut, dirFindErr := dirFindCmd.Cmd.CombinedOutput() + assert.Check(t, dirFindErr, fmt.Sprintf("find: \n%q\n%s", dirFindCmd.Cmd, dirFindOut)) gotDir := strings.TrimSpace(string(dirFindOut)) assert.Equal(t, gotDir, dir, fmt.Sprintf("unexpected find directory result: \n\t(WNT): %q\n\t(GOT): %q", dir, gotDir)) // Check if the file inside the directory in the VM has the same content as // on the host. - catCmd := exec.Command( - igniteBin, - "exec", vmName, - "cat", file.Name(), - ) - catOut, catErr := catCmd.CombinedOutput() - assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Args, catOut)) + catCmd := igniteCmd.New(). + With("exec", vmName). + With("cat", file.Name()) + catOut, catErr := catCmd.Cmd.CombinedOutput() + assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Cmd, catOut)) gotContent := strings.TrimSpace(string(catOut)) assert.Equal(t, gotContent, string(content), fmt.Sprintf("unexpected copied file content:\n\t(WNT): %q\n\t(GOT): %q", content, gotContent)) } @@ -289,62 +251,44 @@ func TestCopyDirectoryFromVMToHost(t *testing.T) { vmName := "e2e_test_copy_dir_from_vm_to_host" + igniteCmd := util.NewCommand(t, igniteBin) + + // Clean-up the following VM. + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() + // Run a VM. - runCmd := exec.Command( - igniteBin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } + igniteCmd.New(). + With("run"). + With("--name=" + vmName). + With("--ssh"). + With(util.DefaultVMImage). + Run() // Create directory inside the VM. rand.Seed(time.Now().UnixNano()) dirPath := fmt.Sprintf("/tmp/ignite-cp-dir-test%d", rand.Intn(10000)) mkdir := fmt.Sprintf("mkdir -p %s", dirPath) - mkdirCmd := exec.Command( - igniteBin, - "exec", vmName, - mkdir, - ) - mkdirOut, mkdirErr := mkdirCmd.CombinedOutput() - assert.Check(t, mkdirErr, fmt.Sprintf("mkdir: \n%q\n%s", mkdirCmd.Args, mkdirOut)) + igniteCmd.New(). + With("exec", vmName). + With(mkdir). + Run() // Create file inside the directory. content := "some content on VM" filePath := filepath.Join(dirPath, "ignite-cp-file") writeFile := fmt.Sprintf("echo %s > %s", content, filePath) - writeFileCmd := exec.Command( - igniteBin, - "exec", vmName, - writeFile, - ) - writeFileOut, writeFileErr := writeFileCmd.CombinedOutput() - assert.Check(t, writeFileErr, fmt.Sprintf("file write: \n%q\n%s", writeFileCmd.Args, writeFileOut)) + igniteCmd.New(). + With("exec", vmName). + With(writeFile). + Run() // Copy the file to host. - copyCmd := exec.Command( - igniteBin, - "cp", - fmt.Sprintf("%s:%s", vmName, dirPath), - dirPath, - ) - copyOut, copyErr := copyCmd.CombinedOutput() - assert.Check(t, copyErr, fmt.Sprintf("copy: \n%q\n%s", copyCmd.Args, copyOut)) + src := fmt.Sprintf("%s:%s", vmName, dirPath) + igniteCmd.New(). + With("cp", src, dirPath). + Run() defer os.RemoveAll(dirPath) // Find copied directory on host. diff --git a/e2e/image_test.go b/e2e/image_test.go index 192ae97bd..aa6dcfe08 100644 --- a/e2e/image_test.go +++ b/e2e/image_test.go @@ -1,11 +1,12 @@ package e2e import ( - "fmt" "os/exec" "testing" "gotest.tools/assert" + + "github.com/weaveworks/ignite/e2e/util" ) func TestImportTinyImage(t *testing.T) { @@ -15,34 +16,32 @@ func TestImportTinyImage(t *testing.T) { // when there's no /etc directory in the image filesystem. testImage := "hello-world:latest" + + igniteCmd := util.NewCommand(t, igniteBin) + // Remove if the image already exists. - rmvImgCmd := exec.Command( - igniteBin, - "image", "rm", testImage, - ) - // Ignore error if the image doesn't exists. - _, _ = rmvImgCmd.CombinedOutput() + // Ignore any remove error. + _, _ = igniteCmd.New(). + With("image", "rm", testImage). + Cmd.CombinedOutput() // Import the image. - importImgCmd := exec.Command( - igniteBin, - "image", "import", testImage, - ) - importImgOut, importImgErr := importImgCmd.CombinedOutput() - assert.Check(t, importImgErr, fmt.Sprintf("image import: \n%q\n%s", importImgCmd.Args, importImgOut)) + igniteCmd.New(). + With("image", "import", testImage). + Run() } func TestDockerImportImage(t *testing.T) { assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set") testImage := "hello-world:latest" + + igniteCmd := util.NewCommand(t, igniteBin) + // Remove if the image already exists. - rmvImgCmd := exec.Command( - igniteBin, - "image", "rm", testImage, - ) - // Ignore error if the image doesn't exists. - _, _ = rmvImgCmd.CombinedOutput() + _, _ = igniteCmd.New(). + With("image", "rm", testImage). + Cmd.CombinedOutput() // Remove image from docker image store if already exists. rmvDockerImgCmd := exec.Command( @@ -53,11 +52,8 @@ func TestDockerImportImage(t *testing.T) { _, _ = rmvDockerImgCmd.CombinedOutput() // Import the image. - importImgCmd := exec.Command( - igniteBin, - "--runtime=docker", - "image", "import", testImage, - ) - importImgOut, importImgErr := importImgCmd.CombinedOutput() - assert.Check(t, importImgErr, fmt.Sprintf("image import: \n%q\n%s", importImgCmd.Args, importImgOut)) + igniteCmd.New(). + WithRuntime("docker"). + With("image", "import", testImage). + Run() } diff --git a/e2e/run_test.go b/e2e/run_test.go index 1399739a8..2c9094ccb 100644 --- a/e2e/run_test.go +++ b/e2e/run_test.go @@ -1,13 +1,13 @@ package e2e import ( - "fmt" "os" - "os/exec" "path" "testing" "gotest.tools/assert" + + "github.com/weaveworks/ignite/e2e/util" ) var ( @@ -20,27 +20,22 @@ var ( func runWithRuntimeAndNetworkPlugin(t *testing.T, vmName, runtime, networkPlugin string) { assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set") - runCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) + igniteCmd := util.NewCommand(t, igniteBin) + + defer igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("rm", "-f"). + With(vmName). + Run() + + igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("run"). + With("--name=" + vmName). + With(util.DefaultVMImage). + Run() } func TestIgniteRunWithDockerAndDockerBridge(t *testing.T) { @@ -75,41 +70,26 @@ func TestIgniteRunWithContainerdAndCNI(t *testing.T) { func runCurl(t *testing.T, vmName, runtime, networkPlugin string) { assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set") - runCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } - - curlCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "exec", vmName, - "curl", "google.com", - ) - curlOut, curlErr := curlCmd.CombinedOutput() - assert.Check(t, curlErr, fmt.Sprintf("curl: \n%q\n%s", curlCmd.Args, curlOut)) + igniteCmd := util.NewCommand(t, igniteBin) + + defer igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("rm", "-f", vmName). + Run() + + igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("run", "--name="+vmName). + With(util.DefaultVMImage). + With("--ssh"). + Run() + + igniteCmd.New(). + With("exec", vmName). + With("curl", "google.com"). + Run() } func TestCurlWithDockerAndDockerBridge(t *testing.T) { diff --git a/e2e/run_volume_test.go b/e2e/run_volume_test.go index 0a45e63aa..1f1d6397a 100644 --- a/e2e/run_volume_test.go +++ b/e2e/run_volume_test.go @@ -8,6 +8,8 @@ import ( "testing" "gotest.tools/assert" + + "github.com/weaveworks/ignite/e2e/util" ) // runVolume is a helper for testing volume persistence @@ -67,99 +69,64 @@ func runVolume(t *testing.T, vmName, runtime, networkPlugin string) { assert.Check(t, detachLoopErr, fmt.Sprintf("loop detach: \n%q\n%s", detachLoopCmd.Args, detachLoopOut)) }() - // Run a vm with the loop-device mounted as a volume @ /my-vol - runCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "--volumes="+loopPath+":/my-vol", - "run", "--name="+vmName, - "weaveworks/ignite-ubuntu", - "--ssh", - ) - runOut, runErr := runCmd.CombinedOutput() + igniteCmd := util.NewCommand(t, igniteBin) - defer func() { - rmvCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() + // Clean-up the following VM. + defer igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("rm", "-f", vmName). + Run() - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) - if runErr != nil { - return - } + // Run a vm with the loop-device mounted as a volume @ /my-vol + igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("run"). + With("--name=" + vmName). + With("--ssh"). + With("--volumes=" + loopPath + ":/my-vol"). + With(util.DefaultVMImage). + Run() // Touch a file in /my-vol - touchCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "exec", vmName, - "touch", "/my-vol/hello-world", - ) - touchOut, touchErr := touchCmd.CombinedOutput() - assert.Check(t, touchErr, fmt.Sprintf("touch: \n%q\n%s", touchCmd.Args, touchOut)) - if touchErr != nil { - return - } - - // Stop the vm - stopCmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "stop", vmName, - ) - stopOut, stopErr := stopCmd.CombinedOutput() - assert.Check(t, stopErr, fmt.Sprintf("vm stop: \n%q\n%s", stopCmd.Args, stopOut)) - if stopErr != nil { - return - } + igniteCmd.New(). + With("exec", vmName). + With("touch", "/my-vol/hello-world"). + Run() + + // Stop the vm without force. + igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("stop", vmName). + Run() + + secondVMName := vmName + "_2" + + // Clean-up the following VM. + defer igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("rm", "-f", secondVMName). + Run() // Start another vm so we can check my-vol - run2Cmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "--volumes="+loopPath+":/my-vol", - "run", "--name="+vmName+"_2", - "weaveworks/ignite-ubuntu", - "--ssh", - ) - run2Out, run2Err := run2Cmd.CombinedOutput() - - defer func() { - rmv2Cmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "rm", "-f", vmName+"_2", - ) - rmv2Out, rmv2Err := rmv2Cmd.CombinedOutput() - assert.Check(t, rmv2Err, fmt.Sprintf("vm removal: \n%q\n%s", rmv2Cmd.Args, rmv2Out)) - }() - - assert.Check(t, run2Err, fmt.Sprintf("vm run: \n%q\n%s", run2Cmd.Args, run2Out)) - if run2Err != nil { - return - } + igniteCmd.New(). + WithRuntime(runtime). + WithNetwork(networkPlugin). + With("run"). + With("--name=" + secondVMName). + With("--ssh"). + With("--volumes=" + loopPath + ":/my-vol"). + With(util.DefaultVMImage). + Run() // Stat the file in /my-vol using the new vm - stat2Cmd := exec.Command( - igniteBin, - "--runtime="+runtime, - "--network-plugin="+networkPlugin, - "exec", vmName+"_2", - "stat", "/my-vol/hello-world", - ) - stat2Out, stat2Err := stat2Cmd.CombinedOutput() - assert.Check(t, stat2Err, fmt.Sprintf("stat2: \n%q\n%s", stat2Cmd.Args, stat2Out)) + igniteCmd.New(). + With("exec", secondVMName). + With("stat", "/my-vol/hello-world"). + Run() } func TestVolumeWithDockerAndDockerBridge(t *testing.T) { diff --git a/e2e/vm_exec_test.go b/e2e/vm_exec_test.go index 4629391a4..56b3ea159 100644 --- a/e2e/vm_exec_test.go +++ b/e2e/vm_exec_test.go @@ -2,11 +2,12 @@ package e2e import ( "fmt" - "os/exec" "strings" "testing" "gotest.tools/assert" + + "github.com/weaveworks/ignite/e2e/util" ) func TestVMExecInteractive(t *testing.T) { @@ -14,47 +15,35 @@ func TestVMExecInteractive(t *testing.T) { vmName := "e2e_test_ignite_exec_interactive" - runCmd := exec.Command( - igniteBin, - "run", "--name="+vmName, - "--ssh", - "weaveworks/ignite-ubuntu", - ) - runOut, runErr := runCmd.CombinedOutput() - - defer func() { - rmvCmd := exec.Command( - igniteBin, - "rm", "-f", vmName, - ) - rmvOut, rmvErr := rmvCmd.CombinedOutput() - assert.Check(t, rmvErr, fmt.Sprintf("vm removal: \n%q\n%s", rmvCmd.Args, rmvOut)) - }() - - assert.Check(t, runErr, fmt.Sprintf("vm run: \n%q\n%s", runCmd.Args, runOut)) + igniteCmd := util.NewCommand(t, igniteBin) + + defer igniteCmd.New(). + With("rm", "-f", vmName). + Run() + + igniteCmd.New(). + With("run", "--name="+vmName). + With(util.DefaultVMImage). + With("--ssh"). + Run() // Pass input data from host and write to a file inside the VM. remoteFileName := "afile.txt" inputContent := "foooo..." input := strings.NewReader(inputContent) - execCmd := exec.Command( - igniteBin, - "exec", vmName, - "tee", remoteFileName, - ) - execCmd.Stdin = input - - execOut, execErr := execCmd.CombinedOutput() - assert.Check(t, execErr, fmt.Sprintf("exec: \n%q\n%s", execCmd.Args, execOut)) + igniteExec := igniteCmd.New(). + With("exec", vmName). + With("tee", remoteFileName) + igniteExec.Cmd.Stdin = input + igniteExec.Run() // Check the file content inside the VM. - catCmd := exec.Command( - igniteBin, - "exec", vmName, - "cat", remoteFileName, - ) - catOut, catErr := catCmd.CombinedOutput() - assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catCmd.Args, catOut)) + catExec := igniteCmd.New(). + With("exec", vmName). + With("cat", remoteFileName) + + catOut, catErr := catExec.Cmd.CombinedOutput() + assert.Check(t, catErr, fmt.Sprintf("cat: \n%q\n%s", catExec.Cmd, catOut)) assert.Equal(t, string(catOut), inputContent, fmt.Sprintf("unexpected file content on host:\n\t(WNT): %q\n\t(GOT): %q", inputContent, string(catOut))) }