github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/testing/environ.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/juju/names"
    11  	gitjujutesting "github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/utils"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/environs/config"
    17  	"github.com/juju/juju/juju/osenv"
    18  	"github.com/juju/juju/utils/ssh"
    19  )
    20  
    21  // FakeAuthKeys holds the authorized key used for testing
    22  // purposes in FakeConfig. It is valid for parsing with the utils/ssh
    23  // authorized-key utilities.
    24  const FakeAuthKeys = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQDP8fPSAMFm2PQGoVUks/FENVUMww1QTK6m++Y2qX9NGHm43kwEzxfoWR77wo6fhBhgFHsQ6ogE/cYLx77hOvjTchMEP74EVxSce0qtDjI7SwYbOpAButRId3g/Ef4STz8= joe@0.1.2.4`
    25  
    26  func init() {
    27  	_, err := ssh.ParseAuthorisedKey(FakeAuthKeys)
    28  	if err != nil {
    29  		panic("FakeAuthKeys does not hold a valid authorized key: " + err.Error())
    30  	}
    31  }
    32  
    33  const FakeDefaultSeries = "trusty"
    34  
    35  // EnvironmentTag is a defined known valid UUID that can be used in testing.
    36  var EnvironmentTag = names.NewEnvironTag("deadbeef-0bad-f00d-0000-4b1d0d06f00d")
    37  
    38  // FakeConfig() returns an environment configuration for a
    39  // fake provider with all required attributes set.
    40  func FakeConfig() Attrs {
    41  	return Attrs{
    42  		"type":                      "someprovider",
    43  		"name":                      "testenv",
    44  		"uuid":                      EnvironmentTag.Id(),
    45  		"authorized-keys":           FakeAuthKeys,
    46  		"firewall-mode":             config.FwInstance,
    47  		"admin-secret":              "fish",
    48  		"ca-cert":                   CACert,
    49  		"ca-private-key":            CAKey,
    50  		"ssl-hostname-verification": true,
    51  		"development":               false,
    52  		"state-port":                19034,
    53  		"api-port":                  17777,
    54  		"default-series":            FakeDefaultSeries,
    55  	}
    56  }
    57  
    58  // EnvironConfig returns a default environment configuration suitable for
    59  // setting in the state.
    60  func EnvironConfig(c *gc.C) *config.Config {
    61  	return CustomEnvironConfig(c, Attrs{"uuid": mustUUID()})
    62  }
    63  
    64  // mustUUID returns a stringified uuid or panics
    65  func mustUUID() string {
    66  	uuid, err := utils.NewUUID()
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	return uuid.String()
    71  }
    72  
    73  // CustomEnvironConfig returns an environment configuration with
    74  // additional specified keys added.
    75  func CustomEnvironConfig(c *gc.C, extra Attrs) *config.Config {
    76  	attrs := FakeConfig().Merge(Attrs{
    77  		"agent-version": "1.2.3",
    78  	}).Merge(extra).Delete("admin-secret", "ca-private-key")
    79  	cfg, err := config.New(config.NoDefaults, attrs)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	return cfg
    82  }
    83  
    84  const (
    85  	SampleEnvName = "erewhemos"
    86  	EnvDefault    = "default:\n  " + SampleEnvName + "\n"
    87  )
    88  
    89  const DefaultMongoPassword = "conn-from-name-secret"
    90  
    91  // Environment names below are explicit as it makes them more readable.
    92  const SingleEnvConfigNoDefault = `
    93  environments:
    94      erewhemos:
    95          type: dummy
    96          state-server: true
    97          authorized-keys: i-am-a-key
    98          admin-secret: ` + DefaultMongoPassword + `
    99  `
   100  
   101  const SingleEnvConfig = EnvDefault + SingleEnvConfigNoDefault
   102  
   103  const MultipleEnvConfigNoDefault = `
   104  environments:
   105      erewhemos:
   106          type: dummy
   107          state-server: true
   108          authorized-keys: i-am-a-key
   109          admin-secret: ` + DefaultMongoPassword + `
   110      erewhemos-2:
   111          type: dummy
   112          state-server: true
   113          authorized-keys: i-am-a-key
   114          admin-secret: ` + DefaultMongoPassword + `
   115  `
   116  
   117  const MultipleEnvConfig = EnvDefault + MultipleEnvConfigNoDefault
   118  
   119  const SampleCertName = "erewhemos"
   120  
   121  // FakeJujuHomeSuite isolates the user's home directory and
   122  // sets up a Juju home with a sample environment and certificate.
   123  type FakeJujuHomeSuite struct {
   124  	JujuOSEnvSuite
   125  	gitjujutesting.FakeHomeSuite
   126  	oldJujuHome string
   127  }
   128  
   129  func (s *FakeJujuHomeSuite) SetUpSuite(c *gc.C) {
   130  	s.JujuOSEnvSuite.SetUpTest(c)
   131  	s.FakeHomeSuite.SetUpTest(c)
   132  }
   133  
   134  func (s *FakeJujuHomeSuite) TearDownSuite(c *gc.C) {
   135  	s.FakeHomeSuite.SetUpTest(c)
   136  	s.JujuOSEnvSuite.SetUpTest(c)
   137  }
   138  
   139  func (s *FakeJujuHomeSuite) SetUpTest(c *gc.C) {
   140  	s.JujuOSEnvSuite.SetUpTest(c)
   141  	s.FakeHomeSuite.SetUpTest(c)
   142  	jujuHome := gitjujutesting.HomePath(".juju")
   143  	err := os.Mkdir(jujuHome, 0700)
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	s.oldJujuHome = osenv.SetJujuHome(jujuHome)
   146  	WriteEnvironments(c, SingleEnvConfig, SampleCertName)
   147  }
   148  
   149  func (s *FakeJujuHomeSuite) TearDownTest(c *gc.C) {
   150  	osenv.SetJujuHome(s.oldJujuHome)
   151  	s.FakeHomeSuite.TearDownTest(c)
   152  	s.JujuOSEnvSuite.TearDownTest(c)
   153  }
   154  
   155  // AssertConfigParameterUpdated updates environment parameter and
   156  // asserts that no errors were encountered.
   157  func (s *FakeJujuHomeSuite) AssertConfigParameterUpdated(c *gc.C, key, value string) {
   158  	s.PatchEnvironment(key, value)
   159  }
   160  
   161  // MakeSampleJujuHome sets up a sample Juju environment.
   162  func MakeSampleJujuHome(c *gc.C) {
   163  	WriteEnvironments(c, SingleEnvConfig, SampleCertName)
   164  }
   165  
   166  // WriteEnvironments creates an environments file with envConfig and certs
   167  // from certNames.
   168  func WriteEnvironments(c *gc.C, envConfig string, certNames ...string) {
   169  	envs := osenv.JujuHomePath("environments.yaml")
   170  	err := ioutil.WriteFile(envs, []byte(envConfig), 0644)
   171  	c.Assert(err, jc.ErrorIsNil)
   172  	for _, name := range certNames {
   173  		err := ioutil.WriteFile(osenv.JujuHomePath(name+"-cert.pem"), []byte(CACert), 0600)
   174  		c.Assert(err, jc.ErrorIsNil)
   175  		err = ioutil.WriteFile(osenv.JujuHomePath(name+"-private-key.pem"), []byte(CAKey), 0600)
   176  		c.Assert(err, jc.ErrorIsNil)
   177  	}
   178  }