github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/lxd"
    17  	"github.com/juju/juju/tools/lxdclient"
    18  )
    19  
    20  type environSuite struct {
    21  	lxd.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&environSuite{})
    25  
    26  func (s *environSuite) TestName(c *gc.C) {
    27  	name := s.Env.Name()
    28  
    29  	c.Check(name, gc.Equals, "lxd")
    30  }
    31  
    32  func (s *environSuite) TestProvider(c *gc.C) {
    33  	provider := s.Env.Provider()
    34  
    35  	c.Check(provider, gc.Equals, lxd.Provider)
    36  }
    37  
    38  func (s *environSuite) TestSetConfigOkay(c *gc.C) {
    39  	err := s.Env.SetConfig(s.Config)
    40  	c.Assert(err, jc.ErrorIsNil)
    41  
    42  	c.Check(lxd.ExposeEnvConfig(s.Env), jc.DeepEquals, s.EnvConfig)
    43  	// Ensure the client did not change.
    44  	c.Check(lxd.ExposeEnvClient(s.Env), gc.Equals, s.Client)
    45  }
    46  
    47  func (s *environSuite) TestSetConfigNoAPI(c *gc.C) {
    48  	err := s.Env.SetConfig(s.Config)
    49  	c.Assert(err, jc.ErrorIsNil)
    50  
    51  	s.Stub.CheckCallNames(c, "asNonLocal")
    52  }
    53  
    54  func (s *environSuite) TestSetConfigMissing(c *gc.C) {
    55  	lxd.UnsetEnvConfig(s.Env)
    56  
    57  	err := s.Env.SetConfig(s.Config)
    58  
    59  	c.Check(err, gc.ErrorMatches, "cannot set config on uninitialized env")
    60  }
    61  
    62  func (s *environSuite) TestConfig(c *gc.C) {
    63  	cfg := s.Env.Config()
    64  
    65  	c.Check(cfg, jc.DeepEquals, s.Config)
    66  }
    67  
    68  func (s *environSuite) TestBootstrapOkay(c *gc.C) {
    69  	s.Common.BootstrapResult = &environs.BootstrapResult{
    70  		Arch:   "amd64",
    71  		Series: "trusty",
    72  		Finalize: func(environs.BootstrapContext, *instancecfg.InstanceConfig) error {
    73  			return nil
    74  		},
    75  	}
    76  
    77  	ctx := envtesting.BootstrapContext(c)
    78  	params := environs.BootstrapParams{}
    79  	result, err := s.Env.Bootstrap(ctx, params)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	c.Check(result.Arch, gc.Equals, "amd64")
    83  	c.Check(result.Series, gc.Equals, "trusty")
    84  	// We don't check bsFinalizer because functions cannot be compared.
    85  	c.Check(result.Finalize, gc.NotNil)
    86  }
    87  
    88  func (s *environSuite) TestBootstrapAPI(c *gc.C) {
    89  	ctx := envtesting.BootstrapContext(c)
    90  	params := environs.BootstrapParams{}
    91  	_, err := s.Env.Bootstrap(ctx, params)
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
    95  		FuncName: "Bootstrap",
    96  		Args: []interface{}{
    97  			ctx,
    98  			params,
    99  		},
   100  	}})
   101  }
   102  
   103  func (s *environSuite) TestDestroy(c *gc.C) {
   104  	err := s.Env.Destroy()
   105  
   106  	c.Check(err, jc.ErrorIsNil)
   107  }
   108  
   109  func (s *environSuite) TestDestroyAPI(c *gc.C) {
   110  	err := s.Env.Destroy()
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	fwname := s.Prefix[:len(s.Prefix)-1]
   114  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
   115  		FuncName: "Ports",
   116  		Args: []interface{}{
   117  			fwname,
   118  		},
   119  	}, {
   120  		FuncName: "Destroy",
   121  		Args:     nil,
   122  	}})
   123  }
   124  
   125  func (s *environSuite) TestDestroyHostedModels(c *gc.C) {
   126  	s.UpdateConfig(c, map[string]interface{}{
   127  		"controller-uuid": s.Config.UUID(),
   128  	})
   129  	s.Stub.ResetCalls()
   130  
   131  	// machine0 is in the controller model.
   132  	machine0 := s.NewRawInstance(c, "juju-whatever-machine-0")
   133  	machine0.InstanceSummary.Metadata["juju-model-uuid"] = s.Config.UUID()
   134  	machine0.InstanceSummary.Metadata["juju-controller-uuid"] = s.Config.UUID()
   135  	// machine1 is not in the controller model, but managed
   136  	// by the same controller.
   137  	machine1 := s.NewRawInstance(c, "juju-whatever-machine-1")
   138  	machine1.InstanceSummary.Metadata["juju-model-uuid"] = "not-" + s.Config.UUID()
   139  	machine1.InstanceSummary.Metadata["juju-controller-uuid"] = s.Config.UUID()
   140  	// machine2 is not managed by the same controller.
   141  	machine2 := s.NewRawInstance(c, "juju-whatever-machine-2")
   142  	machine2.InstanceSummary.Metadata["juju-model-uuid"] = "not-" + s.Config.UUID()
   143  	machine2.InstanceSummary.Metadata["juju-controller-uuid"] = "not-" + s.Config.UUID()
   144  	s.Client.Insts = append(s.Client.Insts, *machine0, *machine1, *machine2)
   145  
   146  	err := s.Env.Destroy()
   147  	c.Assert(err, jc.ErrorIsNil)
   148  
   149  	fwname := s.Prefix[:len(s.Prefix)-1]
   150  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{
   151  		{"Ports", []interface{}{fwname}},
   152  		{"Instances", []interface{}{"juju-", lxdclient.AliveStatuses}},
   153  		{"RemoveInstances", []interface{}{"juju-", []string{machine1.Name}}},
   154  		{"Destroy", nil},
   155  	})
   156  }