github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/imagemetadata/testing/testing.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 "io/ioutil" 9 "path" 10 "sort" 11 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils/set" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/environs/filestorage" 18 "github.com/juju/juju/environs/imagemetadata" 19 "github.com/juju/juju/environs/simplestreams" 20 "github.com/juju/juju/environs/storage" 21 ) 22 23 // PatchOfficialDataSources is used by tests. 24 // We replace one of the urls with the supplied value 25 // and prevent the other from being used. 26 func PatchOfficialDataSources(s *testing.CleanupSuite, url string) { 27 s.PatchValue(&imagemetadata.DefaultUbuntuBaseURL, "") 28 s.PatchValue(&imagemetadata.DefaultJujuBaseURL, url) 29 } 30 31 // ParseMetadataFromDir loads ImageMetadata from the specified directory. 32 func ParseMetadataFromDir(c *gc.C, metadataDir string) []*imagemetadata.ImageMetadata { 33 stor, err := filestorage.NewFileStorageReader(metadataDir) 34 c.Assert(err, jc.ErrorIsNil) 35 return ParseMetadataFromStorage(c, stor) 36 } 37 38 // ParseIndexMetadataFromStorage loads Indices from the specified storage reader. 39 func ParseIndexMetadataFromStorage(c *gc.C, stor storage.StorageReader) (*simplestreams.IndexMetadata, simplestreams.DataSource) { 40 source := storage.NewStorageSimpleStreamsDataSource("test storage reader", stor, "images", simplestreams.DEFAULT_CLOUD_DATA, false) 41 42 // Find the simplestreams index file. 43 params := simplestreams.ValueParams{ 44 DataType: "image-ids", 45 ValueTemplate: imagemetadata.ImageMetadata{}, 46 } 47 const requireSigned = false 48 indexPath := simplestreams.UnsignedIndex("v1", 1) 49 mirrorsPath := simplestreams.MirrorsPath("v1") 50 indexRef, err := simplestreams.GetIndexWithFormat( 51 source, indexPath, "index:1.0", mirrorsPath, requireSigned, simplestreams.CloudSpec{}, params) 52 c.Assert(err, jc.ErrorIsNil) 53 c.Assert(indexRef.Indexes, gc.HasLen, 1) 54 55 imageIndexMetadata := indexRef.Indexes["com.ubuntu.cloud:custom"] 56 c.Assert(imageIndexMetadata, gc.NotNil) 57 return imageIndexMetadata, source 58 } 59 60 // ParseMetadataFromStorage loads ImageMetadata from the specified storage reader. 61 func ParseMetadataFromStorage(c *gc.C, stor storage.StorageReader) []*imagemetadata.ImageMetadata { 62 imageIndexMetadata, source := ParseIndexMetadataFromStorage(c, stor) 63 c.Assert(imageIndexMetadata, gc.NotNil) 64 65 // Read the products file contents. 66 r, err := stor.Get(path.Join("images", imageIndexMetadata.ProductsFilePath)) 67 defer r.Close() 68 c.Assert(err, jc.ErrorIsNil) 69 data, err := ioutil.ReadAll(r) 70 c.Assert(err, jc.ErrorIsNil) 71 72 // Parse the products file metadata. 73 url, err := source.URL(imageIndexMetadata.ProductsFilePath) 74 c.Assert(err, jc.ErrorIsNil) 75 cloudMetadata, err := simplestreams.ParseCloudMetadata(data, "products:1.0", url, imagemetadata.ImageMetadata{}) 76 c.Assert(err, jc.ErrorIsNil) 77 78 // Collate the metadata. 79 imageMetadataMap := make(map[string]*imagemetadata.ImageMetadata) 80 expectedProductIds, imageVersions := make(set.Strings), make(set.Strings) 81 for _, mc := range cloudMetadata.Products { 82 for _, items := range mc.Items { 83 for key, item := range items.Items { 84 imageMetadata := item.(*imagemetadata.ImageMetadata) 85 imageMetadataMap[key] = imageMetadata 86 imageVersions.Add(key) 87 productId := fmt.Sprintf("com.ubuntu.cloud:server:%s:%s", mc.Version, imageMetadata.Arch) 88 expectedProductIds.Add(productId) 89 } 90 } 91 } 92 93 // Make sure index's product IDs are all represented in the products metadata. 94 sort.Strings(imageIndexMetadata.ProductIds) 95 c.Assert(imageIndexMetadata.ProductIds, gc.DeepEquals, expectedProductIds.SortedValues()) 96 97 imageMetadata := make([]*imagemetadata.ImageMetadata, len(imageMetadataMap)) 98 for i, key := range imageVersions.SortedValues() { 99 imageMetadata[i] = imageMetadataMap[key] 100 } 101 return imageMetadata 102 }