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

     1  // Copyright 2013 Joyent Inc.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package joyent_test
     5  
     6  import (
     7  	"os"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cloud"
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  	jp "github.com/juju/juju/provider/joyent"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  func newConfig(c *gc.C, attrs coretesting.Attrs) *config.Config {
    20  	attrs = coretesting.FakeConfig().Merge(attrs)
    21  	cfg, err := config.New(config.UseDefaults, attrs)
    22  	c.Assert(err, jc.ErrorIsNil)
    23  	return cfg
    24  }
    25  
    26  func validAttrs() coretesting.Attrs {
    27  	return coretesting.FakeConfig().Merge(coretesting.Attrs{
    28  		"type": "joyent",
    29  	})
    30  }
    31  
    32  func fakeCloudSpec() environs.CloudSpec {
    33  	cred := fakeCredential()
    34  	return environs.CloudSpec{
    35  		Type:       "joyent",
    36  		Name:       "joyent",
    37  		Region:     "whatever",
    38  		Endpoint:   "test://test.api.joyentcloud.com",
    39  		Credential: &cred,
    40  	}
    41  }
    42  
    43  func fakeCredential() cloud.Credential {
    44  	return cloud.NewCredential(cloud.UserPassAuthType, map[string]string{
    45  		"sdc-user":    "test",
    46  		"sdc-key-id":  "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff",
    47  		"private-key": testPrivateKey,
    48  		"algorithm":   "rsa-sha256",
    49  	})
    50  }
    51  
    52  type ConfigSuite struct {
    53  	coretesting.FakeJujuXDGDataHomeSuite
    54  }
    55  
    56  var _ = gc.Suite(&ConfigSuite{})
    57  
    58  func (s *ConfigSuite) SetUpSuite(c *gc.C) {
    59  	s.FakeJujuXDGDataHomeSuite.SetUpSuite(c)
    60  	jp.RegisterMachinesEndpoint()
    61  	s.AddCleanup(func(*gc.C) { jp.UnregisterMachinesEndpoint() })
    62  }
    63  
    64  type configtest struct {
    65  	info    string
    66  	insert  coretesting.Attrs
    67  	remove  []string
    68  	envVars map[string]string
    69  	expect  coretesting.Attrs
    70  	err     string
    71  }
    72  
    73  var newConfigTests = []configtest{{
    74  	info:   "unknown field is not touched",
    75  	insert: coretesting.Attrs{"unknown-field": 12345},
    76  	expect: coretesting.Attrs{"unknown-field": 12345},
    77  }}
    78  
    79  func (s *ConfigSuite) TestNewModelConfig(c *gc.C) {
    80  	for i, test := range newConfigTests {
    81  		doTest(s, i, test, c)
    82  	}
    83  }
    84  
    85  func doTest(s *ConfigSuite, i int, test configtest, c *gc.C) {
    86  	c.Logf("test %d: %s", i, test.info)
    87  	for k, v := range test.envVars {
    88  		os.Setenv(k, v)
    89  		defer os.Setenv(k, "")
    90  	}
    91  	attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
    92  	testConfig := newConfig(c, attrs)
    93  	environ, err := environs.New(environs.OpenParams{
    94  		Cloud:  fakeCloudSpec(),
    95  		Config: testConfig,
    96  	})
    97  	if test.err == "" {
    98  		c.Check(err, jc.ErrorIsNil)
    99  		if err != nil {
   100  			return
   101  		}
   102  		attrs := environ.Config().AllAttrs()
   103  		for field, value := range test.expect {
   104  			c.Check(attrs[field], gc.Equals, value)
   105  		}
   106  	} else {
   107  		c.Check(environ, gc.IsNil)
   108  		c.Check(err, gc.ErrorMatches, test.err)
   109  	}
   110  }
   111  
   112  var changeConfigTests = []struct {
   113  	info   string
   114  	insert coretesting.Attrs
   115  	remove []string
   116  	expect coretesting.Attrs
   117  	err    string
   118  }{{
   119  	info:   "no change, no error",
   120  	expect: validAttrs(),
   121  }, {
   122  	info:   "can insert unknown field",
   123  	insert: coretesting.Attrs{"unknown": "ignoti"},
   124  	expect: coretesting.Attrs{"unknown": "ignoti"},
   125  }}
   126  
   127  func (s *ConfigSuite) TestValidateChange(c *gc.C) {
   128  	attrs := validAttrs()
   129  	baseConfig := newConfig(c, attrs)
   130  	for i, test := range changeConfigTests {
   131  		c.Logf("test %d: %s", i, test.info)
   132  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
   133  		testConfig := newConfig(c, attrs)
   134  		validatedConfig, err := jp.Provider.Validate(testConfig, baseConfig)
   135  		if test.err == "" {
   136  			c.Check(err, jc.ErrorIsNil)
   137  			if err != nil {
   138  				continue
   139  			}
   140  			attrs := validatedConfig.AllAttrs()
   141  			for field, value := range test.expect {
   142  				c.Check(attrs[field], gc.Equals, value)
   143  			}
   144  		} else {
   145  			c.Check(validatedConfig, gc.IsNil)
   146  			c.Check(err, gc.ErrorMatches, "invalid config change: "+test.err)
   147  		}
   148  	}
   149  }
   150  
   151  func (s *ConfigSuite) TestSetConfig(c *gc.C) {
   152  	baseConfig := newConfig(c, validAttrs())
   153  	for i, test := range changeConfigTests {
   154  		c.Logf("test %d: %s", i, test.info)
   155  		environ, err := environs.New(environs.OpenParams{
   156  			Cloud:  fakeCloudSpec(),
   157  			Config: baseConfig,
   158  		})
   159  		c.Assert(err, jc.ErrorIsNil)
   160  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
   161  		testConfig := newConfig(c, attrs)
   162  		err = environ.SetConfig(testConfig)
   163  		newAttrs := environ.Config().AllAttrs()
   164  		if test.err == "" {
   165  			c.Check(err, jc.ErrorIsNil)
   166  			for field, value := range test.expect {
   167  				c.Check(newAttrs[field], gc.Equals, value)
   168  			}
   169  		} else {
   170  			c.Check(err, gc.ErrorMatches, test.err)
   171  			for field, value := range baseConfig.UnknownAttrs() {
   172  				c.Check(newAttrs[field], gc.Equals, value)
   173  			}
   174  		}
   175  	}
   176  }
   177  
   178  // TODO(wallyworld) - add tests for cloud endpoint passed in via bootstrap args
   179  var bootstrapConfigTests = []struct {
   180  	info   string
   181  	insert coretesting.Attrs
   182  	remove []string
   183  	expect coretesting.Attrs
   184  	err    string
   185  }{{
   186  	info:   "All value provided, nothing to do",
   187  	expect: validAttrs(),
   188  }}
   189  
   190  func (s *ConfigSuite) TestPrepareConfig(c *gc.C) {
   191  	for i, test := range bootstrapConfigTests {
   192  		c.Logf("test %d: %s", i, test.info)
   193  		attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
   194  		testConfig := newConfig(c, attrs)
   195  		preparedConfig, err := jp.Provider.PrepareConfig(environs.PrepareConfigParams{
   196  			Config: testConfig,
   197  			Cloud:  fakeCloudSpec(),
   198  		})
   199  		if test.err == "" {
   200  			c.Check(err, jc.ErrorIsNil)
   201  			attrs := preparedConfig.AllAttrs()
   202  			for field, value := range test.expect {
   203  				c.Check(attrs[field], gc.Equals, value)
   204  			}
   205  		} else {
   206  			c.Check(preparedConfig, gc.IsNil)
   207  			c.Check(err, gc.ErrorMatches, test.err)
   208  		}
   209  	}
   210  }