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