示例#1
0
文件: environ_test.go 项目: bac/juju
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)
}
示例#2
0
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
}
示例#3
0
文件: instance_test.go 项目: bac/juju
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"))
}
示例#4
0
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())
}
示例#5
0
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")
	}
}
示例#6
0
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")
	}
}
示例#7
0
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)
	}
}
示例#8
0
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")
	}
}
示例#9
0
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")
	}
}
示例#10
0
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)
	}
}
示例#11
0
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")
	}
}
示例#12
0
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")
	}
}
示例#13
0
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)
	}
}
示例#14
0
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)
	}
}
示例#15
0
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)
	}
}
示例#16
0
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)
	}
}
示例#17
0
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")
	}
}
示例#18
0
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")
	}
}
示例#19
0
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)
	}
}
示例#20
0
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")
	}
}
示例#21
0
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)
	}
}
示例#22
0
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)
	}
}
示例#23
0
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)
	}
}
示例#24
0
文件: oauth_test.go 项目: bac/juju
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
}
示例#25
0
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)
	}
}
示例#26
0
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
}
示例#27
0
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)
	}
}
示例#28
0
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)
	}
}
示例#29
0
文件: environ_test.go 项目: bac/juju
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)
}
示例#30
0
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")
	}
}