| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package gcp |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-cmp/cmp" |
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/sdk/resource" |
| 13 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 14 |
) |
| 15 |
|
| 16 |
var errTest = errors.New("testError") |
| 17 |
|
| 18 |
const ( |
| 19 |
projectIDValue = "some-projectID" |
| 20 |
regionValue = "some-region" |
| 21 |
functionName = "sample-function" |
| 22 |
) |
| 23 |
|
| 24 |
type metaDataClientImpl struct { |
| 25 |
projectID func() (string, error) |
| 26 |
get func(string) (string, error) |
| 27 |
instanceID func() (string, error) |
| 28 |
} |
| 29 |
|
| 30 |
func (mock *metaDataClientImpl) ProjectID() (string, error) { |
| 31 |
if mock.projectID != nil { |
| 32 |
return mock.projectID() |
| 33 |
} |
| 34 |
return "", nil |
| 35 |
} |
| 36 |
|
| 37 |
func (mock *metaDataClientImpl) Get(key string) (string, error) { |
| 38 |
if mock.get != nil { |
| 39 |
return mock.get(key) |
| 40 |
} |
| 41 |
return "", nil |
| 42 |
} |
| 43 |
|
| 44 |
func (mock *metaDataClientImpl) InstanceID() (string, error) { |
| 45 |
if mock.instanceID != nil { |
| 46 |
return mock.instanceID() |
| 47 |
} |
| 48 |
return "", nil |
| 49 |
} |
| 50 |
|
| 51 |
type want struct { |
| 52 |
res *resource.Resource |
| 53 |
err error |
| 54 |
} |
| 55 |
|
| 56 |
func TestCloudFunctionDetect(t *testing.T) { |
| 57 |
t.Setenv(gcpFunctionNameKey, functionName) |
| 58 |
|
| 59 |
tests := []struct { |
| 60 |
name string |
| 61 |
cr *CloudRun |
| 62 |
expected want |
| 63 |
}{ |
| 64 |
{ |
| 65 |
name: "error in reading ProjectID", |
| 66 |
cr: &CloudRun{ |
| 67 |
mc: &metaDataClientImpl{ |
| 68 |
projectID: func() (string, error) { |
| 69 |
return "", errTest |
| 70 |
}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
expected: want{ |
| 74 |
res: nil, |
| 75 |
err: errTest, |
| 76 |
}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "error in reading region", |
| 80 |
cr: &CloudRun{ |
| 81 |
mc: &metaDataClientImpl{ |
| 82 |
get: func(string) (string, error) { |
| 83 |
return "", errTest |
| 84 |
}, |
| 85 |
}, |
| 86 |
}, |
| 87 |
expected: want{ |
| 88 |
res: nil, |
| 89 |
err: errTest, |
| 90 |
}, |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "success", |
| 94 |
cr: &CloudRun{ |
| 95 |
mc: &metaDataClientImpl{ |
| 96 |
projectID: func() (string, error) { |
| 97 |
return projectIDValue, nil |
| 98 |
}, |
| 99 |
get: func(string) (string, error) { |
| 100 |
return regionValue, nil |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
expected: want{ |
| 105 |
res: resource.NewSchemaless([]attribute.KeyValue{ |
| 106 |
semconv.CloudProviderGCP, |
| 107 |
semconv.CloudPlatformGCPCloudFunctions, |
| 108 |
semconv.FaaSName(functionName), |
| 109 |
semconv.CloudAccountID(projectIDValue), |
| 110 |
semconv.CloudRegion(regionValue), |
| 111 |
}...), |
| 112 |
err: nil, |
| 113 |
}, |
| 114 |
}, |
| 115 |
} |
| 116 |
|
| 117 |
for _, test := range tests { |
| 118 |
detector := cloudFunction{ |
| 119 |
cloudRun: test.cr, |
| 120 |
} |
| 121 |
res, err := detector.Detect(t.Context()) |
| 122 |
if !errors.Is(err, test.expected.err) { |
| 123 |
t.Fatalf("got unexpected failure: %v", err) |
| 124 |
} else if diff := cmp.Diff(test.expected.res, res); diff != "" { |
| 125 |
t.Errorf("detected resource differ from expected (-want, +got)\n%s", diff) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestNotOnCloudFunction(t *testing.T) { |
| 131 |
detector := NewCloudFunction() |
| 132 |
res, err := detector.Detect(t.Context()) |
| 133 |
if err != nil { |
| 134 |
t.Errorf("expected cloud function detector to return error as nil, but returned %v", err) |
| 135 |
} else if res != nil { |
| 136 |
t.Errorf("expected cloud function detector to return resource as nil, but returned %v", res) |
| 137 |
} |
| 138 |
} |