github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmcloudutil/gcp_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package apmcloudutil 19 20 import ( 21 "context" 22 "net/http" 23 "net/http/httptest" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 28 "github.com/waldiirawan/apm-agent-go/v2/model" 29 ) 30 31 func TestGCPCloudMetadata(t *testing.T) { 32 t.Run("gce", func(t *testing.T) { 33 srv, client := newGCEMetadataServer() 34 defer srv.Close() 35 36 for _, provider := range []Provider{Auto, GCP} { 37 var out model.Cloud 38 var logger testLogger 39 assert.True(t, provider.getCloudMetadata(context.Background(), client, &logger, &out)) 40 assert.Zero(t, logger) 41 assert.Equal(t, model.Cloud{ 42 Provider: "gcp", 43 Region: "us-west3", 44 AvailabilityZone: "us-west3-a", 45 Instance: &model.CloudInstance{ 46 ID: "4306570268266786072", 47 Name: "basepi-test", 48 }, 49 Machine: &model.CloudMachine{ 50 Type: "n1-standard-1", 51 }, 52 Project: &model.CloudProject{ 53 ID: "513326162531", 54 Name: "elastic-apm", 55 }, 56 }, out) 57 } 58 }) 59 60 t.Run("cloudrun", func(t *testing.T) { 61 srv, client := newGoogleCloudRunMetadataServer() 62 defer srv.Close() 63 64 var out model.Cloud 65 var logger testLogger 66 assert.True(t, GCP.getCloudMetadata(context.Background(), client, &logger, &out)) 67 assert.Zero(t, logger) 68 assert.Equal(t, model.Cloud{ 69 Provider: "gcp", 70 Region: "australia-southeast1", 71 AvailabilityZone: "australia-southeast1-1", 72 Instance: &model.CloudInstance{ 73 ID: "00bf4bf02ddbda278fb9b4d70365018bd18a7d3ea42991e2cb03320b48a72b69b6d3765ff526347d7b8e0934dda4591cb1be3ead93086f0b390187fae88ee7cf8acdae7383", 74 }, 75 Project: &model.CloudProject{ 76 ID: "513326162531", 77 Name: "elastic-apm", 78 }, 79 }, out) 80 }) 81 } 82 83 func newGCEMetadataServer() (*httptest.Server, *http.Client) { 84 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 85 if r.URL.Path != "/computeMetadata/v1/" { 86 w.WriteHeader(http.StatusNotFound) 87 return 88 } 89 w.Write([]byte(`{ 90 "instance": { 91 "id": 4306570268266786072, 92 "machineType": "projects/513326162531/machineTypes/n1-standard-1", 93 "name": "basepi-test", 94 "zone": "projects/513326162531/zones/us-west3-a" 95 }, 96 "project": {"numericProjectId": 513326162531, "projectId": "elastic-apm"} 97 }`)) 98 })) 99 100 client := &http.Client{Transport: newTargetedRoundTripper("metadata.google.internal", srv.Listener.Addr().String())} 101 return srv, client 102 } 103 104 func newGoogleCloudRunMetadataServer() (*httptest.Server, *http.Client) { 105 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 106 if r.URL.Path != "/computeMetadata/v1/" { 107 w.WriteHeader(http.StatusNotFound) 108 return 109 } 110 w.Write([]byte(`{ 111 "instance": { 112 "id": "00bf4bf02ddbda278fb9b4d70365018bd18a7d3ea42991e2cb03320b48a72b69b6d3765ff526347d7b8e0934dda4591cb1be3ead93086f0b390187fae88ee7cf8acdae7383", 113 "region":"projects/513326162531/regions/australia-southeast1", 114 "zone":"projects/513326162531/zones/australia-southeast1-1" 115 }, 116 "project": { 117 "numericProjectId": 513326162531, 118 "projectId": "elastic-apm" 119 } 120 }`)) 121 })) 122 123 client := &http.Client{Transport: newTargetedRoundTripper("metadata.google.internal", srv.Listener.Addr().String())} 124 return srv, client 125 }