github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/api/agent/upgradeseries/upgradeseries_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package upgradeseries_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  	"go.uber.org/mock/gomock"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/api/agent/upgradeseries"
    15  	"github.com/juju/juju/api/base/mocks"
    16  	"github.com/juju/juju/core/model"
    17  	"github.com/juju/juju/core/status"
    18  	"github.com/juju/juju/rpc/params"
    19  )
    20  
    21  type upgradeSeriesSuite struct {
    22  	testing.IsolationSuite
    23  
    24  	tag                                  names.Tag
    25  	args                                 params.Entities
    26  	upgradeSeriesStartUnitCompletionArgs params.UpgradeSeriesStartUnitCompletionParam
    27  }
    28  
    29  var _ = gc.Suite(&upgradeSeriesSuite{})
    30  
    31  func (s *upgradeSeriesSuite) SetUpTest(c *gc.C) {
    32  	s.tag = names.NewMachineTag("0")
    33  	s.args = params.Entities{Entities: []params.Entity{{Tag: s.tag.String()}}}
    34  	s.upgradeSeriesStartUnitCompletionArgs = params.UpgradeSeriesStartUnitCompletionParam{
    35  		Entities: []params.Entity{{Tag: s.tag.String()}},
    36  	}
    37  	s.IsolationSuite.SetUpTest(c)
    38  }
    39  
    40  func (s *upgradeSeriesSuite) TestMachineStatus(c *gc.C) {
    41  	ctrl := gomock.NewController(c)
    42  	defer ctrl.Finish()
    43  
    44  	fCaller := mocks.NewMockFacadeCaller(ctrl)
    45  
    46  	resultSource := params.UpgradeSeriesStatusResults{
    47  		Results: []params.UpgradeSeriesStatusResult{{
    48  			Status: model.UpgradeSeriesPrepareStarted,
    49  		}},
    50  	}
    51  	fCaller.EXPECT().FacadeCall("MachineStatus", s.args, gomock.Any()).SetArg(2, resultSource)
    52  
    53  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
    54  	status, err := api.MachineStatus()
    55  	c.Assert(err, gc.IsNil)
    56  	c.Check(status, gc.Equals, model.UpgradeSeriesPrepareStarted)
    57  }
    58  
    59  func (s *upgradeSeriesSuite) TestMachineStatusNotFound(c *gc.C) {
    60  	ctrl := gomock.NewController(c)
    61  	defer ctrl.Finish()
    62  
    63  	fCaller := mocks.NewMockFacadeCaller(ctrl)
    64  
    65  	resultSource := params.UpgradeSeriesStatusResults{
    66  		Results: []params.UpgradeSeriesStatusResult{{
    67  			Error: &params.Error{
    68  				Code:    params.CodeNotFound,
    69  				Message: "did not find",
    70  			},
    71  		}},
    72  	}
    73  	fCaller.EXPECT().FacadeCall("MachineStatus", s.args, gomock.Any()).SetArg(2, resultSource)
    74  
    75  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
    76  	status, err := api.MachineStatus()
    77  	c.Assert(err, gc.ErrorMatches, "did not find")
    78  	c.Check(errors.IsNotFound(err), jc.IsTrue)
    79  	c.Check(string(status), gc.Equals, "")
    80  }
    81  
    82  func (s *upgradeSeriesSuite) TestSetMachineStatus(c *gc.C) {
    83  	ctrl := gomock.NewController(c)
    84  	defer ctrl.Finish()
    85  
    86  	fCaller := mocks.NewMockFacadeCaller(ctrl)
    87  
    88  	args := params.UpgradeSeriesStatusParams{
    89  		Params: []params.UpgradeSeriesStatusParam{
    90  			{Status: model.UpgradeSeriesCompleteStarted, Entity: s.args.Entities[0]},
    91  		},
    92  	}
    93  	resultSource := params.ErrorResults{Results: []params.ErrorResult{{}}}
    94  	fCaller.EXPECT().FacadeCall("SetMachineStatus", args, gomock.Any()).SetArg(2, resultSource)
    95  
    96  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
    97  	err := api.SetMachineStatus(model.UpgradeSeriesCompleteStarted, "")
    98  	c.Assert(err, gc.IsNil)
    99  }
   100  
   101  func (s *upgradeSeriesSuite) TestCurrentSeries(c *gc.C) {
   102  	ctrl := gomock.NewController(c)
   103  	defer ctrl.Finish()
   104  
   105  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   106  
   107  	resultSource := params.StringResults{
   108  		Results: []params.StringResult{{
   109  			Result: "xenial",
   110  		}},
   111  	}
   112  	fCaller.EXPECT().FacadeCall("CurrentSeries", s.args, gomock.Any()).SetArg(2, resultSource)
   113  
   114  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   115  	target, err := api.CurrentSeries()
   116  	c.Assert(err, gc.IsNil)
   117  	c.Check(target, gc.Equals, "xenial")
   118  }
   119  
   120  func (s *upgradeSeriesSuite) TestTargetSeries(c *gc.C) {
   121  	ctrl := gomock.NewController(c)
   122  	defer ctrl.Finish()
   123  
   124  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   125  
   126  	resultSource := params.StringResults{
   127  		Results: []params.StringResult{{
   128  			Result: "bionic",
   129  		}},
   130  	}
   131  	fCaller.EXPECT().FacadeCall("TargetSeries", s.args, gomock.Any()).SetArg(2, resultSource)
   132  
   133  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   134  	target, err := api.TargetSeries()
   135  	c.Assert(err, gc.IsNil)
   136  	c.Check(target, gc.Equals, "bionic")
   137  }
   138  
   139  func (s *upgradeSeriesSuite) TestUnitsPrepared(c *gc.C) {
   140  	ctrl := gomock.NewController(c)
   141  	defer ctrl.Finish()
   142  
   143  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   144  
   145  	r0 := names.NewUnitTag("redis/0")
   146  	r1 := names.NewUnitTag("redis/1")
   147  
   148  	resultSource := params.EntitiesResults{
   149  		Results: []params.EntitiesResult{{Entities: []params.Entity{
   150  			{Tag: r0.String()},
   151  			{Tag: r1.String()},
   152  		}}},
   153  	}
   154  	fCaller.EXPECT().FacadeCall("UnitsPrepared", s.args, gomock.Any()).SetArg(2, resultSource)
   155  
   156  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   157  	units, err := api.UnitsPrepared()
   158  	c.Assert(err, gc.IsNil)
   159  
   160  	expected := []names.UnitTag{r0, r1}
   161  	c.Check(units, jc.SameContents, expected)
   162  }
   163  
   164  func (s *upgradeSeriesSuite) TestUnitsCompleted(c *gc.C) {
   165  	ctrl := gomock.NewController(c)
   166  	defer ctrl.Finish()
   167  
   168  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   169  
   170  	p0 := names.NewUnitTag("postgres/0")
   171  	p1 := names.NewUnitTag("postgres/1")
   172  	p2 := names.NewUnitTag("postgres/2")
   173  
   174  	resultSource := params.EntitiesResults{
   175  		Results: []params.EntitiesResult{{Entities: []params.Entity{
   176  			{Tag: p0.String()},
   177  			{Tag: p1.String()},
   178  			{Tag: p2.String()},
   179  		}}},
   180  	}
   181  	fCaller.EXPECT().FacadeCall("UnitsCompleted", s.args, gomock.Any()).SetArg(2, resultSource)
   182  
   183  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   184  	units, err := api.UnitsCompleted()
   185  	c.Assert(err, gc.IsNil)
   186  
   187  	expected := []names.UnitTag{p0, p1, p2}
   188  	c.Check(units, jc.SameContents, expected)
   189  }
   190  
   191  func (s *upgradeSeriesSuite) TestStartUnitCompletion(c *gc.C) {
   192  	ctrl := gomock.NewController(c)
   193  	defer ctrl.Finish()
   194  
   195  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   196  
   197  	resultSource := params.ErrorResults{Results: []params.ErrorResult{{}}}
   198  	fCaller.EXPECT().FacadeCall("StartUnitCompletion", s.upgradeSeriesStartUnitCompletionArgs, gomock.Any()).SetArg(2, resultSource)
   199  
   200  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   201  	err := api.StartUnitCompletion("")
   202  	c.Assert(err, gc.IsNil)
   203  }
   204  
   205  func (s *upgradeSeriesSuite) TestFinishUpgradeSeries(c *gc.C) {
   206  	ctrl := gomock.NewController(c)
   207  	defer ctrl.Finish()
   208  
   209  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   210  
   211  	args := params.UpdateChannelArgs{
   212  		Args: []params.UpdateChannelArg{
   213  			{Channel: "16.04", Entity: s.args.Entities[0]},
   214  		},
   215  	}
   216  	resultSource := params.ErrorResults{Results: []params.ErrorResult{{}}}
   217  	fCaller.EXPECT().FacadeCall("FinishUpgradeSeries", args, gomock.Any()).SetArg(2, resultSource)
   218  
   219  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   220  	err := api.FinishUpgradeSeries("xenial")
   221  	c.Assert(err, gc.IsNil)
   222  }
   223  
   224  func (s *upgradeSeriesSuite) TestSetStatus(c *gc.C) {
   225  	ctrl := gomock.NewController(c)
   226  	defer ctrl.Finish()
   227  
   228  	fCaller := mocks.NewMockFacadeCaller(ctrl)
   229  
   230  	args := params.SetStatus{
   231  		Entities: []params.EntityStatusArgs{
   232  			{
   233  				Tag:    s.tag.String(),
   234  				Status: status.Running.String(),
   235  				Info:   "series upgrade complete started: waiting for something",
   236  			},
   237  		},
   238  	}
   239  	resultSource := params.ErrorResults{Results: []params.ErrorResult{{}}}
   240  	fCaller.EXPECT().FacadeCall("SetInstanceStatus", args, gomock.Any()).SetArg(2, resultSource)
   241  
   242  	api := upgradeseries.NewStateFromCaller(fCaller, s.tag)
   243  	err := api.SetInstanceStatus(model.UpgradeSeriesCompleteStarted, "waiting for something")
   244  	c.Assert(err, gc.IsNil)
   245  }