diff --git a/credentials/alts/utils.go b/credentials/alts/utils.go index 4ed27c605b6..f13aeef1c47 100644 --- a/credentials/alts/utils.go +++ b/credentials/alts/utils.go @@ -83,6 +83,9 @@ var ( // running on GCP. func isRunningOnGCP() bool { manufacturer, err := readManufacturer() + if os.IsNotExist(err) { + return false + } if err != nil { log.Fatalf("failure to read manufacturer information: %v", err) } diff --git a/credentials/alts/utils_test.go b/credentials/alts/utils_test.go index 3c7e43db14a..8935c5fbec8 100644 --- a/credentials/alts/utils_test.go +++ b/credentials/alts/utils_test.go @@ -21,6 +21,7 @@ package alts import ( "context" "io" + "os" "strings" "testing" @@ -28,6 +29,34 @@ import ( "google.golang.org/grpc/peer" ) +func setupManufacturerReader(testOS string, reader func() (io.Reader, error)) func() { + tmpOS := runningOS + tmpReader := manufacturerReader + + // Set test OS and reader function. + runningOS = testOS + manufacturerReader = reader + return func() { + runningOS = tmpOS + manufacturerReader = tmpReader + } + +} + +func setup(testOS string, testReader io.Reader) func() { + reader := func() (io.Reader, error) { + return testReader, nil + } + return setupManufacturerReader(testOS, reader) +} + +func setupError(testOS string, err error) func() { + reader := func() (io.Reader, error) { + return nil, err + } + return setupManufacturerReader(testOS, reader) +} + func TestIsRunningOnGCP(t *testing.T) { for _, tc := range []struct { description string @@ -53,20 +82,12 @@ func TestIsRunningOnGCP(t *testing.T) { } } -func setup(testOS string, testReader io.Reader) func() { - tmpOS := runningOS - tmpReader := manufacturerReader - - // Set test OS and reader function. - runningOS = testOS - manufacturerReader = func() (io.Reader, error) { - return testReader, nil - } - - return func() { - runningOS = tmpOS - manufacturerReader = tmpReader +func TestIsRunningOnGCPNoProductNameFile(t *testing.T) { + reverseFunc := setupError("linux", os.ErrNotExist) + if isRunningOnGCP() { + t.Errorf("ErrNotExist: isRunningOnGCP()=true, want false") } + reverseFunc() } func TestAuthInfoFromContext(t *testing.T) {