github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/avs/client_test.go (about) 1 package avs 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/sirupsen/logrus" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 const ( 13 parentEvaluationID = 42 14 evaluationName = "test_evaluation" 15 ) 16 17 func TestClient_CreateEvaluation(t *testing.T) { 18 t.Run("create evaluation the first time", func(t *testing.T) { 19 // Given 20 server := NewMockAvsServer(t) 21 mockServer := FixMockAvsServer(server) 22 client, err := NewClient(context.TODO(), Config{ 23 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 24 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 25 }, logrus.New()) 26 assert.NoError(t, err) 27 28 // When 29 response, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 30 Name: evaluationName, 31 ParentId: parentEvaluationID, 32 }) 33 34 // Then 35 assert.NoError(t, err) 36 assert.Equal(t, evaluationName, response.Name) 37 assert.NotEmpty(t, server.Evaluations.ParentIDrefs[parentEvaluationID]) 38 }) 39 40 t.Run("create evaluation with token reset", func(t *testing.T) { 41 // Given 42 server := NewMockAvsServer(t) 43 server.TokenExpired = 1 44 mockServer := FixMockAvsServer(server) 45 client, err := NewClient(context.TODO(), Config{ 46 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 47 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 48 }, logrus.New()) 49 assert.NoError(t, err) 50 51 // When 52 response, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 53 Name: evaluationName, 54 ParentId: parentEvaluationID, 55 }) 56 57 // Then 58 assert.NoError(t, err) 59 assert.Equal(t, "test_evaluation", response.Name) 60 assert.NotEmpty(t, server.Evaluations.ParentIDrefs[parentEvaluationID]) 61 }) 62 63 t.Run("401 error during creating evaluation", func(t *testing.T) { 64 // Given 65 server := NewMockAvsServer(t) 66 server.TokenExpired = 2 67 mockServer := FixMockAvsServer(server) 68 client, err := NewClient(context.TODO(), Config{ 69 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 70 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 71 ParentId: parentEvaluationID, 72 }, logrus.New()) 73 assert.NoError(t, err) 74 75 // When 76 _, err = client.CreateEvaluation(&BasicEvaluationCreateRequest{ 77 Name: "test_evaluation", 78 }) 79 80 // Then 81 assert.Error(t, err) 82 assert.Contains(t, err.Error(), "401") 83 }) 84 } 85 86 func TestClient_DeleteEvaluation(t *testing.T) { 87 t.Run("should delete existing evaluation", func(t *testing.T) { 88 // Given 89 server := NewMockAvsServer(t) 90 mockServer := FixMockAvsServer(server) 91 client, err := NewClient(context.TODO(), Config{ 92 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 93 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 94 ParentId: parentEvaluationID, 95 }, logrus.New()) 96 assert.NoError(t, err) 97 98 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 99 Name: "test_evaluation", 100 }) 101 assert.NoError(t, err) 102 103 // When 104 err = client.DeleteEvaluation(resp.Id) 105 106 // Then 107 assert.NoError(t, err) 108 assert.Empty(t, server.Evaluations.BasicEvals) 109 }) 110 111 t.Run("should return error when trying to delete not existing evaluation", func(t *testing.T) { 112 // Given 113 server := NewMockAvsServer(t) 114 mockServer := FixMockAvsServer(server) 115 client, err := NewClient(context.TODO(), Config{ 116 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 117 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 118 ParentId: parentEvaluationID, 119 }, logrus.New()) 120 assert.NoError(t, err) 121 122 _, err = client.CreateEvaluation(&BasicEvaluationCreateRequest{ 123 Name: "test_evaluation", 124 ParentId: parentEvaluationID, 125 }) 126 assert.NoError(t, err) 127 128 // When 129 err = client.DeleteEvaluation(123) 130 131 // Then 132 assert.NoError(t, err) 133 assert.Empty(t, server.Evaluations.BasicEvals[123]) 134 }) 135 } 136 137 func TestClient_GetEvaluation(t *testing.T) { 138 t.Run("should get existing evaluation", func(t *testing.T) { 139 // Given 140 server := NewMockAvsServer(t) 141 mockServer := FixMockAvsServer(server) 142 client, err := NewClient(context.TODO(), Config{ 143 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 144 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 145 ParentId: parentEvaluationID, 146 }, logrus.New()) 147 assert.NoError(t, err) 148 149 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 150 Name: "test_evaluation_create", 151 Description: "custom description", 152 }) 153 assert.NoError(t, err) 154 155 // When 156 getResp, err := client.GetEvaluation(resp.Id) 157 158 // Then 159 assert.NoError(t, err) 160 assert.Equal(t, getResp, resp) 161 }) 162 163 t.Run("should return error when trying to get not existing evaluation", func(t *testing.T) { 164 // Given 165 server := NewMockAvsServer(t) 166 mockServer := FixMockAvsServer(server) 167 client, err := NewClient(context.TODO(), Config{ 168 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 169 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 170 ParentId: parentEvaluationID, 171 }, logrus.New()) 172 assert.NoError(t, err) 173 174 // When 175 _, err = client.GetEvaluation(1) 176 177 // Then 178 assert.Contains(t, err.Error(), "404") 179 }) 180 } 181 182 func TestClient_Status(t *testing.T) { 183 t.Run("should get status", func(t *testing.T) { 184 // Given 185 server := NewMockAvsServer(t) 186 mockServer := FixMockAvsServer(server) 187 client, err := NewClient(context.TODO(), Config{ 188 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 189 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 190 ParentId: parentEvaluationID, 191 }, logrus.New()) 192 assert.NoError(t, err) 193 194 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{}) 195 assert.NoError(t, err) 196 197 // When 198 getResp, err := client.GetEvaluation(resp.Id) 199 200 // Then 201 assert.NoError(t, err) 202 assert.Equal(t, getResp.Status, StatusActive) 203 }) 204 205 t.Run("should update status", func(t *testing.T) { 206 // Given 207 server := NewMockAvsServer(t) 208 mockServer := FixMockAvsServer(server) 209 client, err := NewClient(context.TODO(), Config{ 210 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 211 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 212 ParentId: parentEvaluationID, 213 }, logrus.New()) 214 assert.NoError(t, err) 215 216 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{}) 217 assert.NoError(t, err) 218 219 // When 220 resp, err = client.SetStatus(resp.Id, StatusDeleted) 221 222 // Then 223 assert.NoError(t, err) 224 assert.Equal(t, resp.Status, StatusDeleted) 225 }) 226 227 t.Run("should not update invalid status", func(t *testing.T) { 228 // Given 229 server := NewMockAvsServer(t) 230 mockServer := FixMockAvsServer(server) 231 client, err := NewClient(context.TODO(), Config{ 232 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 233 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 234 ParentId: parentEvaluationID, 235 }, logrus.New()) 236 assert.NoError(t, err) 237 238 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{}) 239 assert.NoError(t, err) 240 241 // When 242 resp, err = client.SetStatus(resp.Id, "") 243 244 // Then 245 assert.Contains(t, err.Error(), "500") 246 }) 247 } 248 249 func TestClient_RemoveReferenceFromParentEval(t *testing.T) { 250 t.Run("should remove reference from parent eval", func(t *testing.T) { 251 // Given 252 server := NewMockAvsServer(t) 253 mockServer := FixMockAvsServer(server) 254 client, err := NewClient(context.TODO(), Config{ 255 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 256 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 257 ParentId: parentEvaluationID, 258 }, logrus.New()) 259 assert.NoError(t, err) 260 261 resp, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 262 Name: "test_evaluation", 263 ParentId: parentEvaluationID, 264 }) 265 assert.NoError(t, err) 266 267 // When 268 err = client.RemoveReferenceFromParentEval(parentEvaluationID, resp.Id) 269 270 // Then 271 assert.NoError(t, err) 272 assert.Empty(t, server.Evaluations.ParentIDrefs[parentEvaluationID]) 273 }) 274 t.Run("should return error when wrong api url provided", func(t *testing.T) { 275 // Given 276 server := NewMockAvsServer(t) 277 mockServer := FixMockAvsServer(server) 278 client, err := NewClient(context.TODO(), Config{ 279 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 280 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", "http://not-existing"), 281 ParentId: parentEvaluationID, 282 }, logrus.New()) 283 assert.NoError(t, err) 284 285 // When 286 err = client.RemoveReferenceFromParentEval(parentEvaluationID, 111) 287 288 // then 289 assert.Error(t, err) 290 }) 291 t.Run("should ignore error when parent evaluation does not contain subevaluation", func(t *testing.T) { 292 // Given 293 server := NewMockAvsServer(t) 294 mockServer := FixMockAvsServer(server) 295 client, err := NewClient(context.TODO(), Config{ 296 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 297 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 298 ParentId: parentEvaluationID, 299 }, logrus.New()) 300 assert.NoError(t, err) 301 302 // When 303 err = client.RemoveReferenceFromParentEval(int64(9999), 111) 304 305 // then 306 assert.NoError(t, err) 307 }) 308 309 } 310 311 func TestClient_AddTag(t *testing.T) { 312 t.Run("should add tag to existing evaluation", func(t *testing.T) { 313 // given 314 server := NewMockAvsServer(t) 315 mockServer := FixMockAvsServer(server) 316 client, err := NewClient(context.TODO(), Config{ 317 OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL), 318 ApiEndpoint: fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL), 319 ParentId: parentEvaluationID, 320 }, logrus.New()) 321 assert.NoError(t, err) 322 323 response, err := client.CreateEvaluation(&BasicEvaluationCreateRequest{ 324 Name: "test_evaluation", 325 ParentId: parentEvaluationID, 326 }) 327 assert.NoError(t, err) 328 329 fixedTag := FixTag() 330 331 // when 332 eval, err := client.AddTag(response.Id, fixedTag) 333 334 // then 335 assert.NoError(t, err) 336 assert.Equal(t, fixedTag, eval.Tags[0]) 337 }) 338 }