main_test.go 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
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 (
	"flag"
21
	"fmt"
22
	"math"
23
	"os"
T
Thomas Stromberg 已提交
24
	"runtime"
25
	"strconv"
26 27
	"strings"
	"testing"
28
	"time"
M
Medya Gh 已提交
29 30

	"k8s.io/minikube/pkg/minikube/driver"
31 32 33 34 35 36 37 38
)

// General configuration: used to set the VM Driver
var startArgs = flag.String("minikube-start-args", "", "Arguments to pass to minikube start")

// Flags for faster local integration testing
var forceProfile = flag.String("profile", "", "force tests to run against a particular profile")
var cleanup = flag.Bool("cleanup", true, "cleanup failed test run")
39
var enableGvisor = flag.Bool("gvisor", false, "run gvisor integration test (slow)")
40
var postMortemLogs = flag.Bool("postmortem-logs", true, "show logs after a failed test run")
41
var timeOutMultiplier = flag.Float64("timeout-multiplier", 1, "multiply the timeout for the tests")
42 43 44 45 46

// Paths to files - normally set for CI
var binaryPath = flag.String("binary", "../../out/minikube", "path to minikube binary")
var testdataDir = flag.String("testdata-dir", "testdata", "the directory relative to test/integration where the testdata lives")

47 48 49 50 51 52
// Node names are consistent, let's store these for easy access later
const (
	SecondNodeName = "m02"
	ThirdNodeName  = "m03"
)

53 54 55
// TestMain is the test main
func TestMain(m *testing.M) {
	flag.Parse()
56 57
	setMaxParallelism()

58 59 60 61
	start := time.Now()
	code := m.Run()
	fmt.Printf("Tests completed in %s (result code %d)\n", time.Since(start), code)
	os.Exit(code)
62 63
}

64 65
// setMaxParallelism caps the max parallelism. Go assumes 1 core per test, whereas minikube needs 2 cores per test.
func setMaxParallelism() {
66 67 68

	flagVal := flag.Lookup("test.parallel").Value.String()
	requested, err := strconv.Atoi(flagVal)
69
	if err != nil {
70
		fmt.Fprintf(os.Stderr, "unable to parse --test.parallel value: %q\n", flagVal)
71 72 73
		return
	}

74 75 76 77 78
	maxp := runtime.GOMAXPROCS(0)

	// Do not ignore what the user has explicitly set
	if requested != maxp {
		fmt.Fprintf(os.Stderr, "--test-parallel=%d was set via flags (system has %d cores)\n", requested, maxp)
79 80 81
		return
	}

82 83
	if maxp == 2 {
		fmt.Fprintf(os.Stderr, "Found %d cores, will not round down core count.\n", maxp)
84 85 86
		return
	}

87
	// Each "minikube start" consumes up to 2 cores, though the average usage is somewhat lower
88
	limit := int(math.Floor(float64(maxp) / 1.75))
89

90
	fmt.Fprintf(os.Stderr, "Found %d cores, limiting parallelism with --test.parallel=%d\n", maxp, limit)
91 92 93 94 95 96
	if err := flag.Set("test.parallel", strconv.Itoa(limit)); err != nil {
		fmt.Fprintf(os.Stderr, "Unable to set test.parallel: %v\n", err)
	}
	runtime.GOMAXPROCS(limit)
}

97 98 99 100 101 102 103 104 105 106 107 108
// StartArgs returns the arguments normally used for starting minikube
func StartArgs() []string {
	return strings.Split(*startArgs, " ")
}

// Target returns where the minikube binary can be found
func Target() string {
	return *binaryPath
}

// NoneDriver returns whether or not this test is using the none driver
func NoneDriver() bool {
109
	return strings.Contains(*startArgs, "--driver=none") || strings.Contains(*startArgs, "--vm-driver=none")
110
}
111 112 113

// HyperVDriver returns whether or not this test is using the Hyper-V driver
func HyperVDriver() bool {
114
	return strings.Contains(*startArgs, "--driver=hyperv") || strings.Contains(*startArgs, "--vm-driver=hyperv")
115
}
T
Thomas Stromberg 已提交
116

M
Medya Gh 已提交
117 118 119 120 121 122 123 124 125 126
// DockerDriver returns whether or not this test is using the docker or podman driver
func DockerDriver() bool {
	return strings.Contains(*startArgs, "--driver=docker") || strings.Contains(*startArgs, "--vm-driver=docker")
}

// PodmanDriver returns whether or not this test is using the docker or podman driver
func PodmanDriver() bool {
	return strings.Contains(*startArgs, "--vm-driver=podman") || strings.Contains(*startArgs, "driver=podman")
}

127 128
// KicDriver returns whether or not this test is using the docker or podman driver
func KicDriver() bool {
M
Medya Gh 已提交
129
	return DockerDriver() || PodmanDriver()
130 131
}

M
Medya Gh 已提交
132 133 134 135 136 137
// GithubActionRunner returns true if running inside a github action runner
func GithubActionRunner() bool {
	// based on https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables
	return os.Getenv("GITHUB_ACTIONS") == "true"
}

I
Ilya Zuyev 已提交
138 139
// arm64Platform returns true if running on arm64/* platform
func arm64Platform() bool {
I
Ilya Zuyev 已提交
140 141 142
	return runtime.GOARCH == "arm64"
}

143 144
// NeedsPortForward returns access to endpoints with this driver needs port forwarding
// (Docker on non-Linux platforms requires ports to be forwarded to 127.0.0.1)
T
Thomas Stromberg 已提交
145
func NeedsPortForward() bool {
M
Medya Gh 已提交
146
	return KicDriver() && (runtime.GOOS == "windows" || runtime.GOOS == "darwin") || driver.IsMicrosoftWSL()
T
Thomas Stromberg 已提交
147 148
}

T
Thomas Stromberg 已提交
149 150 151 152
// CanCleanup returns if cleanup is allowed
func CanCleanup() bool {
	return *cleanup
}
M
Medya Gh 已提交
153 154 155

// Minutes will return timeout in minutes based on how slow the machine is
func Minutes(n int) time.Duration {
156
	return time.Duration(*timeOutMultiplier) * time.Duration(n) * time.Minute
M
Medya Gh 已提交
157
}
M
Medya Gh 已提交
158 159 160

// Seconds will return timeout in minutes based on how slow the machine is
func Seconds(n int) time.Duration {
161
	return time.Duration(*timeOutMultiplier) * time.Duration(n) * time.Second
M
Medya Gh 已提交
162
}
M
Medya Gh 已提交
163 164 165 166 167

// TestingKicBaseImage will return true if the integraiton test is running against a passed --base-image flag
func TestingKicBaseImage() bool {
	return strings.Contains(*startArgs, "base-image")
}