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

     1  // Copyright 2011, 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujutest
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cloud"
    14  	"github.com/juju/juju/environs"
    15  	"github.com/juju/juju/environs/bootstrap"
    16  	"github.com/juju/juju/environs/config"
    17  	"github.com/juju/juju/environs/filestorage"
    18  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    19  	"github.com/juju/juju/environs/storage"
    20  	envtesting "github.com/juju/juju/environs/testing"
    21  	"github.com/juju/juju/instance"
    22  	"github.com/juju/juju/juju/testing"
    23  	"github.com/juju/juju/jujuclient"
    24  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    25  	coretesting "github.com/juju/juju/testing"
    26  	jujuversion "github.com/juju/juju/version"
    27  )
    28  
    29  // Tests is a gocheck suite containing tests verifying juju functionality
    30  // against the environment with the given configuration. The
    31  // tests are not designed to be run against a live server - the Environ
    32  // is opened once for each test, and some potentially expensive operations
    33  // may be executed.
    34  type Tests struct {
    35  	TestConfig     coretesting.Attrs
    36  	Credential     cloud.Credential
    37  	CloudEndpoint  string
    38  	CloudRegion    string
    39  	ControllerUUID string
    40  	envtesting.ToolsFixture
    41  	sstesting.TestDataSuite
    42  
    43  	// ControllerStore holds the controller related informtion
    44  	// such as controllers, accounts, etc., used when preparing
    45  	// the environment. This is initialized by SetUpSuite.
    46  	ControllerStore jujuclient.ClientStore
    47  	toolsStorage    storage.Storage
    48  }
    49  
    50  // Open opens an instance of the testing environment.
    51  func (t *Tests) Open(c *gc.C, cfg *config.Config) environs.Environ {
    52  	e, err := environs.New(environs.OpenParams{
    53  		Cloud:  t.CloudSpec(),
    54  		Config: cfg,
    55  	})
    56  	c.Assert(err, gc.IsNil, gc.Commentf("opening environ %#v", cfg.AllAttrs()))
    57  	c.Assert(e, gc.NotNil)
    58  	return e
    59  }
    60  
    61  func (t *Tests) CloudSpec() environs.CloudSpec {
    62  	credential := t.Credential
    63  	if credential.AuthType() == "" {
    64  		credential = cloud.NewEmptyCredential()
    65  	}
    66  	return environs.CloudSpec{
    67  		Type:       t.TestConfig["type"].(string),
    68  		Name:       t.TestConfig["type"].(string),
    69  		Region:     t.CloudRegion,
    70  		Endpoint:   t.CloudEndpoint,
    71  		Credential: &credential,
    72  	}
    73  }
    74  
    75  // PrepareParams returns the environs.PrepareParams that will be used to call
    76  // environs.Prepare.
    77  func (t *Tests) PrepareParams(c *gc.C) bootstrap.PrepareParams {
    78  	testConfigCopy := t.TestConfig.Merge(nil)
    79  
    80  	return bootstrap.PrepareParams{
    81  		ControllerConfig: coretesting.FakeControllerConfig(),
    82  		ModelConfig:      testConfigCopy,
    83  		Cloud:            t.CloudSpec(),
    84  		ControllerName:   t.TestConfig["name"].(string),
    85  		AdminSecret:      AdminSecret,
    86  	}
    87  }
    88  
    89  // Prepare prepares an instance of the testing environment.
    90  func (t *Tests) Prepare(c *gc.C) environs.Environ {
    91  	return t.PrepareWithParams(c, t.PrepareParams(c))
    92  }
    93  
    94  // PrepareWithParams prepares an instance of the testing environment.
    95  func (t *Tests) PrepareWithParams(c *gc.C, params bootstrap.PrepareParams) environs.Environ {
    96  	e, err := bootstrap.Prepare(envtesting.BootstrapContext(c), t.ControllerStore, params)
    97  	c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", params.ModelConfig))
    98  	c.Assert(e, gc.NotNil)
    99  	return e
   100  }
   101  
   102  func (t *Tests) AssertPrepareFailsWithConfig(c *gc.C, badConfig coretesting.Attrs, errorMatches string) error {
   103  	args := t.PrepareParams(c)
   104  	args.ModelConfig = coretesting.Attrs(args.ModelConfig).Merge(badConfig)
   105  
   106  	e, err := bootstrap.Prepare(envtesting.BootstrapContext(c), t.ControllerStore, args)
   107  	c.Assert(err, gc.ErrorMatches, errorMatches)
   108  	c.Assert(e, gc.IsNil)
   109  	return err
   110  }
   111  
   112  func (t *Tests) SetUpTest(c *gc.C) {
   113  	storageDir := c.MkDir()
   114  	baseURLPath := filepath.Join(storageDir, "tools")
   115  	t.DefaultBaseURL = utils.MakeFileURL(baseURLPath)
   116  	t.ToolsFixture.SetUpTest(c)
   117  	stor, err := filestorage.NewFileStorageWriter(storageDir)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	t.UploadFakeTools(c, stor, "released", "released")
   120  	t.toolsStorage = stor
   121  	t.ControllerStore = jujuclienttesting.NewMemStore()
   122  	t.ControllerUUID = coretesting.FakeControllerConfig().ControllerUUID()
   123  }
   124  
   125  func (t *Tests) TearDownTest(c *gc.C) {
   126  	t.ToolsFixture.TearDownTest(c)
   127  }
   128  
   129  func (t *Tests) TestStartStop(c *gc.C) {
   130  	e := t.Prepare(c)
   131  	cfg, err := e.Config().Apply(map[string]interface{}{
   132  		"agent-version": jujuversion.Current.String(),
   133  	})
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	err = e.SetConfig(cfg)
   136  	c.Assert(err, jc.ErrorIsNil)
   137  
   138  	insts, err := e.Instances(nil)
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	c.Assert(insts, gc.HasLen, 0)
   141  
   142  	inst0, hc := testing.AssertStartInstance(c, e, t.ControllerUUID, "0")
   143  	c.Assert(inst0, gc.NotNil)
   144  	id0 := inst0.Id()
   145  	// Sanity check for hardware characteristics.
   146  	c.Assert(hc.Arch, gc.NotNil)
   147  	c.Assert(hc.Mem, gc.NotNil)
   148  	c.Assert(hc.CpuCores, gc.NotNil)
   149  
   150  	inst1, _ := testing.AssertStartInstance(c, e, t.ControllerUUID, "1")
   151  	c.Assert(inst1, gc.NotNil)
   152  	id1 := inst1.Id()
   153  
   154  	insts, err = e.Instances([]instance.Id{id0, id1})
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Assert(insts, gc.HasLen, 2)
   157  	c.Assert(insts[0].Id(), gc.Equals, id0)
   158  	c.Assert(insts[1].Id(), gc.Equals, id1)
   159  
   160  	// order of results is not specified
   161  	insts, err = e.AllInstances()
   162  	c.Assert(err, jc.ErrorIsNil)
   163  	c.Assert(insts, gc.HasLen, 2)
   164  	c.Assert(insts[0].Id(), gc.Not(gc.Equals), insts[1].Id())
   165  
   166  	err = e.StopInstances(inst0.Id())
   167  	c.Assert(err, jc.ErrorIsNil)
   168  
   169  	insts, err = e.Instances([]instance.Id{id0, id1})
   170  	c.Assert(err, gc.Equals, environs.ErrPartialInstances)
   171  	c.Assert(insts[0], gc.IsNil)
   172  	c.Assert(insts[1].Id(), gc.Equals, id1)
   173  
   174  	insts, err = e.AllInstances()
   175  	c.Assert(err, jc.ErrorIsNil)
   176  	c.Assert(insts[0].Id(), gc.Equals, id1)
   177  }
   178  
   179  func (t *Tests) TestBootstrap(c *gc.C) {
   180  	credential := t.Credential
   181  	if credential.AuthType() == "" {
   182  		credential = cloud.NewEmptyCredential()
   183  	}
   184  
   185  	var regions []cloud.Region
   186  	if t.CloudRegion != "" {
   187  		regions = []cloud.Region{{
   188  			Name:     t.CloudRegion,
   189  			Endpoint: t.CloudEndpoint,
   190  		}}
   191  	}
   192  
   193  	args := bootstrap.BootstrapParams{
   194  		ControllerConfig: coretesting.FakeControllerConfig(),
   195  		CloudName:        t.TestConfig["type"].(string),
   196  		Cloud: cloud.Cloud{
   197  			Type:      t.TestConfig["type"].(string),
   198  			AuthTypes: []cloud.AuthType{credential.AuthType()},
   199  			Regions:   regions,
   200  			Endpoint:  t.CloudEndpoint,
   201  		},
   202  		CloudRegion:         t.CloudRegion,
   203  		CloudCredential:     &credential,
   204  		CloudCredentialName: "credential",
   205  		AdminSecret:         AdminSecret,
   206  		CAPrivateKey:        coretesting.CAKey,
   207  	}
   208  
   209  	e := t.Prepare(c)
   210  	err := bootstrap.Bootstrap(envtesting.BootstrapContext(c), e, args)
   211  	c.Assert(err, jc.ErrorIsNil)
   212  
   213  	controllerInstances, err := e.ControllerInstances(t.ControllerUUID)
   214  	c.Assert(err, jc.ErrorIsNil)
   215  	c.Assert(controllerInstances, gc.Not(gc.HasLen), 0)
   216  
   217  	e2 := t.Open(c, e.Config())
   218  	controllerInstances2, err := e2.ControllerInstances(t.ControllerUUID)
   219  	c.Assert(err, jc.ErrorIsNil)
   220  	c.Assert(controllerInstances2, gc.Not(gc.HasLen), 0)
   221  	c.Assert(controllerInstances2, jc.SameContents, controllerInstances)
   222  
   223  	err = environs.Destroy(e2.Config().Name(), e2, t.ControllerStore)
   224  	c.Assert(err, jc.ErrorIsNil)
   225  
   226  	// Prepare again because Destroy invalidates old environments.
   227  	e3 := t.Prepare(c)
   228  
   229  	err = bootstrap.Bootstrap(envtesting.BootstrapContext(c), e3, args)
   230  	c.Assert(err, jc.ErrorIsNil)
   231  
   232  	err = environs.Destroy(e3.Config().Name(), e3, t.ControllerStore)
   233  	c.Assert(err, jc.ErrorIsNil)
   234  }