github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/advancegeneration_test.go (about) 1 // Copyright 2019 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/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/juju/model" 15 "github.com/juju/juju/cmd/juju/model/mocks" 16 coremodel "github.com/juju/juju/core/model" 17 "github.com/juju/juju/feature" 18 "github.com/juju/juju/jujuclient" 19 "github.com/juju/juju/testing" 20 ) 21 22 type advanceGenerationSuite struct { 23 testing.FakeJujuXDGDataHomeSuite 24 store *jujuclient.MemStore 25 } 26 27 var _ = gc.Suite(&advanceGenerationSuite{}) 28 29 func (s *advanceGenerationSuite) SetUpTest(c *gc.C) { 30 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 31 s.SetFeatureFlags(feature.Generations) 32 s.store = jujuclient.NewMemStore() 33 s.store.CurrentControllerName = "testing" 34 s.store.Controllers["testing"] = jujuclient.ControllerDetails{} 35 s.store.Accounts["testing"] = jujuclient.AccountDetails{ 36 User: "admin", 37 } 38 err := s.store.UpdateModel("testing", "admin/mymodel", jujuclient.ModelDetails{ 39 ModelUUID: testing.ModelTag.Id(), 40 ModelType: coremodel.IAAS, 41 ModelGeneration: coremodel.GenerationNext, 42 }) 43 c.Assert(err, jc.ErrorIsNil) 44 s.store.Models["testing"].CurrentModel = "admin/mymodel" 45 } 46 47 func (s *advanceGenerationSuite) runInit(args ...string) error { 48 cmd := model.NewAdvanceGenerationCommandForTest(nil, s.store) 49 return cmdtesting.InitCommand(cmd, args) 50 } 51 52 func (s *advanceGenerationSuite) TestInitApplication(c *gc.C) { 53 err := s.runInit("ubuntu") 54 c.Assert(err, jc.ErrorIsNil) 55 } 56 57 func (s *advanceGenerationSuite) TestInitUnit(c *gc.C) { 58 err := s.runInit("ubuntu/0") 59 c.Assert(err, jc.ErrorIsNil) 60 } 61 62 func (s *advanceGenerationSuite) TestInitEmpty(c *gc.C) { 63 err := s.runInit() 64 c.Assert(err, gc.ErrorMatches, `unit and/or application names\(s\) must be specified`) 65 } 66 67 func (s *advanceGenerationSuite) TestInitInvalid(c *gc.C) { 68 err := s.runInit("test me") 69 c.Assert(err, gc.ErrorMatches, `invalid application or unit name "test me"`) 70 } 71 72 func (s *advanceGenerationSuite) runCommand(c *gc.C, api model.AdvanceGenerationCommandAPI, args ...string) (*cmd.Context, error) { 73 cmd := model.NewAdvanceGenerationCommandForTest(api, s.store) 74 return cmdtesting.RunCommand(c, cmd, args...) 75 } 76 77 func setUpAdvanceMocks(c *gc.C) (*gomock.Controller, *mocks.MockAdvanceGenerationCommandAPI) { 78 mockController := gomock.NewController(c) 79 mockAdvanceGenerationCommandAPI := mocks.NewMockAdvanceGenerationCommandAPI(mockController) 80 mockAdvanceGenerationCommandAPI.EXPECT().Close() 81 return mockController, mockAdvanceGenerationCommandAPI 82 } 83 84 func (s *advanceGenerationSuite) TestRunCommandNotCompleted(c *gc.C) { 85 mockController, api := setUpAdvanceMocks(c) 86 defer mockController.Finish() 87 88 api.EXPECT().AdvanceGeneration(gomock.Any(), []string{"ubuntu/0", "redis"}).Return(false, nil) 89 90 _, err := s.runCommand(c, api, "ubuntu/0", "redis") 91 c.Assert(err, jc.ErrorIsNil) 92 93 // Ensure the generation did not change in the local store. 94 cName := s.store.CurrentControllerName 95 details, err := s.store.ModelByName(cName, s.store.Models[cName].CurrentModel) 96 c.Assert(err, jc.ErrorIsNil) 97 c.Assert(details.ModelGeneration, gc.Equals, coremodel.GenerationNext) 98 } 99 100 func (s *advanceGenerationSuite) TestRunCommandCompleted(c *gc.C) { 101 mockController, api := setUpAdvanceMocks(c) 102 defer mockController.Finish() 103 104 api.EXPECT().AdvanceGeneration(gomock.Any(), []string{"ubuntu/0", "redis"}).Return(true, nil) 105 106 ctx, err := s.runCommand(c, api, "ubuntu/0", "redis") 107 c.Assert(err, jc.ErrorIsNil) 108 109 // Ensure the generation was changed to "current" in the local store. 110 cName := s.store.CurrentControllerName 111 details, err := s.store.ModelByName(cName, s.store.Models[cName].CurrentModel) 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(details.ModelGeneration, gc.Equals, coremodel.GenerationCurrent) 114 115 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, 116 "generation automatically completed; target generation set to \"current\"\n") 117 } 118 119 func (s *advanceGenerationSuite) TestRunCommandFail(c *gc.C) { 120 mockController, api := setUpAdvanceMocks(c) 121 defer mockController.Finish() 122 123 api.EXPECT().AdvanceGeneration(gomock.Any(), []string{"ubuntu/0"}).Return(false, errors.Errorf("failme")) 124 125 _, err := s.runCommand(c, api, "ubuntu/0") 126 c.Assert(err, gc.ErrorMatches, "failme") 127 }