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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package utilization
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"testing"
)
// Cross agent test types common to each provider's set of test cases.
type testCase struct {
TestName string `json:"testname"`
URIs map[string]jsonResponse `json:"uri"`
EnvVars map[string]envResponse `json:"env_vars"`
ExpectedVendorsHash vendors `json:"expected_vendors_hash"`
ExpectedMetrics map[string]metric `json:"expected_metrics"`
}
type envResponse struct {
Response string `json:"response"`
Timeout bool `json:"timeout"`
}
type jsonResponse struct {
Response json.RawMessage `json:"response"`
Timeout bool `json:"timeout"`
}
type metric struct {
CallCount int `json:"call_count"`
}
var errTimeout = errors.New("timeout")
type mockTransport struct {
t *testing.T
responses map[string]jsonResponse
}
type mockBody struct {
bytes.Reader
closed bool
t *testing.T
}
func (m *mockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
for match, response := range m.responses {
if r.URL.String() == match {
return m.respond(response)
}
}
m.t.Errorf("Unknown request URI: %s", r.URL.String())
return nil, nil
}
func (m *mockTransport) respond(resp jsonResponse) (*http.Response, error) {
if resp.Timeout {
return nil, errTimeout
}
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: &mockBody{
t: m.t,
Reader: *bytes.NewReader(resp.Response),
},
}, nil
}
// This function is included simply so that http.Client doesn't complain.
func (m *mockTransport) CancelRequest(r *http.Request) {}
func (m *mockBody) Close() error {
if m.closed {
m.t.Error("Close of already closed connection!")
}
m.closed = true
return nil
}
func (m *mockBody) ensureClosed() {
if !m.closed {
m.t.Error("Connection was not closed")
}
}
func TestNormaliseValue(t *testing.T) {
testCases := []struct {
name string
input string
expected string
isError bool
}{
{
name: "Valid - empty",
input: "",
expected: "",
isError: false,
},
{
name: "Valid - symbols",
input: ". /-_",
expected: ". /-_",
isError: false,
},
{
name: "Valid - string",
input: "simplesentence",
expected: "simplesentence",
isError: false,
},
{
name: "Invalid - More than 255",
input: `256256256256256256256256256256256256256256256256256256256256
256256256256256256256256256256256256256256256256256256256256256256256256
256256256256256256256256256256256256256256256256256256256256256256256256
2562562562562562562562562562562562562562562562562562`,
expected: "",
isError: true,
},
}
for _, tc := range testCases {
actual, err := normalizeValue(tc.input)
if tc.isError && err == nil {
t.Fatalf("%s: expected error; got nil", tc.name)
} else if !tc.isError {
if err != nil {
t.Fatalf("%s: expected not error; got: %v", tc.name, err)
}
if tc.expected != actual {
t.Fatalf("%s: expected: %s; got: %s", tc.name, tc.expected, actual)
}
}
}
}
|