github.com/newrelic/go-agent@v3.26.0+incompatible/internal/utilization/gcp_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package utilization 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/newrelic/go-agent/internal/crossagent" 11 ) 12 13 func TestCrossAgentGCP(t *testing.T) { 14 var testCases []testCase 15 16 err := crossagent.ReadJSON("utilization_vendor_specific/gcp.json", &testCases) 17 if err != nil { 18 t.Fatalf("reading gcp.json failed: %v", err) 19 } 20 21 for _, testCase := range testCases { 22 client := &http.Client{ 23 Transport: &mockTransport{ 24 t: t, 25 responses: testCase.URIs, 26 }, 27 } 28 29 gcp, err := getGCP(client) 30 31 if testCase.ExpectedVendorsHash.GCP == nil { 32 if err == nil { 33 t.Fatalf("%s: expected error; got nil", testCase.TestName) 34 } 35 } else { 36 if err != nil { 37 t.Fatalf("%s: expected no error; got %v", testCase.TestName, err) 38 } 39 40 if gcp.ID != testCase.ExpectedVendorsHash.GCP.ID { 41 t.Fatalf("%s: ID incorrect; expected: %s; got: %s", testCase.TestName, testCase.ExpectedVendorsHash.GCP.ID, gcp.ID) 42 } 43 44 if gcp.MachineType != testCase.ExpectedVendorsHash.GCP.MachineType { 45 t.Fatalf("%s: MachineType incorrect; expected: %s; got: %s", testCase.TestName, testCase.ExpectedVendorsHash.GCP.MachineType, gcp.MachineType) 46 } 47 48 if gcp.Name != testCase.ExpectedVendorsHash.GCP.Name { 49 t.Fatalf("%s: Name incorrect; expected: %s; got: %s", testCase.TestName, testCase.ExpectedVendorsHash.GCP.Name, gcp.Name) 50 } 51 52 if gcp.Zone != testCase.ExpectedVendorsHash.GCP.Zone { 53 t.Fatalf("%s: Zone incorrect; expected: %s; got: %s", testCase.TestName, testCase.ExpectedVendorsHash.GCP.Zone, gcp.Zone) 54 } 55 } 56 } 57 } 58 59 func TestStripGCPPrefix(t *testing.T) { 60 testCases := []struct { 61 input string 62 expected string 63 }{ 64 {"foo/bar", "bar"}, 65 {"/foo/bar", "bar"}, 66 {"/foo/bar/", ""}, 67 {"foo", "foo"}, 68 {"", ""}, 69 } 70 71 for _, tc := range testCases { 72 actual := stripGCPPrefix(tc.input) 73 if tc.expected != actual { 74 t.Fatalf("input: %s; expected: %s; actual: %s", tc.input, tc.expected, actual) 75 } 76 } 77 }