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