Make a better mock interface, detect integration tests

pull/7896/head
Thomas Stromberg 2020-04-29 09:46:20 -07:00
parent 0cd6a01180
commit e9bf83d5f0
3 changed files with 22 additions and 13 deletions

View File

@ -29,10 +29,14 @@ import (
) )
var ( var (
// Mock allows tests to toggle making actual HTTP downloads mockMode = false
Mock = false
) )
// EnableMock allows tests to selectively enable if downloads are mocked
func EnableMock(b bool) {
mockMode = b
}
// download is a well-configured atomic download function // download is a well-configured atomic download function
func download(src string, dst string) error { func download(src string, dst string) error {
tmpDst := dst + ".download" tmpDst := dst + ".download"
@ -54,7 +58,7 @@ func download(src string, dst string) error {
} }
// Don't bother with getter.MockGetter, as we don't provide a way to inspect the outcome // Don't bother with getter.MockGetter, as we don't provide a way to inspect the outcome
if Mock { if mockMode {
glog.Infof("Mock download: %s -> %s", src, dst) glog.Infof("Mock download: %s -> %s", src, dst)
// Callers expect the file to exist // Callers expect the file to exist
_, err := os.Create(dst) _, err := os.Create(dst)
@ -62,7 +66,7 @@ func download(src string, dst string) error {
} }
// Politely prevent tests from shooting themselves in the foot // Politely prevent tests from shooting themselves in the foot
if underTest() { if withinUnitTest() {
return fmt.Errorf("unmocked download under test") return fmt.Errorf("unmocked download under test")
} }
@ -73,7 +77,12 @@ func download(src string, dst string) error {
return os.Rename(tmpDst, dst) return os.Rename(tmpDst, dst)
} }
// detect if we are under test // withinUnitTset detects if we are in running within a unit-test
func underTest() bool { func withinUnitTest() bool {
// Nope, it's the integration test
if flag.Lookup("minikube-start-args") != nil || strings.HasPrefix(filepath.Base(os.Args[0]), "e2e-") {
return false
}
return flag.Lookup("test.v") != nil || strings.HasSuffix(os.Args[0], "test") return flag.Lookup("test.v") != nil || strings.HasSuffix(os.Args[0], "test")
} }

View File

@ -83,7 +83,7 @@ func TestCopyBinary(t *testing.T) {
} }
func TestCacheBinariesForBootstrapper(t *testing.T) { func TestCacheBinariesForBootstrapper(t *testing.T) {
download.Mock = true download.EnableMock(true)
oldMinikubeHome := os.Getenv("MINIKUBE_HOME") oldMinikubeHome := os.Getenv("MINIKUBE_HOME")
defer os.Setenv("MINIKUBE_HOME", oldMinikubeHome) defer os.Setenv("MINIKUBE_HOME", oldMinikubeHome)

View File

@ -71,7 +71,7 @@ var defaultClusterConfig = config.ClusterConfig{
} }
func TestCreateHost(t *testing.T) { func TestCreateHost(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)
@ -116,7 +116,7 @@ func TestCreateHost(t *testing.T) {
} }
func TestStartHostExists(t *testing.T) { func TestStartHostExists(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)
@ -155,7 +155,7 @@ func TestStartHostExists(t *testing.T) {
} }
func TestStartHostErrMachineNotExist(t *testing.T) { func TestStartHostErrMachineNotExist(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)
@ -202,7 +202,7 @@ func TestStartHostErrMachineNotExist(t *testing.T) {
} }
func TestStartStoppedHost(t *testing.T) { func TestStartStoppedHost(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)
@ -239,7 +239,7 @@ func TestStartStoppedHost(t *testing.T) {
} }
func TestStartHost(t *testing.T) { func TestStartHost(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)
@ -269,7 +269,7 @@ func TestStartHost(t *testing.T) {
} }
func TestStartHostConfig(t *testing.T) { func TestStartHostConfig(t *testing.T) {
download.Mock = true download.EnableMock(true)
RegisterMockDriver(t) RegisterMockDriver(t)
api := tests.NewMockAPI(t) api := tests.NewMockAPI(t)