29
29
CONTAINER_NAME = "CONTAINER_NAME"
30
30
HOSTNAME = "HOSTNAME"
31
31
POD_NAME = "POD_NAME"
32
+ KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST"
32
33
33
34
GCE_RESOURCES_JSON_STRING = {
34
35
"instance" : {"id" : "instance_id" , "zone" : "projects/123/zones/zone" },
@@ -85,13 +86,34 @@ class TestGKEResourceFinder(unittest.TestCase):
85
86
def tearDown (self ) -> None :
86
87
clear_gke_env_vars ()
87
88
89
+ # pylint: disable=unused-argument
90
+ def test_not_running_on_gke (self , getter ):
91
+ pop_environ_key (KUBERNETES_SERVICE_HOST )
92
+ found_resources = get_gke_resources ()
93
+ self .assertEqual (found_resources , {})
94
+
88
95
# pylint: disable=unused-argument
89
96
def test_missing_container_name (self , getter ):
97
+ os .environ [KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
90
98
pop_environ_key (CONTAINER_NAME )
91
- self .assertEqual (get_gke_resources (), {})
99
+ found_resources = get_gke_resources ()
100
+ self .assertEqual (
101
+ found_resources ,
102
+ {
103
+ "cloud.account.id" : "project_id" ,
104
+ "k8s.cluster.name" : "cluster_name" ,
105
+ "k8s.namespace.name" : "" ,
106
+ "host.id" : "instance_id" ,
107
+ "k8s.pod.name" : "" ,
108
+ "cloud.zone" : "zone" ,
109
+ "cloud.provider" : "gcp" ,
110
+ "gcp.resource_type" : "gke_container" ,
111
+ },
112
+ )
92
113
93
114
# pylint: disable=unused-argument
94
115
def test_environment_empty_strings (self , getter ):
116
+ os .environ [KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
95
117
os .environ [CONTAINER_NAME ] = ""
96
118
os .environ [NAMESPACE ] = ""
97
119
found_resources = get_gke_resources ()
@@ -111,6 +133,7 @@ def test_environment_empty_strings(self, getter):
111
133
)
112
134
113
135
def test_missing_namespace_file (self , getter ):
136
+ os .environ [KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
114
137
os .environ [CONTAINER_NAME ] = "container_name"
115
138
found_resources = get_gke_resources ()
116
139
self .assertEqual (
@@ -129,6 +152,7 @@ def test_missing_namespace_file(self, getter):
129
152
)
130
153
131
154
def test_finding_gke_resources (self , getter ):
155
+ os .environ [KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
132
156
os .environ [NAMESPACE ] = "namespace"
133
157
os .environ [CONTAINER_NAME ] = "container_name"
134
158
os .environ [HOSTNAME ] = "host_name"
@@ -150,6 +174,7 @@ def test_finding_gke_resources(self, getter):
150
174
)
151
175
152
176
def test_finding_gke_resources_with_pod_name (self , getter ):
177
+ os .environ [KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
153
178
os .environ [NAMESPACE ] = "namespace"
154
179
os .environ [CONTAINER_NAME ] = "container_name"
155
180
os .environ [HOSTNAME ] = "host_name"
@@ -182,6 +207,7 @@ def tearDown(self) -> None:
182
207
def test_finding_gce_resources (self , getter ):
183
208
# The necessary env variables were not set for GKE resource detection
184
209
# to succeed. We should be falling back to detecting GCE resources
210
+ pop_environ_key (KUBERNETES_SERVICE_HOST )
185
211
resource_finder = GoogleCloudResourceDetector ()
186
212
getter .return_value .json .return_value = GCE_RESOURCES_JSON_STRING
187
213
found_resources = resource_finder .detect ()
@@ -219,7 +245,7 @@ def test_finding_gce_resources(self, getter):
219
245
def test_finding_gke_resources (self , getter ):
220
246
# The necessary env variables were set for GKE resource detection
221
247
# to succeed. No GCE resource info should be extracted
222
-
248
+ os . environ [ KUBERNETES_SERVICE_HOST ] = "10.0.0.1"
223
249
os .environ [NAMESPACE ] = "namespace"
224
250
os .environ [CONTAINER_NAME ] = "container_name"
225
251
os .environ [HOSTNAME ] = "host_name"
0 commit comments