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