github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/core/instance"
    15  	"github.com/juju/juju/environs"
    16  	"github.com/juju/juju/environs/bootstrap"
    17  	"github.com/juju/juju/environs/config"
    18  	"github.com/juju/juju/environs/context"
    19  	"github.com/juju/juju/environs/filestorage"
    20  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    21  	"github.com/juju/juju/environs/storage"
    22  	envtesting "github.com/juju/juju/environs/testing"
    23  	"github.com/juju/juju/juju/testing"
    24  	"github.com/juju/juju/jujuclient"
    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 information
    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  	// ProviderCallContext holds the context to be used to make
    50  	// calls to a cloud provider.
    51  	ProviderCallContext context.ProviderCallContext
    52  }
    53  
    54  // Open opens an instance of the testing environment.
    55  func (t *Tests) Open(c *gc.C, cfg *config.Config) environs.Environ {
    56  	e, err := environs.New(environs.OpenParams{
    57  		Cloud:  t.CloudSpec(),
    58  		Config: cfg,
    59  	})
    60  	c.Assert(err, gc.IsNil, gc.Commentf("opening environ %#v", cfg.AllAttrs()))
    61  	c.Assert(e, gc.NotNil)
    62  	return e
    63  }
    64  
    65  func (t *Tests) CloudSpec() environs.CloudSpec {
    66  	credential := t.Credential
    67  	if credential.AuthType() == "" {
    68  		credential = cloud.NewEmptyCredential()
    69  	}
    70  	return environs.CloudSpec{
    71  		Type:       t.TestConfig["type"].(string),
    72  		Name:       t.TestConfig["type"].(string),
    73  		Region:     t.CloudRegion,
    74  		Endpoint:   t.CloudEndpoint,
    75  		Credential: &credential,
    76  	}
    77  }
    78  
    79  // PrepareParams returns the environs.PrepareParams that will be used to call
    80  // environs.Prepare.
    81  func (t *Tests) PrepareParams(c *gc.C) bootstrap.PrepareParams {
    82  	testConfigCopy := t.TestConfig.Merge(nil)
    83  
    84  	return bootstrap.PrepareParams{
    85  		ControllerConfig: coretesting.FakeControllerConfig(),
    86  		ModelConfig:      testConfigCopy,
    87  		Cloud:            t.CloudSpec(),
    88  		ControllerName:   t.TestConfig["name"].(string),
    89  		AdminSecret:      AdminSecret,
    90  	}
    91  }
    92  
    93  // Prepare prepares an instance of the testing environment.
    94  func (t *Tests) Prepare(c *gc.C) environs.Environ {
    95  	return t.PrepareWithParams(c, t.PrepareParams(c))
    96  }
    97  
    98  // PrepareWithParams prepares an instance of the testing environment.
    99  func (t *Tests) PrepareWithParams(c *gc.C, params bootstrap.PrepareParams) environs.Environ {
   100  	e, err := bootstrap.PrepareController(false, envtesting.BootstrapContext(c), t.ControllerStore, params)
   101  	c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", params.ModelConfig))
   102  	c.Assert(e, gc.NotNil)
   103  	return e.(environs.Environ)
   104  }
   105  
   106  func (t *Tests) AssertPrepareFailsWithConfig(c *gc.C, badConfig coretesting.Attrs, errorMatches string) error {
   107  	args := t.PrepareParams(c)
   108  	args.ModelConfig = coretesting.Attrs(args.ModelConfig).Merge(badConfig)
   109  
   110  	e, err := bootstrap.PrepareController(false, envtesting.BootstrapContext(c), t.ControllerStore, args)
   111  	c.Assert(err, gc.ErrorMatches, errorMatches)
   112  	c.Assert(e, gc.IsNil)
   113  	return err
   114  }
   115  
   116  func (t *Tests) SetUpTest(c *gc.C) {
   117  	storageDir := c.MkDir()
   118  	baseURLPath := filepath.Join(storageDir, "tools")
   119  	t.DefaultBaseURL = utils.MakeFileURL(baseURLPath)
   120  	t.ToolsFixture.SetUpTest(c)
   121  	stor, err := filestorage.NewFileStorageWriter(storageDir)
   122  	c.Assert(err, jc.ErrorIsNil)
   123  	t.UploadFakeTools(c, stor, "released", "released")
   124  	t.toolsStorage = stor
   125  	t.ControllerStore = jujuclient.NewMemStore()
   126  	t.ControllerUUID = coretesting.FakeControllerConfig().ControllerUUID()
   127  	t.ProviderCallContext = context.NewCloudCallContext()
   128  }
   129  
   130  func (t *Tests) TearDownTest(c *gc.C) {
   131  	t.ToolsFixture.TearDownTest(c)
   132  }
   133  
   134  func (t *Tests) TestStartStop(c *gc.C) {
   135  	e := t.Prepare(c)
   136  	cfg, err := e.Config().Apply(map[string]interface{}{
   137  		"agent-version": jujuversion.Current.String(),
   138  	})
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	err = e.SetConfig(cfg)
   141  	c.Assert(err, jc.ErrorIsNil)
   142  
   143  	insts, err := e.Instances(t.ProviderCallContext, nil)
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	c.Assert(insts, gc.HasLen, 0)
   146  
   147  	inst0, hc := testing.AssertStartInstance(c, e, t.ProviderCallContext, t.ControllerUUID, "0")
   148  	c.Assert(inst0, gc.NotNil)
   149  	id0 := inst0.Id()
   150  	// Sanity check for hardware characteristics.
   151  	c.Assert(hc.Arch, gc.NotNil)
   152  	c.Assert(hc.Mem, gc.NotNil)
   153  	c.Assert(hc.CpuCores, gc.NotNil)
   154  
   155  	inst1, _ := testing.AssertStartInstance(c, e, t.ProviderCallContext, t.ControllerUUID, "1")
   156  	c.Assert(inst1, gc.NotNil)
   157  	id1 := inst1.Id()
   158  
   159  	insts, err = e.Instances(t.ProviderCallContext, []instance.Id{id0, id1})
   160  	c.Assert(err, jc.ErrorIsNil)
   161  	c.Assert(insts, gc.HasLen, 2)
   162  	c.Assert(insts[0].Id(), gc.Equals, id0)
   163  	c.Assert(insts[1].Id(), gc.Equals, id1)
   164  
   165  	// order of results is not specified
   166  	insts, err = e.AllInstances(t.ProviderCallContext)
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	c.Assert(insts, gc.HasLen, 2)
   169  	c.Assert(insts[0].Id(), gc.Not(gc.Equals), insts[1].Id())
   170  
   171  	err = e.StopInstances(t.ProviderCallContext, inst0.Id())
   172  	c.Assert(err, jc.ErrorIsNil)
   173  
   174  	insts, err = e.Instances(t.ProviderCallContext, []instance.Id{id0, id1})
   175  	c.Assert(err, gc.Equals, environs.ErrPartialInstances)
   176  	c.Assert(insts[0], gc.IsNil)
   177  	c.Assert(insts[1].Id(), gc.Equals, id1)
   178  
   179  	insts, err = e.AllInstances(t.ProviderCallContext)
   180  	c.Assert(err, jc.ErrorIsNil)
   181  	c.Assert(insts[0].Id(), gc.Equals, id1)
   182  }
   183  
   184  func (t *Tests) TestBootstrap(c *gc.C) {
   185  	credential := t.Credential
   186  	if credential.AuthType() == "" {
   187  		credential = cloud.NewEmptyCredential()
   188  	}
   189  
   190  	var regions []cloud.Region
   191  	if t.CloudRegion != "" {
   192  		regions = []cloud.Region{{
   193  			Name:     t.CloudRegion,
   194  			Endpoint: t.CloudEndpoint,
   195  		}}
   196  	}
   197  
   198  	args := bootstrap.BootstrapParams{
   199  		ControllerConfig: coretesting.FakeControllerConfig(),
   200  		Cloud: cloud.Cloud{
   201  			Name:      t.TestConfig["type"].(string),
   202  			Type:      t.TestConfig["type"].(string),
   203  			AuthTypes: []cloud.AuthType{credential.AuthType()},
   204  			Regions:   regions,
   205  			Endpoint:  t.CloudEndpoint,
   206  		},
   207  		CloudRegion:         t.CloudRegion,
   208  		CloudCredential:     &credential,
   209  		CloudCredentialName: "credential",
   210  		AdminSecret:         AdminSecret,
   211  		CAPrivateKey:        coretesting.CAKey,
   212  	}
   213  
   214  	e := t.Prepare(c)
   215  	err := bootstrap.Bootstrap(envtesting.BootstrapContext(c), e, t.ProviderCallContext, args)
   216  	c.Assert(err, jc.ErrorIsNil)
   217  
   218  	controllerInstances, err := e.ControllerInstances(t.ProviderCallContext, t.ControllerUUID)
   219  	c.Assert(err, jc.ErrorIsNil)
   220  	c.Assert(controllerInstances, gc.Not(gc.HasLen), 0)
   221  
   222  	e2 := t.Open(c, e.Config())
   223  	controllerInstances2, err := e2.ControllerInstances(t.ProviderCallContext, t.ControllerUUID)
   224  	c.Assert(err, jc.ErrorIsNil)
   225  	c.Assert(controllerInstances2, gc.Not(gc.HasLen), 0)
   226  	c.Assert(controllerInstances2, jc.SameContents, controllerInstances)
   227  
   228  	err = environs.Destroy(e2.Config().Name(), e2, t.ProviderCallContext, t.ControllerStore)
   229  	c.Assert(err, jc.ErrorIsNil)
   230  
   231  	// Prepare again because Destroy invalidates old environments.
   232  	e3 := t.Prepare(c)
   233  
   234  	err = bootstrap.Bootstrap(envtesting.BootstrapContext(c), e3, t.ProviderCallContext, args)
   235  	c.Assert(err, jc.ErrorIsNil)
   236  
   237  	err = environs.Destroy(e3.Config().Name(), e3, t.ProviderCallContext, t.ControllerStore)
   238  	c.Assert(err, jc.ErrorIsNil)
   239  }