github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/open_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environs_test 5 6 import ( 7 stdcontext "context" 8 9 "github.com/juju/errors" 10 gitjujutesting "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/utils/v3" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/core/model" 16 "github.com/juju/juju/environs" 17 "github.com/juju/juju/environs/bootstrap" 18 environscloudspec "github.com/juju/juju/environs/cloudspec" 19 "github.com/juju/juju/environs/config" 20 "github.com/juju/juju/environs/context" 21 "github.com/juju/juju/environs/filestorage" 22 sstesting "github.com/juju/juju/environs/simplestreams/testing" 23 envtesting "github.com/juju/juju/environs/testing" 24 envtools "github.com/juju/juju/environs/tools" 25 "github.com/juju/juju/juju/keys" 26 "github.com/juju/juju/jujuclient" 27 "github.com/juju/juju/provider/dummy" 28 "github.com/juju/juju/testing" 29 jujuversion "github.com/juju/juju/version" 30 ) 31 32 type OpenSuite struct { 33 testing.FakeJujuXDGDataHomeSuite 34 envtesting.ToolsFixture 35 } 36 37 var _ = gc.Suite(&OpenSuite{}) 38 39 func (s *OpenSuite) SetUpTest(c *gc.C) { 40 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 41 s.ToolsFixture.SetUpTest(c) 42 s.PatchValue(&keys.JujuPublicKey, sstesting.SignedMetadataPublicKey) 43 } 44 45 func (s *OpenSuite) TearDownTest(c *gc.C) { 46 dummy.Reset(c) 47 s.ToolsFixture.TearDownTest(c) 48 s.FakeJujuXDGDataHomeSuite.TearDownTest(c) 49 } 50 51 func (s *OpenSuite) TestNewDummyEnviron(c *gc.C) { 52 s.PatchValue(&jujuversion.Current, testing.FakeVersionNumber) 53 // matches *Settings.Map() 54 cfg, err := config.New(config.NoDefaults, dummySampleConfig()) 55 c.Assert(err, jc.ErrorIsNil) 56 ctx := envtesting.BootstrapContext(stdcontext.TODO(), c) 57 cache := jujuclient.NewMemStore() 58 controllerCfg := testing.FakeControllerConfig() 59 bootstrapEnviron, err := bootstrap.PrepareController(false, ctx, cache, bootstrap.PrepareParams{ 60 ControllerConfig: controllerCfg, 61 ControllerName: cfg.Name(), 62 ModelConfig: cfg.AllAttrs(), 63 Cloud: dummy.SampleCloudSpec(), 64 AdminSecret: "admin-secret", 65 }) 66 c.Assert(err, jc.ErrorIsNil) 67 env := bootstrapEnviron.(environs.Environ) 68 69 storageDir := c.MkDir() 70 s.PatchValue(&envtools.DefaultBaseURL, storageDir) 71 stor, err := filestorage.NewFileStorageWriter(storageDir) 72 c.Assert(err, jc.ErrorIsNil) 73 envtesting.UploadFakeTools(c, stor, cfg.AgentStream(), cfg.AgentStream()) 74 err = bootstrap.Bootstrap(ctx, env, context.NewEmptyCloudCallContext(), bootstrap.BootstrapParams{ 75 ControllerConfig: controllerCfg, 76 AdminSecret: "admin-secret", 77 CAPrivateKey: testing.CAKey, 78 SupportedBootstrapBases: testing.FakeSupportedJujuBases, 79 }) 80 c.Assert(err, jc.ErrorIsNil) 81 82 // New controller should have been added to collection. 83 foundController, err := cache.ControllerByName(cfg.Name()) 84 c.Assert(err, jc.ErrorIsNil) 85 c.Assert(foundController.ControllerUUID, gc.DeepEquals, controllerCfg.ControllerUUID()) 86 } 87 88 func (s *OpenSuite) TestUpdateEnvInfo(c *gc.C) { 89 store := jujuclient.NewMemStore() 90 ctx := envtesting.BootstrapContext(stdcontext.TODO(), c) 91 uuid := utils.MustNewUUID().String() 92 cfg, err := config.New(config.UseDefaults, map[string]interface{}{ 93 "type": "dummy", 94 "name": "admin-model", 95 "uuid": uuid, 96 }) 97 c.Assert(err, jc.ErrorIsNil) 98 controllerCfg := testing.FakeControllerConfig() 99 _, err = bootstrap.PrepareController(false, ctx, store, bootstrap.PrepareParams{ 100 ControllerConfig: controllerCfg, 101 ControllerName: "controller-name", 102 ModelConfig: cfg.AllAttrs(), 103 Cloud: dummy.SampleCloudSpec(), 104 AdminSecret: "admin-secret", 105 }) 106 c.Assert(err, jc.ErrorIsNil) 107 108 foundController, err := store.ControllerByName("controller-name") 109 c.Assert(err, jc.ErrorIsNil) 110 c.Assert(foundController.ControllerUUID, gc.Not(gc.Equals), "") 111 c.Assert(foundController.CACert, gc.Not(gc.Equals), "") 112 foundModel, err := store.ModelByName("controller-name", "admin/admin-model") 113 c.Assert(err, jc.ErrorIsNil) 114 c.Assert(foundModel, jc.DeepEquals, &jujuclient.ModelDetails{ 115 ModelUUID: cfg.UUID(), 116 ModelType: model.IAAS, 117 }) 118 } 119 120 func (*OpenSuite) TestNewUnknownEnviron(c *gc.C) { 121 env, err := environs.New(stdcontext.TODO(), environs.OpenParams{ 122 Cloud: environscloudspec.CloudSpec{ 123 Type: "wondercloud", 124 }, 125 }) 126 c.Assert(err, gc.ErrorMatches, "no registered provider for.*") 127 c.Assert(env, gc.IsNil) 128 } 129 130 func (*OpenSuite) TestNew(c *gc.C) { 131 cfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge( 132 testing.Attrs{ 133 "controller": false, 134 "name": "erewhemos", 135 }, 136 )) 137 c.Assert(err, jc.ErrorIsNil) 138 e, err := environs.New(stdcontext.TODO(), environs.OpenParams{ 139 Cloud: dummy.SampleCloudSpec(), 140 Config: cfg, 141 }) 142 c.Assert(err, jc.ErrorIsNil) 143 _, err = e.ControllerInstances(context.NewEmptyCloudCallContext(), "uuid") 144 c.Assert(err, gc.ErrorMatches, "model is not prepared") 145 } 146 147 func (*OpenSuite) TestDestroy(c *gc.C) { 148 cfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge( 149 testing.Attrs{ 150 "name": "erewhemos", 151 }, 152 )) 153 c.Assert(err, jc.ErrorIsNil) 154 155 store := jujuclient.NewMemStore() 156 // Prepare the environment and sanity-check that 157 // the config storage info has been made. 158 controllerCfg := testing.FakeControllerConfig() 159 ctx := envtesting.BootstrapContext(stdcontext.TODO(), c) 160 bootstrapEnviron, err := bootstrap.PrepareController(false, ctx, store, bootstrap.PrepareParams{ 161 ControllerConfig: controllerCfg, 162 ControllerName: "controller-name", 163 ModelConfig: cfg.AllAttrs(), 164 Cloud: dummy.SampleCloudSpec(), 165 AdminSecret: "admin-secret", 166 }) 167 c.Assert(err, jc.ErrorIsNil) 168 e := bootstrapEnviron.(environs.Environ) 169 _, err = store.ControllerByName("controller-name") 170 c.Assert(err, jc.ErrorIsNil) 171 172 callCtx := context.NewEmptyCloudCallContext() 173 err = environs.Destroy("controller-name", e, callCtx, store) 174 c.Assert(err, jc.ErrorIsNil) 175 176 // Check that the environment has actually been destroyed 177 // and that the controller details been removed too. 178 _, err = e.ControllerInstances(callCtx, controllerCfg.ControllerUUID()) 179 c.Assert(err, gc.ErrorMatches, "model is not prepared") 180 _, err = store.ControllerByName("controller-name") 181 c.Assert(err, jc.Satisfies, errors.IsNotFound) 182 } 183 184 func (*OpenSuite) TestDestroyNotFound(c *gc.C) { 185 var env destroyControllerEnv 186 store := jujuclient.NewMemStore() 187 err := environs.Destroy("fnord", &env, context.NewEmptyCloudCallContext(), store) 188 c.Assert(err, jc.ErrorIsNil) 189 env.CheckCallNames(c) // no controller details, no call 190 } 191 192 type destroyControllerEnv struct { 193 environs.Environ 194 gitjujutesting.Stub 195 } 196 197 func (e *destroyControllerEnv) DestroyController(ctx context.ProviderCallContext, uuid string) error { 198 e.MethodCall(e, "DestroyController", ctx, uuid) 199 return e.NextErr() 200 }