github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/common/upgradeseries_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	apitesting "github.com/juju/juju/api/base/testing"
    14  	"github.com/juju/juju/api/common"
    15  	"github.com/juju/juju/core/model"
    16  	"github.com/juju/juju/rpc/params"
    17  )
    18  
    19  type upgradeSeriesSuite struct {
    20  	testing.IsolationSuite
    21  	tag names.Tag
    22  }
    23  
    24  var _ = gc.Suite(&upgradeSeriesSuite{})
    25  
    26  func (s *upgradeSeriesSuite) SetUpTest(c *gc.C) {
    27  	s.tag = names.NewMachineTag("0")
    28  }
    29  
    30  func (s *upgradeSeriesSuite) TestWatchUpgradeSeriesNotifications(c *gc.C) {
    31  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    32  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    33  		c.Assert(name, gc.Equals, "WatchUpgradeSeriesNotifications")
    34  		c.Assert(args, jc.DeepEquals, params.Entities{Entities: []params.Entity{
    35  			{Tag: s.tag.String()},
    36  		}})
    37  		*(response.(*params.NotifyWatchResults)) = params.NotifyWatchResults{
    38  			Results: []params.NotifyWatchResult{{
    39  				NotifyWatcherId: "1",
    40  				Error:           nil,
    41  			}},
    42  		}
    43  		return nil
    44  	}
    45  	apiCaller := apitesting.APICallerFunc(
    46  		func(objType string,
    47  			version int,
    48  			id, request string,
    49  			a, result interface{},
    50  		) error {
    51  			c.Check(objType, gc.Equals, "NotifyWatcher")
    52  			c.Check(id, gc.Equals, "1")
    53  			c.Check(request, gc.Equals, "Next")
    54  			c.Check(a, gc.IsNil)
    55  			return nil
    56  		},
    57  	)
    58  	facadeCaller.ReturnRawAPICaller = apitesting.BestVersionCaller{APICallerFunc: apiCaller, BestVersion: 1}
    59  
    60  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
    61  	_, err := api.WatchUpgradeSeriesNotifications()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  }
    64  
    65  func (s *upgradeSeriesSuite) TestUpgradeSeriesStatusWithComplete(c *gc.C) {
    66  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    67  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    68  		c.Assert(name, gc.Equals, "UpgradeSeriesUnitStatus")
    69  		c.Assert(args, jc.DeepEquals, params.Entities{Entities: []params.Entity{
    70  			{Tag: s.tag.String()},
    71  		}})
    72  		*(response.(*params.UpgradeSeriesStatusResults)) = params.UpgradeSeriesStatusResults{
    73  			Results: []params.UpgradeSeriesStatusResult{{
    74  				Status: model.UpgradeSeriesCompleted,
    75  				Target: "focal",
    76  			}},
    77  		}
    78  		return nil
    79  	}
    80  
    81  	sts, target, err := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag).UpgradeSeriesUnitStatus()
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	c.Check(sts, gc.Equals, model.UpgradeSeriesCompleted)
    84  	c.Check(target, gc.Equals, "focal")
    85  }
    86  
    87  func (s *upgradeSeriesSuite) TestUpgradeSeriesStatusNotFound(c *gc.C) {
    88  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    89  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    90  		c.Assert(name, gc.Equals, "UpgradeSeriesUnitStatus")
    91  		c.Assert(args, jc.DeepEquals, params.Entities{Entities: []params.Entity{
    92  			{Tag: s.tag.String()},
    93  		}})
    94  		*(response.(*params.UpgradeSeriesStatusResults)) = params.UpgradeSeriesStatusResults{
    95  			Results: []params.UpgradeSeriesStatusResult{{
    96  				Error: &params.Error{
    97  					Code:    params.CodeNotFound,
    98  					Message: `testing`,
    99  				},
   100  			}},
   101  		}
   102  		return nil
   103  	}
   104  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
   105  	_, _, err := api.UpgradeSeriesUnitStatus()
   106  	c.Assert(err, gc.ErrorMatches, "testing")
   107  	c.Check(errors.IsNotFound(err), jc.IsTrue)
   108  }
   109  
   110  func (s *upgradeSeriesSuite) TestUpgradeSeriesStatusMultiple(c *gc.C) {
   111  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   112  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   113  		c.Assert(name, gc.Equals, "UpgradeSeriesUnitStatus")
   114  		c.Assert(args, jc.DeepEquals, params.Entities{Entities: []params.Entity{
   115  			{Tag: s.tag.String()},
   116  		}})
   117  		*(response.(*params.UpgradeSeriesStatusResults)) = params.UpgradeSeriesStatusResults{
   118  			Results: []params.UpgradeSeriesStatusResult{
   119  				{Status: "prepare started"},
   120  				{Status: "prepare completed"},
   121  			},
   122  		}
   123  		return nil
   124  	}
   125  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
   126  	_, _, err := api.UpgradeSeriesUnitStatus()
   127  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2")
   128  }
   129  
   130  func (s *upgradeSeriesSuite) TestSetUpgradeSeriesStatus(c *gc.C) {
   131  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   132  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   133  		c.Assert(name, gc.Equals, "SetUpgradeSeriesUnitStatus")
   134  		c.Assert(args, jc.DeepEquals, params.UpgradeSeriesStatusParams{
   135  			Params: []params.UpgradeSeriesStatusParam{{
   136  				Entity: params.Entity{Tag: s.tag.String()},
   137  				Status: model.UpgradeSeriesError,
   138  			}},
   139  		})
   140  		*(response.(*params.ErrorResults)) = params.ErrorResults{
   141  			Results: []params.ErrorResult{{
   142  				Error: nil,
   143  			}},
   144  		}
   145  		return nil
   146  	}
   147  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
   148  	err := api.SetUpgradeSeriesUnitStatus(model.UpgradeSeriesError, "")
   149  	c.Assert(err, jc.ErrorIsNil)
   150  }
   151  
   152  func (s *upgradeSeriesSuite) TestSetUpgradeSeriesStatusNotOne(c *gc.C) {
   153  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   154  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   155  		c.Assert(name, gc.Equals, "SetUpgradeSeriesUnitStatus")
   156  		c.Assert(args, jc.DeepEquals, params.UpgradeSeriesStatusParams{
   157  			Params: []params.UpgradeSeriesStatusParam{{
   158  				Entity: params.Entity{Tag: s.tag.String()},
   159  				Status: model.UpgradeSeriesError,
   160  			}},
   161  		})
   162  		*(response.(*params.ErrorResults)) = params.ErrorResults{
   163  			Results: []params.ErrorResult{},
   164  		}
   165  		return nil
   166  	}
   167  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
   168  	err := api.SetUpgradeSeriesUnitStatus(model.UpgradeSeriesError, "")
   169  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0")
   170  }
   171  
   172  func (s *upgradeSeriesSuite) TestSetUpgradeSeriesStatusResultError(c *gc.C) {
   173  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   174  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   175  		c.Assert(name, gc.Equals, "SetUpgradeSeriesUnitStatus")
   176  		c.Assert(args, jc.DeepEquals, params.UpgradeSeriesStatusParams{
   177  			Params: []params.UpgradeSeriesStatusParam{{
   178  				Entity: params.Entity{Tag: s.tag.String()},
   179  				Status: model.UpgradeSeriesError,
   180  			}},
   181  		})
   182  		*(response.(*params.ErrorResults)) = params.ErrorResults{
   183  			Results: []params.ErrorResult{{
   184  				Error: &params.Error{Message: "error in call"},
   185  			}},
   186  		}
   187  		return nil
   188  	}
   189  	api := common.NewUpgradeSeriesAPI(&facadeCaller, s.tag)
   190  	err := api.SetUpgradeSeriesUnitStatus(model.UpgradeSeriesError, "")
   191  	c.Assert(err, gc.ErrorMatches, "error in call")
   192  }