github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/storage_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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  type StorageSerializationSuite struct {
    15  	SliceSerializationSuite
    16  }
    17  
    18  var _ = gc.Suite(&StorageSerializationSuite{})
    19  
    20  func (s *StorageSerializationSuite) SetUpTest(c *gc.C) {
    21  	s.SliceSerializationSuite.SetUpTest(c)
    22  	s.importName = "storages"
    23  	s.sliceName = "storages"
    24  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    25  		return importStorages(m)
    26  	}
    27  	s.testFields = func(m map[string]interface{}) {
    28  		m["storages"] = []interface{}{}
    29  	}
    30  }
    31  
    32  func testStorageMap() map[interface{}]interface{} {
    33  	return map[interface{}]interface{}{
    34  		"id":    "db/0",
    35  		"kind":  "magic",
    36  		"owner": "application-postgresql",
    37  		"name":  "db",
    38  		"attachments": []interface{}{
    39  			"postgresql/0",
    40  			"postgresql/1",
    41  		},
    42  	}
    43  }
    44  
    45  func testStorage() *storage {
    46  	v := newStorage(testStorageArgs())
    47  	return v
    48  }
    49  
    50  func testStorageArgs() StorageArgs {
    51  	return StorageArgs{
    52  		Tag:   names.NewStorageTag("db/0"),
    53  		Kind:  "magic",
    54  		Owner: names.NewApplicationTag("postgresql"),
    55  		Name:  "db",
    56  		Attachments: []names.UnitTag{
    57  			names.NewUnitTag("postgresql/0"),
    58  			names.NewUnitTag("postgresql/1"),
    59  		},
    60  	}
    61  }
    62  
    63  func (s *StorageSerializationSuite) TestNewStorage(c *gc.C) {
    64  	storage := testStorage()
    65  
    66  	c.Check(storage.Tag(), gc.Equals, names.NewStorageTag("db/0"))
    67  	c.Check(storage.Kind(), gc.Equals, "magic")
    68  	owner, err := storage.Owner()
    69  	c.Check(err, jc.ErrorIsNil)
    70  	c.Check(owner, gc.Equals, names.NewApplicationTag("postgresql"))
    71  	c.Check(storage.Name(), gc.Equals, "db")
    72  	c.Check(storage.Attachments(), jc.DeepEquals, []names.UnitTag{
    73  		names.NewUnitTag("postgresql/0"),
    74  		names.NewUnitTag("postgresql/1"),
    75  	})
    76  }
    77  
    78  func (s *StorageSerializationSuite) TestStorageValid(c *gc.C) {
    79  	storage := testStorage()
    80  	c.Assert(storage.Validate(), jc.ErrorIsNil)
    81  }
    82  
    83  func (s *StorageSerializationSuite) TestStorageValidMissingID(c *gc.C) {
    84  	v := newStorage(StorageArgs{})
    85  	err := v.Validate()
    86  	c.Check(err, gc.ErrorMatches, `storage missing id not valid`)
    87  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    88  }
    89  
    90  func (s *StorageSerializationSuite) TestStorageMatches(c *gc.C) {
    91  	bytes, err := yaml.Marshal(testStorage())
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	var source map[interface{}]interface{}
    95  	err = yaml.Unmarshal(bytes, &source)
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	c.Assert(source, jc.DeepEquals, testStorageMap())
    98  }
    99  
   100  func (s *StorageSerializationSuite) exportImport(c *gc.C, storage_ *storage) *storage {
   101  	initial := storages{
   102  		Version:   1,
   103  		Storages_: []*storage{storage_},
   104  	}
   105  
   106  	bytes, err := yaml.Marshal(initial)
   107  	c.Assert(err, jc.ErrorIsNil)
   108  
   109  	var source map[string]interface{}
   110  	err = yaml.Unmarshal(bytes, &source)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	storages, err := importStorages(source)
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	c.Assert(storages, gc.HasLen, 1)
   116  	return storages[0]
   117  }
   118  
   119  func (s *StorageSerializationSuite) TestParsingSerializedData(c *gc.C) {
   120  	original := testStorage()
   121  	storage := s.exportImport(c, original)
   122  	c.Assert(storage, jc.DeepEquals, original)
   123  }