Пример #1
0
func (s *UnitFacadeSuite) TestGetResourceInfoOkay(c *gc.C) {
	opened1 := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
	res1 := opened1.Resource
	opened2 := resourcetesting.NewResource(c, s.stub, "eggs", "a-application", "other data")
	res2 := opened2.Resource
	store := &stubUnitDataStore{Stub: s.stub}
	store.ReturnListResources = resource.ServiceResources{
		Resources: []resource.Resource{res1, res2},
	}
	uf := server.UnitFacade{DataStore: store}

	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
		ResourceNames: []string{"spam", "eggs"},
	})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "ListResources")
	c.Check(results, jc.DeepEquals, private.ResourcesResult{
		Resources: []private.ResourceResult{{
			Resource: api.Resource2API(res1),
		}, {
			Resource: api.Resource2API(res2),
		}},
	})
}
Пример #2
0
func newPersistenceResource(c *gc.C, serviceID, name string) (storedResource, resourceDoc) {
	content := name
	opened := resourcetesting.NewResource(c, nil, name, serviceID, content)
	res := opened.Resource

	stored := storedResource{
		Resource:    res,
		storagePath: "application-" + serviceID + "/resources/" + name,
	}

	doc := resourceDoc{
		DocID:         "resource#" + res.ID,
		ID:            res.ID,
		ApplicationID: res.ApplicationID,

		Name:        res.Name,
		Type:        res.Type.String(),
		Path:        res.Path,
		Description: res.Description,

		Origin:      res.Origin.String(),
		Revision:    res.Revision,
		Fingerprint: res.Fingerprint.Bytes(),
		Size:        res.Size,

		Username:  res.Username,
		Timestamp: res.Timestamp,

		StoragePath: stored.storagePath,
	}

	return stored, doc
}
Пример #3
0
func newResource(c *gc.C, name, username, data string) (resource.Resource, api.Resource) {
	opened := resourcetesting.NewResource(c, nil, name, "a-service", data)
	res := opened.Resource
	res.Username = username
	if username == "" {
		res.Timestamp = time.Time{}
	}

	apiRes := api.Resource{
		CharmResource: api.CharmResource{
			Name:        name,
			Type:        "file",
			Path:        res.Path,
			Origin:      "upload",
			Revision:    0,
			Fingerprint: res.Fingerprint.Bytes(),
			Size:        res.Size,
		},
		ID:        res.ID,
		ServiceID: res.ServiceID,
		Username:  username,
		Timestamp: res.Timestamp,
	}

	return res, apiRes
}
Пример #4
0
func (s *LegacyHTTPHandlerSuite) TestServeHTTPDownloadOkay(c *gc.C) {
	s.deps.ReturnNewResourceOpener = s.opener
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	s.deps.ReturnHandleDownload = opened
	h := &server.LegacyHTTPHandler{
		LegacyHTTPHandlerDeps: s.deps,
	}
	req, err := http.NewRequest("GET", "...", nil)
	c.Assert(err, jc.ErrorIsNil)

	h.ServeHTTP(s.resp, req)

	s.stub.CheckCallNames(c,
		"NewResourceOpener",
		"HandleDownload",
		"UpdateDownloadResponse",
		"WriteHeader",
		"Copy",
		"Close",
	)
	s.stub.CheckCall(c, 0, "NewResourceOpener", req)
	s.stub.CheckCall(c, 1, "HandleDownload", s.opener, req)
	s.stub.CheckCall(c, 2, "UpdateDownloadResponse", s.resp, opened.Resource)
	s.stub.CheckCall(c, 3, "WriteHeader", http.StatusOK)
	s.stub.CheckCall(c, 4, "Copy", s.resp, opened)
}
Пример #5
0
func newCharmResource(c *gc.C, name, data string, rev int) charmresource.Resource {
	opened := resourcetesting.NewResource(c, nil, name, "a-application", data)
	chRes := opened.Resource.Resource
	chRes.Origin = charmresource.OriginStore
	chRes.Revision = rev
	return chRes
}
Пример #6
0
func newResource(c *gc.C, name, username, data string) (resource.Resource, api.Resource) {
	opened := resourcetesting.NewResource(c, nil, name, "a-application", data)
	res := opened.Resource
	res.Revision = 1
	res.Username = username
	if username == "" {
		// Note that resourcetesting.NewResource() returns a resources
		// with a username and timestamp set. So if the username was
		// "un-set" then we have to also unset the timestamp.
		res.Timestamp = time.Time{}
	}

	apiRes := api.Resource{
		CharmResource: api.CharmResource{
			Name:        name,
			Type:        "file",
			Path:        res.Path,
			Origin:      "upload",
			Revision:    1,
			Fingerprint: res.Fingerprint.Bytes(),
			Size:        res.Size,
		},
		ID:            res.ID,
		ApplicationID: res.ApplicationID,
		Username:      username,
		Timestamp:     res.Timestamp,
	}

	return res, apiRes
}
Пример #7
0
func (s *DirectorySuite) TestWrite(c *gc.C) {
	res := resourcetesting.NewResource(c, s.stub.Stub, "spam", "a-application", "some data")
	stub := &stubDirectory{
		internalStub: s.stub,
	}
	stub.ReturnInfo = res.Resource
	opened := stub
	dataDir := "/var/lib/juju/agents/unit-spam-1/resources"
	deps := s.stub
	spec := internal.NewDirectorySpec(dataDir, "eggs", deps)
	s.stub.ResetCalls()
	dir := internal.NewDirectory(spec, deps)

	err := dir.Write(opened)
	c.Assert(err, jc.ErrorIsNil)

	stub.CheckCallNames(c,
		"Info",
		"Content",
		"Join",
		"CreateWriter",
		"WriteContent",
		"CloseAndLog",
	)
}
Пример #8
0
func newStoreResource(c *gc.C, name, data string) resource.Resource {
	opened := resourcetesting.NewResource(c, nil, name, "a-application", data)
	res := opened.Resource
	res.Origin = charmresource.OriginStore
	res.Revision = 1
	res.Username = ""
	res.Timestamp = time.Time{}
	return res
}
Пример #9
0
func newStoreResource(c *gc.C, name, serviceID string, revision int) resource.Resource {
	content := name
	opened := resourcetesting.NewResource(c, nil, name, serviceID, content)
	res := opened.Resource
	res.Origin = charmresource.OriginStore
	res.Revision = revision
	err := res.Validate()
	c.Assert(err, jc.ErrorIsNil)
	return res
}
Пример #10
0
func newResource(c *gc.C, stub *testing.Stub, name, content string) (resource.Resource, io.ReadCloser) {
	opened := resourcetesting.NewResource(c, stub, name, "a-application", content)
	res := opened.Resource
	if content != "" {
		return res, opened.ReadCloser
	}
	res.Username = ""
	res.Timestamp = time.Time{}
	return res, nil
}
Пример #11
0
func (s *UnitFacadeClientSuite) TestGetResource(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
	s.api.setResource(opened.Resource, opened)
	cl := client.NewUnitFacadeClient(s.api, s.api)

	info, content, err := cl.GetResource("spam")
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "Do", "FacadeCall")
	c.Check(info, jc.DeepEquals, opened.Resource)
	c.Check(content, jc.DeepEquals, opened)
}
Пример #12
0
func (s pushResource) step(c *gc.C, ctx *context) {
	opened := resourcetesting.NewResource(c, &gt.Stub{}, "data", ctx.unit.ApplicationName(), "the bytes")

	res, err := ctx.st.Resources()
	c.Assert(err, jc.ErrorIsNil)
	_, err = res.SetResource(
		ctx.unit.ApplicationName(),
		opened.Username,
		opened.Resource.Resource,
		opened.ReadCloser,
	)
	c.Assert(err, jc.ErrorIsNil)
}
Пример #13
0
func (s *ResourceSuite) TestOpenResourceSizeMismatch(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
	s.persist.ReturnGetResource = opened.Resource
	s.persist.ReturnGetResourcePath = "application-a-application/resources/spam"
	content := opened.Content()
	content.Size += 1
	s.storage.ReturnGet = content
	st := NewState(s.raw)
	s.stub.ResetCalls()

	_, _, err := st.OpenResource("a-application", "spam")

	s.stub.CheckCallNames(c, "GetResource", "Get")
	c.Check(err, gc.ErrorMatches, `storage returned a size \(10\) which doesn't match resource metadata \(9\)`)
}
Пример #14
0
func (s *ResourceSuite) TestOpenResourceOkay(c *gc.C) {
	data := "some data"
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", data)
	s.persist.ReturnGetResource = opened.Resource
	s.persist.ReturnGetResourcePath = "application-a-application/resources/spam"
	s.storage.ReturnGet = opened.Content()
	st := NewState(s.raw)
	s.stub.ResetCalls()

	info, reader, err := st.OpenResource("a-application", "spam")
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "GetResource", "Get")
	s.stub.CheckCall(c, 1, "Get", "application-a-application/resources/spam")
	c.Check(info, jc.DeepEquals, opened.Resource)
	c.Check(reader, gc.Equals, opened.ReadCloser)
}
Пример #15
0
func (s *DownloadSuite) TestHandleDownload(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	s.deps.ReturnExtractDownloadRequest = "spam"
	s.store.ReturnOpenResource = opened
	req, err := http.NewRequest("GET", "...", nil)
	c.Assert(err, jc.ErrorIsNil)

	res, reader, err := server.HandleDownload(req, s.deps)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c,
		"ExtractDownloadRequest",
		"OpenResource",
	)
	c.Check(res, jc.DeepEquals, opened.Resource)
	c.Check(reader, gc.Equals, opened.ReadCloser)
}
Пример #16
0
func (s *UnitFacadeSuite) TestGetResourceInfoEmpty(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
	store := &stubUnitDataStore{Stub: s.stub}
	store.ReturnListResources = resource.ServiceResources{
		Resources: []resource.Resource{opened.Resource},
	}
	uf := server.UnitFacade{DataStore: store}

	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
		ResourceNames: []string{},
	})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "ListResources")
	c.Check(results, jc.DeepEquals, private.ResourcesResult{
		Resources: []private.ResourceResult{},
	})
}
Пример #17
0
func (s *LegacyHTTPHandlerSuite) TestIntegration(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	s.store.ReturnOpenResource = opened
	s.deps.ReturnConnect = s.store
	h := server.NewLegacyHTTPHandler(s.deps.Connect)
	req, err := api.NewHTTPDownloadRequest("spam")
	c.Assert(err, jc.ErrorIsNil)
	req.URL, err = url.ParseRequestURI("https://api:17017/units/eggs/1/resources/spam?:resource=spam")
	c.Assert(err, jc.ErrorIsNil)
	resp := &fakeResponseWriter{
		stubResponseWriter: s.resp,
	}

	h.ServeHTTP(resp, req)

	resp.checkWritten(c, "some data", http.Header{
		"Content-Type":   []string{api.ContentTypeRaw},
		"Content-Length": []string{"9"}, // len("some data")
		"Content-Sha384": []string{opened.Fingerprint.String()},
	})
}
Пример #18
0
func (s *LegacyHTTPHandlerSuite) TestServeHTTPDownloadCopyFailed(c *gc.C) {
	s.deps.ReturnHandleDownload = resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	h := &server.LegacyHTTPHandler{
		LegacyHTTPHandlerDeps: s.deps,
	}
	failure := errors.New("<failure>")
	s.stub.SetErrors(nil, nil, failure)
	req, err := http.NewRequest("GET", "...", nil)
	c.Assert(err, jc.ErrorIsNil)

	h.ServeHTTP(s.resp, req)

	s.stub.CheckCallNames(c,
		"NewResourceOpener",
		"HandleDownload",
		"UpdateDownloadResponse",
		"WriteHeader",
		"Copy",
		"Close",
	)
}
Пример #19
0
func (s *ResourceSuite) TestOpenResourceForUniterOkay(c *gc.C) {
	data := "some data"
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", data)
	s.persist.ReturnGetResource = opened.Resource
	s.persist.ReturnGetResourcePath = "application-a-application/resources/spam"
	s.storage.ReturnGet = opened.Content()
	unit := newUnit(s.stub, "a-application/0")
	st := NewState(s.raw)
	s.stub.ResetCalls()

	info, reader, err := st.OpenResourceForUniter(unit, "spam")
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "ApplicationName", "GetResource", "Get", "Name", "SetUnitResourceProgress")
	s.stub.CheckCall(c, 2, "Get", "application-a-application/resources/spam")
	c.Check(info, jc.DeepEquals, opened.Resource)

	b, err := ioutil.ReadAll(reader)
	// note ioutil.ReadAll converts EOF to nil
	c.Check(err, jc.ErrorIsNil)
	c.Check(b, gc.DeepEquals, []byte(data))
}
Пример #20
0
func (s *UnitFacadeSuite) TestGetResourceInfoNotFound(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	store := &stubUnitDataStore{Stub: s.stub}
	store.ReturnListResources = resource.ServiceResources{
		Resources: []resource.Resource{opened.Resource},
	}
	uf := server.UnitFacade{DataStore: store}

	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
		ResourceNames: []string{"eggs"},
	})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "ListResources")
	c.Check(results, jc.DeepEquals, private.ResourcesResult{
		Resources: []private.ResourceResult{{
			ErrorResult: params.ErrorResult{
				Error: common.ServerError(errors.NotFoundf(`resource "eggs"`)),
			},
		}},
	})
}
Пример #21
0
func newResource(c *gc.C, name, data string) resource.Resource {
	opened := resourcetesting.NewResource(c, nil, name, "a-service", data)
	res := opened.Resource
	res.Timestamp = time.Unix(res.Timestamp.Unix(), 0)
	return res
}
Пример #22
0
func newUploadResource(c *gc.C, name, data string) resource.Resource {
	opened := resourcetesting.NewResource(c, nil, name, "a-application", data)
	return opened.Resource
}
Пример #23
0
func (HelpersSuite) TestServiceResources2API(c *gc.C) {
	res1 := resourcetesting.NewResource(c, nil, "res1", "a-service", "data").Resource
	res2 := resourcetesting.NewResource(c, nil, "res2", "a-service", "data2").Resource

	tag0 := names.NewUnitTag("a-service/0")
	tag1 := names.NewUnitTag("a-service/1")

	chres1 := res1.Resource
	chres2 := res2.Resource
	chres1.Revision++
	chres2.Revision++

	svcRes := resource.ServiceResources{
		Resources: []resource.Resource{
			res1,
			res2,
		},
		UnitResources: []resource.UnitResources{
			{
				Tag: tag0,
				Resources: []resource.Resource{
					res1,
					res2,
				},
				DownloadProgress: map[string]int64{
					res2.Name: 2,
				},
			},
			{
				Tag: tag1,
			},
		},
		CharmStoreResources: []charmresource.Resource{
			chres1,
			chres2,
		},
	}

	result := api.ServiceResources2APIResult(svcRes)

	apiRes1 := api.Resource2API(res1)
	apiRes2 := api.Resource2API(res2)

	apiChRes1 := api.CharmResource2API(chres1)
	apiChRes2 := api.CharmResource2API(chres2)

	c.Check(result, jc.DeepEquals, api.ResourcesResult{
		Resources: []api.Resource{
			apiRes1,
			apiRes2,
		},
		UnitResources: []api.UnitResources{
			{
				Entity: params.Entity{
					Tag: "unit-a-service-0",
				},
				Resources: []api.Resource{
					apiRes1,
					apiRes2,
				},
				DownloadProgress: map[string]int64{
					res2.Name: 2,
				},
			},
			{
				// we should have a listing for every unit, even if they
				// have no resources.
				Entity: params.Entity{
					Tag: "unit-a-service-1",
				},
			},
		},
		CharmStoreResources: []api.CharmResource{
			apiChRes1,
			apiChRes2,
		},
	})
}