github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/utilization/gcp_test.go (about)

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