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