github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/storage/storage_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  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/juju/status"
    17  )
    18  
    19  type storageSuite struct {
    20  	baseStorageSuite
    21  }
    22  
    23  var _ = gc.Suite(&storageSuite{})
    24  
    25  func (s *storageSuite) TestStorageListEmpty(c *gc.C) {
    26  	s.state.allStorageInstances = func() ([]state.StorageInstance, error) {
    27  		s.calls = append(s.calls, allStorageInstancesCall)
    28  		return []state.StorageInstance{}, nil
    29  	}
    30  
    31  	found, err := s.api.ListStorageDetails(
    32  		params.StorageFilters{[]params.StorageFilter{{}}},
    33  	)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	c.Assert(found.Results, gc.HasLen, 1)
    36  	c.Assert(found.Results[0].Error, gc.IsNil)
    37  	c.Assert(found.Results[0].Result, gc.HasLen, 0)
    38  	s.assertCalls(c, []string{allStorageInstancesCall})
    39  }
    40  
    41  func (s *storageSuite) TestStorageListFilesystem(c *gc.C) {
    42  	found, err := s.api.ListStorageDetails(
    43  		params.StorageFilters{[]params.StorageFilter{{}}},
    44  	)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  
    47  	expectedCalls := []string{
    48  		allStorageInstancesCall,
    49  		storageInstanceFilesystemCall,
    50  		storageInstanceAttachmentsCall,
    51  		unitAssignedMachineCall,
    52  		storageInstanceCall,
    53  		storageInstanceFilesystemCall,
    54  		storageInstanceFilesystemAttachmentCall,
    55  	}
    56  	s.assertCalls(c, expectedCalls)
    57  
    58  	c.Assert(found.Results, gc.HasLen, 1)
    59  	c.Assert(found.Results[0].Error, gc.IsNil)
    60  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
    61  	wantedDetails := s.createTestStorageDetails()
    62  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, wantedDetails)
    63  }
    64  
    65  func (s *storageSuite) TestStorageListVolume(c *gc.C) {
    66  	s.storageInstance.kind = state.StorageKindBlock
    67  	found, err := s.api.ListStorageDetails(
    68  		params.StorageFilters{[]params.StorageFilter{{}}},
    69  	)
    70  	c.Assert(err, jc.ErrorIsNil)
    71  
    72  	expectedCalls := []string{
    73  		allStorageInstancesCall,
    74  		storageInstanceVolumeCall,
    75  		storageInstanceAttachmentsCall,
    76  		unitAssignedMachineCall,
    77  		storageInstanceCall,
    78  		storageInstanceVolumeCall,
    79  	}
    80  	s.assertCalls(c, expectedCalls)
    81  
    82  	c.Assert(found.Results, gc.HasLen, 1)
    83  	c.Assert(found.Results[0].Error, gc.IsNil)
    84  	c.Assert(found.Results[0].Result, gc.HasLen, 1)
    85  	wantedDetails := s.createTestStorageDetails()
    86  	wantedDetails.Kind = params.StorageKindBlock
    87  	wantedDetails.Status.Status = status.Attached
    88  	c.Assert(found.Results[0].Result[0], jc.DeepEquals, wantedDetails)
    89  }
    90  
    91  func (s *storageSuite) TestStorageListError(c *gc.C) {
    92  	msg := "list test error"
    93  	s.state.allStorageInstances = func() ([]state.StorageInstance, error) {
    94  		s.calls = append(s.calls, allStorageInstancesCall)
    95  		return []state.StorageInstance{}, errors.Errorf(msg)
    96  	}
    97  
    98  	found, err := s.api.ListStorageDetails(
    99  		params.StorageFilters{[]params.StorageFilter{{}}},
   100  	)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(found.Results, gc.HasLen, 1)
   103  	c.Assert(found.Results[0].Error, gc.ErrorMatches, msg)
   104  
   105  	expectedCalls := []string{allStorageInstancesCall}
   106  	s.assertCalls(c, expectedCalls)
   107  }
   108  
   109  func (s *storageSuite) TestStorageListInstanceError(c *gc.C) {
   110  	msg := "list test error"
   111  	s.state.storageInstance = func(sTag names.StorageTag) (state.StorageInstance, error) {
   112  		s.calls = append(s.calls, storageInstanceCall)
   113  		c.Assert(sTag, jc.DeepEquals, s.storageTag)
   114  		return nil, errors.Errorf(msg)
   115  	}
   116  
   117  	found, err := s.api.ListStorageDetails(
   118  		params.StorageFilters{[]params.StorageFilter{{}}},
   119  	)
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	expectedCalls := []string{
   123  		allStorageInstancesCall,
   124  		storageInstanceFilesystemCall,
   125  		storageInstanceAttachmentsCall,
   126  		unitAssignedMachineCall,
   127  		storageInstanceCall,
   128  	}
   129  	s.assertCalls(c, expectedCalls)
   130  	c.Assert(found.Results, gc.HasLen, 1)
   131  	c.Assert(found.Results[0].Error, gc.ErrorMatches,
   132  		fmt.Sprintf("getting details for storage data/0: getting storage instance: %v", msg),
   133  	)
   134  }
   135  
   136  func (s *storageSuite) TestStorageListAttachmentError(c *gc.C) {
   137  	s.state.storageInstanceAttachments = func(tag names.StorageTag) ([]state.StorageAttachment, error) {
   138  		s.calls = append(s.calls, storageInstanceAttachmentsCall)
   139  		c.Assert(tag, jc.DeepEquals, s.storageTag)
   140  		return []state.StorageAttachment{}, errors.Errorf("list test error")
   141  	}
   142  
   143  	found, err := s.api.ListStorageDetails(
   144  		params.StorageFilters{[]params.StorageFilter{{}}},
   145  	)
   146  	c.Assert(err, jc.ErrorIsNil)
   147  
   148  	expectedCalls := []string{
   149  		allStorageInstancesCall,
   150  		storageInstanceFilesystemCall,
   151  		storageInstanceAttachmentsCall,
   152  	}
   153  	s.assertCalls(c, expectedCalls)
   154  	c.Assert(found.Results, gc.HasLen, 1)
   155  	c.Assert(found.Results[0].Error, gc.ErrorMatches,
   156  		"getting details for storage data/0: list test error")
   157  }
   158  
   159  func (s *storageSuite) TestStorageListMachineError(c *gc.C) {
   160  	msg := "list test error"
   161  	s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) {
   162  		s.calls = append(s.calls, unitAssignedMachineCall)
   163  		c.Assert(u, jc.DeepEquals, s.unitTag)
   164  		return names.MachineTag{}, errors.Errorf(msg)
   165  	}
   166  
   167  	found, err := s.api.ListStorageDetails(
   168  		params.StorageFilters{[]params.StorageFilter{{}}},
   169  	)
   170  	c.Assert(err, jc.ErrorIsNil)
   171  
   172  	expectedCalls := []string{
   173  		allStorageInstancesCall,
   174  		storageInstanceFilesystemCall,
   175  		storageInstanceAttachmentsCall,
   176  		unitAssignedMachineCall,
   177  	}
   178  	s.assertCalls(c, expectedCalls)
   179  	c.Assert(found.Results, gc.HasLen, 1)
   180  	c.Assert(found.Results[0].Error, gc.ErrorMatches,
   181  		fmt.Sprintf("getting details for storage data/0: %v", msg),
   182  	)
   183  }
   184  
   185  func (s *storageSuite) TestStorageListFilesystemError(c *gc.C) {
   186  	msg := "list test error"
   187  	s.state.storageInstanceFilesystem = func(sTag names.StorageTag) (state.Filesystem, error) {
   188  		s.calls = append(s.calls, storageInstanceFilesystemCall)
   189  		c.Assert(sTag, jc.DeepEquals, s.storageTag)
   190  		return nil, errors.Errorf(msg)
   191  	}
   192  
   193  	found, err := s.api.ListStorageDetails(
   194  		params.StorageFilters{[]params.StorageFilter{{}}},
   195  	)
   196  	c.Assert(err, jc.ErrorIsNil)
   197  
   198  	expectedCalls := []string{
   199  		allStorageInstancesCall,
   200  		storageInstanceFilesystemCall,
   201  	}
   202  	s.assertCalls(c, expectedCalls)
   203  	c.Assert(found.Results, gc.HasLen, 1)
   204  	c.Assert(found.Results[0].Error, gc.ErrorMatches,
   205  		fmt.Sprintf("getting details for storage data/0: %v", msg),
   206  	)
   207  }
   208  
   209  func (s *storageSuite) TestStorageListFilesystemAttachmentError(c *gc.C) {
   210  	msg := "list test error"
   211  	s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) {
   212  		s.calls = append(s.calls, unitAssignedMachineCall)
   213  		c.Assert(u, jc.DeepEquals, s.unitTag)
   214  		return s.machineTag, errors.Errorf(msg)
   215  	}
   216  
   217  	found, err := s.api.ListStorageDetails(
   218  		params.StorageFilters{[]params.StorageFilter{{}}},
   219  	)
   220  	c.Assert(err, jc.ErrorIsNil)
   221  
   222  	expectedCalls := []string{
   223  		allStorageInstancesCall,
   224  		storageInstanceFilesystemCall,
   225  		storageInstanceAttachmentsCall,
   226  		unitAssignedMachineCall,
   227  	}
   228  	s.assertCalls(c, expectedCalls)
   229  	c.Assert(found.Results, gc.HasLen, 1)
   230  	c.Assert(found.Results[0].Error, gc.ErrorMatches,
   231  		fmt.Sprintf("getting details for storage data/0: %v", msg),
   232  	)
   233  }
   234  
   235  func (s *storageSuite) createTestStorageDetails() params.StorageDetails {
   236  	return params.StorageDetails{
   237  		StorageTag: s.storageTag.String(),
   238  		OwnerTag:   s.unitTag.String(),
   239  		Kind:       params.StorageKindFilesystem,
   240  		Status: params.EntityStatus{
   241  			Status: "attached",
   242  		},
   243  		Attachments: map[string]params.StorageAttachmentDetails{
   244  			s.unitTag.String(): params.StorageAttachmentDetails{
   245  				s.storageTag.String(),
   246  				s.unitTag.String(),
   247  				s.machineTag.String(),
   248  				"", // location
   249  			},
   250  		},
   251  	}
   252  }
   253  
   254  func (s *storageSuite) assertInstanceInfoError(c *gc.C, obtained params.StorageDetailsResult, wanted params.StorageDetailsResult, expected string) {
   255  	if expected != "" {
   256  		c.Assert(errors.Cause(obtained.Error), gc.ErrorMatches, fmt.Sprintf(".*%v.*", expected))
   257  		c.Assert(obtained.Result, gc.IsNil)
   258  	} else {
   259  		c.Assert(obtained.Error, gc.IsNil)
   260  		c.Assert(obtained, jc.DeepEquals, wanted)
   261  	}
   262  }
   263  
   264  func (s *storageSuite) TestShowStorageEmpty(c *gc.C) {
   265  	found, err := s.api.StorageDetails(params.Entities{})
   266  	c.Assert(err, jc.ErrorIsNil)
   267  	c.Assert(found.Results, gc.HasLen, 0)
   268  }
   269  
   270  func (s *storageSuite) TestShowStorageInvalidTag(c *gc.C) {
   271  	// Only storage tags are permitted
   272  	found, err := s.api.StorageDetails(params.Entities{
   273  		Entities: []params.Entity{{Tag: "machine-1"}},
   274  	})
   275  	c.Assert(err, jc.ErrorIsNil)
   276  	c.Assert(found.Results, gc.HasLen, 1)
   277  	c.Assert(found.Results[0].Error, gc.ErrorMatches, `"machine-1" is not a valid storage tag`)
   278  }
   279  
   280  func (s *storageSuite) TestShowStorage(c *gc.C) {
   281  	entity := params.Entity{Tag: s.storageTag.String()}
   282  
   283  	found, err := s.api.StorageDetails(
   284  		params.Entities{Entities: []params.Entity{entity}},
   285  	)
   286  	c.Assert(err, jc.ErrorIsNil)
   287  	c.Assert(found.Results, gc.HasLen, 1)
   288  
   289  	one := found.Results[0]
   290  	c.Assert(one.Error, gc.IsNil)
   291  
   292  	expected := params.StorageDetails{
   293  		StorageTag: s.storageTag.String(),
   294  		OwnerTag:   s.unitTag.String(),
   295  		Kind:       params.StorageKindFilesystem,
   296  		Status: params.EntityStatus{
   297  			Status: "attached",
   298  		},
   299  		Attachments: map[string]params.StorageAttachmentDetails{
   300  			s.unitTag.String(): params.StorageAttachmentDetails{
   301  				s.storageTag.String(),
   302  				s.unitTag.String(),
   303  				s.machineTag.String(),
   304  				"",
   305  			},
   306  		},
   307  	}
   308  	c.Assert(one.Result, jc.DeepEquals, &expected)
   309  }
   310  
   311  func (s *storageSuite) TestShowStorageInvalidId(c *gc.C) {
   312  	storageTag := "foo"
   313  	entity := params.Entity{Tag: storageTag}
   314  
   315  	found, err := s.api.StorageDetails(params.Entities{Entities: []params.Entity{entity}})
   316  	c.Assert(err, jc.ErrorIsNil)
   317  	c.Assert(found.Results, gc.HasLen, 1)
   318  	s.assertInstanceInfoError(c, found.Results[0], params.StorageDetailsResult{}, `"foo" is not a valid tag`)
   319  }