github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/filesystem_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 FilesystemSerializationSuite struct {
    15  	SliceSerializationSuite
    16  	StatusHistoryMixinSuite
    17  }
    18  
    19  var _ = gc.Suite(&FilesystemSerializationSuite{})
    20  
    21  func (s *FilesystemSerializationSuite) SetUpTest(c *gc.C) {
    22  	s.SliceSerializationSuite.SetUpTest(c)
    23  	s.importName = "filesystems"
    24  	s.sliceName = "filesystems"
    25  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    26  		return importFilesystems(m)
    27  	}
    28  	s.testFields = func(m map[string]interface{}) {
    29  		m["filesystems"] = []interface{}{}
    30  	}
    31  	s.StatusHistoryMixinSuite.creator = func() HasStatusHistory {
    32  		return testFilesystem()
    33  	}
    34  	s.StatusHistoryMixinSuite.serializer = func(c *gc.C, initial interface{}) HasStatusHistory {
    35  		return s.exportImport(c, initial.(*filesystem))
    36  	}
    37  }
    38  
    39  func testFilesystemMap() map[interface{}]interface{} {
    40  	return map[interface{}]interface{}{
    41  		"id":             "1234",
    42  		"storage-id":     "test/1",
    43  		"volume-id":      "4321",
    44  		"binding":        "machine-42",
    45  		"provisioned":    true,
    46  		"size":           int(20 * gig),
    47  		"pool":           "swimming",
    48  		"filesystem-id":  "some filesystem id",
    49  		"status":         minimalStatusMap(),
    50  		"status-history": emptyStatusHistoryMap(),
    51  		"attachments": map[interface{}]interface{}{
    52  			"version":     1,
    53  			"attachments": []interface{}{},
    54  		},
    55  	}
    56  }
    57  
    58  func testFilesystem() *filesystem {
    59  	v := newFilesystem(testFilesystemArgs())
    60  	v.SetStatus(minimalStatusArgs())
    61  	return v
    62  }
    63  
    64  func testFilesystemArgs() FilesystemArgs {
    65  	return FilesystemArgs{
    66  		Tag:          names.NewFilesystemTag("1234"),
    67  		Storage:      names.NewStorageTag("test/1"),
    68  		Volume:       names.NewVolumeTag("4321"),
    69  		Binding:      names.NewMachineTag("42"),
    70  		Provisioned:  true,
    71  		Size:         20 * gig,
    72  		Pool:         "swimming",
    73  		FilesystemID: "some filesystem id",
    74  	}
    75  }
    76  
    77  func (s *FilesystemSerializationSuite) TestNewFilesystem(c *gc.C) {
    78  	filesystem := testFilesystem()
    79  
    80  	c.Check(filesystem.Tag(), gc.Equals, names.NewFilesystemTag("1234"))
    81  	c.Check(filesystem.Storage(), gc.Equals, names.NewStorageTag("test/1"))
    82  	c.Check(filesystem.Volume(), gc.Equals, names.NewVolumeTag("4321"))
    83  	binding, err := filesystem.Binding()
    84  	c.Check(err, jc.ErrorIsNil)
    85  	c.Check(binding, gc.Equals, names.NewMachineTag("42"))
    86  	c.Check(filesystem.Provisioned(), jc.IsTrue)
    87  	c.Check(filesystem.Size(), gc.Equals, 20*gig)
    88  	c.Check(filesystem.Pool(), gc.Equals, "swimming")
    89  	c.Check(filesystem.FilesystemID(), gc.Equals, "some filesystem id")
    90  
    91  	c.Check(filesystem.Attachments(), gc.HasLen, 0)
    92  }
    93  
    94  func (s *FilesystemSerializationSuite) TestFilesystemValid(c *gc.C) {
    95  	filesystem := testFilesystem()
    96  	c.Assert(filesystem.Validate(), jc.ErrorIsNil)
    97  }
    98  
    99  func (s *FilesystemSerializationSuite) TestFilesystemValidMissingID(c *gc.C) {
   100  	v := newFilesystem(FilesystemArgs{})
   101  	err := v.Validate()
   102  	c.Check(err, gc.ErrorMatches, `filesystem missing id not valid`)
   103  	c.Check(err, jc.Satisfies, errors.IsNotValid)
   104  }
   105  
   106  func (s *FilesystemSerializationSuite) TestFilesystemValidMissingSize(c *gc.C) {
   107  	v := newFilesystem(FilesystemArgs{
   108  		Tag: names.NewFilesystemTag("123"),
   109  	})
   110  	err := v.Validate()
   111  	c.Check(err, gc.ErrorMatches, `filesystem "123" missing size not valid`)
   112  	c.Check(err, jc.Satisfies, errors.IsNotValid)
   113  }
   114  
   115  func (s *FilesystemSerializationSuite) TestFilesystemValidMissingStatus(c *gc.C) {
   116  	v := newFilesystem(FilesystemArgs{
   117  		Tag:  names.NewFilesystemTag("123"),
   118  		Size: 5,
   119  	})
   120  	err := v.Validate()
   121  	c.Check(err, gc.ErrorMatches, `filesystem "123" missing status not valid`)
   122  	c.Check(err, jc.Satisfies, errors.IsNotValid)
   123  }
   124  
   125  func (s *FilesystemSerializationSuite) TestFilesystemValidMinimal(c *gc.C) {
   126  	v := newFilesystem(FilesystemArgs{
   127  		Tag:  names.NewFilesystemTag("123"),
   128  		Size: 5,
   129  	})
   130  	v.SetStatus(minimalStatusArgs())
   131  	err := v.Validate()
   132  	c.Check(err, jc.ErrorIsNil)
   133  }
   134  
   135  func (s *FilesystemSerializationSuite) TestFilesystemMatches(c *gc.C) {
   136  	bytes, err := yaml.Marshal(testFilesystem())
   137  	c.Assert(err, jc.ErrorIsNil)
   138  
   139  	var source map[interface{}]interface{}
   140  	err = yaml.Unmarshal(bytes, &source)
   141  	c.Assert(err, jc.ErrorIsNil)
   142  	c.Assert(source, jc.DeepEquals, testFilesystemMap())
   143  }
   144  
   145  func (s *FilesystemSerializationSuite) exportImport(c *gc.C, filesystem_ *filesystem) *filesystem {
   146  	initial := filesystems{
   147  		Version:      1,
   148  		Filesystems_: []*filesystem{filesystem_},
   149  	}
   150  
   151  	bytes, err := yaml.Marshal(initial)
   152  	c.Assert(err, jc.ErrorIsNil)
   153  
   154  	var source map[string]interface{}
   155  	err = yaml.Unmarshal(bytes, &source)
   156  	c.Assert(err, jc.ErrorIsNil)
   157  
   158  	filesystems, err := importFilesystems(source)
   159  	c.Assert(err, jc.ErrorIsNil)
   160  	c.Assert(filesystems, gc.HasLen, 1)
   161  	return filesystems[0]
   162  }
   163  
   164  func (s *FilesystemSerializationSuite) TestAddingAttachments(c *gc.C) {
   165  	// The core code does not care about duplicates, so we'll just add
   166  	// the same attachment twice.
   167  	original := testFilesystem()
   168  	attachment1 := original.AddAttachment(testFilesystemAttachmentArgs("1"))
   169  	attachment2 := original.AddAttachment(testFilesystemAttachmentArgs("2"))
   170  	filesystem := s.exportImport(c, original)
   171  	c.Assert(filesystem, jc.DeepEquals, original)
   172  	attachments := filesystem.Attachments()
   173  	c.Assert(attachments, gc.HasLen, 2)
   174  	c.Check(attachments[0], jc.DeepEquals, attachment1)
   175  	c.Check(attachments[1], jc.DeepEquals, attachment2)
   176  }
   177  
   178  func (s *FilesystemSerializationSuite) TestParsingSerializedData(c *gc.C) {
   179  	original := testFilesystem()
   180  	original.AddAttachment(testFilesystemAttachmentArgs())
   181  	filesystem := s.exportImport(c, original)
   182  	c.Assert(filesystem, jc.DeepEquals, original)
   183  }
   184  
   185  type FilesystemAttachmentSerializationSuite struct {
   186  	SliceSerializationSuite
   187  }
   188  
   189  var _ = gc.Suite(&FilesystemAttachmentSerializationSuite{})
   190  
   191  func (s *FilesystemAttachmentSerializationSuite) SetUpTest(c *gc.C) {
   192  	s.SliceSerializationSuite.SetUpTest(c)
   193  	s.importName = "filesystem attachments"
   194  	s.sliceName = "attachments"
   195  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
   196  		return importFilesystemAttachments(m)
   197  	}
   198  	s.testFields = func(m map[string]interface{}) {
   199  		m["attachments"] = []interface{}{}
   200  	}
   201  }
   202  
   203  func testFilesystemAttachmentMap() map[interface{}]interface{} {
   204  	return map[interface{}]interface{}{
   205  		"machine-id":  "42",
   206  		"provisioned": true,
   207  		"read-only":   true,
   208  		"mount-point": "/some/dir",
   209  	}
   210  }
   211  
   212  func testFilesystemAttachment() *filesystemAttachment {
   213  	return newFilesystemAttachment(testFilesystemAttachmentArgs())
   214  }
   215  
   216  func testFilesystemAttachmentArgs(id ...string) FilesystemAttachmentArgs {
   217  	machineID := "42"
   218  	if len(id) > 0 {
   219  		machineID = id[0]
   220  	}
   221  	return FilesystemAttachmentArgs{
   222  		Machine:     names.NewMachineTag(machineID),
   223  		Provisioned: true,
   224  		ReadOnly:    true,
   225  		MountPoint:  "/some/dir",
   226  	}
   227  }
   228  
   229  func (s *FilesystemAttachmentSerializationSuite) TestNewFilesystemAttachment(c *gc.C) {
   230  	attachment := testFilesystemAttachment()
   231  
   232  	c.Check(attachment.Machine(), gc.Equals, names.NewMachineTag("42"))
   233  	c.Check(attachment.Provisioned(), jc.IsTrue)
   234  	c.Check(attachment.ReadOnly(), jc.IsTrue)
   235  	c.Check(attachment.MountPoint(), gc.Equals, "/some/dir")
   236  }
   237  
   238  func (s *FilesystemAttachmentSerializationSuite) TestFilesystemAttachmentMatches(c *gc.C) {
   239  	bytes, err := yaml.Marshal(testFilesystemAttachment())
   240  	c.Assert(err, jc.ErrorIsNil)
   241  
   242  	var source map[interface{}]interface{}
   243  	err = yaml.Unmarshal(bytes, &source)
   244  	c.Assert(err, jc.ErrorIsNil)
   245  	c.Assert(source, jc.DeepEquals, testFilesystemAttachmentMap())
   246  }
   247  
   248  func (s *FilesystemAttachmentSerializationSuite) exportImport(c *gc.C, attachment *filesystemAttachment) *filesystemAttachment {
   249  	initial := filesystemAttachments{
   250  		Version:      1,
   251  		Attachments_: []*filesystemAttachment{attachment},
   252  	}
   253  
   254  	bytes, err := yaml.Marshal(initial)
   255  	c.Assert(err, jc.ErrorIsNil)
   256  
   257  	var source map[string]interface{}
   258  	err = yaml.Unmarshal(bytes, &source)
   259  	c.Assert(err, jc.ErrorIsNil)
   260  
   261  	attachments, err := importFilesystemAttachments(source)
   262  	c.Assert(err, jc.ErrorIsNil)
   263  	c.Assert(attachments, gc.HasLen, 1)
   264  	return attachments[0]
   265  }
   266  
   267  func (s *FilesystemAttachmentSerializationSuite) TestParsingSerializedData(c *gc.C) {
   268  	original := testFilesystemAttachment()
   269  	attachment := s.exportImport(c, original)
   270  	c.Assert(attachment, jc.DeepEquals, original)
   271  }