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

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  /*
     5  The existing machinemanager_test.go uses a home grown mocking mechanism.
     6  I wanted to establish the new suffixed file to have a place to start systematically moving those tests to use gomock.
     7  There are two benefits to this
     8  
     9  1) We can work piecemeal
    10  2) We don't have to mix two mocking styles (in attempt to preserve one file) when transitioning between mocking styles
    11  
    12  The plan is to start moving those old style tests and when finished delete the old file and mv the new file.
    13  */
    14  
    15  package machinemanager_test
    16  
    17  import (
    18  	"github.com/golang/mock/gomock"
    19  	"github.com/juju/errors"
    20  	jc "github.com/juju/testing/checkers"
    21  	gc "gopkg.in/check.v1"
    22  	"gopkg.in/juju/names.v2"
    23  
    24  	"github.com/juju/juju/api/base/mocks"
    25  	"github.com/juju/juju/api/machinemanager"
    26  	"github.com/juju/juju/apiserver/params"
    27  	jujutesting "github.com/juju/juju/testing"
    28  )
    29  
    30  var _ = gc.Suite(&NewMachineManagerSuite{})
    31  
    32  type NewMachineManagerSuite struct {
    33  	jujutesting.BaseSuite
    34  
    35  	clientFacade *mocks.MockClientFacade
    36  	facade       *mocks.MockFacadeCaller
    37  
    38  	tag    names.Tag
    39  	args   params.Entities
    40  	client *machinemanager.Client
    41  }
    42  
    43  func (s *NewMachineManagerSuite) SetUpTest(c *gc.C) {
    44  	s.tag = names.NewMachineTag("0")
    45  	s.args = params.Entities{Entities: []params.Entity{{Tag: s.tag.String()}}}
    46  
    47  	s.BaseSuite.SetUpTest(c)
    48  }
    49  
    50  func (s *NewMachineManagerSuite) TestUpgradeSeriesValidate(c *gc.C) {
    51  	defer s.setup(c).Finish()
    52  
    53  	args := params.UpdateSeriesArgs{
    54  		Args: []params.UpdateSeriesArg{
    55  			{
    56  				Entity: params.Entity{Tag: names.NewMachineTag(s.tag.String()).String()},
    57  				Series: "xenial",
    58  			},
    59  		},
    60  	}
    61  	result := params.UpgradeSeriesUnitsResult{
    62  		UnitNames: []string{"ubuntu/0", "ubuntu/1"},
    63  	}
    64  	results := params.UpgradeSeriesUnitsResults{Results: []params.UpgradeSeriesUnitsResult{result}}
    65  	s.facade.EXPECT().FacadeCall("UpgradeSeriesValidate", args, gomock.Any()).SetArg(2, results)
    66  
    67  	unitNames, err := s.client.UpgradeSeriesValidate(s.tag.String(), "xenial")
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(unitNames, gc.DeepEquals, result.UnitNames)
    70  }
    71  
    72  func (s *NewMachineManagerSuite) TestUpgradeSeriesPrepareAlreadyInProgress(c *gc.C) {
    73  	defer s.setup(c).Finish()
    74  
    75  	arg := params.UpdateSeriesArg{
    76  		Entity: params.Entity{Tag: s.tag.String()},
    77  		Series: "xenial",
    78  		Force:  true,
    79  	}
    80  	resultSource := params.ErrorResult{
    81  		Error: &params.Error{
    82  			Message: "lock already exists",
    83  			Code:    params.CodeAlreadyExists,
    84  		},
    85  	}
    86  	s.facade.EXPECT().FacadeCall("UpgradeSeriesPrepare", arg, gomock.Any()).SetArg(2, resultSource)
    87  
    88  	err := s.client.UpgradeSeriesPrepare(s.tag.Id(), "xenial", true)
    89  	c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
    90  }
    91  
    92  func (s *NewMachineManagerSuite) setup(c *gc.C) *gomock.Controller {
    93  	ctrl := gomock.NewController(c)
    94  
    95  	s.clientFacade = mocks.NewMockClientFacade(ctrl)
    96  	s.facade = mocks.NewMockFacadeCaller(ctrl)
    97  
    98  	s.clientFacade.EXPECT().BestAPIVersion().Return(5)
    99  
   100  	s.client = machinemanager.ConstructClient(s.clientFacade, s.facade)
   101  
   102  	return ctrl
   103  }