github.com/google/cloudprober@v0.11.3/surfacers/datadog/client_test.go (about) 1 // Copyright 2021 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package datadog 16 17 import ( 18 "encoding/json" 19 "io" 20 "os" 21 "reflect" 22 "testing" 23 "time" 24 ) 25 26 func TestNewClient(t *testing.T) { 27 cAPIKey, cAppKey := "c-apiKey", "c-appKey" 28 eAPIKey, eAppKey := "e-apiKey", "e-appKey" 29 30 tests := []struct { 31 desc string 32 apiKey string 33 appKey string 34 server string 35 env map[string]string 36 wantClient *ddClient 37 }{ 38 { 39 desc: "keys-from-config", 40 apiKey: cAPIKey, 41 appKey: cAppKey, 42 server: "", 43 wantClient: &ddClient{ 44 apiKey: cAPIKey, 45 appKey: cAppKey, 46 server: defaultServer, 47 }, 48 }, 49 { 50 desc: "keys-from-env", 51 env: map[string]string{ 52 "DD_API_KEY": eAPIKey, 53 "DD_APP_KEY": eAppKey, 54 }, 55 server: "test-server", 56 wantClient: &ddClient{ 57 apiKey: eAPIKey, 58 appKey: eAppKey, 59 server: "test-server", 60 }, 61 }, 62 } 63 64 for _, test := range tests { 65 t.Run(test.desc, func(t *testing.T) { 66 for k, v := range test.env { 67 os.Setenv(k, v) 68 } 69 70 c := newClient(test.server, test.apiKey, test.appKey) 71 if !reflect.DeepEqual(c, test.wantClient) { 72 t.Errorf("got client: %v, want client: %v", c, test.wantClient) 73 } 74 }) 75 } 76 } 77 78 func TestNewRequest(t *testing.T) { 79 ts := time.Now().Unix() 80 tags := []string{"probe:cloudprober_http"} 81 metricType := "count" 82 83 testSeries := []ddSeries{ 84 { 85 Metric: "cloudprober.success", 86 Points: [][]float64{[]float64{float64(ts), 99}}, 87 Tags: &tags, 88 Type: &metricType, 89 }, 90 { 91 Metric: "cloudprober.total", 92 Points: [][]float64{[]float64{float64(ts), 100}}, 93 Tags: &tags, 94 Type: &metricType, 95 }, 96 } 97 98 testClient := newClient("", "test-api-key", "test-app-key") 99 req, err := testClient.newRequest(testSeries) 100 101 if err != nil { 102 t.Errorf("Unexpected error: %v", err) 103 } 104 105 // Check URL 106 wantURL := "https://api.datadoghq.com/api/v1/series" 107 if req.URL.String() != wantURL { 108 t.Errorf("Got URL: %s, wanted: %s", req.URL.String(), wantURL) 109 } 110 111 // Check request headers 112 for k, v := range map[string]string{ 113 "DD-API-KEY": "test-api-key", 114 "DD-APP-KEY": "test-app-key", 115 } { 116 if req.Header.Get(k) != v { 117 t.Errorf("%s header: %s, wanted: %s", k, req.Header.Get(k), v) 118 } 119 } 120 121 // Check request body 122 b, err := io.ReadAll(req.Body) 123 if err != nil { 124 t.Errorf("Error reading request body: %v", err) 125 } 126 data := map[string][]ddSeries{} 127 if err := json.Unmarshal(b, &data); err != nil { 128 t.Errorf("Error unmarshaling request body: %v", err) 129 } 130 if !reflect.DeepEqual(data["series"], testSeries) { 131 t.Errorf("s.Series: %v, testSeries: %v", data["series"], testSeries) 132 } 133 }