github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/provider/lxd/provider_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  	"fmt"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	"github.com/juju/utils/series"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cloud"
    16  	"github.com/juju/juju/environs"
    17  	envtesting "github.com/juju/juju/environs/testing"
    18  	"github.com/juju/juju/provider/lxd"
    19  	"github.com/juju/juju/tools/lxdclient"
    20  )
    21  
    22  // This is a quick hack to make wily pass with it's default, but unsupported,
    23  // version of LXD. Wily is supported until 2016-7-??. AFAIU LXD will not be
    24  // backported to wily... so we have this:|
    25  // TODO(redir): Remove after wiley or in yakkety.
    26  func skipIfWily(c *gc.C) {
    27  	if series.HostSeries() == "wily" {
    28  		cfg, _ := lxdclient.Config{}.WithDefaults()
    29  		_, err := lxdclient.Connect(cfg)
    30  		// We try to create a client here. On wily this should fail, because
    31  		// the default 0.20 lxd version should make juju/tools/lxdclient return
    32  		// an error.
    33  		if err != nil {
    34  			c.Skip(fmt.Sprintf("Skipping LXD tests because %s", err))
    35  		}
    36  	}
    37  }
    38  
    39  var (
    40  	_ = gc.Suite(&providerSuite{})
    41  	_ = gc.Suite(&ProviderFunctionalSuite{})
    42  )
    43  
    44  type providerSuite struct {
    45  	lxd.BaseSuite
    46  
    47  	provider environs.EnvironProvider
    48  }
    49  
    50  func (s *providerSuite) SetUpTest(c *gc.C) {
    51  	s.BaseSuite.SetUpTest(c)
    52  
    53  	provider, err := environs.Provider("lxd")
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	s.provider = provider
    56  }
    57  
    58  func (s *providerSuite) TestDetectRegions(c *gc.C) {
    59  	c.Assert(s.provider, gc.Implements, new(environs.CloudRegionDetector))
    60  	regions, err := s.provider.(environs.CloudRegionDetector).DetectRegions()
    61  	c.Assert(err, jc.ErrorIsNil)
    62  	c.Assert(regions, jc.DeepEquals, []cloud.Region{{Name: "localhost"}})
    63  }
    64  
    65  func (s *providerSuite) TestRegistered(c *gc.C) {
    66  	c.Check(s.provider, gc.Equals, lxd.Provider)
    67  }
    68  
    69  func (s *providerSuite) TestValidate(c *gc.C) {
    70  	validCfg, err := s.provider.Validate(s.Config, nil)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	validAttrs := validCfg.AllAttrs()
    73  
    74  	c.Check(s.Config.AllAttrs(), gc.DeepEquals, validAttrs)
    75  }
    76  
    77  func (s *providerSuite) TestSecretAttrs(c *gc.C) {
    78  	obtainedAttrs, err := s.provider.SecretAttrs(s.Config)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  
    81  	c.Check(obtainedAttrs, gc.DeepEquals, map[string]string{"client-key": ""})
    82  }
    83  
    84  type ProviderFunctionalSuite struct {
    85  	lxd.BaseSuite
    86  
    87  	provider environs.EnvironProvider
    88  }
    89  
    90  func (s *ProviderFunctionalSuite) SetUpTest(c *gc.C) {
    91  	if !s.IsRunningLocally(c) {
    92  		c.Skip("LXD not running locally")
    93  	}
    94  
    95  	// TODO(redir): Remove after wily or in yakkety.
    96  	skipIfWily(c)
    97  
    98  	s.BaseSuite.SetUpTest(c)
    99  
   100  	provider, err := environs.Provider("lxd")
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	s.provider = provider
   104  }
   105  
   106  func (s *ProviderFunctionalSuite) TestOpen(c *gc.C) {
   107  	env, err := s.provider.Open(s.Config)
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	envConfig := env.Config()
   110  
   111  	c.Check(envConfig.Name(), gc.Equals, "testenv")
   112  }
   113  
   114  func (s *ProviderFunctionalSuite) TestBootstrapConfig(c *gc.C) {
   115  	cfg, err := s.provider.BootstrapConfig(environs.BootstrapConfigParams{
   116  		Config: s.Config,
   117  	})
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	c.Check(cfg, gc.NotNil)
   120  }
   121  
   122  func (s *ProviderFunctionalSuite) TestPrepareForBootstrap(c *gc.C) {
   123  	env, err := s.provider.PrepareForBootstrap(envtesting.BootstrapContext(c), s.Config)
   124  	c.Assert(err, jc.ErrorIsNil)
   125  	c.Check(env, gc.NotNil)
   126  }