github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/apiserver/storage/filesystemlist_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  type filesystemSuite struct {
    16  	baseStorageSuite
    17  }
    18  
    19  var _ = gc.Suite(&filesystemSuite{})
    20  
    21  func (s *filesystemSuite) expectedFilesystemDetailsResult() params.FilesystemDetailsResult {
    22  	return params.FilesystemDetailsResult{
    23  		Result: &params.FilesystemDetails{
    24  			FilesystemTag: s.filesystemTag.String(),
    25  			Status: params.EntityStatus{
    26  				Status: "attached",
    27  			},
    28  			MachineAttachments: map[string]params.FilesystemAttachmentInfo{
    29  				s.machineTag.String(): params.FilesystemAttachmentInfo{},
    30  			},
    31  			Storage: &params.StorageDetails{
    32  				StorageTag: "storage-data-0",
    33  				OwnerTag:   "unit-mysql-0",
    34  				Kind:       params.StorageKindFilesystem,
    35  				Status: params.EntityStatus{
    36  					Status: "attached",
    37  				},
    38  				Attachments: map[string]params.StorageAttachmentDetails{
    39  					"unit-mysql-0": params.StorageAttachmentDetails{
    40  						StorageTag: "storage-data-0",
    41  						UnitTag:    "unit-mysql-0",
    42  						MachineTag: "machine-66",
    43  					},
    44  				},
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func (s *filesystemSuite) TestListFilesystemsEmptyFilter(c *gc.C) {
    51  	found, err := s.api.ListFilesystems(params.FilesystemFilter{})
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	c.Assert(found.Results, gc.HasLen, 1)
    54  	c.Assert(found.Results[0], gc.DeepEquals, s.expectedFilesystemDetailsResult())
    55  }
    56  
    57  func (s *filesystemSuite) TestListFilesystemsError(c *gc.C) {
    58  	msg := "inventing error"
    59  	s.state.allFilesystems = func() ([]state.Filesystem, error) {
    60  		return nil, errors.New(msg)
    61  	}
    62  	_, err := s.api.ListFilesystems(params.FilesystemFilter{})
    63  	c.Assert(err, gc.ErrorMatches, msg)
    64  }
    65  
    66  func (s *filesystemSuite) TestListFilesystemsNoFilesystems(c *gc.C) {
    67  	s.state.allFilesystems = func() ([]state.Filesystem, error) {
    68  		return nil, nil
    69  	}
    70  	results, err := s.api.ListFilesystems(params.FilesystemFilter{})
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(results.Results, gc.HasLen, 0)
    73  }
    74  
    75  func (s *filesystemSuite) TestListFilesystemsFilter(c *gc.C) {
    76  	filter := params.FilesystemFilter{
    77  		Machines: []string{s.machineTag.String()},
    78  	}
    79  	found, err := s.api.ListFilesystems(filter)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(found.Results, gc.HasLen, 1)
    82  	c.Assert(found.Results[0], jc.DeepEquals, s.expectedFilesystemDetailsResult())
    83  }
    84  
    85  func (s *filesystemSuite) TestListFilesystemsFilterNonMatching(c *gc.C) {
    86  	filter := params.FilesystemFilter{
    87  		Machines: []string{"machine-42"},
    88  	}
    89  	found, err := s.api.ListFilesystems(filter)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	c.Assert(found.Results, gc.HasLen, 0)
    92  }
    93  
    94  func (s *filesystemSuite) TestListFilesystemsFilesystemInfo(c *gc.C) {
    95  	s.filesystem.info = &state.FilesystemInfo{
    96  		Size: 123,
    97  	}
    98  	expected := s.expectedFilesystemDetailsResult()
    99  	expected.Result.Info.Size = 123
   100  	found, err := s.api.ListFilesystems(params.FilesystemFilter{})
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(found.Results, gc.HasLen, 1)
   103  	c.Assert(found.Results[0], jc.DeepEquals, expected)
   104  }
   105  
   106  func (s *filesystemSuite) TestListFilesystemsAttachmentInfo(c *gc.C) {
   107  	s.filesystemAttachment.info = &state.FilesystemAttachmentInfo{
   108  		MountPoint: "/tmp",
   109  		ReadOnly:   true,
   110  	}
   111  	expected := s.expectedFilesystemDetailsResult()
   112  	expected.Result.MachineAttachments[s.machineTag.String()] = params.FilesystemAttachmentInfo{
   113  		MountPoint: "/tmp",
   114  		ReadOnly:   true,
   115  	}
   116  	expectedStorageAttachmentDetails := expected.Result.Storage.Attachments["unit-mysql-0"]
   117  	expectedStorageAttachmentDetails.Location = "/tmp"
   118  	expected.Result.Storage.Attachments["unit-mysql-0"] = expectedStorageAttachmentDetails
   119  	found, err := s.api.ListFilesystems(params.FilesystemFilter{})
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	c.Assert(found.Results, gc.HasLen, 1)
   122  	c.Assert(found.Results[0], jc.DeepEquals, expected)
   123  }
   124  
   125  func (s *filesystemSuite) TestListFilesystemsVolumeBacked(c *gc.C) {
   126  	s.filesystem.volume = &s.volumeTag
   127  	expected := s.expectedFilesystemDetailsResult()
   128  	expected.Result.VolumeTag = s.volumeTag.String()
   129  	found, err := s.api.ListFilesystems(params.FilesystemFilter{})
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	c.Assert(found.Results, gc.HasLen, 1)
   132  	c.Assert(found.Results[0], jc.DeepEquals, expected)
   133  }