github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/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  	envtesting.ToolsFixture
    40  	sstesting.TestDataSuite
    41  
    42  	// ControllerStore holds the controller related informtion
    43  	// such as controllers, accounts, etc., used when preparing
    44  	// the environment. This is initialized by SetUpSuite.
    45  	ControllerStore jujuclient.ClientStore
    46  	toolsStorage    storage.Storage
    47  }
    48  
    49  // Open opens an instance of the testing environment.
    50  func (t *Tests) Open(c *gc.C, cfg *config.Config) environs.Environ {
    51  	e, err := environs.New(cfg)
    52  	c.Assert(err, gc.IsNil, gc.Commentf("opening environ %#v", cfg.AllAttrs()))
    53  	c.Assert(e, gc.NotNil)
    54  	return e
    55  }
    56  
    57  // Prepare prepares an instance of the testing environment.
    58  func (t *Tests) Prepare(c *gc.C) environs.Environ {
    59  	credential := t.Credential
    60  	if credential.AuthType() == "" {
    61  		credential = cloud.NewEmptyCredential()
    62  	}
    63  	args := environs.PrepareParams{
    64  		BaseConfig:     t.TestConfig,
    65  		Credential:     credential,
    66  		ControllerName: t.TestConfig["name"].(string),
    67  		CloudName:      t.TestConfig["type"].(string),
    68  		CloudEndpoint:  t.CloudEndpoint,
    69  		CloudRegion:    t.CloudRegion,
    70  	}
    71  	e, err := environs.Prepare(envtesting.BootstrapContext(c), t.ControllerStore, args)
    72  	c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", t.TestConfig))
    73  	c.Assert(e, gc.NotNil)
    74  	return e
    75  }
    76  
    77  func (t *Tests) SetUpTest(c *gc.C) {
    78  	storageDir := c.MkDir()
    79  	baseUrlPath := filepath.Join(storageDir, "tools")
    80  	t.DefaultBaseURL = utils.MakeFileURL(baseUrlPath)
    81  	t.ToolsFixture.SetUpTest(c)
    82  	stor, err := filestorage.NewFileStorageWriter(storageDir)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	t.UploadFakeTools(c, stor, "released", "released")
    85  	t.toolsStorage = stor
    86  	t.ControllerStore = jujuclienttesting.NewMemStore()
    87  }
    88  
    89  func (t *Tests) TearDownTest(c *gc.C) {
    90  	t.ToolsFixture.TearDownTest(c)
    91  }
    92  
    93  func (t *Tests) TestStartStop(c *gc.C) {
    94  	e := t.Prepare(c)
    95  	cfg, err := e.Config().Apply(map[string]interface{}{
    96  		"agent-version": jujuversion.Current.String(),
    97  	})
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	err = e.SetConfig(cfg)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	insts, err := e.Instances(nil)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(insts, gc.HasLen, 0)
   105  
   106  	inst0, hc := testing.AssertStartInstance(c, e, "0")
   107  	c.Assert(inst0, gc.NotNil)
   108  	id0 := inst0.Id()
   109  	// Sanity check for hardware characteristics.
   110  	c.Assert(hc.Arch, gc.NotNil)
   111  	c.Assert(hc.Mem, gc.NotNil)
   112  	c.Assert(hc.CpuCores, gc.NotNil)
   113  
   114  	inst1, _ := testing.AssertStartInstance(c, e, "1")
   115  	c.Assert(inst1, gc.NotNil)
   116  	id1 := inst1.Id()
   117  
   118  	insts, err = e.Instances([]instance.Id{id0, id1})
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(insts, gc.HasLen, 2)
   121  	c.Assert(insts[0].Id(), gc.Equals, id0)
   122  	c.Assert(insts[1].Id(), gc.Equals, id1)
   123  
   124  	// order of results is not specified
   125  	insts, err = e.AllInstances()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(insts, gc.HasLen, 2)
   128  	c.Assert(insts[0].Id(), gc.Not(gc.Equals), insts[1].Id())
   129  
   130  	err = e.StopInstances(inst0.Id())
   131  	c.Assert(err, jc.ErrorIsNil)
   132  
   133  	insts, err = e.Instances([]instance.Id{id0, id1})
   134  	c.Assert(err, gc.Equals, environs.ErrPartialInstances)
   135  	c.Assert(insts[0], gc.IsNil)
   136  	c.Assert(insts[1].Id(), gc.Equals, id1)
   137  
   138  	insts, err = e.AllInstances()
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	c.Assert(insts[0].Id(), gc.Equals, id1)
   141  }
   142  
   143  func (t *Tests) TestBootstrap(c *gc.C) {
   144  	e := t.Prepare(c)
   145  	err := bootstrap.Bootstrap(envtesting.BootstrapContext(c), e, bootstrap.BootstrapParams{})
   146  	c.Assert(err, jc.ErrorIsNil)
   147  
   148  	controllerInstances, err := e.ControllerInstances()
   149  	c.Assert(err, jc.ErrorIsNil)
   150  	c.Assert(controllerInstances, gc.Not(gc.HasLen), 0)
   151  
   152  	e2 := t.Open(c, e.Config())
   153  	controllerInstances2, err := e2.ControllerInstances()
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	c.Assert(controllerInstances2, gc.Not(gc.HasLen), 0)
   156  	c.Assert(controllerInstances2, jc.SameContents, controllerInstances)
   157  
   158  	err = environs.Destroy(e2.Config().Name(), e2, t.ControllerStore)
   159  	c.Assert(err, jc.ErrorIsNil)
   160  
   161  	// Prepare again because Destroy invalidates old environments.
   162  	e3 := t.Prepare(c)
   163  
   164  	err = bootstrap.Bootstrap(envtesting.BootstrapContext(c), e3, bootstrap.BootstrapParams{})
   165  	c.Assert(err, jc.ErrorIsNil)
   166  
   167  	err = environs.Destroy(e3.Config().Name(), e3, t.ControllerStore)
   168  	c.Assert(err, jc.ErrorIsNil)
   169  }