github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/cloudsigma/config_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudsigma
     5  
     6  import (
     7  	gc "gopkg.in/check.v1"
     8  
     9  	"github.com/altoros/gosigma/mock"
    10  	"github.com/juju/juju/cloud"
    11  	"github.com/juju/juju/environs"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  func newConfig(c *gc.C, attrs testing.Attrs) *config.Config {
    17  	attrs = testing.FakeConfig().Merge(attrs)
    18  	cfg, err := config.New(config.NoDefaults, attrs)
    19  	c.Assert(err, gc.IsNil)
    20  	return cfg
    21  }
    22  
    23  func validAttrs() testing.Attrs {
    24  	return testing.FakeConfig().Merge(testing.Attrs{
    25  		"type": "cloudsigma",
    26  		"uuid": "f54aac3a-9dcd-4a0c-86b5-24091478478c",
    27  	})
    28  }
    29  
    30  func fakeCloudSpec() environs.CloudSpec {
    31  	cred := fakeCredential()
    32  	return environs.CloudSpec{
    33  		Type:       "cloudsigma",
    34  		Name:       "cloudsigma",
    35  		Region:     "testregion",
    36  		Endpoint:   "https://0.1.2.3:2000/api/2.0/",
    37  		Credential: &cred,
    38  	}
    39  }
    40  
    41  func fakeCredential() cloud.Credential {
    42  	return cloud.NewCredential(cloud.UserPassAuthType, map[string]string{
    43  		"username": mock.TestUser,
    44  		"password": mock.TestPassword,
    45  	})
    46  }
    47  
    48  type configSuite struct {
    49  	testing.BaseSuite
    50  }
    51  
    52  func (s *configSuite) SetUpSuite(c *gc.C) {
    53  	s.BaseSuite.SetUpSuite(c)
    54  }
    55  
    56  func (s *configSuite) SetUpTest(c *gc.C) {
    57  	s.BaseSuite.SetUpTest(c)
    58  	// speed up tests, do not create heavy stuff inside providers created withing this test suite
    59  	s.PatchValue(&newClient, func(environs.CloudSpec, string) (*environClient, error) {
    60  		return nil, nil
    61  	})
    62  }
    63  
    64  var _ = gc.Suite(&configSuite{})
    65  
    66  func (s *configSuite) TestNewModelConfig(c *gc.C) {
    67  
    68  	type checker struct {
    69  		checker gc.Checker
    70  		value   interface{}
    71  	}
    72  
    73  	var newConfigTests = []struct {
    74  		info   string
    75  		insert testing.Attrs
    76  		remove []string
    77  		expect testing.Attrs
    78  		err    string
    79  	}{}
    80  
    81  	for i, test := range newConfigTests {
    82  		c.Logf("test %d: %s", i, test.info)
    83  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
    84  		testConfig := newConfig(c, attrs)
    85  		environ, err := environs.New(environs.OpenParams{
    86  			Cloud:  fakeCloudSpec(),
    87  			Config: testConfig,
    88  		})
    89  		if test.err == "" {
    90  			c.Check(err, gc.IsNil)
    91  			attrs := environ.Config().AllAttrs()
    92  			for field, value := range test.expect {
    93  				if chk, ok := value.(checker); ok {
    94  					c.Check(attrs[field], chk.checker, chk.value)
    95  				} else {
    96  					c.Check(attrs[field], gc.Equals, value)
    97  				}
    98  			}
    99  		} else {
   100  			c.Check(environ, gc.IsNil)
   101  			c.Check(err, gc.ErrorMatches, test.err)
   102  		}
   103  	}
   104  }
   105  
   106  var changeConfigTests = []struct {
   107  	info   string
   108  	insert testing.Attrs
   109  	remove []string
   110  	expect testing.Attrs
   111  	err    string
   112  }{{
   113  	info:   "no change, no error",
   114  	expect: validAttrs(),
   115  }}
   116  
   117  func (s *configSuite) TestValidateChange(c *gc.C) {
   118  	baseConfig := newConfig(c, validAttrs())
   119  	for i, test := range changeConfigTests {
   120  		c.Logf("test %d: %s", i, test.info)
   121  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
   122  		testConfig := newConfig(c, attrs)
   123  		validatedConfig, err := providerInstance.Validate(testConfig, baseConfig)
   124  		if test.err == "" {
   125  			c.Check(err, gc.IsNil)
   126  			attrs := validatedConfig.AllAttrs()
   127  			for field, value := range test.expect {
   128  				c.Check(attrs[field], gc.Equals, value)
   129  			}
   130  		} else {
   131  			c.Check(validatedConfig, gc.IsNil)
   132  			c.Check(err, gc.ErrorMatches, "invalid config.*: "+test.err)
   133  		}
   134  
   135  		// reverse change
   136  		validatedConfig, err = providerInstance.Validate(baseConfig, testConfig)
   137  		if test.err == "" {
   138  			c.Check(err, gc.IsNil)
   139  			attrs := validatedConfig.AllAttrs()
   140  			for field, value := range validAttrs() {
   141  				c.Check(attrs[field], gc.Equals, value)
   142  			}
   143  		} else {
   144  			c.Check(validatedConfig, gc.IsNil)
   145  			c.Check(err, gc.ErrorMatches, "invalid .*config.*: "+test.err)
   146  		}
   147  	}
   148  }
   149  
   150  func (s *configSuite) TestSetConfig(c *gc.C) {
   151  	baseConfig := newConfig(c, validAttrs())
   152  	for i, test := range changeConfigTests {
   153  		c.Logf("test %d: %s", i, test.info)
   154  		environ, err := environs.New(environs.OpenParams{
   155  			Cloud:  fakeCloudSpec(),
   156  			Config: baseConfig,
   157  		})
   158  		c.Assert(err, gc.IsNil)
   159  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
   160  		testConfig := newConfig(c, attrs)
   161  		err = environ.SetConfig(testConfig)
   162  		newAttrs := environ.Config().AllAttrs()
   163  		if test.err == "" {
   164  			c.Check(err, gc.IsNil)
   165  			for field, value := range test.expect {
   166  				c.Check(newAttrs[field], gc.Equals, value)
   167  			}
   168  		} else {
   169  			c.Check(err, gc.ErrorMatches, test.err)
   170  			for field, value := range baseConfig.UnknownAttrs() {
   171  				c.Check(newAttrs[field], gc.Equals, value)
   172  			}
   173  		}
   174  	}
   175  }
   176  
   177  func (s *configSuite) TestModelConfig(c *gc.C) {
   178  	testConfig := newConfig(c, validAttrs())
   179  	ecfg, err := validateConfig(testConfig, nil)
   180  	c.Assert(ecfg, gc.NotNil)
   181  	c.Assert(err, gc.IsNil)
   182  }
   183  
   184  func (s *configSuite) TestInvalidConfigChange(c *gc.C) {
   185  	oldAttrs := validAttrs().Merge(testing.Attrs{"name": "123"})
   186  	oldConfig := newConfig(c, oldAttrs)
   187  	newAttrs := validAttrs().Merge(testing.Attrs{"name": "321"})
   188  	newConfig := newConfig(c, newAttrs)
   189  
   190  	oldecfg, _ := providerInstance.Validate(oldConfig, nil)
   191  	c.Assert(oldecfg, gc.NotNil)
   192  
   193  	newecfg, err := providerInstance.Validate(newConfig, oldecfg)
   194  	c.Assert(newecfg, gc.IsNil)
   195  	c.Assert(err, gc.NotNil)
   196  }