mirror of https://github.com/k3s-io/k3s
Merge pull request #11128 from justinsb/e2e_pd_contents
e2e: Add test to pd that disk contents are preservedpull/6/head
commit
ce25e164e7
|
@ -18,10 +18,12 @@ package e2e
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
math_rand "math/rand"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bytes"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
|
@ -86,6 +88,12 @@ var _ = Describe("Pod Disks", func() {
|
|||
|
||||
expectNoError(waitForPodRunning(c, host0Pod.Name))
|
||||
|
||||
testFile := "/testpd/tracker"
|
||||
testFileContents := fmt.Sprintf("%v", math_rand.Int())
|
||||
|
||||
expectNoError(writeFileOnPod(c, host0Pod.Name, testFile, testFileContents))
|
||||
Logf("Wrote value: %v", testFileContents)
|
||||
|
||||
By("deleting host0Pod")
|
||||
expectNoError(podClient.Delete(host0Pod.Name, nil), "Failed to delete host0Pod")
|
||||
|
||||
|
@ -95,6 +103,12 @@ var _ = Describe("Pod Disks", func() {
|
|||
|
||||
expectNoError(waitForPodRunning(c, host1Pod.Name))
|
||||
|
||||
v, err := readFileOnPod(c, host1Pod.Name, testFile)
|
||||
expectNoError(err)
|
||||
Logf("Read value: %v", v)
|
||||
|
||||
Expect(strings.TrimSpace(v)).To(Equal(strings.TrimSpace(testFileContents)))
|
||||
|
||||
By("deleting host1Pod")
|
||||
expectNoError(podClient.Delete(host1Pod.Name, nil), "Failed to delete host1Pod")
|
||||
|
||||
|
@ -173,6 +187,48 @@ var _ = Describe("Pod Disks", func() {
|
|||
})
|
||||
})
|
||||
|
||||
func kubectlExec(namespace string, podName string, args ...string) ([]byte, []byte, error) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmdArgs := []string{"exec", fmt.Sprintf("--namespace=%v", namespace), podName}
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
|
||||
cmd := kubectlCmd(cmdArgs...)
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
|
||||
Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args, " "))
|
||||
err := cmd.Run()
|
||||
return stdout.Bytes(), stderr.Bytes(), err
|
||||
}
|
||||
|
||||
// Write a file using kubectl exec echo <contents> > <path>
|
||||
// Because of the primitive technique we're using here, we only allow ASCII alphanumeric characters
|
||||
func writeFileOnPod(c *client.Client, podName string, path string, contents string) error {
|
||||
By("writing a file in the container")
|
||||
allowedCharacters := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
for _, c := range contents {
|
||||
if !strings.ContainsRune(allowedCharacters, c) {
|
||||
return fmt.Errorf("Unsupported character in string to write: %v", c)
|
||||
}
|
||||
}
|
||||
command := fmt.Sprintf("echo '%s' > '%s'", contents, path)
|
||||
stdout, stderr, err := kubectlExec(api.NamespaceDefault, podName, "--", "/bin/sh", "-c", command)
|
||||
if err != nil {
|
||||
Logf("error running kubectl exec to write file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Read a file using kubectl exec cat <path>
|
||||
func readFileOnPod(c *client.Client, podName string, path string) (string, error) {
|
||||
By("reading a file in the container")
|
||||
|
||||
stdout, stderr, err := kubectlExec(api.NamespaceDefault, podName, "--", "cat", path)
|
||||
if err != nil {
|
||||
Logf("error running kubectl exec to read file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
|
||||
}
|
||||
return string(stdout), err
|
||||
}
|
||||
|
||||
func createPD() (string, error) {
|
||||
if testContext.Provider == "gce" || testContext.Provider == "gke" {
|
||||
pdName := fmt.Sprintf("%s-%s", testContext.prefix, string(util.NewUUID()))
|
||||
|
@ -245,7 +301,8 @@ func testPDPod(diskName, targetHost string, readOnly bool) *api.Pod {
|
|||
Containers: []api.Container{
|
||||
{
|
||||
Name: "testpd",
|
||||
Image: "gcr.io/google_containers/pause",
|
||||
Image: "gcr.io/google_containers/busybox",
|
||||
Command: []string{"sleep", "600"},
|
||||
VolumeMounts: []api.VolumeMount{
|
||||
{
|
||||
Name: "testpd",
|
||||
|
|
Loading…
Reference in New Issue