Skip to content

Commit

Permalink
Improve test coverage by correctly simulating a non-GKE GCE VM
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinmit committed Sep 24, 2024
1 parent bb8d9a8 commit 166eefb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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)
}

Expand Down
8 changes: 7 additions & 1 deletion detectors/gcp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 166eefb

Please sign in to comment.