github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/cloudimagemetadata_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type CloudImageMetadataSerializationSuite struct {
    13  	SliceSerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&CloudImageMetadataSerializationSuite{})
    17  
    18  func (s *CloudImageMetadataSerializationSuite) SetUpTest(c *gc.C) {
    19  	s.SliceSerializationSuite.SetUpTest(c)
    20  	s.importName = "cloudimagemetadata"
    21  	s.sliceName = "cloudimagemetadata"
    22  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    23  		return importCloudImageMetadata(m)
    24  	}
    25  	s.testFields = func(m map[string]interface{}) {
    26  		m["cloudimagemetadata"] = []interface{}{}
    27  	}
    28  }
    29  
    30  func (s *CloudImageMetadataSerializationSuite) TestNewCloudImageMetadata(c *gc.C) {
    31  	storageSize := uint64(3)
    32  	args := CloudImageMetadataArgs{
    33  		Stream:          "stream",
    34  		Region:          "region-test",
    35  		Version:         "14.04",
    36  		Series:          "trusty",
    37  		Arch:            "arch",
    38  		VirtType:        "virtType-test",
    39  		RootStorageType: "rootStorageType-test",
    40  		RootStorageSize: &storageSize,
    41  		Source:          "test",
    42  		Priority:        0,
    43  		ImageId:         "foo",
    44  		DateCreated:     0,
    45  	}
    46  	metadata := newCloudImageMetadata(args)
    47  	c.Check(metadata.Stream(), gc.Equals, args.Stream)
    48  	c.Check(metadata.Region(), gc.Equals, args.Region)
    49  	c.Check(metadata.Version(), gc.Equals, args.Version)
    50  	c.Check(metadata.Series(), gc.Equals, args.Series)
    51  	c.Check(metadata.Arch(), gc.Equals, args.Arch)
    52  	c.Check(metadata.VirtType(), gc.Equals, args.VirtType)
    53  	c.Check(metadata.RootStorageType(), gc.Equals, args.RootStorageType)
    54  	value, ok := metadata.RootStorageSize()
    55  	c.Check(ok, jc.IsTrue)
    56  	c.Check(value, gc.Equals, *args.RootStorageSize)
    57  	c.Check(metadata.Source(), gc.Equals, args.Source)
    58  	c.Check(metadata.Priority(), gc.Equals, args.Priority)
    59  	c.Check(metadata.ImageId(), gc.Equals, args.ImageId)
    60  	c.Check(metadata.DateCreated(), gc.Equals, args.DateCreated)
    61  }
    62  
    63  func (s *CloudImageMetadataSerializationSuite) TestParsingSerializedData(c *gc.C) {
    64  	storageSize := uint64(3)
    65  	initial := cloudimagemetadataset{
    66  		Version: 1,
    67  		CloudImageMetadata_: []*cloudimagemetadata{
    68  			newCloudImageMetadata(CloudImageMetadataArgs{
    69  				Stream:          "stream",
    70  				Region:          "region-test",
    71  				Version:         "14.04",
    72  				Series:          "trusty",
    73  				Arch:            "arch",
    74  				VirtType:        "virtType-test",
    75  				RootStorageType: "rootStorageType-test",
    76  				RootStorageSize: &storageSize,
    77  				Source:          "test",
    78  				Priority:        0,
    79  				ImageId:         "foo",
    80  				DateCreated:     0,
    81  			}),
    82  			newCloudImageMetadata(CloudImageMetadataArgs{
    83  				Stream:  "stream",
    84  				Region:  "region-test",
    85  				Version: "14.04",
    86  			}),
    87  		},
    88  	}
    89  
    90  	bytes, err := yaml.Marshal(initial)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	var source map[string]interface{}
    94  	err = yaml.Unmarshal(bytes, &source)
    95  	c.Assert(err, jc.ErrorIsNil)
    96  
    97  	metadata, err := importCloudImageMetadata(source)
    98  	c.Assert(err, jc.ErrorIsNil)
    99  
   100  	c.Assert(metadata, jc.DeepEquals, initial.CloudImageMetadata_)
   101  }