This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: run Go tests inside driver container
This has multiple advantages: - we test in exactly the environment in which the code will run - it's faster than bringing up a separate VM - no need to remember a separate make target to run all tests Speed is important both for CI and developers. Running the tests in an existing cluster just takes 9 seconds and can be done using the same command as running other tests individually: make test_e2e TEST_E2E_FOCUS=gotests.*pmem-device-manager Because it runs so quickly, it's okay to simply repeat this testing in each CI step which runs E2E tests.
- Loading branch information
Showing
5 changed files
with
138 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2019 Intel Corporation. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package gotests | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/kubernetes/test/e2e/framework" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/intel/pmem-csi/test/e2e/pod" | ||
) | ||
|
||
var _ = Describe("gotests", func() { | ||
f := framework.NewDefaultFramework("gotests") | ||
f.SkipNamespaceCreation = true | ||
|
||
// Register one test for each package. | ||
for _, pkg := range strings.Split(os.Getenv("TEST_PKGS"), " ") { | ||
pkg := pkg | ||
It(pkg, func() { runGoTest(f, pkg) }) | ||
} | ||
}) | ||
|
||
// runGoTest builds and copies the Go test binary into the PMEM-CSI | ||
// driver container and executes it there. This way it runs in exactly | ||
// the same environment as the driver (the distro's kernel, our | ||
// container user space), which may or may not expose bugs that are | ||
// not found when running those tests on the build host. | ||
func runGoTest(f *framework.Framework, pkg string) { | ||
root := os.Getenv("REPO_ROOT") | ||
var err error | ||
|
||
build := exec.Command("/bin/sh", "-c", os.Getenv("TEST_CMD")+" -c -o _work/test.test "+pkg) | ||
build.Stdout = GinkgoWriter | ||
build.Stderr = GinkgoWriter | ||
build.Dir = root | ||
By("Compiling with: " + strings.Join(build.Args, " ")) | ||
err = build.Run() | ||
framework.ExpectNoError(err, "compile test program for %s", pkg) | ||
|
||
label := labels.SelectorFromSet(labels.Set(map[string]string{"app": "pmem-csi-node"})) | ||
pods, err := f.ClientSet.CoreV1().Pods("default").List(metav1.ListOptions{LabelSelector: label.String()}) | ||
framework.ExpectNoError(err, "list PMEM-CSI pods") | ||
Expect(pods.Items).NotTo(BeEmpty(), "have PMEM-CSI pods") | ||
pmem := pods.Items[0] | ||
|
||
By(fmt.Sprintf("Running in PMEM-CSI pod %s", pmem.Name)) | ||
pod.RunInPod(f, root, | ||
[]string{"_work/test.test", "_work/evil-ca", "_work/pmem-ca"}, | ||
"if _work/test.test -h 2>&1 | grep -q ginkgo; then "+ | ||
"TEST_WORK=_work _work/test.test -test.v -ginkgo.v; else "+ | ||
"TEST_WORK=_work _work/test.test -test.v -ginkgo.v; fi", | ||
pmem.Namespace, pmem.Name, "pmem-driver") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2019 Intel Corporation. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package pod | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
|
||
"k8s.io/kubernetes/test/e2e/framework" | ||
|
||
. "github.com/onsi/ginkgo" | ||
) | ||
|
||
// RunInPod optionally tars up some files or directories, unpacks them in a container, | ||
// and executes a shell command. Any error is treated as test failure. | ||
func RunInPod(f *framework.Framework, rootdir string, items []string, command string, namespace, pod, container string) { | ||
var input io.Reader | ||
var cmdPrefix string | ||
if len(items) > 0 { | ||
args := []string{"-cf", "-"} | ||
args = append(args, items...) | ||
tar := exec.Command("tar", args...) | ||
tar.Stderr = GinkgoWriter | ||
tar.Dir = rootdir | ||
pipe, err := tar.StdoutPipe() | ||
framework.ExpectNoError(err, "create pipe for tar") | ||
err = tar.Start() | ||
framework.ExpectNoError(err, "run tar") | ||
defer func() { | ||
err = tar.Wait() | ||
framework.ExpectNoError(err, "tar runtime error") | ||
}() | ||
|
||
input = pipe | ||
cmdPrefix = "tar -xf - &&" | ||
} | ||
|
||
options := framework.ExecOptions{ | ||
Command: []string{ | ||
"/bin/sh", | ||
"-c", | ||
cmdPrefix + command, | ||
}, | ||
Namespace: namespace, | ||
PodName: pod, | ||
ContainerName: container, | ||
Stdin: input, | ||
CaptureStdout: true, | ||
CaptureStderr: true, | ||
} | ||
stdout, stderr, err := f.ExecWithOptions(options) | ||
framework.ExpectNoError(err, "command failed in namespace %s, pod/container %s/%s:\nstderr:\n%s\nstdout:%s\n", | ||
namespace, pod, container, stderr, stdout) | ||
fmt.Fprintf(GinkgoWriter, "stderr:\n%s\nstdout:\n%s", | ||
stdout, stderr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters