func (s *ListResourcesSuite) TestTooMany(c *gc.C) {
	s.facade.FacadeCallFn = func(_ string, _, response interface{}) error {
		typedResponse, ok := response.(*api.ResourcesResults)
		c.Assert(ok, jc.IsTrue)

		typedResponse.Results = []api.ResourcesResult{{
			Resources: nil,
		}, {
			Resources: nil,
		}, {
			Resources: nil,
		}}

		return nil
	}

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service", "other-service"}
	results, err := cl.ListResources(services)

	c.Check(results, gc.HasLen, 0)
	c.Check(err, gc.ErrorMatches, `.*got invalid data from server \(expected 2 results, got 3\).*`)
	s.stub.CheckCallNames(c, "FacadeCall")
}
func (s *ListResourcesSuite) TestBulk(c *gc.C) {
	expected1, apiResult1 := newResourceResult(c, "a-service", "spam")
	s.facade.apiResults["a-service"] = apiResult1
	expected2, apiResult2 := newResourceResult(c, "other-service", "eggs", "ham")
	s.facade.apiResults["other-service"] = apiResult2

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service", "other-service"}
	results, err := cl.ListResources(services)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(results, jc.DeepEquals, []resource.ServiceResources{
		{Resources: expected1},
		{Resources: expected2},
	})
	c.Check(s.stub.Calls(), gc.HasLen, 1)
	s.stub.CheckCall(c, 0, "FacadeCall",
		"ListResources",
		&api.ListResourcesArgs{[]params.Entity{
			{
				Tag: "service-a-service",
			}, {
				Tag: "service-other-service",
			},
		}},
		&api.ResourcesResults{
			Results: []api.ResourcesResult{
				apiResult1,
				apiResult2,
			},
		},
	)
}
Exemplo n.º 3
0
func (s *UploadSuite) TestPendingOkay(c *gc.C) {
	res, apiResult := newResourceResult(c, "a-service", "spam")
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	expected := uuid.String()
	s.response.Resource = apiResult.Resources[0]
	data := "<data>"
	fp, err := charmresource.GenerateFingerprint(strings.NewReader(data))
	c.Assert(err, jc.ErrorIsNil)
	req, err := http.NewRequest("PUT", "/services/a-service/resources/spam", nil)
	c.Assert(err, jc.ErrorIsNil)
	req.Header.Set("Content-Type", "application/octet-stream")
	req.Header.Set("Content-SHA384", fp.String())
	req.Header.Set("Content-Length", fmt.Sprint(len(data)))
	req.ContentLength = int64(len(data))
	req.URL.RawQuery = "pendingid=" + expected
	reader := &stubFile{stub: s.stub}
	reader.returnRead = strings.NewReader(data)
	s.facade.pendingIDs = []string{expected}
	cl := client.NewClient(s.facade, s, s.facade)

	uploadID, err := cl.AddPendingResource("a-service", res[0].Resource, reader)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c,
		"FacadeCall",
		"Read",
		"Read",
		"Seek",
		"Do",
	)
	s.stub.CheckCall(c, 4, "Do", req, reader, s.response)
	c.Check(uploadID, gc.Equals, expected)
}
Exemplo n.º 4
0
func (s *ListResourcesSuite) TestOkay(c *gc.C) {
	expected, apiResult := newResourceResult(c, "a-application", "spam")
	s.facade.apiResults["a-application"] = apiResult

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-application"}
	results, err := cl.ListResources(services)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(results, jc.DeepEquals, []resource.ServiceResources{
		{Resources: expected},
	})
	c.Check(s.stub.Calls(), gc.HasLen, 1)
	s.stub.CheckCall(c, 0, "FacadeCall",
		"ListResources",
		&api.ListResourcesArgs{[]params.Entity{{
			Tag: "application-a-application",
		}}},
		&api.ResourcesResults{
			Results: []api.ResourcesResult{
				apiResult,
			},
		},
	)
}
Exemplo n.º 5
0
func (s *UploadSuite) TestBadService(c *gc.C) {
	cl := client.NewClient(s.facade, s, s.facade)

	err := cl.Upload("???", "spam", nil)

	c.Check(err, gc.ErrorMatches, `.*invalid service.*`)
	s.stub.CheckNoCalls(c)
}
func (s *ListResourcesSuite) TestBadServices(c *gc.C) {
	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"???"}
	_, err := cl.ListResources(services)

	c.Check(err, gc.ErrorMatches, `.*invalid service.*`)
	s.stub.CheckNoCalls(c)
}
func (s *ListResourcesSuite) TestServiceNotFound(c *gc.C) {
	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service"}
	_, err := cl.ListResources(services)

	c.Check(err, jc.Satisfies, errors.IsNotFound)
	s.stub.CheckCallNames(c, "FacadeCall")
}
func (s *ListResourcesSuite) TestNoServices(c *gc.C) {
	cl := client.NewClient(s.facade, s, s.facade)

	var services []string
	results, err := cl.ListResources(services)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(results, gc.HasLen, 0)
	s.stub.CheckCallNames(c, "FacadeCall")
}
Exemplo n.º 9
0
func (s *UploadSuite) TestPendingBadService(c *gc.C) {
	res, _ := newResourceResult(c, "a-service", "spam")
	s.facade.FacadeCallFn = nil
	cl := client.NewClient(s.facade, s, s.facade)

	_, err := cl.AddPendingResource("???", res[0].Resource, nil)

	c.Check(err, gc.ErrorMatches, `.*invalid service.*`)
	s.stub.CheckNoCalls(c)
}
Exemplo n.º 10
0
func newAPIClient(apiCaller api.Connection) (*client.Client, error) {
	caller := base.NewFacadeCallerForVersion(apiCaller, resource.ComponentName, server.Version)

	httpClient, err := apiCaller.HTTPClient()
	if err != nil {
		return nil, errors.Trace(err)
	}
	// The apiCaller takes care of prepending /environment/<envUUID>.
	apiClient := client.NewClient(caller, httpClient, apiCaller)
	return apiClient, nil
}
Exemplo n.º 11
0
func (s *UploadSuite) TestBadRequest(c *gc.C) {
	reader := &stubFile{stub: s.stub}
	cl := client.NewClient(s.facade, s, s.facade)
	failure := errors.New("<failure>")
	s.stub.SetErrors(failure)

	err := cl.Upload("a-service", "spam", reader)

	c.Check(errors.Cause(err), gc.Equals, failure)
	s.stub.CheckCallNames(c, "Read")
}
Exemplo n.º 12
0
func (s *UploadSuite) TestRequestFailed(c *gc.C) {
	reader := &stubFile{stub: s.stub}
	reader.returnRead = strings.NewReader("<data>")
	cl := client.NewClient(s.facade, s, s.facade)
	failure := errors.New("<failure>")
	s.stub.SetErrors(nil, nil, nil, failure)

	err := cl.Upload("a-service", "spam", reader)

	c.Check(errors.Cause(err), gc.Equals, failure)
	s.stub.CheckCallNames(c, "Read", "Read", "Seek", "Do")
}
func (s *ListResourcesSuite) TestServiceEmpty(c *gc.C) {
	s.facade.apiResults["a-service"] = api.ResourcesResult{}

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service"}
	results, err := cl.ListResources(services)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(results, jc.DeepEquals, []resource.ServiceResources{
		{},
	})
	s.stub.CheckCallNames(c, "FacadeCall")
}
func (s *ListResourcesSuite) TestServerError(c *gc.C) {
	failure := errors.New("<failure>")
	s.facade.FacadeCallFn = func(_ string, _, _ interface{}) error {
		return failure
	}

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service"}
	_, err := cl.ListResources(services)

	c.Check(err, gc.ErrorMatches, `<failure>`)
	s.stub.CheckCallNames(c, "FacadeCall")
}
Exemplo n.º 15
0
func (s *UploadSuite) TestPendingBadRequest(c *gc.C) {
	res, _ := newResource(c, "spam", "", "")
	chRes := res.Resource
	reader := &stubFile{stub: s.stub}
	s.facade.pendingIDs = []string{"some-unique-id"}
	cl := client.NewClient(s.facade, s, s.facade)
	failure := errors.New("<failure>")
	s.stub.SetErrors(nil, failure)

	_, err := cl.AddPendingResource("a-service", chRes, reader)

	c.Check(errors.Cause(err), gc.Equals, failure)
	s.stub.CheckCallNames(c, "FacadeCall", "Read")
}
Exemplo n.º 16
0
func (s *UploadSuite) TestPendingNoFile(c *gc.C) {
	res, apiResult := newResourceResult(c, "a-service", "spam")
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	expected := uuid.String()
	s.response.Resource = apiResult.Resources[0]
	s.facade.pendingIDs = []string{expected}
	cl := client.NewClient(s.facade, s, s.facade)

	uploadID, err := cl.AddPendingResource("a-service", res[0].Resource, nil)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c,
		"FacadeCall",
	)
	c.Check(uploadID, gc.Equals, expected)
}
Exemplo n.º 17
0
func (s *UploadSuite) TestPendingRequestFailed(c *gc.C) {
	res, _ := newResourceResult(c, "a-service", "spam")
	reader := &stubFile{stub: s.stub}
	reader.returnRead = strings.NewReader("<data>")
	s.facade.pendingIDs = []string{"some-unique-id"}
	cl := client.NewClient(s.facade, s, s.facade)
	failure := errors.New("<failure>")
	s.stub.SetErrors(nil, nil, nil, nil, failure)

	_, err := cl.AddPendingResource("a-service", res[0].Resource, reader)

	c.Check(errors.Cause(err), gc.Equals, failure)
	s.stub.CheckCallNames(c,
		"FacadeCall",
		"Read",
		"Read",
		"Seek",
		"Do",
	)
}
Exemplo n.º 18
0
func (s *UploadSuite) TestOkay(c *gc.C) {
	data := "<data>"
	_, s.response.Resource = newResource(c, "spam", "a-user", data)
	fp, err := charmresource.GenerateFingerprint(strings.NewReader(data))
	c.Assert(err, jc.ErrorIsNil)
	req, err := http.NewRequest("PUT", "/services/a-service/resources/spam", nil)
	c.Assert(err, jc.ErrorIsNil)
	req.Header.Set("Content-Type", "application/octet-stream")
	req.Header.Set("Content-SHA384", fp.String())
	req.Header.Set("Content-Length", fmt.Sprint(len(data)))
	req.ContentLength = int64(len(data))
	reader := &stubFile{stub: s.stub}
	reader.returnRead = strings.NewReader(data)
	cl := client.NewClient(s.facade, s, s.facade)

	err = cl.Upload("a-service", "spam", reader)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "Read", "Read", "Seek", "Do")
	s.stub.CheckCall(c, 3, "Do", req, reader, s.response)
}
func (s *ListResourcesSuite) TestConversionFailed(c *gc.C) {
	s.facade.FacadeCallFn = func(_ string, _, response interface{}) error {
		typedResponse, ok := response.(*api.ResourcesResults)
		c.Assert(ok, jc.IsTrue)

		var res api.Resource
		res.Name = "spam"
		typedResponse.Results = []api.ResourcesResult{{
			Resources: []api.Resource{
				res,
			},
		}}

		return nil
	}

	cl := client.NewClient(s.facade, s, s.facade)

	services := []string{"a-service"}
	_, err := cl.ListResources(services)

	c.Check(err, gc.ErrorMatches, `.*got bad data.*`)
	s.stub.CheckCallNames(c, "FacadeCall")
}
Exemplo n.º 20
0
func (s *UploadSuite) TestPendingResources(c *gc.C) {
	res, apiResult := newResourceResult(c, "a-service", "spam")
	resources := []charmresource.Resource{res[0].Resource}
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	expected := []string{uuid.String()}
	s.response.Resource = apiResult.Resources[0]
	s.facade.pendingIDs = expected
	cURL := charm.MustParseURL("cs:~a-user/trusty/spam-5")
	cl := client.NewClient(s.facade, s, s.facade)

	pendingIDs, err := cl.AddPendingResources(client.AddPendingResourcesArgs{
		ServiceID: "a-service",
		CharmID: charmstore.CharmID{
			URL: cURL,
		},
		Resources: resources,
	})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "FacadeCall")
	//s.stub.CheckCall(c, 0, "FacadeCall", "AddPendingResources", args, result)
	c.Check(pendingIDs, jc.DeepEquals, expected)
}