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