github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/jujuclient/models_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuclient_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/jujuclient" 15 "github.com/juju/juju/testing" 16 ) 17 18 type ModelsSuite struct { 19 testing.FakeJujuXDGDataHomeSuite 20 store jujuclient.ModelStore 21 } 22 23 var _ = gc.Suite(&ModelsSuite{}) 24 25 func (s *ModelsSuite) SetUpTest(c *gc.C) { 26 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 27 s.store = jujuclient.NewFileClientStore() 28 writeTestModelsFile(c) 29 } 30 31 func (s *ModelsSuite) TestModelByNameNoFile(c *gc.C) { 32 err := os.Remove(jujuclient.JujuModelsPath()) 33 c.Assert(err, jc.ErrorIsNil) 34 details, err := s.store.ModelByName("not-found", "admin/admin") 35 c.Assert(err, gc.ErrorMatches, "models for controller not-found not found") 36 c.Assert(details, gc.IsNil) 37 } 38 39 func (s *ModelsSuite) TestModelByNameControllerNotFound(c *gc.C) { 40 details, err := s.store.ModelByName("not-found", "admin/admin") 41 c.Assert(err, gc.ErrorMatches, "models for controller not-found not found") 42 c.Assert(details, gc.IsNil) 43 } 44 45 func (s *ModelsSuite) TestModelByNameModelNotFound(c *gc.C) { 46 details, err := s.store.ModelByName("kontroll", "admin/not-found") 47 c.Assert(err, gc.ErrorMatches, "model kontroll:admin/not-found not found") 48 c.Assert(details, gc.IsNil) 49 } 50 51 func (s *ModelsSuite) TestModelByName(c *gc.C) { 52 details, err := s.store.ModelByName("kontroll", "admin/admin") 53 c.Assert(err, jc.ErrorIsNil) 54 c.Assert(details, gc.NotNil) 55 c.Assert(*details, jc.DeepEquals, testControllerModels["kontroll"].Models["admin/admin"]) 56 } 57 58 func (s *ModelsSuite) TestAllModelsNoFile(c *gc.C) { 59 err := os.Remove(jujuclient.JujuModelsPath()) 60 c.Assert(err, jc.ErrorIsNil) 61 models, err := s.store.AllModels("not-found") 62 c.Assert(err, gc.ErrorMatches, "models for controller not-found not found") 63 c.Assert(models, gc.HasLen, 0) 64 } 65 66 func (s *ModelsSuite) TestAllModels(c *gc.C) { 67 models, err := s.store.AllModels("kontroll") 68 c.Assert(err, jc.ErrorIsNil) 69 c.Assert(models, jc.DeepEquals, testControllerModels["kontroll"].Models) 70 } 71 72 func (s *ModelsSuite) TestCurrentModel(c *gc.C) { 73 current, err := s.store.CurrentModel("kontroll") 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(current, gc.Equals, "admin/my-model") 76 } 77 78 func (s *ModelsSuite) TestCurrentModelNotSet(c *gc.C) { 79 _, err := s.store.CurrentModel("ctrl") 80 c.Assert(err, jc.Satisfies, errors.IsNotFound) 81 } 82 83 func (s *ModelsSuite) TestCurrentModelControllerNotFound(c *gc.C) { 84 _, err := s.store.CurrentModel("not-found") 85 c.Assert(err, jc.Satisfies, errors.IsNotFound) 86 } 87 88 func (s *ModelsSuite) TestSetCurrentModelControllerNotFound(c *gc.C) { 89 err := s.store.SetCurrentModel("not-found", "admin/admin") 90 c.Assert(err, jc.Satisfies, errors.IsNotFound) 91 } 92 93 func (s *ModelsSuite) TestSetCurrentModelModelNotFound(c *gc.C) { 94 err := s.store.SetCurrentModel("kontroll", "admin/not-found") 95 c.Assert(err, jc.Satisfies, errors.IsNotFound) 96 } 97 98 func (s *ModelsSuite) TestSetCurrentModel(c *gc.C) { 99 err := s.store.SetCurrentModel("kontroll", "admin/admin") 100 c.Assert(err, jc.ErrorIsNil) 101 all, err := jujuclient.ReadModelsFile(jujuclient.JujuModelsPath()) 102 c.Assert(err, jc.ErrorIsNil) 103 c.Assert(all["kontroll"].CurrentModel, gc.Equals, "admin/admin") 104 } 105 106 func (s *ModelsSuite) TestUpdateModelNewController(c *gc.C) { 107 testModelDetails := jujuclient.ModelDetails{"test.uuid"} 108 err := s.store.UpdateModel("new-controller", "admin/new-model", testModelDetails) 109 c.Assert(err, jc.ErrorIsNil) 110 models, err := s.store.AllModels("new-controller") 111 c.Assert(err, jc.ErrorIsNil) 112 c.Assert(models, jc.DeepEquals, map[string]jujuclient.ModelDetails{ 113 "admin/new-model": testModelDetails, 114 }) 115 } 116 117 func (s *ModelsSuite) TestUpdateModelExistingControllerAndModelNewModel(c *gc.C) { 118 testModelDetails := jujuclient.ModelDetails{"test.uuid"} 119 err := s.store.UpdateModel("kontroll", "admin/new-model", testModelDetails) 120 c.Assert(err, jc.ErrorIsNil) 121 models, err := s.store.AllModels("kontroll") 122 c.Assert(err, jc.ErrorIsNil) 123 c.Assert(models, jc.DeepEquals, map[string]jujuclient.ModelDetails{ 124 "admin/admin": kontrollAdminModelDetails, 125 "admin/my-model": kontrollMyModelModelDetails, 126 "admin/new-model": testModelDetails, 127 }) 128 } 129 130 func (s *ModelsSuite) TestUpdateModelOverwrites(c *gc.C) { 131 testModelDetails := jujuclient.ModelDetails{"test.uuid"} 132 for i := 0; i < 2; i++ { 133 // Twice so we exercise the code path of updating with 134 // identical details. 135 err := s.store.UpdateModel("kontroll", "admin/admin", testModelDetails) 136 c.Assert(err, jc.ErrorIsNil) 137 details, err := s.store.ModelByName("kontroll", "admin/admin") 138 c.Assert(err, jc.ErrorIsNil) 139 c.Assert(*details, jc.DeepEquals, testModelDetails) 140 } 141 } 142 143 func (s *ModelsSuite) TestUpdateModelEmptyModels(c *gc.C) { 144 // This test exists to exercise a bug caused by the 145 // presence of a file with an empty "models" field, 146 // that would lead to a panic. 147 err := ioutil.WriteFile(jujuclient.JujuModelsPath(), []byte(` 148 controllers: 149 ctrl: 150 models: 151 `[1:]), 0644) 152 c.Assert(err, jc.ErrorIsNil) 153 154 testModelDetails := jujuclient.ModelDetails{"test.uuid"} 155 err = s.store.UpdateModel("ctrl", "admin/admin", testModelDetails) 156 c.Assert(err, jc.ErrorIsNil) 157 models, err := s.store.AllModels("ctrl") 158 c.Assert(err, jc.ErrorIsNil) 159 c.Assert(models, jc.DeepEquals, map[string]jujuclient.ModelDetails{ 160 "admin/admin": testModelDetails, 161 }) 162 } 163 164 func (s *ModelsSuite) TestRemoveModelNoFile(c *gc.C) { 165 err := os.Remove(jujuclient.JujuModelsPath()) 166 c.Assert(err, jc.ErrorIsNil) 167 err = s.store.RemoveModel("not-found", "admin/admin") 168 c.Assert(err, jc.Satisfies, errors.IsNotFound) 169 } 170 171 func (s *ModelsSuite) TestRemoveModelControllerNotFound(c *gc.C) { 172 err := s.store.RemoveModel("not-found", "admin/admin") 173 c.Assert(err, jc.Satisfies, errors.IsNotFound) 174 } 175 176 func (s *ModelsSuite) TestRemoveModelNotFound(c *gc.C) { 177 err := s.store.RemoveModel("kontroll", "admin/not-found") 178 c.Assert(err, jc.Satisfies, errors.IsNotFound) 179 } 180 181 func (s *ModelsSuite) TestRemoveModel(c *gc.C) { 182 err := s.store.RemoveModel("kontroll", "admin/admin") 183 c.Assert(err, jc.ErrorIsNil) 184 _, err = s.store.ModelByName("kontroll", "admin/admin") 185 c.Assert(err, jc.Satisfies, errors.IsNotFound) 186 } 187 188 func (s *ModelsSuite) TestRemoveControllerRemovesModels(c *gc.C) { 189 store := jujuclient.NewFileClientStore() 190 err := store.AddController("kontroll", jujuclient.ControllerDetails{ 191 ControllerUUID: "abc", 192 CACert: "woop", 193 }) 194 c.Assert(err, jc.ErrorIsNil) 195 err = store.RemoveController("kontroll") 196 c.Assert(err, jc.ErrorIsNil) 197 198 models, err := jujuclient.ReadModelsFile(jujuclient.JujuModelsPath()) 199 c.Assert(err, jc.ErrorIsNil) 200 _, ok := models["admin/kontroll"] 201 c.Assert(ok, jc.IsFalse) // kontroll models are removed 202 }