-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for cloud run detector.
- Loading branch information
Showing
3 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"go.opentelemetry.io/otel/label" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
var ( | ||
notOnGCE = func() bool { return false } | ||
onGCE = func() bool { return true } | ||
) | ||
|
||
func getenv(m map[string]string) func(string) string { | ||
return func(s string) string { | ||
if m == nil { | ||
return "" | ||
} | ||
return m[s] | ||
} | ||
} | ||
|
||
type client struct { | ||
m map[string]string | ||
} | ||
|
||
func (c *client) Get(s string) (string, error) { | ||
got, ok := c.m[s] | ||
if !ok { | ||
return "", fmt.Errorf("%q do not exist", s) | ||
} else if got == "" { | ||
return "", fmt.Errorf("%q is empty", s) | ||
} | ||
return got, nil | ||
} | ||
|
||
func (c *client) InstanceID() (string, error) { | ||
return c.Get("instance/id") | ||
} | ||
|
||
func (c *client) ProjectID() (string, error) { | ||
return c.Get("project/project-id") | ||
} | ||
|
||
var _ metadataClient = (*client)(nil) | ||
|
||
func TestCloudRunDetector_NotOnGCE(t *testing.T) { | ||
ctx := context.Background() | ||
c := NewCloudRun() | ||
c.setupForTest(nil, notOnGCE, getenv(nil)) | ||
|
||
if res, err := c.Detect(ctx); res != nil || err != nil { | ||
t.Errorf("Expect c.Detect(ctx) to return (nil, nil), got (%v, %v)", res, err) | ||
} | ||
} | ||
|
||
func TestCloudRunDetector_ExpectSuccess(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
metadata := map[string]string{ | ||
"project/project-id": "foo", | ||
"instance/id": "bar", | ||
"instance/region": "utopia", | ||
} | ||
envvars := map[string]string{ | ||
"K_SERVICE": "x-service", | ||
} | ||
want := resource.New( | ||
label.String("cloud.account.id", "foo"), | ||
label.String("cloud.provider", "gcp"), | ||
label.String("cloud.region", "utopia"), | ||
label.String("service.instance.id", "bar"), | ||
label.String("service.name", "x-service"), | ||
label.String("service.namespace", "x-service"), | ||
) | ||
c := NewCloudRun() | ||
c.setupForTest(&client{m: metadata}, onGCE, getenv(envvars)) | ||
|
||
if res, err := c.Detect(ctx); err != nil { | ||
t.Fatalf("got unexpected failure: %v", err) | ||
} else if diff := cmp.Diff(want, res); diff != "" { | ||
t.Errorf("detected resource differ from expected (-want, +got)\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCloudRunDetector_ExpectFail(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
metadata map[string]string | ||
envvars map[string]string | ||
}{ | ||
{ | ||
name: "Missing ProjectID", | ||
metadata: map[string]string{ | ||
"instance/id": "bar", | ||
"instance/region": "utopia", | ||
}, | ||
envvars: map[string]string{ | ||
"K_SERVICE": "x-service", | ||
}, | ||
}, | ||
{ | ||
name: "Missing InstanceID", | ||
metadata: map[string]string{ | ||
"project/project-id": "foo", | ||
"instance/region": "utopia", | ||
}, | ||
envvars: map[string]string{ | ||
"K_SERVICE": "x-service", | ||
}, | ||
}, | ||
{ | ||
name: "Missing Region", | ||
metadata: map[string]string{ | ||
"project/project-id": "foo", | ||
"instance/id": "bar", | ||
}, | ||
envvars: map[string]string{ | ||
"K_SERVICE": "x-service", | ||
}, | ||
}, | ||
{ | ||
name: "Missing K_SERVICE envvar", | ||
metadata: map[string]string{ | ||
"project/project-id": "foo", | ||
"instance/id": "bar", | ||
"instance/region": "utopia", | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
c := NewCloudRun() | ||
c.setupForTest(&client{m: test.metadata}, onGCE, getenv(test.envvars)) | ||
|
||
if res, err := c.Detect(ctx); err == nil { | ||
t.Errorf("Expect c.Detect(ctx) to return error, got nil (resource: %v)", res) | ||
} else { | ||
t.Logf("err: %v", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters