github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/lxd/environ_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxd_test
     7  
     8  import (
     9  	gitjujutesting "github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cloudconfig/instancecfg"
    14  	"github.com/juju/juju/environs"
    15  	envtesting "github.com/juju/juju/environs/testing"
    16  	"github.com/juju/juju/provider/common"
    17  	"github.com/juju/juju/provider/lxd"
    18  	coretesting "github.com/juju/juju/testing"
    19  	"github.com/juju/juju/tools/lxdclient"
    20  )
    21  
    22  type environSuite struct {
    23  	lxd.BaseSuite
    24  }
    25  
    26  var _ = gc.Suite(&environSuite{})
    27  
    28  func (s *environSuite) TestName(c *gc.C) {
    29  	name := s.Env.Name()
    30  
    31  	c.Check(name, gc.Equals, "lxd")
    32  }
    33  
    34  func (s *environSuite) TestProvider(c *gc.C) {
    35  	provider := s.Env.Provider()
    36  
    37  	c.Check(provider, gc.Equals, lxd.Provider)
    38  }
    39  
    40  func (s *environSuite) TestSetConfigOkay(c *gc.C) {
    41  	err := s.Env.SetConfig(s.Config)
    42  	c.Assert(err, jc.ErrorIsNil)
    43  
    44  	c.Check(lxd.ExposeEnvConfig(s.Env), jc.DeepEquals, s.EnvConfig)
    45  	// Ensure the client did not change.
    46  	c.Check(lxd.ExposeEnvClient(s.Env), gc.Equals, s.Client)
    47  }
    48  
    49  func (s *environSuite) TestSetConfigNoAPI(c *gc.C) {
    50  	err := s.Env.SetConfig(s.Config)
    51  
    52  	c.Assert(err, jc.ErrorIsNil)
    53  }
    54  
    55  func (s *environSuite) TestConfig(c *gc.C) {
    56  	cfg := s.Env.Config()
    57  
    58  	c.Check(cfg, jc.DeepEquals, s.Config)
    59  }
    60  
    61  func (s *environSuite) TestBootstrapOkay(c *gc.C) {
    62  	s.Common.BootstrapResult = &environs.BootstrapResult{
    63  		Arch:   "amd64",
    64  		Series: "trusty",
    65  		Finalize: func(environs.BootstrapContext, *instancecfg.InstanceConfig, environs.BootstrapDialOpts) error {
    66  			return nil
    67  		},
    68  	}
    69  
    70  	ctx := envtesting.BootstrapContext(c)
    71  	params := environs.BootstrapParams{
    72  		ControllerConfig: coretesting.FakeControllerConfig(),
    73  	}
    74  	result, err := s.Env.Bootstrap(ctx, params)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	c.Check(result.Arch, gc.Equals, "amd64")
    78  	c.Check(result.Series, gc.Equals, "trusty")
    79  	// We don't check bsFinalizer because functions cannot be compared.
    80  	c.Check(result.Finalize, gc.NotNil)
    81  }
    82  
    83  func (s *environSuite) TestBootstrapAPI(c *gc.C) {
    84  	ctx := envtesting.BootstrapContext(c)
    85  	params := environs.BootstrapParams{
    86  		ControllerConfig: coretesting.FakeControllerConfig(),
    87  	}
    88  	_, err := s.Env.Bootstrap(ctx, params)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
    92  		FuncName: "Bootstrap",
    93  		Args: []interface{}{
    94  			ctx,
    95  			params,
    96  		},
    97  	}})
    98  }
    99  
   100  func (s *environSuite) TestDestroy(c *gc.C) {
   101  	err := s.Env.Destroy()
   102  
   103  	c.Check(err, jc.ErrorIsNil)
   104  }
   105  
   106  func (s *environSuite) TestDestroyAPI(c *gc.C) {
   107  	err := s.Env.Destroy()
   108  	c.Assert(err, jc.ErrorIsNil)
   109  
   110  	fwname := common.EnvFullName(s.Env.Config().UUID())
   111  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
   112  		FuncName: "Ports",
   113  		Args: []interface{}{
   114  			fwname,
   115  		},
   116  	}, {
   117  		FuncName: "Destroy",
   118  		Args:     nil,
   119  	}})
   120  }
   121  
   122  func (s *environSuite) TestDestroyHostedModels(c *gc.C) {
   123  	s.UpdateConfig(c, map[string]interface{}{
   124  		"controller-uuid": s.Config.UUID(),
   125  	})
   126  	s.Stub.ResetCalls()
   127  
   128  	// machine0 is in the controller model.
   129  	machine0 := s.NewRawInstance(c, "juju-controller-machine-0")
   130  	machine0.InstanceSummary.Metadata["juju-model-uuid"] = s.Config.UUID()
   131  	machine0.InstanceSummary.Metadata["juju-controller-uuid"] = s.Config.UUID()
   132  	// machine1 is not in the controller model, but managed
   133  	// by the same controller.
   134  	machine1 := s.NewRawInstance(c, "juju-hosted-machine-1")
   135  	machine1.InstanceSummary.Metadata["juju-model-uuid"] = "not-" + s.Config.UUID()
   136  	machine1.InstanceSummary.Metadata["juju-controller-uuid"] = s.Config.UUID()
   137  	// machine2 is not managed by the same controller.
   138  	machine2 := s.NewRawInstance(c, "juju-controller-machine-2")
   139  	machine2.InstanceSummary.Metadata["juju-model-uuid"] = "not-" + s.Config.UUID()
   140  	machine2.InstanceSummary.Metadata["juju-controller-uuid"] = "not-" + s.Config.UUID()
   141  	s.Client.Insts = append(s.Client.Insts, *machine0, *machine1, *machine2)
   142  
   143  	err := s.Env.DestroyController(s.Config.UUID())
   144  	c.Assert(err, jc.ErrorIsNil)
   145  
   146  	fwname := common.EnvFullName(s.Env.Config().UUID())
   147  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{
   148  		{"Ports", []interface{}{fwname}},
   149  		{"Destroy", nil},
   150  		{"Instances", []interface{}{"juju-", lxdclient.AliveStatuses}},
   151  		{"Instances", []interface{}{"juju-", []string{}}},
   152  		{"RemoveInstances", []interface{}{"juju-", []string{machine1.Name}}},
   153  	})
   154  }
   155  
   156  func (s *environSuite) TestPrepareForBootstrap(c *gc.C) {
   157  	err := s.Env.PrepareForBootstrap(envtesting.BootstrapContext(c))
   158  	c.Assert(err, jc.ErrorIsNil)
   159  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{
   160  		{"SetConfig", []interface{}{"core.https_address", "[::]"}},
   161  	})
   162  }