diff --git a/detectors/gcp/detector_test.go b/detectors/gcp/detector_test.go index 705bcf0c..16a5f5e7 100644 --- a/detectors/gcp/detector_test.go +++ b/detectors/gcp/detector_test.go @@ -43,7 +43,11 @@ func TestCloudPlatformAppEngineStandard(t *testing.T) { } func TestCloudPlatformGKE(t *testing.T) { - d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{ + d := NewTestDetector(&FakeMetadataProvider{ + InstanceAttributes: map[string]string{ + clusterNameMetadataAttr: "cluster-name", + }, + }, &FakeOSProvider{ Vars: map[string]string{ k8sServiceHostEnv: "foo", }, @@ -53,12 +57,18 @@ func TestCloudPlatformGKE(t *testing.T) { } func TestCloudPlatformK8sNotGKE(t *testing.T) { - d := NewTestDetector(&FakeMetadataProvider{Err: fmt.Errorf("foo")}, &FakeOSProvider{ + d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{ Vars: map[string]string{ k8sServiceHostEnv: "foo", }, }) platform := d.CloudPlatform() + assert.Equal(t, platform, GCE) +} + +func TestCloudPlatformUnknown(t *testing.T) { + d := NewTestDetector(&FakeMetadataProvider{Err: fmt.Errorf("no metadata server")}, &FakeOSProvider{}) + platform := d.CloudPlatform() assert.Equal(t, platform, UnknownPlatform) } diff --git a/detectors/gcp/utils_test.go b/detectors/gcp/utils_test.go index 212c4418..28417725 100644 --- a/detectors/gcp/utils_test.go +++ b/detectors/gcp/utils_test.go @@ -14,6 +14,8 @@ package gcp +import "cloud.google.com/go/compute/metadata" + func NewTestDetector(metadata *FakeMetadataProvider, os *FakeOSProvider) *Detector { return &Detector{metadata: metadata, os: os} } @@ -69,7 +71,11 @@ func (f *FakeMetadataProvider) InstanceAttributeValue(s string) (string, error) if f.Err != nil { return "", f.Err } - return f.InstanceAttributes[s], nil + value, ok := f.InstanceAttributes[s] + if ok { + return value, nil + } + return "", metadata.NotDefinedError(s) } type FakeOSProvider struct {