func (s *environSuite) TestStopInstancesNotFound(c *gc.C) { env := s.openEnviron(c) sender0 := mocks.NewSender() sender0.AppendResponse(mocks.NewResponseWithStatus( "vm not found", http.StatusNotFound, )) sender1 := mocks.NewSender() sender1.AppendResponse(mocks.NewResponseWithStatus( "vm not found", http.StatusNotFound, )) s.sender = azuretesting.Senders{sender0, sender1} err := env.StopInstances("a", "b") c.Assert(err, jc.ErrorIsNil) }
func newAsynchronousResponseWithError() *http.Response { r := mocks.NewResponseWithStatus("400 Bad Request", http.StatusBadRequest) mocks.SetRetryHeader(r, retryDelay) r.Request = mocks.NewRequestForURL(mocks.TestURL) r.Body = mocks.NewBody(errorResponse) return r }
func (s *instanceSuite) TestInstanceClosePorts(c *gc.C) { inst := s.getInstance(c) sender := mocks.NewSender() notFoundSender := mocks.NewSender() notFoundSender.AppendResponse(mocks.NewResponseWithStatus( "rule not found", http.StatusNotFound, )) s.sender = azuretesting.Senders{sender, notFoundSender} err := inst.ClosePorts("0", []jujunetwork.PortRange{{ Protocol: "tcp", FromPort: 1000, ToPort: 1000, }, { Protocol: "udp", FromPort: 1000, ToPort: 2000, }}) c.Assert(err, jc.ErrorIsNil) c.Assert(s.requests, gc.HasLen, 2) c.Assert(s.requests[0].Method, gc.Equals, "DELETE") c.Assert(s.requests[0].URL.Path, gc.Equals, securityRulePath("machine-0-tcp-1000")) c.Assert(s.requests[1].Method, gc.Equals, "DELETE") c.Assert(s.requests[1].URL.Path, gc.Equals, securityRulePath("machine-0-udp-1000-2000")) }
func TestClientPollAsNeededPollsForDuration(t *testing.T) { c := Client{} c.PollingMode = PollUntilDuration c.PollingDuration = 10 * time.Millisecond s := mocks.NewSender() s.EmitStatus("202 Accepted", http.StatusAccepted) c.Sender = s r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(r) s.SetResponse(r) d1 := 10 * time.Millisecond start := time.Now() resp, _ := c.PollAsNeeded(r) d2 := time.Now().Sub(start) if d2 < d1 { t.Errorf("autorest: Client#PollAsNeeded did not poll for the expected duration -- expected %v, actual %v", d1.Seconds(), d2.Seconds()) } Respond(resp, ByClosing()) }
func TestNewPollingRequestDoesNotReturnARequestWhenLocationHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) req, _ := NewPollingRequest(resp, nil) if req != nil { t.Fatal("autorest: NewPollingRequest returned an http.Request when the Location header was missing") } }
func TestClientShouldPoll(t *testing.T) { c := &Client{PollingMode: PollUntilAttempts} r := mocks.NewResponseWithStatus("202 Accepted", 202) if !c.ShouldPoll(r) { t.Error("autorest: Client#ShouldPoll failed to return true for an http.Response that requires polling") } }
func TestDoRetryForStatusCodesWithSuccess(t *testing.T) { client := mocks.NewSender() client.AppendAndRepeatResponse(mocks.NewResponseWithStatus("408 Request Timeout", http.StatusRequestTimeout), 2) client.AppendResponse(mocks.NewResponseWithStatus("200 OK", http.StatusOK)) r, _ := SendWithSender(client, mocks.NewRequest(), DoRetryForStatusCodes(5, time.Duration(2*time.Second), http.StatusRequestTimeout), ) Respond(r, ByClosing()) if client.Attempts() != 3 { t.Fatalf("autorest: Sender#DoRetryForStatusCodes -- Got: StatusCode %v in %v attempts; Want: StatusCode 200 OK in 2 attempts -- ", r.Status, client.Attempts()-1) } }
func TestResponseRequiresPollingDefaultsToAcceptedStatusCode(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) if !ResponseRequiresPolling(resp) { t.Error("autorest: ResponseRequiresPolling failed to create a request for default 202 Accepted status code") } }
func TestCreatePollingRequestDoesNotReturnARequestWhenLocationHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("500 ServerError", 500) req, _ := CreatePollingRequest(resp, NullAuthorizer{}) if req != nil { t.Error("autorest: CreatePollingRequest returned an http.Request when the Location header was missing") } }
func TestGetLocationReturnsEmptyStringForMissingLocation(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) l := GetLocation(resp) if len(l) != 0 { t.Fatalf("autorest: GetLocation return a value without a Location header -- received %v", l) } }
func TestResponseRequiresPollingReturnsFalseForUnexpectedStatusCodes(t *testing.T) { resp := mocks.NewResponseWithStatus("500 ServerError", 500) addAcceptedHeaders(resp) if ResponseRequiresPolling(resp) { t.Error("autorest: ResponseRequiresPolling did not return false when ignoring a status code") } }
func TestCreatePollingRequestLeavesBodyOpenWhenLocationHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("500 ServerError", 500) CreatePollingRequest(resp, NullAuthorizer{}) if !resp.Body.(*mocks.Body).IsOpen() { t.Error("autorest: CreatePollingRequest closed the http.Request Body when the Location header was missing") } }
func TestGetRetryDelayReturnsDefaultDelayIfRetryHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) d := GetRetryDelay(resp, DefaultPollingDelay) if d != DefaultPollingDelay { t.Errorf("autorest: GetRetryDelay failed to returned the default delay for a missing Retry-After header -- expected %v, received %v", DefaultPollingDelay, d) } }
func TestGetRetryDelay(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) d := GetRetryDelay(resp, DefaultPollingDelay) if d != testDelay { t.Errorf("autorest: GetRetryDelay failed to returned the expected delay -- expected %v, received %v", testDelay, d) } }
func TestCreatePollingRequestProvidesTheURL(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) req, _ := CreatePollingRequest(resp, NullAuthorizer{}) if req.URL.String() != testUrl { t.Errorf("autorest: CreatePollingRequest did not create an HTTP with the expected URL -- received %v, expected %v", req.URL, testUrl) } }
func TestGetLocation(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) l := GetLocation(resp) if len(l) == 0 { t.Fatalf("autorest: GetLocation failed to return Location header -- expected %v, received %v", mocks.TestURL, l) } }
func TestCreatePollingRequestClosesTheResponseBody(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) CreatePollingRequest(resp, NullAuthorizer{}) if resp.Body.(*mocks.Body).IsOpen() { t.Error("autorest: CreatePollingRequest failed to close the response body when creating a new request") } }
func TestNewPollingRequestReturnsAnErrorWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) _, err := NewPollingRequest(resp, mockFailingAuthorizer{}) if err == nil { t.Error("autorest: NewPollingRequest failed to return an error when Prepare fails") } }
func TestNewPollingRequestReturnsAGetRequest(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, nil) if req.Method != "GET" { t.Fatalf("autorest: NewPollingRequest did not create an HTTP GET request -- actual method %v", req.Method) } }
func TestClientIsPollingAllowedIgnoredPollingMode(t *testing.T) { c := Client{PollingMode: DoNotPoll} r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) err := c.IsPollingAllowed(r) if err == nil { t.Error("autorest: Client#IsPollingAllowed failed to return an error when polling is disabled") } }
func TestClientIsPollingAllowedIgnoresDisabledForIgnoredStatusCode(t *testing.T) { c := Client{PollingMode: PollUntilAttempts} r := mocks.NewResponseWithStatus("400 BadRequest", http.StatusBadRequest) err := c.IsPollingAllowed(r) if err != nil { t.Errorf("autorest: Client#IsPollingAllowed returned an error for an http.Response that requires polling (%v)", err) } }
func TestClientIsPollingAllowed(t *testing.T) { c := Client{PollingMode: PollUntilAttempts} r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) err := c.IsPollingAllowed(r) if err != nil { t.Errorf("autorest: Client#IsPollingAllowed returned an error for an http.Response that requires polling (%v)", err) } }
func TestGetRetryAfter(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) d := GetRetryAfter(resp, DefaultPollingDelay) if d != mocks.TestDelay { t.Fatalf("autorest: GetRetryAfter failed to returned the expected delay -- expected %v, received %v", mocks.TestDelay, d) } }
func oauthConfigSender() autorest.Sender { sender := mocks.NewSender() resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized) mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{ `authorization_uri="https://testing.invalid/` + fakeTenantId + `"`, }) sender.AppendResponse(resp) return sender }
func TestCreatePollingRequestReturnsAGetRequest(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) req, _ := CreatePollingRequest(resp, NullAuthorizer{}) if req.Method != "GET" { t.Errorf("autorest: CreatePollingRequest did not create an HTTP GET request -- actual method %v", req.Method) } }
func newAsynchronousResponse() *http.Response { r := mocks.NewResponseWithStatus("201 Created", http.StatusCreated) r.Body = mocks.NewBody(fmt.Sprintf(pollingStateFormat, operationInProgress)) mocks.SetResponseHeader(r, http.CanonicalHeaderKey(headerAsyncOperation), mocks.TestAzureAsyncURL) mocks.SetResponseHeader(r, http.CanonicalHeaderKey(autorest.HeaderLocation), mocks.TestLocationURL) mocks.SetRetryHeader(r, retryDelay) r.Request = mocks.NewRequestForURL(mocks.TestURL) return r }
func TestNewPollingRequestProvidesTheURL(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, nil) if req.URL.String() != mocks.TestURL { t.Fatalf("autorest: NewPollingRequest did not create an HTTP with the expected URL -- received %v, expected %v", req.URL, mocks.TestURL) } }
func TestCreatePollingRequestLeavesBodyOpenWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) resp.Header.Set(http.CanonicalHeaderKey(headerLocation), testBadUrl) _, err := CreatePollingRequest(resp, NullAuthorizer{}) if !resp.Body.(*mocks.Body).IsOpen() { t.Errorf("autorest: CreatePollingRequest closed the http.Request Body when Prepare returned an error (%v)", err) } }
func (s *environSuite) TestAllInstancesResourceGroupNotFound(c *gc.C) { env := s.openEnviron(c) sender := mocks.NewSender() sender.AppendResponse(mocks.NewResponseWithStatus( "resource group not found", http.StatusNotFound, )) s.sender = azuretesting.Senders{sender} _, err := env.AllInstances() c.Assert(err, jc.ErrorIsNil) }
func TestCreatePollingRequestReturnsAnErrorWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", 202) addAcceptedHeaders(resp) resp.Header.Set(http.CanonicalHeaderKey(headerLocation), testBadUrl) _, err := CreatePollingRequest(resp, NullAuthorizer{}) if err == nil { t.Error("autorest: CreatePollingRequest failed to return an error when Prepare fails") } }