Skip to content

Commit

Permalink
Add unit test for cloud run detector.
Browse files Browse the repository at this point in the history
  • Loading branch information
yegle committed Nov 19, 2020
1 parent 5d5c9c9 commit 79bcca9
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 7 deletions.
41 changes: 34 additions & 7 deletions detectors/gcp/cloud-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,45 @@ import (
"go.opentelemetry.io/otel/semconv"
)

type metadataClient interface {
ProjectID() (string, error)
Get(string) (string, error)
InstanceID() (string, error)
}

// CloudRun collects resource information of Cloud Run instance.
type CloudRun struct{}
type CloudRun struct {
mc metadataClient
onGCE func() bool
getenv func(string) string
}

// compile time assertion that CloudRun implements the resource.Detector
// interface.
var _ resource.Detector = (*CloudRun)(nil)

// NewCloudRun creates a CloudRun detector
// Specify nil to use the default metadata client.
func NewCloudRun() *CloudRun {
return &CloudRun{
mc: metadata.NewClient(nil),
onGCE: metadata.OnGCE,
getenv: os.Getenv,
}
}

// for test only
func (c *CloudRun) setupForTest(mc metadataClient, ongce func() bool, getenv func(string) string) {
c.mc = mc
c.onGCE = ongce
c.getenv = getenv
}

// Detect detects associated resources when running on Cloud Run hosts.
func (cloudrun *CloudRun) Detect(ctx context.Context) (*resource.Resource, error) {
func (c *CloudRun) Detect(ctx context.Context) (*resource.Resource, error) {
// .OnGCE is actually testing whether the metadata server is available.
// Metadata server is supported on Cloud Run.
if !metadata.OnGCE() {
if !c.onGCE() {
return nil, nil
}

Expand All @@ -47,19 +74,19 @@ func (cloudrun *CloudRun) Detect(ctx context.Context) (*resource.Resource, error

var errInfo []string

if projectID, err := metadata.ProjectID(); hasProblem(err) {
if projectID, err := c.mc.ProjectID(); hasProblem(err) {
errInfo = append(errInfo, err.Error())
} else if projectID != "" {
labels = append(labels, semconv.CloudAccountIDKey.String(projectID))
}

if region, err := metadata.Get("instance/region"); hasProblem(err) {
if region, err := c.mc.Get("instance/region"); hasProblem(err) {
errInfo = append(errInfo, err.Error())
} else if region != "" {
labels = append(labels, semconv.CloudRegionKey.String(region))
}

if instanceID, err := metadata.InstanceID(); hasProblem(err) {
if instanceID, err := c.mc.InstanceID(); hasProblem(err) {
errInfo = append(errInfo, err.Error())
} else if instanceID != "" {
labels = append(labels, semconv.ServiceInstanceIDKey.String(instanceID))
Expand All @@ -69,7 +96,7 @@ func (cloudrun *CloudRun) Detect(ctx context.Context) (*resource.Resource, error
// See https://cloud.google.com/run/docs/reference/container-contract
// The same K_SERVICE value ultimately maps to both `namespace` and
// `job` label of `generic_task` metric type.
if service := os.Getenv("K_SERVICE"); service == "" {
if service := c.getenv("K_SERVICE"); service == "" {
errInfo = append(errInfo, "envvar K_SERVICE contains empty string.")
} else {
labels = append(labels,
Expand Down
149 changes: 149 additions & 0 deletions detectors/gcp/cloud-run_test.go
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)
}
})
}
}
1 change: 1 addition & 0 deletions detectors/gcp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
cloud.google.com/go v0.72.0
github.com/google/go-cmp v0.5.2
go.opentelemetry.io/otel v0.13.0
go.opentelemetry.io/otel/sdk v0.13.0
)

0 comments on commit 79bcca9

Please sign in to comment.