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

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gce_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/environs"
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/provider/gce"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type ConfigSuite struct {
    17  	gce.BaseSuite
    18  
    19  	config *config.Config
    20  }
    21  
    22  var _ = gc.Suite(&ConfigSuite{})
    23  
    24  func (s *ConfigSuite) SetUpTest(c *gc.C) {
    25  	s.BaseSuite.SetUpTest(c)
    26  
    27  	cfg, err := testing.ModelConfig(c).Apply(gce.ConfigAttrs)
    28  	c.Assert(err, jc.ErrorIsNil)
    29  	s.config = cfg
    30  }
    31  
    32  // TODO(ericsnow) Each test only deals with a single field, so having
    33  // multiple values in insert and remove (in configTestSpec) is a little
    34  // misleading and unecessary.
    35  
    36  // configTestSpec defines a subtest to run in a table driven test.
    37  type configTestSpec struct {
    38  	// info describes the subtest.
    39  	info string
    40  	// insert holds attrs that should be merged into the config.
    41  	insert testing.Attrs
    42  	// remove has the names of attrs that should be removed.
    43  	remove []string
    44  	// expect defines the expected attributes in a success case.
    45  	expect testing.Attrs
    46  	// err is the error message to expect in a failure case.
    47  	err string
    48  }
    49  
    50  func (ts configTestSpec) checkSuccess(c *gc.C, value interface{}, err error) {
    51  	if !c.Check(err, jc.ErrorIsNil) {
    52  		return
    53  	}
    54  
    55  	var cfg *config.Config
    56  	switch typed := value.(type) {
    57  	case *config.Config:
    58  		cfg = typed
    59  	case environs.Environ:
    60  		cfg = typed.Config()
    61  	}
    62  
    63  	attrs := cfg.AllAttrs()
    64  	for field, value := range ts.expect {
    65  		c.Check(attrs[field], gc.Equals, value)
    66  	}
    67  }
    68  
    69  func (ts configTestSpec) checkFailure(c *gc.C, err error, msg string) {
    70  	c.Check(err, gc.ErrorMatches, msg+": "+ts.err)
    71  }
    72  
    73  func (ts configTestSpec) checkAttrs(c *gc.C, attrs map[string]interface{}, cfg *config.Config) {
    74  	for field, expected := range cfg.UnknownAttrs() {
    75  		value := attrs[field]
    76  		c.Check(value, gc.Equals, expected)
    77  	}
    78  }
    79  
    80  func (ts configTestSpec) attrs() testing.Attrs {
    81  	return gce.ConfigAttrs.Merge(ts.insert).Delete(ts.remove...)
    82  }
    83  
    84  func (ts configTestSpec) newConfig(c *gc.C) *config.Config {
    85  	attrs := ts.attrs()
    86  	cfg, err := testing.ModelConfig(c).Apply(attrs)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	return cfg
    89  }
    90  
    91  func (ts configTestSpec) fixCfg(c *gc.C, cfg *config.Config) *config.Config {
    92  	newCfg, err := cfg.Apply(ts.insert)
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	return newCfg
    95  }
    96  
    97  func updateAttrs(attrs, updates testing.Attrs) testing.Attrs {
    98  	updated := make(testing.Attrs, len(attrs))
    99  	for k, v := range attrs {
   100  		updated[k] = v
   101  	}
   102  	for k, v := range updates {
   103  		updated[k] = v
   104  	}
   105  	return updated
   106  }
   107  
   108  var newConfigTests = []configTestSpec{{
   109  	info:   "unknown field is not touched",
   110  	insert: testing.Attrs{"unknown-field": 12345},
   111  	expect: testing.Attrs{"unknown-field": 12345},
   112  }}
   113  
   114  func (s *ConfigSuite) TestNewModelConfig(c *gc.C) {
   115  	for i, test := range newConfigTests {
   116  		c.Logf("test %d: %s", i, test.info)
   117  
   118  		testConfig := test.newConfig(c)
   119  		environ, err := environs.New(environs.OpenParams{
   120  			Cloud:  gce.MakeTestCloudSpec(),
   121  			Config: testConfig,
   122  		})
   123  
   124  		// Check the result
   125  		if test.err != "" {
   126  			test.checkFailure(c, err, "invalid config")
   127  		} else {
   128  			test.checkSuccess(c, environ, err)
   129  		}
   130  	}
   131  }
   132  
   133  // TODO(wwitzel3) refactor to provider_test file
   134  func (s *ConfigSuite) TestValidateNewConfig(c *gc.C) {
   135  	for i, test := range newConfigTests {
   136  		c.Logf("test %d: %s", i, test.info)
   137  
   138  		testConfig := test.newConfig(c)
   139  		validatedConfig, err := gce.Provider.Validate(testConfig, nil)
   140  
   141  		// Check the result
   142  		if test.err != "" {
   143  			test.checkFailure(c, err, "invalid config")
   144  		} else {
   145  			c.Check(validatedConfig, gc.NotNil)
   146  			test.checkSuccess(c, validatedConfig, err)
   147  		}
   148  	}
   149  }
   150  
   151  // TODO(wwitzel3) refactor to the provider_test file
   152  func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) {
   153  	for i, test := range newConfigTests {
   154  		c.Logf("test %d: %s", i, test.info)
   155  
   156  		// Validate the new config (relative to the old one) using the
   157  		// provider.
   158  		_, err := gce.Provider.Validate(s.config, test.newConfig(c))
   159  		if test.err != "" {
   160  			// In this test, we case only about the validating
   161  			// the old configuration, not about the success cases.
   162  			test.checkFailure(c, err, "invalid config: invalid base config")
   163  		}
   164  	}
   165  }
   166  
   167  var changeConfigTests = []configTestSpec{{
   168  	info:   "no change, no error",
   169  	expect: gce.ConfigAttrs,
   170  }, {
   171  	info:   "can insert unknown field",
   172  	insert: testing.Attrs{"unknown": "ignoti"},
   173  	expect: testing.Attrs{"unknown": "ignoti"},
   174  }}
   175  
   176  // TODO(wwitzel3) refactor this to the provider_test file.
   177  func (s *ConfigSuite) TestValidateChange(c *gc.C) {
   178  	for i, test := range changeConfigTests {
   179  		c.Logf("test %d: %s", i, test.info)
   180  
   181  		testConfig := test.newConfig(c)
   182  		validatedConfig, err := gce.Provider.Validate(testConfig, s.config)
   183  
   184  		// Check the result.
   185  		if test.err != "" {
   186  			test.checkFailure(c, err, "invalid config")
   187  		} else {
   188  			test.checkSuccess(c, validatedConfig, err)
   189  		}
   190  	}
   191  }
   192  
   193  func (s *ConfigSuite) TestSetConfig(c *gc.C) {
   194  	for i, test := range changeConfigTests {
   195  		c.Logf("test %d: %s", i, test.info)
   196  
   197  		environ, err := environs.New(environs.OpenParams{
   198  			Cloud:  gce.MakeTestCloudSpec(),
   199  			Config: s.config,
   200  		})
   201  		c.Assert(err, jc.ErrorIsNil)
   202  
   203  		testConfig := test.newConfig(c)
   204  		err = environ.SetConfig(testConfig)
   205  
   206  		// Check the result.
   207  		if test.err != "" {
   208  			test.checkFailure(c, err, "invalid config change")
   209  			test.checkAttrs(c, environ.Config().AllAttrs(), s.config)
   210  		} else {
   211  			test.checkSuccess(c, environ.Config(), err)
   212  		}
   213  	}
   214  }
   215  
   216  func (*ConfigSuite) TestSchema(c *gc.C) {
   217  	fields := gce.Provider.(environs.ProviderSchema).Schema()
   218  	// Check that all the fields defined in environs/config
   219  	// are in the returned schema.
   220  	globalFields, err := config.Schema(nil)
   221  	c.Assert(err, gc.IsNil)
   222  	for name, field := range globalFields {
   223  		c.Check(fields[name], jc.DeepEquals, field)
   224  	}
   225  }