account for GCE in addons test

pull/10730/head
Sharif Elgamal 2021-03-09 14:32:02 -08:00
parent 15c44bf7ac
commit eb1ee1f931
4 changed files with 45 additions and 17 deletions

View File

@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/exec"
"path"
@ -53,6 +52,7 @@ import (
"k8s.io/minikube/pkg/minikube/storageclass"
"k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/minikube/sysinit"
"k8s.io/minikube/pkg/util"
"k8s.io/minikube/pkg/util/retry"
)
@ -514,7 +514,7 @@ func enableOrDisableGCPAuth(cfg *config.ClusterConfig, name string, val string)
}
func enableAddonGCPAuth(cfg *config.ClusterConfig) error {
if !Force && isGCE() {
if !Force && util.IsOnGCE() {
exit.Message(reason.InternalCredsNotFound, "It seems that you are running in GCE, which means authentication should work without the GCP Auth addon. If you would still like to authenticate using a credentials file, use the --force flag.")
}
@ -587,16 +587,3 @@ func disableAddonGCPAuth(cfg *config.ClusterConfig) error {
return nil
}
func isGCE() bool {
resp, err := http.Get("http://metadata.google.internal")
if err != nil {
return false
}
if resp.Header.Get("Metadata-Flavor") == "Google" {
return true
}
return false
}

View File

@ -18,6 +18,7 @@ package util
import (
"fmt"
"net/http"
"os"
"os/user"
"path/filepath"
@ -109,3 +110,17 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
func ParseKubernetesVersion(version string) (semver.Version, error) {
return semver.Make(version[1:])
}
// IsOnGCE determines whether minikube is currently running on GCE.
func IsOnGCE() bool {
resp, err := http.Get("http://metadata.google.internal")
if err != nil {
return false
}
if resp.Header.Get("Metadata-Flavor") == "Google" {
return true
}
return false
}

View File

@ -36,6 +36,7 @@ import (
"github.com/hashicorp/go-retryablehttp"
"k8s.io/minikube/pkg/kapi"
"k8s.io/minikube/pkg/util"
"k8s.io/minikube/pkg/util/retry"
)
@ -58,18 +59,43 @@ func TestAddons(t *testing.T) {
t.Fatalf("Failed setting GOOGLE_CLOUD_PROJECT env var: %v", err)
}
args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--force", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver", "--addons=gcp-auth"}, StartArgs()...)
args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver"}, StartArgs()...)
if !(runtime.GOOS == "darwin" && KicDriver()) { // macos docker driver does not support ingress
args = append(args, "--addons=ingress")
}
if !arm64Platform() {
args = append(args, "--addons=helm-tiller")
}
if !util.IsOnGCE() {
args = append(args, "--addons=gcp-auth")
}
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Fatalf("%s failed: %v", rr.Command(), err)
}
// If we're running the integration tests on GCE, which is frequently the case, first check to make sure we exit out properly,
// then use force to actually test using creds.
if util.IsOnGCE() {
args = append([]string{"addons", "enable", "gcp-auth"})
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err == nil {
t.Errorf("Expected error but didn't get one. command %v, output %v", rr.Command(), rr.Output())
} else {
if !strings.Contains(rr.Output(), "It seems that you are running in GCE") {
t.Errorf("Unexpected error message: %v", rr.Output())
} else {
// ok, use force here since we are in GCE
// do not use --force unless absolutely necessary
args = append(args, "--force")
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Errorf("%s failed: %v", rr.Command(), err)
}
}
}
}
// Parallelized tests
t.Run("parallel", func(t *testing.T) {
tests := []struct {

View File

@ -86,7 +86,7 @@ func (rr RunResult) Output() string {
return sb.String()
}
// Run is a test helper to log a command being executed \_(ツ)_/¯
// Run is a test helper to log a command being executed ¯\_(ツ)_/¯
func Run(t *testing.T, cmd *exec.Cmd) (*RunResult, error) {
t.Helper()
rr := &RunResult{Args: cmd.Args}