12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import os
15
16
import unittest
16
17
from unittest import mock
17
18
20
21
_GCP_METADATA_URL ,
21
22
GoogleCloudResourceDetector ,
22
23
get_gce_resources ,
24
+ get_gke_resources ,
23
25
)
24
26
25
- RESOURCES_JSON_STRING = {
27
+ NAMESPACE = "NAMESPACE"
28
+ CONTAINER_NAME = "CONTAINER_NAME"
29
+ HOSTNAME = "HOSTNAME"
30
+ POD_NAME = "POD_NAME"
31
+
32
+ GCE_RESOURCES_JSON_STRING = {
26
33
"instance" : {"id" : "instance_id" , "zone" : "projects/123/zones/zone" },
27
34
"project" : {"projectId" : "project_id" },
28
35
}
29
36
37
+ GKE_RESOURCES_JSON_STRING = {
38
+ "instance" : {
39
+ "id" : "instance_id" ,
40
+ "zone" : "projects/123/zones/zone" ,
41
+ "attributes" : {"cluster-name" : "cluster_name" },
42
+ },
43
+ "project" : {"projectId" : "project_id" },
44
+ }
45
+
30
46
47
+ @mock .patch (
48
+ "opentelemetry.tools.resource_detector.requests.get" ,
49
+ ** {"return_value.json.return_value" : GCE_RESOURCES_JSON_STRING }
50
+ )
31
51
class TestGCEResourceFinder (unittest .TestCase ):
32
- @mock .patch ("opentelemetry.tools.resource_detector.requests.get" )
33
52
def test_finding_gce_resources (self , getter ):
34
- getter .return_value .json .return_value = RESOURCES_JSON_STRING
35
53
found_resources = get_gce_resources ()
36
54
self .assertEqual (getter .call_args_list [0 ][0 ][0 ], _GCP_METADATA_URL )
37
55
self .assertEqual (
@@ -46,11 +64,123 @@ def test_finding_gce_resources(self, getter):
46
64
)
47
65
48
66
67
+ def pop_environ_key (key ):
68
+ if key in os .environ :
69
+ os .environ .pop (key )
70
+
71
+
72
+ def clear_gke_env_vars ():
73
+ pop_environ_key (CONTAINER_NAME )
74
+ pop_environ_key (NAMESPACE )
75
+ pop_environ_key (HOSTNAME )
76
+ pop_environ_key (POD_NAME )
77
+
78
+
79
+ @mock .patch (
80
+ "opentelemetry.tools.resource_detector.requests.get" ,
81
+ ** {"return_value.json.return_value" : GKE_RESOURCES_JSON_STRING }
82
+ )
83
+ class TestGKEResourceFinder (unittest .TestCase ):
84
+ def tearDown (self ) -> None :
85
+ clear_gke_env_vars ()
86
+
87
+ # pylint: disable=unused-argument
88
+ def test_missing_container_name (self , getter ):
89
+ pop_environ_key (CONTAINER_NAME )
90
+ self .assertEqual (get_gke_resources (), {})
91
+
92
+ # pylint: disable=unused-argument
93
+ def test_environment_empty_strings (self , getter ):
94
+ os .environ [CONTAINER_NAME ] = ""
95
+ os .environ [NAMESPACE ] = ""
96
+ found_resources = get_gke_resources ()
97
+ self .assertEqual (
98
+ found_resources ,
99
+ {
100
+ "cloud.account.id" : "project_id" ,
101
+ "k8s.cluster.name" : "cluster_name" ,
102
+ "k8s.namespace.name" : "" ,
103
+ "host.id" : "instance_id" ,
104
+ "k8s.pod.name" : "" ,
105
+ "container.name" : "" ,
106
+ "cloud.zone" : "zone" ,
107
+ "cloud.provider" : "gcp" ,
108
+ "gcp.resource_type" : "gke_container" ,
109
+ },
110
+ )
111
+
112
+ def test_missing_namespace_file (self , getter ):
113
+ os .environ [CONTAINER_NAME ] = "container_name"
114
+ found_resources = get_gke_resources ()
115
+ self .assertEqual (
116
+ found_resources ,
117
+ {
118
+ "cloud.account.id" : "project_id" ,
119
+ "k8s.cluster.name" : "cluster_name" ,
120
+ "k8s.namespace.name" : "" ,
121
+ "host.id" : "instance_id" ,
122
+ "k8s.pod.name" : "" ,
123
+ "container.name" : "container_name" ,
124
+ "cloud.zone" : "zone" ,
125
+ "cloud.provider" : "gcp" ,
126
+ "gcp.resource_type" : "gke_container" ,
127
+ },
128
+ )
129
+
130
+ def test_finding_gke_resources (self , getter ):
131
+ os .environ [NAMESPACE ] = "namespace"
132
+ os .environ [CONTAINER_NAME ] = "container_name"
133
+ os .environ [HOSTNAME ] = "host_name"
134
+ found_resources = get_gke_resources ()
135
+ self .assertEqual (getter .call_args_list [0 ][0 ][0 ], _GCP_METADATA_URL )
136
+ self .assertEqual (
137
+ found_resources ,
138
+ {
139
+ "cloud.account.id" : "project_id" ,
140
+ "k8s.cluster.name" : "cluster_name" ,
141
+ "k8s.namespace.name" : "namespace" ,
142
+ "host.id" : "instance_id" ,
143
+ "k8s.pod.name" : "host_name" ,
144
+ "container.name" : "container_name" ,
145
+ "cloud.zone" : "zone" ,
146
+ "cloud.provider" : "gcp" ,
147
+ "gcp.resource_type" : "gke_container" ,
148
+ },
149
+ )
150
+
151
+ def test_finding_gke_resources_with_pod_name (self , getter ):
152
+ os .environ [NAMESPACE ] = "namespace"
153
+ os .environ [CONTAINER_NAME ] = "container_name"
154
+ os .environ [HOSTNAME ] = "host_name"
155
+ os .environ [POD_NAME ] = "pod_name"
156
+ found_resources = get_gke_resources ()
157
+ self .assertEqual (getter .call_args_list [0 ][0 ][0 ], _GCP_METADATA_URL )
158
+ self .assertEqual (
159
+ found_resources ,
160
+ {
161
+ "cloud.account.id" : "project_id" ,
162
+ "k8s.cluster.name" : "cluster_name" ,
163
+ "k8s.namespace.name" : "namespace" ,
164
+ "host.id" : "instance_id" ,
165
+ "k8s.pod.name" : "pod_name" ,
166
+ "container.name" : "container_name" ,
167
+ "cloud.zone" : "zone" ,
168
+ "cloud.provider" : "gcp" ,
169
+ "gcp.resource_type" : "gke_container" ,
170
+ },
171
+ )
172
+
173
+
174
+ @mock .patch ("opentelemetry.tools.resource_detector.requests.get" )
49
175
class TestGoogleCloudResourceDetector (unittest .TestCase ):
50
- @mock .patch ("opentelemetry.tools.resource_detector.requests.get" )
51
- def test_finding_resources (self , getter ):
176
+ def tearDown (self ) -> None :
177
+ clear_gke_env_vars ()
178
+
179
+ def test_finding_gce_resources (self , getter ):
180
+ # The necessary env variables were not set for GKE resource detection
181
+ # to succeed. We should be falling back to detecting GCE resources
52
182
resource_finder = GoogleCloudResourceDetector ()
53
- getter .return_value .json .return_value = RESOURCES_JSON_STRING
183
+ getter .return_value .json .return_value = GCE_RESOURCES_JSON_STRING
54
184
found_resources = resource_finder .detect ()
55
185
self .assertEqual (getter .call_args_list [0 ][0 ][0 ], _GCP_METADATA_URL )
56
186
self .assertEqual (
@@ -82,3 +212,56 @@ def test_finding_resources(self, getter):
82
212
}
83
213
),
84
214
)
215
+
216
+ def test_finding_gke_resources (self , getter ):
217
+ # The necessary env variables were set for GKE resource detection
218
+ # to succeed. No GCE resource info should be extracted
219
+
220
+ os .environ [NAMESPACE ] = "namespace"
221
+ os .environ [CONTAINER_NAME ] = "container_name"
222
+ os .environ [HOSTNAME ] = "host_name"
223
+
224
+ resource_finder = GoogleCloudResourceDetector ()
225
+ getter .return_value .json .return_value = GKE_RESOURCES_JSON_STRING
226
+ found_resources = resource_finder .detect ()
227
+ self .assertEqual (getter .call_args_list [0 ][0 ][0 ], _GCP_METADATA_URL )
228
+ self .assertEqual (
229
+ found_resources ,
230
+ Resource (
231
+ labels = {
232
+ "cloud.account.id" : "project_id" ,
233
+ "k8s.cluster.name" : "cluster_name" ,
234
+ "k8s.namespace.name" : "namespace" ,
235
+ "host.id" : "instance_id" ,
236
+ "k8s.pod.name" : "host_name" ,
237
+ "container.name" : "container_name" ,
238
+ "cloud.zone" : "zone" ,
239
+ "cloud.provider" : "gcp" ,
240
+ "gcp.resource_type" : "gke_container" ,
241
+ }
242
+ ),
243
+ )
244
+ self .assertEqual (getter .call_count , 1 )
245
+
246
+ def test_resource_finding_fallback (self , getter ):
247
+ # The environment variables imply its on GKE, but the metadata doesn't
248
+ # have GKE information
249
+ getter .return_value .json .return_value = GCE_RESOURCES_JSON_STRING
250
+ os .environ [CONTAINER_NAME ] = "container_name"
251
+
252
+ # This detection will cause an error in get_gke_resources and should
253
+ # swallow the error and fall back to get_gce_resources
254
+ resource_finder = GoogleCloudResourceDetector ()
255
+ found_resources = resource_finder .detect ()
256
+ self .assertEqual (
257
+ found_resources ,
258
+ Resource (
259
+ labels = {
260
+ "host.id" : "instance_id" ,
261
+ "cloud.provider" : "gcp" ,
262
+ "cloud.account.id" : "project_id" ,
263
+ "cloud.zone" : "zone" ,
264
+ "gcp.resource_type" : "gce_instance" ,
265
+ }
266
+ ),
267
+ )
0 commit comments