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