mount_test.go 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// +build integration

/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
A
Aaron Prindle 已提交
26
	"strings"
27 28 29
	"testing"
	"time"

30
	"k8s.io/apimachinery/pkg/labels"
31 32 33 34 35
	"k8s.io/minikube/test/integration/util"
)

func testMounting(t *testing.T) {
	t.Parallel()
A
Aaron Prindle 已提交
36 37 38
	if strings.Contains(*args, "--vm-driver=none") {
		t.Skip("skipping test for none driver as it does not need mount")
	}
39 40 41 42 43 44 45 46 47 48 49
	minikubeRunner := util.MinikubeRunner{
		Args:       *args,
		BinaryPath: *binaryPath,
		T:          t}

	tempDir, err := ioutil.TempDir("", "mounttest")
	if err != nil {
		t.Fatalf("Unexpected error while creating tempDir: %s", err)
	}
	defer os.RemoveAll(tempDir)

50
	mountCmd := fmt.Sprintf("mount %s:/mount-9p", tempDir)
51 52 53 54
	cmd := minikubeRunner.RunDaemon(mountCmd)
	defer cmd.Process.Kill()

	kubectlRunner := util.NewKubectlRunner(t)
55
	podName := "busybox-mount"
56 57 58 59 60 61 62 63 64 65 66 67
	podPath, _ := filepath.Abs("testdata/busybox-mount-test.yaml")

	// Write file in mounted dir from host
	expected := "test\n"
	files := []string{"fromhost", "fromhostremove"}
	for _, file := range files {
		path := filepath.Join(tempDir, file)
		err = ioutil.WriteFile(path, []byte(expected), 0644)
		if err != nil {
			t.Fatalf("Unexpected error while writing file %s: %s.", path, err)
		}
	}
68 69 70

	// Create the pods we need outside the main test loop.
	setupTest := func() error {
71 72 73
		if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath}); err != nil {
			return err
		}
74 75 76 77
		return nil
	}
	defer kubectlRunner.RunCommand([]string{"delete", "-f", podPath})

78
	if err := util.Retry(t, setupTest, 5*time.Second, 40); err != nil {
79 80 81
		t.Fatal("mountTest failed with error:", err)
	}

82 83 84 85 86 87 88 89
	client, err := util.GetClient()
	if err != nil {
		t.Fatalf("getting kubernetes client: %s", err)
	}
	selector := labels.SelectorFromSet(labels.Set(map[string]string{"integration-test": "busybox-mount"}))
	if err := util.WaitForPodsWithLabelRunning(client, "default", selector); err != nil {
		t.Fatalf("Error waiting for busybox mount pod to be up: %s", err)
	}
90

91
	mountTest := func() error {
92 93 94
		path := filepath.Join(tempDir, "frompod")
		out, err := ioutil.ReadFile(path)
		if err != nil {
95
			return err
96 97 98 99 100 101 102 103
		}
		// test that file written from pod can be read from host echo test > /mount-9p/frompod; in pod
		if string(out) != expected {
			t.Fatalf("Expected file %s to contain text %s, was %s.", path, expected, out)
		}

		// test that file written from host was read in by the pod via cat /mount-9p/fromhost;
		if out, err = kubectlRunner.RunCommand([]string{"logs", podName}); err != nil {
104
			return err
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		}
		if string(out) != expected {
			t.Fatalf("Expected file %s to contain text %s, was %s.", path, expected, out)
		}

		// test that fromhostremove was deleted by the pod from the mount via rm /mount-9p/fromhostremove
		path = filepath.Join(tempDir, "fromhostremove")
		if _, err := os.Stat(path); err == nil {
			t.Fatalf("Expected file %s to be removed", path, expected, out)
		}

		// test that frompodremove can be deleted on the host
		path = filepath.Join(tempDir, "frompodremove")
		if err := os.Remove(path); err != nil {
			t.Fatalf("Unexpected error removing file %s: %s", path, err)
		}

		return nil
	}
124
	if err := util.Retry(t, mountTest, 5*time.Second, 40); err != nil {
125 126 127 128
		t.Fatal("mountTest failed with error:", err)
	}

}