func (env *mockEnviron) GetToolsSources() ([]simplestreams.DataSource, error) { if env.getToolsSources != nil { return env.getToolsSources() } datasource := storage.NewStorageSimpleStreamsDataSource("test cloud storage", env.Storage(), storage.BaseToolsPath) return []simplestreams.DataSource{datasource}, nil }
func (s *datasourceSuite) TestFetchWithRetry(c *gc.C) { stor := &fakeStorage{shouldRetry: true} ds := storage.NewStorageSimpleStreamsDataSource("test datasource", stor, "base") ds.SetAllowRetry(true) _, _, err := ds.Fetch("foo/bar/data.txt") c.Assert(err, gc.ErrorMatches, "an error") c.Assert(stor.getName, gc.Equals, "base/foo/bar/data.txt") c.Assert(stor.invokeCount, gc.Equals, 10) }
func (s *datasourceSuite) TestURLWithBasePath(c *gc.C) { sampleData := "hello world" s.stor.Put("base/bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "base") url, err := ds.URL("bar") c.Assert(err, gc.IsNil) expectedURL, _ := s.stor.URL("base/bar") c.Assert(url, gc.Equals, expectedURL) }
// ParseMetadataFromStorage loads ImageMetadata from the specified storage reader. func ParseMetadataFromStorage(c *gc.C, stor storage.StorageReader) []*imagemetadata.ImageMetadata { source := storage.NewStorageSimpleStreamsDataSource("test storage reader", stor, "images") // Find the simplestreams index file. params := simplestreams.ValueParams{ DataType: "image-ids", ValueTemplate: imagemetadata.ImageMetadata{}, } const requireSigned = false indexPath := simplestreams.UnsignedIndex indexRef, err := simplestreams.GetIndexWithFormat( source, indexPath, "index:1.0", requireSigned, simplestreams.CloudSpec{}, params) c.Assert(err, gc.IsNil) c.Assert(indexRef.Indexes, gc.HasLen, 1) imageIndexMetadata := indexRef.Indexes["com.ubuntu.cloud:custom"] c.Assert(imageIndexMetadata, gc.NotNil) // Read the products file contents. r, err := stor.Get(path.Join("images", imageIndexMetadata.ProductsFilePath)) defer r.Close() c.Assert(err, gc.IsNil) data, err := ioutil.ReadAll(r) c.Assert(err, gc.IsNil) // Parse the products file metadata. url, err := source.URL(imageIndexMetadata.ProductsFilePath) c.Assert(err, gc.IsNil) cloudMetadata, err := simplestreams.ParseCloudMetadata(data, "products:1.0", url, imagemetadata.ImageMetadata{}) c.Assert(err, gc.IsNil) // Collate the metadata. imageMetadataMap := make(map[string]*imagemetadata.ImageMetadata) var expectedProductIds set.Strings var imageVersions set.Strings for _, mc := range cloudMetadata.Products { for _, items := range mc.Items { for key, item := range items.Items { imageMetadata := item.(*imagemetadata.ImageMetadata) imageMetadataMap[key] = imageMetadata imageVersions.Add(key) productId := fmt.Sprintf("com.ubuntu.cloud:server:%s:%s", mc.Version, imageMetadata.Arch) expectedProductIds.Add(productId) } } } // Make sure index's product IDs are all represented in the products metadata. sort.Strings(imageIndexMetadata.ProductIds) c.Assert(imageIndexMetadata.ProductIds, gc.DeepEquals, expectedProductIds.SortedValues()) imageMetadata := make([]*imagemetadata.ImageMetadata, len(imageMetadataMap)) for i, key := range imageVersions.SortedValues() { imageMetadata[i] = imageMetadataMap[key] } return imageMetadata }
func (s *datasourceSuite) TestFetchWithNoRetry(c *gc.C) { // NB shouldRetry below is true indicating the fake storage is capable of // retrying, not that it will retry. stor := &fakeStorage{shouldRetry: true} ds := storage.NewStorageSimpleStreamsDataSource("test datasource", stor, "base") _, _, err := ds.Fetch("foo/bar/data.txt") c.Assert(err, gc.ErrorMatches, "an error") c.Assert(stor.getName, gc.Equals, "base/foo/bar/data.txt") c.Assert(stor.invokeCount, gc.Equals, 1) }
func (s *datasourceSuite) TestFetchWithBasePath(c *gc.C) { sampleData := "hello world" s.stor.Put("base/foo/bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "base") rc, url, err := ds.Fetch("foo/bar/data.txt") c.Assert(err, gc.IsNil) defer rc.Close() c.Assert(url, gc.Equals, s.baseURL+"base/foo/bar/data.txt") data, err := ioutil.ReadAll(rc) c.Assert(data, gc.DeepEquals, []byte(sampleData)) }
// readMetadata reads the image metadata from metadataStore. func readMetadata(metadataStore storage.Storage) ([]*ImageMetadata, error) { // Read any existing metadata so we can merge the new tools metadata with what's there. dataSource := storage.NewStorageSimpleStreamsDataSource("existing metadata", metadataStore, storage.BaseImagesPath) imageConstraint := NewImageConstraint(simplestreams.LookupParams{}) existingMetadata, _, err := Fetch( []simplestreams.DataSource{dataSource}, simplestreams.DefaultIndexPath, imageConstraint, false) if err != nil && !errors.IsNotFound(err) { return nil, err } return existingMetadata, nil }
// ReadMetadata returns the tools metadata from the given storage. func ReadMetadata(store storage.StorageReader) ([]*ToolsMetadata, error) { dataSource := storage.NewStorageSimpleStreamsDataSource("existing metadata", store, storage.BaseToolsPath) toolsConstraint, err := makeToolsConstraint(simplestreams.CloudSpec{}, -1, -1, coretools.Filter{}) if err != nil { return nil, err } metadata, _, err := Fetch( []simplestreams.DataSource{dataSource}, simplestreams.DefaultIndexPath, toolsConstraint, false) if err != nil && !errors.IsNotFound(err) { return nil, err } return metadata, nil }
// GetToolsSources returns a list of sources which are used to search for simplestreams tools metadata. func (e *environ) GetToolsSources() ([]simplestreams.DataSource, error) { return []simplestreams.DataSource{ storage.NewStorageSimpleStreamsDataSource("cloud storage", e.Storage(), storage.BaseToolsPath)}, nil }
// GetToolsSources returns a list of sources which are used to search for simplestreams tools metadata. func (env *joyentEnviron) GetToolsSources() ([]simplestreams.DataSource, error) { // Add the simplestreams source off the control bucket. sources := []simplestreams.DataSource{ storage.NewStorageSimpleStreamsDataSource("cloud storage", env.Storage(), storage.BaseToolsPath)} return sources, nil }
// ParseMetadataFromStorage loads ToolsMetadata from the specified storage reader. func ParseMetadataFromStorage(c *gc.C, stor storage.StorageReader, expectMirrors bool) []*tools.ToolsMetadata { source := storage.NewStorageSimpleStreamsDataSource("test storage reader", stor, "tools") params := simplestreams.ValueParams{ DataType: tools.ContentDownload, ValueTemplate: tools.ToolsMetadata{}, } const requireSigned = false indexPath := simplestreams.UnsignedIndex indexRef, err := simplestreams.GetIndexWithFormat( source, indexPath, "index:1.0", requireSigned, simplestreams.CloudSpec{}, params) c.Assert(err, gc.IsNil) c.Assert(indexRef.Indexes, gc.HasLen, 1) toolsIndexMetadata := indexRef.Indexes["com.ubuntu.juju:released:tools"] c.Assert(toolsIndexMetadata, gc.NotNil) // Read the products file contents. r, err := stor.Get(path.Join("tools", toolsIndexMetadata.ProductsFilePath)) defer r.Close() c.Assert(err, gc.IsNil) data, err := ioutil.ReadAll(r) c.Assert(err, gc.IsNil) url, err := source.URL(toolsIndexMetadata.ProductsFilePath) c.Assert(err, gc.IsNil) cloudMetadata, err := simplestreams.ParseCloudMetadata(data, "products:1.0", url, tools.ToolsMetadata{}) c.Assert(err, gc.IsNil) toolsMetadataMap := make(map[string]*tools.ToolsMetadata) var expectedProductIds set.Strings var toolsVersions set.Strings for _, mc := range cloudMetadata.Products { for _, items := range mc.Items { for key, item := range items.Items { toolsMetadata := item.(*tools.ToolsMetadata) toolsMetadataMap[key] = toolsMetadata toolsVersions.Add(key) seriesVersion, err := ubuntu.SeriesVersion(toolsMetadata.Release) c.Assert(err, gc.IsNil) productId := fmt.Sprintf("com.ubuntu.juju:%s:%s", seriesVersion, toolsMetadata.Arch) expectedProductIds.Add(productId) } } } // Make sure index's product IDs are all represented in the products metadata. sort.Strings(toolsIndexMetadata.ProductIds) c.Assert(toolsIndexMetadata.ProductIds, gc.DeepEquals, expectedProductIds.SortedValues()) toolsMetadata := make([]*tools.ToolsMetadata, len(toolsMetadataMap)) for i, key := range toolsVersions.SortedValues() { toolsMetadata[i] = toolsMetadataMap[key] } if expectMirrors { r, err = stor.Get(path.Join("tools", simplestreams.UnsignedMirror)) defer r.Close() c.Assert(err, gc.IsNil) data, err = ioutil.ReadAll(r) c.Assert(err, gc.IsNil) c.Assert(string(data), jc.Contains, `"mirrors":`) c.Assert(err, gc.IsNil) } return toolsMetadata }
func (env *mockEnviron) GetImageSources() ([]simplestreams.DataSource, error) { datasource := storage.NewStorageSimpleStreamsDataSource("test cloud storage", env.Storage(), storage.BaseImagesPath) return []simplestreams.DataSource{datasource}, nil }
// GetToolsSources returns a list of sources which are used to search for simplestreams tools metadata. func (e *bootstrapEnviron) GetToolsSources() ([]simplestreams.DataSource, error) { // Add the simplestreams source off the control bucket. return []simplestreams.DataSource{ storage.NewStorageSimpleStreamsDataSource("cloud storage", e.Storage(), storage.BaseToolsPath)}, nil }