mount_test.go 4.9 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
// +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"
23
	"io"
24 25 26
	"io/ioutil"
	"os"
	"path/filepath"
27
	"runtime"
A
Aaron Prindle 已提交
28
	"strings"
29 30 31
	"testing"
	"time"

32
	"k8s.io/apimachinery/pkg/labels"
M
Matt Rickard 已提交
33
	pkgutil "k8s.io/minikube/pkg/util"
34 35 36 37 38
	"k8s.io/minikube/test/integration/util"
)

func testMounting(t *testing.T) {
	t.Parallel()
39 40 41
	if runtime.GOOS == "darwin" {
		t.Skip("mount tests disabled in darwin due to timeout (issue#3200)")
	}
A
Aaron Prindle 已提交
42 43 44
	if strings.Contains(*args, "--vm-driver=none") {
		t.Skip("skipping test for none driver as it does not need mount")
	}
45
	minikubeRunner := NewMinikubeRunner(t)
46 47 48

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

53 54 55 56 57 58 59 60 61 62
	var mountCmd string
	if len(minikubeRunner.MountArgs) > 0 {
		mountCmd = fmt.Sprintf("mount %s %s:/mount-9p", minikubeRunner.MountArgs, tempDir)
	} else {
		mountCmd = fmt.Sprintf("mount %s:/mount-9p", tempDir)
	}
	if testing.Verbose() {
		fmt.Fprintf(os.Stderr, "runing mount cmd: %s\n", mountCmd)
	}
	cmd, out, serr := minikubeRunner.RunDaemon2(mountCmd)
63 64 65 66 67 68
	defer func() {
		err := cmd.Process.Kill()
		if err != nil {
			t.Logf("Failed to kill mount command: %v", err)
		}
	}()
69

70 71 72 73 74
	if testing.Verbose() {
		go io.Copy(os.Stderr, out)
		go io.Copy(os.Stderr, serr)
	}

75
	kubectlRunner := util.NewKubectlRunner(t)
76
	podName := "busybox-mount"
77 78 79 80 81 82 83 84 85
	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 {
86
			t.Fatalf("Unexpected error while writing file %s: %v", path, err)
87 88
		}
	}
89 90 91

	// Create the pods we need outside the main test loop.
	setupTest := func() error {
92 93 94
		if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath}); err != nil {
			return err
		}
95 96
		return nil
	}
T
Thomas Stromberg 已提交
97 98 99 100 101
	defer func() {
		if out, err := kubectlRunner.RunCommand([]string{"delete", "-f", podPath}); err != nil {
			t.Logf("delete -f %s failed: %v\noutput: %s\n", podPath, err, out)
		}
	}()
102

103
	if err := util.Retry(t, setupTest, 5*time.Second, 40); err != nil {
104 105 106
		t.Fatal("mountTest failed with error:", err)
	}

M
Matt Rickard 已提交
107
	client, err := pkgutil.GetClient()
108
	if err != nil {
109
		t.Fatalf("getting kubernetes client: %v", err)
110 111
	}
	selector := labels.SelectorFromSet(labels.Set(map[string]string{"integration-test": "busybox-mount"}))
M
Matt Rickard 已提交
112
	if err := pkgutil.WaitForPodsWithLabelRunning(client, "default", selector); err != nil {
113
		t.Fatalf("Error waiting for busybox mount pod to be up: %v", err)
114
	}
115

116
	mountTest := func() error {
117 118 119
		path := filepath.Join(tempDir, "frompod")
		out, err := ioutil.ReadFile(path)
		if err != nil {
120
			return err
121 122 123 124 125 126 127 128
		}
		// 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 {
129
			return err
130 131 132 133 134
		}
		if string(out) != expected {
			t.Fatalf("Expected file %s to contain text %s, was %s.", path, expected, out)
		}

135 136 137 138 139 140 141 142 143
		// test file timestamps are correct
		files := []string{"fromhost", "frompod"}
		for _, file := range files {
			statCmd := fmt.Sprintf("stat /mount-9p/%s", file)
			statOutput, err := minikubeRunner.SSH(statCmd)
			if err != nil {
				t.Fatalf("%v", err)
			}

144 145 146 147
			if runtime.GOOS == "windows" {
				if strings.Contains(statOutput, "Access: 1970-01-01") {
					t.Fatalf("Invalid access time\n%s", statOutput)
				}
148 149 150 151 152 153 154
			}

			if strings.Contains(statOutput, "Modify: 1970-01-01") {
				t.Fatalf("Invalid modify time\n%s", statOutput)
			}
		}

155 156 157
		// 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 {
M
Matt Rickard 已提交
158
			t.Fatalf("Expected file %s to be removed", path)
159 160 161 162 163
		}

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

		return nil
	}
169
	if err := util.Retry(t, mountTest, 5*time.Second, 40); err != nil {
170
		t.Fatalf("mountTest failed with error: %v", err)
171 172 173
	}

}