github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/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) expectedFilesystemDetails() params.FilesystemDetails {
    22  	return params.FilesystemDetails{
    23  		FilesystemTag: s.filesystemTag.String(),
    24  		Life:          "alive",
    25  		Status: params.EntityStatus{
    26  			Status: "attached",
    27  		},
    28  		MachineAttachments: map[string]params.FilesystemAttachmentDetails{
    29  			s.machineTag.String(): {
    30  				Life: "dead",
    31  			},
    32  		},
    33  		UnitAttachments: map[string]params.FilesystemAttachmentDetails{},
    34  		Storage: &params.StorageDetails{
    35  			StorageTag: "storage-data-0",
    36  			OwnerTag:   "unit-mysql-0",
    37  			Kind:       params.StorageKindFilesystem,
    38  			Life:       "dying",
    39  			Status: params.EntityStatus{
    40  				Status: "attached",
    41  			},
    42  			Attachments: map[string]params.StorageAttachmentDetails{
    43  				"unit-mysql-0": {
    44  					StorageTag: "storage-data-0",
    45  					UnitTag:    "unit-mysql-0",
    46  					MachineTag: "machine-66",
    47  					Life:       "alive",
    48  				},
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func (s *filesystemSuite) TestListFilesystemsEmptyFilter(c *gc.C) {
    55  	found, err := s.api.ListFilesystems(params.FilesystemFilters{
    56  		[]params.FilesystemFilter{{}},
    57  	})
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(found.Results, gc.HasLen, 1)
    60  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
    61  	c.Assert(found.Results[0].Result[0], gc.DeepEquals, s.expectedFilesystemDetails())
    62  }
    63  
    64  func (s *filesystemSuite) TestListFilesystemsError(c *gc.C) {
    65  	msg := "inventing error"
    66  	s.storageAccessor.allFilesystems = func() ([]state.Filesystem, error) {
    67  		return nil, errors.New(msg)
    68  	}
    69  	results, err := s.api.ListFilesystems(params.FilesystemFilters{
    70  		[]params.FilesystemFilter{{}},
    71  	})
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	c.Assert(results.Results, gc.HasLen, 1)
    74  	c.Assert(results.Results[0].Error, gc.ErrorMatches, msg)
    75  }
    76  
    77  func (s *filesystemSuite) TestListFilesystemsNoFilesystems(c *gc.C) {
    78  	s.storageAccessor.allFilesystems = func() ([]state.Filesystem, error) {
    79  		return nil, nil
    80  	}
    81  	results, err := s.api.ListFilesystems(params.FilesystemFilters{})
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	c.Assert(results.Results, gc.HasLen, 0)
    84  }
    85  
    86  func (s *filesystemSuite) TestListFilesystemsFilter(c *gc.C) {
    87  	filters := []params.FilesystemFilter{{
    88  		Machines: []string{s.machineTag.String()},
    89  	}}
    90  	found, err := s.api.ListFilesystems(params.FilesystemFilters{filters})
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(found.Results, gc.HasLen, 1)
    93  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
    94  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, s.expectedFilesystemDetails())
    95  }
    96  
    97  func (s *filesystemSuite) TestListFilesystemsFilterNonMatching(c *gc.C) {
    98  	filters := []params.FilesystemFilter{{
    99  		Machines: []string{"machine-42"},
   100  	}}
   101  	found, err := s.api.ListFilesystems(params.FilesystemFilters{filters})
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	c.Assert(found.Results, gc.HasLen, 1)
   104  	c.Assert(found.Results[0].Error, gc.IsNil)
   105  	c.Assert(found.Results[0].Result, gc.HasLen, 0)
   106  }
   107  
   108  func (s *filesystemSuite) TestListFilesystemsFilesystemInfo(c *gc.C) {
   109  	s.filesystem.info = &state.FilesystemInfo{
   110  		Size: 123,
   111  	}
   112  	expected := s.expectedFilesystemDetails()
   113  	expected.Info.Size = 123
   114  	found, err := s.api.ListFilesystems(params.FilesystemFilters{
   115  		[]params.FilesystemFilter{{}},
   116  	})
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	c.Assert(found.Results, gc.HasLen, 1)
   119  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
   120  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, expected)
   121  }
   122  
   123  func (s *filesystemSuite) TestListFilesystemsAttachmentInfo(c *gc.C) {
   124  	s.filesystemAttachment.info = &state.FilesystemAttachmentInfo{
   125  		MountPoint: "/tmp",
   126  		ReadOnly:   true,
   127  	}
   128  	s.state.assignedMachine = s.machineTag.Id()
   129  	expected := s.expectedFilesystemDetails()
   130  	expected.MachineAttachments[s.machineTag.String()] = params.FilesystemAttachmentDetails{
   131  		FilesystemAttachmentInfo: params.FilesystemAttachmentInfo{
   132  			MountPoint: "/tmp",
   133  			ReadOnly:   true,
   134  		},
   135  		Life: "dead",
   136  	}
   137  	expectedStorageAttachmentDetails := expected.Storage.Attachments["unit-mysql-0"]
   138  	expectedStorageAttachmentDetails.Location = "/tmp"
   139  	expected.Storage.Attachments["unit-mysql-0"] = expectedStorageAttachmentDetails
   140  	found, err := s.api.ListFilesystems(params.FilesystemFilters{
   141  		[]params.FilesystemFilter{{}},
   142  	})
   143  	c.Assert(err, jc.ErrorIsNil)
   144  	c.Assert(found.Results, gc.HasLen, 1)
   145  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
   146  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, expected)
   147  }
   148  
   149  func (s *filesystemSuite) TestListFilesystemsVolumeBacked(c *gc.C) {
   150  	s.filesystem.volume = &s.volumeTag
   151  	expected := s.expectedFilesystemDetails()
   152  	expected.VolumeTag = s.volumeTag.String()
   153  	found, err := s.api.ListFilesystems(params.FilesystemFilters{
   154  		[]params.FilesystemFilter{{}},
   155  	})
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Assert(found.Results, gc.HasLen, 1)
   158  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
   159  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, expected)
   160  }