github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/switchgeneration_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model_test 5 6 import ( 7 "github.com/golang/mock/gomock" 8 "github.com/juju/cmd" 9 "github.com/juju/cmd/cmdtesting" 10 "github.com/juju/juju/cmd/juju/model/mocks" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/juju/model" 15 coremodel "github.com/juju/juju/core/model" 16 "github.com/juju/juju/feature" 17 "github.com/juju/juju/jujuclient" 18 "github.com/juju/juju/testing" 19 ) 20 21 type switchGenerationSuite struct { 22 testing.FakeJujuXDGDataHomeSuite 23 store *jujuclient.MemStore 24 } 25 26 var _ = gc.Suite(&switchGenerationSuite{}) 27 28 func (s *switchGenerationSuite) SetUpTest(c *gc.C) { 29 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 30 s.SetFeatureFlags(feature.Generations) 31 s.store = jujuclient.NewMemStore() 32 s.store.CurrentControllerName = "testing" 33 s.store.Controllers["testing"] = jujuclient.ControllerDetails{} 34 s.store.Accounts["testing"] = jujuclient.AccountDetails{ 35 User: "admin", 36 } 37 err := s.store.UpdateModel("testing", "admin/mymodel", jujuclient.ModelDetails{ 38 ModelUUID: testing.ModelTag.Id(), 39 ModelType: coremodel.IAAS, 40 ModelGeneration: coremodel.GenerationCurrent, 41 }) 42 c.Assert(err, jc.ErrorIsNil) 43 s.store.Models["testing"].CurrentModel = "admin/mymodel" 44 } 45 46 func (s *switchGenerationSuite) runInit(args ...string) error { 47 cmd := model.NewSwitchGenerationCommandForTest(nil, s.store) 48 return cmdtesting.InitCommand(cmd, args) 49 } 50 51 func (s *switchGenerationSuite) TestInitNext(c *gc.C) { 52 err := s.runInit("next") 53 c.Assert(err, jc.ErrorIsNil) 54 } 55 56 func (s *switchGenerationSuite) TestInitCurrent(c *gc.C) { 57 err := s.runInit("current") 58 c.Assert(err, jc.ErrorIsNil) 59 } 60 61 func (s *switchGenerationSuite) TestInitFail(c *gc.C) { 62 err := s.runInit() 63 c.Assert(err, gc.ErrorMatches, "Must specify 'current' or 'next'") 64 } 65 66 func (s *switchGenerationSuite) runCommand(c *gc.C, api model.SwitchGenerationCommandAPI, args ...string) (*cmd.Context, error) { 67 cmd := model.NewSwitchGenerationCommandForTest(api, s.store) 68 return cmdtesting.RunCommand(c, cmd, args...) 69 } 70 71 func setUpSwitchMocks(c *gc.C) (*gomock.Controller, *mocks.MockSwitchGenerationCommandAPI) { 72 mockController := gomock.NewController(c) 73 mockSwitchGenerationCommandAPI := mocks.NewMockSwitchGenerationCommandAPI(mockController) 74 mockSwitchGenerationCommandAPI.EXPECT().Close() 75 return mockController, mockSwitchGenerationCommandAPI 76 } 77 78 func (s *switchGenerationSuite) TestRunCommandCurrent(c *gc.C) { 79 ctx, err := s.runCommand(c, nil, "current") 80 c.Assert(err, jc.ErrorIsNil) 81 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "target generation set to current\n") 82 83 cName := s.store.CurrentControllerName 84 details, err := s.store.ModelByName(cName, s.store.Models[cName].CurrentModel) 85 c.Assert(err, jc.ErrorIsNil) 86 c.Assert(details.ModelGeneration, gc.Equals, coremodel.GenerationCurrent) 87 } 88 89 func (s *switchGenerationSuite) TestRunCommandNextGenExists(c *gc.C) { 90 mockController, mockSwitchGenerationCommandAPI := setUpSwitchMocks(c) 91 defer mockController.Finish() 92 93 mockSwitchGenerationCommandAPI.EXPECT().HasNextGeneration(gomock.Any()).Return(true, nil) 94 95 ctx, err := s.runCommand(c, mockSwitchGenerationCommandAPI, "next") 96 c.Assert(err, jc.ErrorIsNil) 97 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "target generation set to next\n") 98 99 cName := s.store.CurrentControllerName 100 details, err := s.store.ModelByName(cName, s.store.Models[cName].CurrentModel) 101 c.Assert(err, jc.ErrorIsNil) 102 c.Assert(details.ModelGeneration, gc.Equals, coremodel.GenerationNext) 103 } 104 105 func (s *switchGenerationSuite) TestRunCommandNextNoGenError(c *gc.C) { 106 mockController, mockSwitchGenerationCommandAPI := setUpSwitchMocks(c) 107 defer mockController.Finish() 108 109 mockSwitchGenerationCommandAPI.EXPECT().HasNextGeneration(gomock.Any()).Return(false, nil) 110 111 _, err := s.runCommand(c, mockSwitchGenerationCommandAPI, "next") 112 c.Assert(err, gc.ErrorMatches, "this model has no next generation") 113 }