none_test.go 2.6 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
// +build linux

/*
Copyright 2019 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 (
23
	"context"
24
	"os"
25
	"os/exec"
26 27 28 29 30
	"os/user"
	"path/filepath"
	"strconv"
	"syscall"
	"testing"
31

32
	"k8s.io/minikube/pkg/minikube/localpath"
33 34
)

35 36 37
// None-driver specific test for CHANGE_MINIKUBE_NONE_USER
func TestChangeNoneUser(t *testing.T) {
	if !NoneDriver() {
38 39
		t.Skip("Only test none driver.")
	}
40
	MaybeParallel(t)
41

42
	profile := UniqueProfileName("none")
M
Medya Gh 已提交
43
	ctx, cancel := context.WithTimeout(context.Background(), Minutes(10))
44
	defer CleanupWithLogs(t, profile, cancel)
45

46 47
	startArgs := append([]string{"CHANGE_MINIKUBE_NONE_USER=true", Target(), "start", "--wait=false"}, StartArgs()...)
	rr, err := Run(t, exec.CommandContext(ctx, "/usr/bin/env", startArgs...))
48
	if err != nil {
49
		t.Errorf("%s failed: %v", rr.Args, err)
50
	}
51 52

	rr, err = Run(t, exec.CommandContext(ctx, Target(), "delete"))
53
	if err != nil {
54
		t.Errorf("%s failed: %v", rr.Args, err)
55
	}
56 57

	rr, err = Run(t, exec.CommandContext(ctx, "/usr/bin/env", startArgs...))
58
	if err != nil {
59
		t.Errorf("%s failed: %v", rr.Args, err)
60 61
	}

62 63 64
	rr, err = Run(t, exec.CommandContext(ctx, Target(), "status"))
	if err != nil {
		t.Errorf("%s failed: %v", rr.Args, err)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	}

	username := os.Getenv("SUDO_USER")
	if username == "" {
		t.Fatal("Expected $SUDO_USER env to not be empty")
	}
	u, err := user.Lookup(username)
	if err != nil {
		t.Fatalf("Getting user failed: %v", err)
	}
	uid, err := strconv.Atoi(u.Uid)
	if err != nil {
		t.Errorf("Failed to convert uid to int: %v", err)
	}

80 81 82 83 84 85 86
	// Retrieve the kube config from env
	kubeConfig := os.Getenv("KUBECONFIG")
	if kubeConfig == "" {
		kubeConfig = filepath.Join(u.HomeDir, ".kube/config")
	}

	for _, p := range []string{localpath.MiniPath(), kubeConfig} {
87 88 89
		info, err := os.Stat(p)
		if err != nil {
			t.Errorf("stat(%s): %v", p, err)
90 91
			continue
		}
T
Thomas Stromberg 已提交
92
		if info == nil || info.Sys() == nil {
93 94
			t.Errorf("nil info for %s", p)
			continue
95 96 97 98 99
		}
		got := info.Sys().(*syscall.Stat_t).Uid
		if got != uint32(uid) {
			t.Errorf("uid(%s) = %d, want %d", p, got, uint32(uid))
		}
100 101
	}
}