github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmcloudutil/azure_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  	"os"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"github.com/waldiirawan/apm-agent-go/v2/model"
    30  )
    31  
    32  func TestAzureCloudMetadata(t *testing.T) {
    33  	srv, client := newAzureMetadataServer()
    34  	defer srv.Close()
    35  
    36  	for _, provider := range []Provider{Auto, Azure} {
    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: "azure",
    43  			Region:   "westus2",
    44  			Instance: &model.CloudInstance{
    45  				ID:   "e11ebedc-019d-427f-84dd-56cd4388d3a8",
    46  				Name: "basepi-test",
    47  			},
    48  			Machine: &model.CloudMachine{
    49  				Type: "Standard_D2s_v3",
    50  			},
    51  			Project: &model.CloudProject{
    52  				Name: "basepi-testing",
    53  			},
    54  			Account: &model.CloudAccount{
    55  				ID: "7657426d-c4c3-44ac-88a2-3b2cd59e6dba",
    56  			},
    57  		}, out)
    58  	}
    59  }
    60  
    61  func TestAzureAppServiceCloudMetadata(t *testing.T) {
    62  	client := &http.Client{Transport: newTargetedRoundTripper("", "testing.invalid")}
    63  
    64  	os.Setenv("WEBSITE_OWNER_NAME", "f5940f10-2e30-3e4d-a259-63451ba6dae4+elastic-apm-AustraliaEastwebspace")
    65  	os.Setenv("WEBSITE_RESOURCE_GROUP", "resource_group")
    66  	os.Setenv("WEBSITE_SITE_NAME", "site_name")
    67  	os.Setenv("WEBSITE_INSTANCE_ID", "instance_id")
    68  	defer func() {
    69  		os.Unsetenv("WEBSITE_OWNER_NAME")
    70  		os.Unsetenv("WEBSITE_RESOURCE_GROUP")
    71  		os.Unsetenv("WEBSITE_SITE_NAME")
    72  		os.Unsetenv("WEBSITE_INSTANCE_ID")
    73  	}()
    74  
    75  	for _, provider := range []Provider{Auto, Azure} {
    76  		var out model.Cloud
    77  		var logger testLogger
    78  		assert.True(t, provider.getCloudMetadata(context.Background(), client, &logger, &out))
    79  		assert.Zero(t, logger)
    80  		assert.Equal(t, model.Cloud{
    81  			Provider: "azure",
    82  			Region:   "AustraliaEast",
    83  			Instance: &model.CloudInstance{
    84  				ID:   "instance_id",
    85  				Name: "site_name",
    86  			},
    87  			Project: &model.CloudProject{
    88  				Name: "resource_group",
    89  			},
    90  			Account: &model.CloudAccount{
    91  				ID: "f5940f10-2e30-3e4d-a259-63451ba6dae4",
    92  			},
    93  		}, out)
    94  	}
    95  }
    96  
    97  func newAzureMetadataServer() (*httptest.Server, *http.Client) {
    98  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    99  		if r.URL.Path != "/metadata/instance/compute" {
   100  			w.WriteHeader(http.StatusNotFound)
   101  			return
   102  		}
   103  		w.Write([]byte(`{
   104      "location": "westus2",
   105      "name": "basepi-test",
   106      "resourceGroupName": "basepi-testing",
   107      "subscriptionId": "7657426d-c4c3-44ac-88a2-3b2cd59e6dba",
   108      "vmId": "e11ebedc-019d-427f-84dd-56cd4388d3a8",
   109      "vmScaleSetName": "",
   110      "vmSize": "Standard_D2s_v3",
   111      "zone": ""
   112  }`))
   113  	}))
   114  
   115  	client := &http.Client{Transport: newTargetedRoundTripper("169.254.169.254", srv.Listener.Addr().String())}
   116  	return srv, client
   117  }