1515import * as sinon from 'sinon' ;
1616import * as metadata from 'gcp-metadata' ;
1717
18- import { GcpDetector , GcpDetectorSync } from '../../src/detector/detector' ;
18+ import { GcpDetectorSync } from '../../src/detector/detector' ;
1919import {
2020 detectResources ,
2121 ResourceDetector ,
@@ -29,196 +29,6 @@ async function detectAndWait(detector: ResourceDetector): Promise<Resource> {
2929 return resource ;
3030}
3131
32- describe ( 'GcpDetector' , ( ) => {
33- let metadataStub : sinon . SinonStubbedInstance < typeof metadata > ;
34- let envStub : NodeJS . ProcessEnv ;
35- beforeEach ( ( ) => {
36- metadataStub = sinon . stub ( metadata ) ;
37- metadataStub . isAvailable . resolves ( true ) ;
38- metadataStub . project . withArgs ( 'project-id' ) . resolves ( 'fake-project-id' ) ;
39-
40- envStub = sinon . replace ( process , 'env' , { } ) ;
41- } ) ;
42-
43- afterEach ( ( ) => {
44- sinon . restore ( ) ;
45- } ) ;
46-
47- it ( 'returns empty resource when metadata server is not available' , async ( ) => {
48- metadataStub . isAvailable . resolves ( false ) ;
49- const resource = await new GcpDetector ( ) . detect ( ) ;
50- assert . deepStrictEqual ( resource . attributes , { } ) ;
51- } ) ;
52-
53- describe ( 'detects a GKE resource' , ( ) => {
54- beforeEach ( ( ) => {
55- envStub . KUBERNETES_SERVICE_HOST = 'fake-service-host' ;
56- metadataStub . instance
57- . withArgs ( 'id' )
58- . resolves ( 12345 )
59-
60- . withArgs ( 'attributes/cluster-name' )
61- . resolves ( 'fake-cluster-name' ) ;
62- } ) ;
63-
64- it ( 'zonal' , async ( ) => {
65- metadataStub . instance
66- . withArgs ( 'attributes/cluster-location' )
67- . resolves ( 'us-east4-b' ) ;
68- const resource = await new GcpDetector ( ) . detect ( ) ;
69- assert . deepStrictEqual ( resource . attributes , {
70- 'cloud.account.id' : 'fake-project-id' ,
71- 'cloud.availability_zone' : 'us-east4-b' ,
72- 'cloud.platform' : 'gcp_kubernetes_engine' ,
73- 'cloud.provider' : 'gcp' ,
74- 'host.id' : '12345' ,
75- 'k8s.cluster.name' : 'fake-cluster-name' ,
76- } ) ;
77- } ) ;
78-
79- it ( 'regional' , async ( ) => {
80- metadataStub . instance
81- . withArgs ( 'attributes/cluster-location' )
82- . resolves ( 'us-east4' ) ;
83- const resource = await new GcpDetector ( ) . detect ( ) ;
84- assert . deepStrictEqual ( resource . attributes , {
85- 'cloud.account.id' : 'fake-project-id' ,
86- 'cloud.platform' : 'gcp_kubernetes_engine' ,
87- 'cloud.provider' : 'gcp' ,
88- 'cloud.region' : 'us-east4' ,
89- 'host.id' : '12345' ,
90- 'k8s.cluster.name' : 'fake-cluster-name' ,
91- } ) ;
92- } ) ;
93- } ) ;
94-
95- it ( 'detects a GCE resource' , async ( ) => {
96- metadataStub . instance
97- . withArgs ( 'id' )
98- . resolves ( 12345 )
99-
100- . withArgs ( 'machine-type' )
101- . resolves ( 'fake-machine-type' )
102-
103- . withArgs ( 'name' )
104- . resolves ( 'fake-name' )
105-
106- . withArgs ( 'zone' )
107- . resolves ( 'projects/233510669999/zones/us-east4-b' ) ;
108-
109- const resource = await new GcpDetector ( ) . detect ( ) ;
110- assert . deepStrictEqual ( resource . attributes , {
111- 'cloud.account.id' : 'fake-project-id' ,
112- 'cloud.availability_zone' : 'us-east4-b' ,
113- 'cloud.platform' : 'gcp_compute_engine' ,
114- 'cloud.provider' : 'gcp' ,
115- 'cloud.region' : 'us-east4' ,
116- 'host.id' : '12345' ,
117- 'host.name' : 'fake-name' ,
118- 'host.type' : 'fake-machine-type' ,
119- } ) ;
120- } ) ;
121-
122- it ( 'detects a Cloud Run resource' , async ( ) => {
123- envStub . K_CONFIGURATION = 'fake-configuration' ;
124- envStub . K_SERVICE = 'fake-service' ;
125- envStub . K_REVISION = 'fake-revision' ;
126- metadataStub . instance
127- . withArgs ( 'id' )
128- . resolves ( 12345 )
129-
130- . withArgs ( 'region' )
131- . resolves ( 'projects/233510669999/regions/us-east4' ) ;
132-
133- const resource = await new GcpDetector ( ) . detect ( ) ;
134- assert . deepStrictEqual ( resource . attributes , {
135- 'cloud.account.id' : 'fake-project-id' ,
136- 'cloud.platform' : 'gcp_cloud_run' ,
137- 'cloud.provider' : 'gcp' ,
138- 'cloud.region' : 'us-east4' ,
139- 'faas.instance' : '12345' ,
140- 'faas.name' : 'fake-service' ,
141- 'faas.version' : 'fake-revision' ,
142- } ) ;
143- } ) ;
144-
145- it ( 'detects a Cloud Functions resource' , async ( ) => {
146- envStub . FUNCTION_TARGET = 'fake-function-target' ;
147- envStub . K_SERVICE = 'fake-service' ;
148- envStub . K_REVISION = 'fake-revision' ;
149- metadataStub . instance
150- . withArgs ( 'id' )
151- . resolves ( 12345 )
152-
153- . withArgs ( 'region' )
154- . resolves ( 'projects/233510669999/regions/us-east4' ) ;
155-
156- const resource = await new GcpDetector ( ) . detect ( ) ;
157- assert . deepStrictEqual ( resource . attributes , {
158- 'cloud.account.id' : 'fake-project-id' ,
159- 'cloud.platform' : 'gcp_cloud_functions' ,
160- 'cloud.provider' : 'gcp' ,
161- 'cloud.region' : 'us-east4' ,
162- 'faas.instance' : '12345' ,
163- 'faas.name' : 'fake-service' ,
164- 'faas.version' : 'fake-revision' ,
165- } ) ;
166- } ) ;
167-
168- it ( 'detects a App Engine Standard resource' , async ( ) => {
169- envStub . GAE_ENV = 'standard' ;
170- envStub . GAE_SERVICE = 'fake-service' ;
171- envStub . GAE_VERSION = 'fake-version' ;
172- envStub . GAE_INSTANCE = 'fake-instance' ;
173- metadataStub . instance . withArgs ( 'zone' ) . resolves ( 'us-east4-b' ) ;
174- metadataStub . instance
175- . withArgs ( 'region' )
176- . resolves ( 'projects/233510669999/regions/us-east4' ) ;
177-
178- const resource = await new GcpDetector ( ) . detect ( ) ;
179- assert . deepStrictEqual ( resource . attributes , {
180- 'cloud.account.id' : 'fake-project-id' ,
181- 'cloud.availability_zone' : 'us-east4-b' ,
182- 'cloud.platform' : 'gcp_app_engine' ,
183- 'cloud.provider' : 'gcp' ,
184- 'cloud.region' : 'us-east4' ,
185- 'faas.instance' : 'fake-instance' ,
186- 'faas.name' : 'fake-service' ,
187- 'faas.version' : 'fake-version' ,
188- } ) ;
189- } ) ;
190-
191- it ( 'detects a App Engine Flex resource' , async ( ) => {
192- envStub . GAE_SERVICE = 'fake-service' ;
193- envStub . GAE_VERSION = 'fake-version' ;
194- envStub . GAE_INSTANCE = 'fake-instance' ;
195- metadataStub . instance
196- . withArgs ( 'zone' )
197- . resolves ( 'projects/233510669999/zones/us-east4-b' ) ;
198-
199- const resource = await new GcpDetector ( ) . detect ( ) ;
200- assert . deepStrictEqual ( resource . attributes , {
201- 'cloud.account.id' : 'fake-project-id' ,
202- 'cloud.availability_zone' : 'us-east4-b' ,
203- 'cloud.platform' : 'gcp_app_engine' ,
204- 'cloud.provider' : 'gcp' ,
205- 'cloud.region' : 'us-east4' ,
206- 'faas.instance' : 'fake-instance' ,
207- 'faas.name' : 'fake-service' ,
208- 'faas.version' : 'fake-version' ,
209- } ) ;
210- } ) ;
211-
212- it ( 'detects empty resource when nothing else can be detected' , async ( ) => {
213- // gcp-metadata throws when it can't access the metadata server
214- metadataStub . instance . rejects ( ) ;
215- metadataStub . project . rejects ( ) ;
216-
217- const resource = await new GcpDetector ( ) . detect ( ) ;
218- assert . deepStrictEqual ( resource . attributes , { } ) ;
219- } ) ;
220- } ) ;
221-
22232describe ( 'GcpDetectorSync' , ( ) => {
22333 let metadataStub : sinon . SinonStubbedInstance < typeof metadata > ;
22434 let envStub : NodeJS . ProcessEnv ;
0 commit comments