Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
4gust committed Nov 25, 2024
1 parent 33386f0 commit 23290c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
1 change: 0 additions & 1 deletion apps/managedidentity/managedidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const (
// retry codes for IMDS
var retryCodesForIMDS = []int{
http.StatusNotFound, // 404
http.StatusRequestTimeout, // 408
http.StatusGone, // 410
http.StatusTooManyRequests, // 429
http.StatusInternalServerError, // 500
Expand Down
24 changes: 8 additions & 16 deletions apps/managedidentity/managedidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,13 @@ func TestRetryFunction(t *testing.T) {
t.Fatal(err)
}
reqBody := bytes.NewBufferString(tt.requestBody)
req, _ := http.NewRequest("POST", "https://example.com", reqBody)
req, err := http.NewRequest("POST", "https://example.com", reqBody)
if err != nil {
t.Fatal(err)
}
finalResp, err := client.retry(tt.maxRetries, req)
if err != nil {
t.Fatalf("error was not expected %s", err)
t.Fatal(err)
}
if finalResp.StatusCode != tt.expectedStatus {
t.Fatalf("Expected status code %d, got %d", tt.expectedStatus, finalResp.StatusCode)
Expand All @@ -245,26 +248,15 @@ func TestRetryFunction(t *testing.T) {
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
finalResp.Body.Close() // Close the body after reading
finalResp.Body.Close()
if string(bodyBytes) != tt.expectedBody {
t.Fatalf("Expected body %q, got %q", tt.expectedBody, bodyBytes)
}
if req.Body != nil {
reqBodyBytes, err := io.ReadAll(req.Body)
if err != nil {
t.Fatalf("Failed to read request body: %v", err)
}
req.Body.Close()

if string(reqBodyBytes) != tt.requestBody {
t.Fatalf("Expected request body %q, got %q", tt.requestBody, reqBodyBytes)
}
}
})
}
}

func Test_RetryPolicy_For_AcquireToken_Failure(t *testing.T) {
func Test_RetryPolicy_For_AcquireToken(t *testing.T) {
testCases := []struct {
numberOfFails int
expectedFail bool
Expand Down Expand Up @@ -317,7 +309,7 @@ func Test_RetryPolicy_For_AcquireToken_Failure(t *testing.T) {
}
} else {
if err != nil {
t.Fatalf("should have encountered the error")
t.Fatal(err)
}
if resp.AccessToken != token {
t.Fatalf("wanted %q, got %q", token, resp.AccessToken)
Expand Down

0 comments on commit 23290c4

Please sign in to comment.