github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/bootstrap/prepare_test.go (about)

     1  // Copyright 2012, 2013, 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package bootstrap_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/controller"
    12  	"github.com/juju/juju/environs/bootstrap"
    13  	"github.com/juju/juju/environs/config"
    14  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    15  	envtesting "github.com/juju/juju/environs/testing"
    16  	"github.com/juju/juju/juju/keys"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    19  	"github.com/juju/juju/provider/dummy"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  type PrepareSuite struct {
    24  	testing.FakeJujuXDGDataHomeSuite
    25  	envtesting.ToolsFixture
    26  }
    27  
    28  var _ = gc.Suite(&PrepareSuite{})
    29  
    30  func (s *PrepareSuite) SetUpTest(c *gc.C) {
    31  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    32  	s.ToolsFixture.SetUpTest(c)
    33  	s.PatchValue(&keys.JujuPublicKey, sstesting.SignedMetadataPublicKey)
    34  }
    35  
    36  func (s *PrepareSuite) TearDownTest(c *gc.C) {
    37  	dummy.Reset(c)
    38  	s.ToolsFixture.TearDownTest(c)
    39  	s.FakeJujuXDGDataHomeSuite.TearDownTest(c)
    40  }
    41  
    42  func (*PrepareSuite) TestPrepare(c *gc.C) {
    43  	baselineAttrs := dummy.SampleConfig().Merge(testing.Attrs{
    44  		"controller": false,
    45  		"name":       "erewhemos",
    46  		"test-mode":  true,
    47  	}).Delete(
    48  		"admin-secret",
    49  	)
    50  	cfg, err := config.New(config.NoDefaults, baselineAttrs)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	controllerStore := jujuclienttesting.NewMemStore()
    53  	ctx := envtesting.BootstrapContext(c)
    54  	controllerCfg := controller.Config{
    55  		controller.ControllerUUIDKey:       testing.ControllerTag.Id(),
    56  		controller.CACertKey:               testing.CACert,
    57  		controller.APIPort:                 17777,
    58  		controller.StatePort:               1234,
    59  		controller.SetNUMAControlPolicyKey: true,
    60  	}
    61  	_, err = bootstrap.Prepare(ctx, controllerStore, bootstrap.PrepareParams{
    62  		ControllerConfig: controllerCfg,
    63  		ControllerName:   cfg.Name(),
    64  		ModelConfig:      cfg.AllAttrs(),
    65  		Cloud:            dummy.SampleCloudSpec(),
    66  		AdminSecret:      "admin-secret",
    67  	})
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	// Check that controller was cached
    71  	foundController, err := controllerStore.ControllerByName(cfg.Name())
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	c.Assert(foundController.ControllerUUID, gc.DeepEquals, controllerCfg.ControllerUUID())
    74  	c.Assert(foundController.Cloud, gc.Equals, "dummy")
    75  
    76  	// Check that bootstrap config was written
    77  	bootstrapCfg, err := controllerStore.BootstrapConfigForController(cfg.Name())
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(bootstrapCfg, jc.DeepEquals, &jujuclient.BootstrapConfig{
    80  		ControllerConfig: controller.Config{
    81  			controller.APIPort:                 17777,
    82  			controller.StatePort:               1234,
    83  			controller.SetNUMAControlPolicyKey: true,
    84  		},
    85  		Config: map[string]interface{}{
    86  			"default-series":            "xenial",
    87  			"firewall-mode":             "instance",
    88  			"ssl-hostname-verification": true,
    89  			"logging-config":            "<root>=DEBUG;unit=DEBUG",
    90  			"secret":                    "pork",
    91  			"authorized-keys":           testing.FakeAuthKeys,
    92  			"type":                      "dummy",
    93  			"name":                      "erewhemos",
    94  			"controller":                false,
    95  			"development":               false,
    96  			"test-mode":                 true,
    97  		},
    98  		ControllerModelUUID:   cfg.UUID(),
    99  		Cloud:                 "dummy",
   100  		CloudRegion:           "dummy-region",
   101  		CloudType:             "dummy",
   102  		CloudEndpoint:         "dummy-endpoint",
   103  		CloudIdentityEndpoint: "dummy-identity-endpoint",
   104  		CloudStorageEndpoint:  "dummy-storage-endpoint",
   105  	})
   106  
   107  	// Check we cannot call Prepare again.
   108  	_, err = bootstrap.Prepare(ctx, controllerStore, bootstrap.PrepareParams{
   109  		ControllerConfig: controllerCfg,
   110  		ControllerName:   cfg.Name(),
   111  		ModelConfig:      cfg.AllAttrs(),
   112  		Cloud:            dummy.SampleCloudSpec(),
   113  		AdminSecret:      "admin-secret",
   114  	})
   115  	c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
   116  	c.Assert(err, gc.ErrorMatches, `controller "erewhemos" already exists`)
   117  }