github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/uniter/storage_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     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  
    12  	"github.com/juju/juju/api/base/testing"
    13  	"github.com/juju/juju/api/uniter"
    14  	"github.com/juju/juju/apiserver/params"
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  var _ = gc.Suite(&storageSuite{})
    19  
    20  type storageSuite struct {
    21  	coretesting.BaseSuite
    22  }
    23  
    24  const expectedVersion = 9
    25  
    26  func (s *storageSuite) TestUnitStorageAttachments(c *gc.C) {
    27  	storageAttachmentIds := []params.StorageAttachmentId{{
    28  		StorageTag: "storage-whatever-0",
    29  		UnitTag:    "unit-mysql-0",
    30  	}}
    31  
    32  	var called bool
    33  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    34  		c.Check(objType, gc.Equals, "Uniter")
    35  		c.Check(version, gc.Equals, expectedVersion)
    36  		c.Check(id, gc.Equals, "")
    37  		c.Check(request, gc.Equals, "UnitStorageAttachments")
    38  		c.Check(arg, gc.DeepEquals, params.Entities{
    39  			Entities: []params.Entity{{Tag: "unit-mysql-0"}},
    40  		})
    41  		c.Assert(result, gc.FitsTypeOf, &params.StorageAttachmentIdsResults{})
    42  		*(result.(*params.StorageAttachmentIdsResults)) = params.StorageAttachmentIdsResults{
    43  			Results: []params.StorageAttachmentIdsResult{{
    44  				Result: params.StorageAttachmentIds{storageAttachmentIds},
    45  			}},
    46  		}
    47  		called = true
    48  		return nil
    49  	})
    50  
    51  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
    52  	attachmentIds, err := st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
    53  	c.Check(err, jc.ErrorIsNil)
    54  	c.Check(called, jc.IsTrue)
    55  	c.Assert(attachmentIds, gc.DeepEquals, storageAttachmentIds)
    56  }
    57  
    58  func (s *storageSuite) TestDestroyUnitStorageAttachments(c *gc.C) {
    59  	var called bool
    60  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    61  		c.Check(objType, gc.Equals, "Uniter")
    62  		c.Check(version, gc.Equals, expectedVersion)
    63  		c.Check(id, gc.Equals, "")
    64  		c.Check(request, gc.Equals, "DestroyUnitStorageAttachments")
    65  		c.Check(arg, gc.DeepEquals, params.Entities{
    66  			Entities: []params.Entity{{Tag: "unit-mysql-0"}},
    67  		})
    68  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
    69  		*(result.(*params.ErrorResults)) = params.ErrorResults{
    70  			Results: []params.ErrorResult{{}},
    71  		}
    72  		called = true
    73  		return nil
    74  	})
    75  
    76  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
    77  	err := st.DestroyUnitStorageAttachments(names.NewUnitTag("mysql/0"))
    78  	c.Check(err, jc.ErrorIsNil)
    79  	c.Check(called, jc.IsTrue)
    80  }
    81  
    82  func (s *storageSuite) TestStorageAttachmentResultCountMismatch(c *gc.C) {
    83  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    84  		*(result.(*params.StorageAttachmentIdsResults)) = params.StorageAttachmentIdsResults{
    85  			[]params.StorageAttachmentIdsResult{{}, {}},
    86  		}
    87  		return nil
    88  	})
    89  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
    90  	_, err := st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
    91  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2")
    92  }
    93  
    94  func (s *storageSuite) TestAPIErrors(c *gc.C) {
    95  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    96  		return errors.New("bad")
    97  	})
    98  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
    99  	_, err := st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
   100  	c.Check(err, gc.ErrorMatches, "bad")
   101  }
   102  
   103  func (s *storageSuite) TestWatchUnitStorageAttachments(c *gc.C) {
   104  	var called bool
   105  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   106  		c.Check(objType, gc.Equals, "Uniter")
   107  		c.Check(version, gc.Equals, expectedVersion)
   108  		c.Check(id, gc.Equals, "")
   109  		c.Check(request, gc.Equals, "WatchUnitStorageAttachments")
   110  		c.Check(arg, gc.DeepEquals, params.Entities{
   111  			Entities: []params.Entity{{Tag: "unit-mysql-0"}},
   112  		})
   113  		c.Assert(result, gc.FitsTypeOf, &params.StringsWatchResults{})
   114  		*(result.(*params.StringsWatchResults)) = params.StringsWatchResults{
   115  			Results: []params.StringsWatchResult{{
   116  				Error: &params.Error{Message: "FAIL"},
   117  			}},
   118  		}
   119  		called = true
   120  		return nil
   121  	})
   122  
   123  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   124  	_, err := st.WatchUnitStorageAttachments(names.NewUnitTag("mysql/0"))
   125  	c.Check(err, gc.ErrorMatches, "FAIL")
   126  	c.Check(called, jc.IsTrue)
   127  }
   128  
   129  func (s *storageSuite) TestWatchStorageAttachments(c *gc.C) {
   130  	var called bool
   131  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   132  		c.Check(objType, gc.Equals, "Uniter")
   133  		c.Check(version, gc.Equals, expectedVersion)
   134  		c.Check(id, gc.Equals, "")
   135  		c.Check(request, gc.Equals, "WatchStorageAttachments")
   136  		c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
   137  			Ids: []params.StorageAttachmentId{{
   138  				StorageTag: "storage-data-0",
   139  				UnitTag:    "unit-mysql-0",
   140  			}},
   141  		})
   142  		c.Assert(result, gc.FitsTypeOf, &params.NotifyWatchResults{})
   143  		*(result.(*params.NotifyWatchResults)) = params.NotifyWatchResults{
   144  			Results: []params.NotifyWatchResult{{
   145  				Error: &params.Error{Message: "FAIL"},
   146  			}},
   147  		}
   148  		called = true
   149  		return nil
   150  	})
   151  
   152  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   153  	_, err := st.WatchStorageAttachment(names.NewStorageTag("data/0"), names.NewUnitTag("mysql/0"))
   154  	c.Check(err, gc.ErrorMatches, "FAIL")
   155  	c.Check(called, jc.IsTrue)
   156  }
   157  
   158  func (s *storageSuite) TestStorageAttachments(c *gc.C) {
   159  	storageAttachment := params.StorageAttachment{
   160  		StorageTag: "storage-whatever-0",
   161  		OwnerTag:   "application-mysql",
   162  		UnitTag:    "unit-mysql-0",
   163  		Kind:       params.StorageKindBlock,
   164  		Location:   "/dev/sda",
   165  	}
   166  
   167  	var called bool
   168  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   169  		c.Check(objType, gc.Equals, "Uniter")
   170  		c.Check(version, gc.Equals, expectedVersion)
   171  		c.Check(id, gc.Equals, "")
   172  		c.Check(request, gc.Equals, "StorageAttachments")
   173  		c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
   174  			Ids: []params.StorageAttachmentId{{
   175  				StorageTag: "storage-data-0",
   176  				UnitTag:    "unit-mysql-0",
   177  			}},
   178  		})
   179  		c.Assert(result, gc.FitsTypeOf, &params.StorageAttachmentResults{})
   180  		*(result.(*params.StorageAttachmentResults)) = params.StorageAttachmentResults{
   181  			Results: []params.StorageAttachmentResult{{
   182  				Result: storageAttachment,
   183  			}},
   184  		}
   185  		called = true
   186  		return nil
   187  	})
   188  
   189  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   190  	attachment, err := st.StorageAttachment(names.NewStorageTag("data/0"), names.NewUnitTag("mysql/0"))
   191  	c.Check(err, jc.ErrorIsNil)
   192  	c.Check(called, jc.IsTrue)
   193  	c.Assert(attachment, gc.DeepEquals, storageAttachment)
   194  }
   195  
   196  func (s *storageSuite) TestStorageAttachmentLife(c *gc.C) {
   197  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   198  		c.Check(objType, gc.Equals, "Uniter")
   199  		c.Check(version, gc.Equals, expectedVersion)
   200  		c.Check(id, gc.Equals, "")
   201  		c.Check(request, gc.Equals, "StorageAttachmentLife")
   202  		c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
   203  			Ids: []params.StorageAttachmentId{{
   204  				StorageTag: "storage-data-0",
   205  				UnitTag:    "unit-mysql-0",
   206  			}},
   207  		})
   208  		c.Assert(result, gc.FitsTypeOf, &params.LifeResults{})
   209  		*(result.(*params.LifeResults)) = params.LifeResults{
   210  			Results: []params.LifeResult{{
   211  				Life: params.Dying,
   212  			}},
   213  		}
   214  		return nil
   215  	})
   216  
   217  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   218  	results, err := st.StorageAttachmentLife([]params.StorageAttachmentId{{
   219  		StorageTag: "storage-data-0",
   220  		UnitTag:    "unit-mysql-0",
   221  	}})
   222  	c.Check(err, jc.ErrorIsNil)
   223  	c.Assert(results, jc.DeepEquals, []params.LifeResult{{Life: params.Dying}})
   224  }
   225  
   226  func (s *storageSuite) TestRemoveStorageAttachment(c *gc.C) {
   227  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   228  		c.Check(objType, gc.Equals, "Uniter")
   229  		c.Check(version, gc.Equals, expectedVersion)
   230  		c.Check(id, gc.Equals, "")
   231  		c.Check(request, gc.Equals, "RemoveStorageAttachments")
   232  		c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
   233  			Ids: []params.StorageAttachmentId{{
   234  				StorageTag: "storage-data-0",
   235  				UnitTag:    "unit-mysql-0",
   236  			}},
   237  		})
   238  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   239  		*(result.(*params.ErrorResults)) = params.ErrorResults{
   240  			Results: []params.ErrorResult{{
   241  				Error: &params.Error{Message: "yoink"},
   242  			}},
   243  		}
   244  		return nil
   245  	})
   246  
   247  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   248  	err := st.RemoveStorageAttachment(names.NewStorageTag("data/0"), names.NewUnitTag("mysql/0"))
   249  	c.Check(err, gc.ErrorMatches, "yoink")
   250  }