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