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