github.com/verrazzano/verrazzano@v1.7.1/tools/psr/backend/workers/opensearch/getlogs/getlogs_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package getlogs 5 6 import ( 7 "errors" 8 "github.com/stretchr/testify/assert" 9 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 10 "github.com/verrazzano/verrazzano/tools/psr/backend/config" 11 "io" 12 "net/http" 13 "strings" 14 "testing" 15 ) 16 17 type fakeHTTP struct { 18 resp *http.Response 19 httpDoError error 20 } 21 22 type fakeBody struct { 23 bodyData string 24 httpReadError error 25 } 26 27 var _ httpClientI = &fakeHTTP{} 28 29 // TestGetters tests the worker getters 30 // GIVEN a worker 31 // 32 // WHEN the getter methods are calls 33 // THEN ensure that the correct results are returned 34 func TestGetters(t *testing.T) { 35 w, err := NewGetLogsWorker() 36 assert.NoError(t, err) 37 38 wd := w.GetWorkerDesc() 39 assert.Equal(t, config.WorkerTypeOpsGetLogs, wd.WorkerType) 40 assert.Equal(t, "The log getter worker performs GET requests on the OpenSearch endpoint", wd.Description) 41 assert.Equal(t, metricsPrefix, wd.MetricsPrefix) 42 43 el := w.GetEnvDescList() 44 assert.Len(t, el, 0) 45 46 logged := w.WantLoopInfoLogged() 47 assert.False(t, logged) 48 } 49 50 func TestGetMetricDescList(t *testing.T) { 51 tests := []struct { 52 name string 53 fqName string 54 help string 55 }{ 56 {name: "1", fqName: metricsPrefix + "_success_count_total", help: "The total number of successful OpenSearch GET requests"}, 57 {name: "2", fqName: metricsPrefix + "_failure_count_total", help: "The total number of successful OpenSearch GET requests"}, 58 {name: "3", fqName: metricsPrefix + "_success_latency_nanoseconds", help: "The latency of successful OpenSearch GET requests in nanoseconds"}, 59 {name: "4", fqName: metricsPrefix + "_failure_latency_nanoseconds", help: "The latency of failed OpenSearch GET requests in nanoseconds"}, 60 {name: "5", fqName: metricsPrefix + "_data_chars_total", help: "The total number of characters return from OpenSearch get request"}, 61 } 62 for _, test := range tests { 63 t.Run(test.name, func(t *testing.T) { 64 wi, err := NewGetLogsWorker() 65 assert.NoError(t, err) 66 w := wi.(worker) 67 assert.NoError(t, err) 68 dl := w.GetMetricDescList() 69 var found int 70 for _, d := range dl { 71 s := d.String() 72 if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) { 73 found++ 74 } 75 } 76 assert.Equal(t, 1, found) 77 }) 78 } 79 } 80 81 func TestGetMetricList(t *testing.T) { 82 tests := []struct { 83 name string 84 fqName string 85 help string 86 }{ 87 {name: "1", fqName: metricsPrefix + "_success_count_total", help: "The total number of successful OpenSearch GET requests"}, 88 {name: "2", fqName: metricsPrefix + "_failure_count_total", help: "The total number of successful OpenSearch GET requests"}, 89 {name: "3", fqName: metricsPrefix + "_success_latency_nanoseconds", help: "The latency of successful OpenSearch GET requests in nanoseconds"}, 90 {name: "4", fqName: metricsPrefix + "_failure_latency_nanoseconds", help: "The latency of failed OpenSearch GET requests in nanoseconds"}, 91 {name: "5", fqName: metricsPrefix + "_data_chars_total", help: "The total number of characters return from OpenSearch get request"}, 92 } 93 for _, test := range tests { 94 t.Run(test.name, func(t *testing.T) { 95 wi, err := NewGetLogsWorker() 96 w := wi.(worker) 97 assert.NoError(t, err) 98 ml := w.GetMetricList() 99 var found int 100 for _, m := range ml { 101 s := m.Desc().String() 102 if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) { 103 found++ 104 } 105 } 106 assert.Equal(t, 1, found) 107 }) 108 } 109 } 110 111 // TestDoWork tests the DoWork method 112 // GIVEN a worker 113 // 114 // WHEN the DoWork methods is called 115 // THEN ensure that the correct results are returned 116 func TestDoWork(t *testing.T) { 117 tests := []struct { 118 name string 119 doworkError error 120 httpDoError error 121 nilResp bool 122 statusCode int 123 reqCount int 124 successCount int 125 failureCount int 126 fakeBody 127 }{ 128 { 129 name: "1", 130 statusCode: 200, 131 reqCount: 1, 132 successCount: 1, 133 failureCount: 0, 134 fakeBody: fakeBody{ 135 bodyData: "bodydata", 136 }, 137 }, 138 { 139 name: "2", 140 reqCount: 1, 141 successCount: 0, 142 failureCount: 1, 143 doworkError: errors.New("error"), 144 httpDoError: errors.New("error"), 145 fakeBody: fakeBody{ 146 bodyData: "bodydata", 147 }, 148 }, 149 { 150 name: "3", 151 statusCode: 500, 152 doworkError: errors.New("error"), 153 reqCount: 1, 154 successCount: 0, 155 failureCount: 1, 156 fakeBody: fakeBody{ 157 bodyData: "bodydata", 158 httpReadError: errors.New("error"), 159 }, 160 }, 161 { 162 name: "4", 163 doworkError: errors.New("GET request to endpoint received a nil response"), 164 nilResp: true, 165 reqCount: 1, 166 successCount: 0, 167 failureCount: 1, 168 fakeBody: fakeBody{ 169 bodyData: "bodydata", 170 httpReadError: errors.New("error"), 171 }, 172 }, 173 } 174 for _, test := range tests { 175 t.Run(test.name, func(t *testing.T) { 176 f := httpClient 177 defer func() { 178 httpClient = f 179 }() 180 var resp *http.Response 181 if !test.nilResp { 182 resp = &http.Response{ 183 StatusCode: test.statusCode, 184 Body: &test.fakeBody, 185 ContentLength: int64(len(test.bodyData)), 186 } 187 } 188 httpClient = &fakeHTTP{ 189 httpDoError: test.doworkError, 190 resp: resp, 191 } 192 193 wi, err := NewGetLogsWorker() 194 assert.NoError(t, err) 195 w := wi.(worker) 196 197 err = w.DoWork(config.CommonConfig{ 198 WorkerType: "Fake", 199 }, vzlog.DefaultLogger()) 200 if test.doworkError == nil && test.httpDoError == nil && test.httpReadError == nil { 201 assert.NoError(t, err) 202 } else { 203 assert.Error(t, err) 204 } 205 206 assert.Equal(t, int64(test.successCount), w.openSearchGetSuccessCountTotal.Val) 207 assert.Equal(t, int64(test.failureCount), w.openSearchGetFailureCountTotal.Val) 208 }) 209 } 210 } 211 212 func (f *fakeHTTP) Do(_ *http.Request) (resp *http.Response, err error) { 213 return f.resp, f.httpDoError 214 } 215 216 func (f fakeBody) ReadAll(d []byte) (n int, err error) { 217 if f.httpReadError != nil { 218 return 0, f.httpReadError 219 } 220 copy(d, f.bodyData) 221 return len(f.bodyData), nil 222 } 223 224 func (f fakeBody) Read(d []byte) (n int, err error) { 225 if f.httpReadError != nil { 226 return 0, f.httpReadError 227 } 228 copy(d, f.bodyData) 229 return len(f.bodyData), io.EOF 230 } 231 232 func (f fakeBody) Close() error { 233 return nil 234 }