credentials/alts: Assume not running on GCP if DMI not present (#2996)

fixes #2995
This commit is contained in:
Michael Hudson-Doyle 2019-08-27 12:06:13 +12:00 коммит произвёл Cesar Ghali
Родитель 45bd2846a3
Коммит d5a36f00e6
2 изменённых файлов: 37 добавлений и 13 удалений

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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) {