github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/migrations/externalcontrollers_test.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migrations 5 6 import ( 7 "github.com/juju/description/v5" 8 "github.com/juju/errors" 9 "github.com/juju/names/v5" 10 jc "github.com/juju/testing/checkers" 11 "go.uber.org/mock/gomock" 12 gc "gopkg.in/check.v1" 13 ) 14 15 type ExternalControllersExportSuite struct{} 16 17 var _ = gc.Suite(&ExternalControllersExportSuite{}) 18 19 func (s *ExternalControllersExportSuite) TestExportExternalController(c *gc.C) { 20 ctrl := gomock.NewController(c) 21 defer ctrl.Finish() 22 23 entities := []MigrationRemoteApplication{ 24 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 25 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 26 }), 27 } 28 29 extCtrlModel := s.migrationExternalController(ctrl, func(expect *MockMigrationExternalControllerMockRecorder) { 30 expect.ID().Return("f47ac10b-58cc-4372-a567-0e02b2c3d479").Times(2) 31 expect.Addrs().Return([]string{"10.0.0.1/24"}) 32 expect.Alias().Return("magic") 33 expect.CACert().Return("magic-ca-cert") 34 expect.Models().Return([]string{"xxxx-yyyy-zzzz"}) 35 }) 36 37 externalController := NewMockExternalController(ctrl) 38 39 source := NewMockExternalControllerSource(ctrl) 40 source.EXPECT().AllRemoteApplications().Return(entities, nil) 41 source.EXPECT().ControllerForModel("uuid-2").Return(extCtrlModel, nil) 42 43 model := NewMockExternalControllerModel(ctrl) 44 model.EXPECT().AddExternalController(description.ExternalControllerArgs{ 45 Tag: names.NewControllerTag("f47ac10b-58cc-4372-a567-0e02b2c3d479"), 46 Addrs: []string{"10.0.0.1/24"}, 47 Alias: "magic", 48 CACert: "magic-ca-cert", 49 Models: []string{"xxxx-yyyy-zzzz"}, 50 }).Return(externalController) 51 52 migration := ExportExternalControllers{} 53 err := migration.Execute(source, model) 54 c.Assert(err, jc.ErrorIsNil) 55 } 56 57 func (s *ExternalControllersExportSuite) TestExportExternalControllerRequestsExternalControllerOnceWithSameUUID(c *gc.C) { 58 ctrl := gomock.NewController(c) 59 defer ctrl.Finish() 60 61 entities := []MigrationRemoteApplication{ 62 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 63 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 64 }), 65 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 66 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 67 }), 68 } 69 70 extCtrlModel := s.migrationExternalController(ctrl, func(expect *MockMigrationExternalControllerMockRecorder) { 71 expect.ID().Return("f47ac10b-58cc-4372-a567-0e02b2c3d479").Times(2) 72 expect.Addrs().Return([]string{"10.0.0.1/24"}) 73 expect.Alias().Return("magic") 74 expect.CACert().Return("magic-ca-cert") 75 expect.Models().Return([]string{"xxxx-yyyy-zzzz"}) 76 }) 77 78 externalController := NewMockExternalController(ctrl) 79 80 source := NewMockExternalControllerSource(ctrl) 81 source.EXPECT().AllRemoteApplications().Return(entities, nil) 82 source.EXPECT().ControllerForModel("uuid-2").Return(extCtrlModel, nil) 83 84 model := NewMockExternalControllerModel(ctrl) 85 model.EXPECT().AddExternalController(description.ExternalControllerArgs{ 86 Tag: names.NewControllerTag("f47ac10b-58cc-4372-a567-0e02b2c3d479"), 87 Addrs: []string{"10.0.0.1/24"}, 88 Alias: "magic", 89 CACert: "magic-ca-cert", 90 Models: []string{"xxxx-yyyy-zzzz"}, 91 }).Return(externalController) 92 93 migration := ExportExternalControllers{} 94 err := migration.Execute(source, model) 95 c.Assert(err, jc.ErrorIsNil) 96 } 97 98 func (s *ExternalControllersExportSuite) TestExportExternalControllerRequestsExternalControllerOnceWithSameController(c *gc.C) { 99 ctrl := gomock.NewController(c) 100 defer ctrl.Finish() 101 102 entities := []MigrationRemoteApplication{ 103 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 104 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 105 }), 106 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 107 expect.SourceModel().Return(names.NewModelTag("uuid-3")) 108 }), 109 } 110 111 extCtrlModel := s.migrationExternalController(ctrl, func(expect *MockMigrationExternalControllerMockRecorder) { 112 expect.ID().Return("f47ac10b-58cc-4372-a567-0e02b2c3d479").Times(3) 113 expect.Addrs().Return([]string{"10.0.0.1/24"}) 114 expect.Alias().Return("magic") 115 expect.CACert().Return("magic-ca-cert") 116 expect.Models().Return([]string{"xxxx-yyyy-zzzz"}) 117 }) 118 119 externalController := NewMockExternalController(ctrl) 120 121 source := NewMockExternalControllerSource(ctrl) 122 source.EXPECT().AllRemoteApplications().Return(entities, nil) 123 source.EXPECT().ControllerForModel("uuid-2").Return(extCtrlModel, nil) 124 source.EXPECT().ControllerForModel("uuid-3").Return(extCtrlModel, nil) 125 126 model := NewMockExternalControllerModel(ctrl) 127 model.EXPECT().AddExternalController(description.ExternalControllerArgs{ 128 Tag: names.NewControllerTag("f47ac10b-58cc-4372-a567-0e02b2c3d479"), 129 Addrs: []string{"10.0.0.1/24"}, 130 Alias: "magic", 131 CACert: "magic-ca-cert", 132 Models: []string{"xxxx-yyyy-zzzz"}, 133 }).Return(externalController) 134 135 migration := ExportExternalControllers{} 136 err := migration.Execute(source, model) 137 c.Assert(err, jc.ErrorIsNil) 138 } 139 140 func (s *ExternalControllersExportSuite) TestExportExternalControllerWithNoControllerNotFound(c *gc.C) { 141 ctrl := gomock.NewController(c) 142 defer ctrl.Finish() 143 144 entities := []MigrationRemoteApplication{ 145 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 146 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 147 }), 148 } 149 150 source := NewMockExternalControllerSource(ctrl) 151 source.EXPECT().AllRemoteApplications().Return(entities, nil) 152 source.EXPECT().ControllerForModel("uuid-2").Return(nil, errors.NotFoundf("not found")) 153 154 model := NewMockExternalControllerModel(ctrl) 155 156 migration := ExportExternalControllers{} 157 err := migration.Execute(source, model) 158 c.Assert(err, jc.ErrorIsNil) 159 } 160 161 func (s *ExternalControllersExportSuite) TestExportExternalControllerFailsGettingRemoteApplicationEntities(c *gc.C) { 162 ctrl := gomock.NewController(c) 163 defer ctrl.Finish() 164 165 source := NewMockExternalControllerSource(ctrl) 166 source.EXPECT().AllRemoteApplications().Return(nil, errors.New("fail")) 167 168 model := NewMockExternalControllerModel(ctrl) 169 170 migration := ExportExternalControllers{} 171 err := migration.Execute(source, model) 172 c.Assert(err, gc.ErrorMatches, "fail") 173 } 174 175 func (s *ExternalControllersExportSuite) TestExportExternalControllerFailsGettingExternalControllerEntities(c *gc.C) { 176 ctrl := gomock.NewController(c) 177 defer ctrl.Finish() 178 179 entities := []MigrationRemoteApplication{ 180 s.migrationRemoteApplication(ctrl, func(expect *MockMigrationRemoteApplicationMockRecorder) { 181 expect.SourceModel().Return(names.NewModelTag("uuid-2")) 182 }), 183 } 184 185 source := NewMockExternalControllerSource(ctrl) 186 source.EXPECT().AllRemoteApplications().Return(entities, nil) 187 source.EXPECT().ControllerForModel("uuid-2").Return(nil, errors.New("fail")) 188 189 model := NewMockExternalControllerModel(ctrl) 190 191 migration := ExportExternalControllers{} 192 err := migration.Execute(source, model) 193 c.Assert(err, gc.ErrorMatches, "fail") 194 } 195 196 func (s *ExternalControllersExportSuite) migrationExternalController(ctrl *gomock.Controller, fn func(expect *MockMigrationExternalControllerMockRecorder)) *MockMigrationExternalController { 197 entity := NewMockMigrationExternalController(ctrl) 198 fn(entity.EXPECT()) 199 return entity 200 } 201 202 func (s *ExternalControllersExportSuite) migrationRemoteApplication(ctrl *gomock.Controller, fn func(expect *MockMigrationRemoteApplicationMockRecorder)) *MockMigrationRemoteApplication { 203 entity := NewMockMigrationRemoteApplication(ctrl) 204 fn(entity.EXPECT()) 205 return entity 206 }