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

     1  package utilization
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"net/http"
     8  	"testing"
     9  )
    10  
    11  // Cross agent test types common to each provider's set of test cases.
    12  type testCase struct {
    13  	TestName            string                  `json:"testname"`
    14  	URIs                map[string]jsonResponse `json:"uri"`
    15  	EnvVars             map[string]envResponse  `json:"env_vars"`
    16  	ExpectedVendorsHash vendors                 `json:"expected_vendors_hash"`
    17  	ExpectedMetrics     map[string]metric       `json:"expected_metrics"`
    18  }
    19  
    20  type envResponse struct {
    21  	Response string `json:"response"`
    22  	Timeout  bool   `json:"timeout"`
    23  }
    24  
    25  type jsonResponse struct {
    26  	Response json.RawMessage `json:"response"`
    27  	Timeout  bool            `json:"timeout"`
    28  }
    29  
    30  type metric struct {
    31  	CallCount int `json:"call_count"`
    32  }
    33  
    34  var errTimeout = errors.New("timeout")
    35  
    36  type mockTransport struct {
    37  	t         *testing.T
    38  	responses map[string]jsonResponse
    39  }
    40  
    41  type mockBody struct {
    42  	bytes.Reader
    43  	closed bool
    44  	t      *testing.T
    45  }
    46  
    47  func (m *mockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    48  	for match, response := range m.responses {
    49  		if r.URL.String() == match {
    50  			return m.respond(response)
    51  		}
    52  	}
    53  
    54  	m.t.Errorf("Unknown request URI: %s", r.URL.String())
    55  	return nil, nil
    56  }
    57  
    58  func (m *mockTransport) respond(resp jsonResponse) (*http.Response, error) {
    59  	if resp.Timeout {
    60  		return nil, errTimeout
    61  	}
    62  
    63  	return &http.Response{
    64  		Status:     "200 OK",
    65  		StatusCode: 200,
    66  		Body: &mockBody{
    67  			t:      m.t,
    68  			Reader: *bytes.NewReader(resp.Response),
    69  		},
    70  	}, nil
    71  }
    72  
    73  // This function is included simply so that http.Client doesn't complain.
    74  func (m *mockTransport) CancelRequest(r *http.Request) {}
    75  
    76  func (m *mockBody) Close() error {
    77  	if m.closed {
    78  		m.t.Error("Close of already closed connection!")
    79  	}
    80  
    81  	m.closed = true
    82  	return nil
    83  }
    84  
    85  func (m *mockBody) ensureClosed() {
    86  	if !m.closed {
    87  		m.t.Error("Connection was not closed")
    88  	}
    89  }
    90  
    91  func TestNormaliseValue(t *testing.T) {
    92  	testCases := []struct {
    93  		name     string
    94  		input    string
    95  		expected string
    96  		isError  bool
    97  	}{
    98  		{
    99  			name:     "Valid - empty",
   100  			input:    "",
   101  			expected: "",
   102  			isError:  false,
   103  		},
   104  		{
   105  			name:     "Valid - symbols",
   106  			input:    ". /-_",
   107  			expected: ". /-_",
   108  			isError:  false,
   109  		},
   110  		{
   111  			name:     "Valid - string",
   112  			input:    "simplesentence",
   113  			expected: "simplesentence",
   114  			isError:  false,
   115  		},
   116  		{
   117  			name: "Invalid - More than 255",
   118  			input: `256256256256256256256256256256256256256256256256256256256256
   119  			256256256256256256256256256256256256256256256256256256256256256256256256
   120  			256256256256256256256256256256256256256256256256256256256256256256256256
   121  			2562562562562562562562562562562562562562562562562562`,
   122  			expected: "",
   123  			isError:  true,
   124  		},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		actual, err := normalizeValue(tc.input)
   129  
   130  		if tc.isError && err == nil {
   131  			t.Fatalf("%s: expected error; got nil", tc.name)
   132  		} else if !tc.isError {
   133  			if err != nil {
   134  				t.Fatalf("%s: expected not error; got: %v", tc.name, err)
   135  			}
   136  			if tc.expected != actual {
   137  				t.Fatalf("%s: expected: %s; got: %s", tc.name, tc.expected, actual)
   138  			}
   139  		}
   140  	}
   141  }