@@ -23,6 +23,7 @@ import (
2323 "fmt"
2424 "io"
2525 "net/http"
26+ "sort"
2627 "strings"
2728 "testing"
2829 "time"
@@ -45,10 +46,10 @@ var (
4546)
4647
4748func Test_AWS_DeleteResource (t * testing.T ) {
48- ctx := context . Background ()
49+ ctx := t . Context ()
4950
5051 myTest := test .NewUCPTest (t , "Test_AWS_DeleteResource" , func (t * testing.T , url string , roundTripper http.RoundTripper ) {
51- logGroupName := generateLogGroupName ()
52+ logGroupName := generateLogGroupName (t )
5253 setupTestAWSResource (t , ctx , logGroupName )
5354 resourceID , err := validation .GetResourceIdentifier (ctx , logGroupResourceType , logGroupName )
5455 require .NoError (t , err )
@@ -71,7 +72,7 @@ func Test_AWS_DeleteResource(t *testing.T) {
7172 require .NoError (t , err )
7273 deleteResponse , err := roundTripper .RoundTrip (deleteRequest )
7374 require .NoError (t , err )
74- require . Equal (t , http . StatusAccepted , deleteResponse . StatusCode )
75+ requireResponseStatus (t , deleteResponse , http . StatusAccepted )
7576
7677 // Get the operation status url from the Azure-Asyncoperation header
7778 deleteResponseCompletionUrl := deleteResponse .Header ["Azure-Asyncoperation" ][0 ]
@@ -106,10 +107,10 @@ func Test_AWS_DeleteResource(t *testing.T) {
106107}
107108
108109func Test_AWS_ListResources (t * testing.T ) {
109- ctx := context . Background ()
110+ ctx := t . Context ()
110111
111112 myTest := test .NewUCPTest (t , "Test_AWS_ListResources" , func (t * testing.T , url string , roundTripper http.RoundTripper ) {
112- var logGroupName = generateLogGroupName ()
113+ var logGroupName = generateLogGroupName (t )
113114 setupTestAWSResource (t , ctx , logGroupName )
114115 resourceID , err := validation .GetResourceIdentifier (ctx , logGroupResourceType , logGroupName )
115116 require .NoError (t , err )
@@ -126,7 +127,7 @@ func Test_AWS_ListResources(t *testing.T) {
126127 listResponse , err := roundTripper .RoundTrip (listRequest )
127128 require .NoError (t , err )
128129
129- require . Equal (t , http . StatusOK , listResponse . StatusCode )
130+ requireResponseStatus (t , listResponse , http . StatusOK )
130131
131132 defer listResponse .Body .Close ()
132133 payload , err := io .ReadAll (listResponse .Body )
@@ -197,6 +198,41 @@ func waitForSuccess(t *testing.T, ctx context.Context, awsClient aws.AWSCloudCon
197198 require .NoError (t , err )
198199}
199200
200- func generateLogGroupName () string {
201+ func generateLogGroupName (t * testing.T ) string {
202+ t .Helper ()
201203 return "ucpfunctionaltest-" + uuid .NewString ()
202204}
205+
206+ func requireResponseStatus (t * testing.T , response * http.Response , expectedStatus int ) {
207+ t .Helper ()
208+ require .NotNil (t , response )
209+
210+ if response .StatusCode == expectedStatus {
211+ return
212+ }
213+
214+ body , err := io .ReadAll (response .Body )
215+ require .NoError (t , err )
216+ require .NoError (t , response .Body .Close ())
217+
218+ response .Body = io .NopCloser (bytes .NewReader (body ))
219+
220+ headerKeys := make ([]string , 0 , len (response .Header ))
221+ for key := range response .Header {
222+ headerKeys = append (headerKeys , key )
223+ }
224+ sort .Strings (headerKeys )
225+
226+ headers := make ([]string , 0 , len (headerKeys ))
227+ for _ , key := range headerKeys {
228+ headers = append (headers , fmt .Sprintf ("%s=%s" , key , strings .Join (response .Header .Values (key ), "," )))
229+ }
230+
231+ require .Failf (t , "unexpected response status" ,
232+ "expected status %d, got %d\n headers: %s\n body: %s" ,
233+ expectedStatus ,
234+ response .StatusCode ,
235+ strings .Join (headers , "; " ),
236+ string (body ),
237+ )
238+ }
0 commit comments