github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 unnecessary.
    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  var newConfigTests = []configTestSpec{{
    92  	info:   "unknown field is not touched",
    93  	insert: testing.Attrs{"unknown-field": 12345},
    94  	expect: testing.Attrs{"unknown-field": 12345},
    95  }}
    96  
    97  func (s *ConfigSuite) TestNewModelConfig(c *gc.C) {
    98  	for i, test := range newConfigTests {
    99  		c.Logf("test %d: %s", i, test.info)
   100  
   101  		testConfig := test.newConfig(c)
   102  		environ, err := environs.New(environs.OpenParams{
   103  			Cloud:  gce.MakeTestCloudSpec(),
   104  			Config: testConfig,
   105  		})
   106  
   107  		// Check the result
   108  		if test.err != "" {
   109  			test.checkFailure(c, err, "invalid config")
   110  		} else {
   111  			test.checkSuccess(c, environ, err)
   112  		}
   113  	}
   114  }
   115  
   116  // TODO(wwitzel3) refactor to provider_test file
   117  func (s *ConfigSuite) TestValidateNewConfig(c *gc.C) {
   118  	for i, test := range newConfigTests {
   119  		c.Logf("test %d: %s", i, test.info)
   120  
   121  		testConfig := test.newConfig(c)
   122  		validatedConfig, err := gce.Provider.Validate(testConfig, nil)
   123  
   124  		// Check the result
   125  		if test.err != "" {
   126  			test.checkFailure(c, err, "invalid config")
   127  		} else {
   128  			c.Check(validatedConfig, gc.NotNil)
   129  			test.checkSuccess(c, validatedConfig, err)
   130  		}
   131  	}
   132  }
   133  
   134  // TODO(wwitzel3) refactor to the provider_test file
   135  func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) {
   136  	for i, test := range newConfigTests {
   137  		c.Logf("test %d: %s", i, test.info)
   138  
   139  		// Validate the new config (relative to the old one) using the
   140  		// provider.
   141  		_, err := gce.Provider.Validate(s.config, test.newConfig(c))
   142  		if test.err != "" {
   143  			// In this test, we case only about the validating
   144  			// the old configuration, not about the success cases.
   145  			test.checkFailure(c, err, "invalid config: invalid base config")
   146  		}
   147  	}
   148  }
   149  
   150  var changeConfigTests = []configTestSpec{{
   151  	info:   "no change, no error",
   152  	expect: gce.ConfigAttrs,
   153  }, {
   154  	info:   "can insert unknown field",
   155  	insert: testing.Attrs{"unknown": "ignoti"},
   156  	expect: testing.Attrs{"unknown": "ignoti"},
   157  }}
   158  
   159  // TODO(wwitzel3) refactor this to the provider_test file.
   160  func (s *ConfigSuite) TestValidateChange(c *gc.C) {
   161  	for i, test := range changeConfigTests {
   162  		c.Logf("test %d: %s", i, test.info)
   163  
   164  		testConfig := test.newConfig(c)
   165  		validatedConfig, err := gce.Provider.Validate(testConfig, s.config)
   166  
   167  		// Check the result.
   168  		if test.err != "" {
   169  			test.checkFailure(c, err, "invalid config")
   170  		} else {
   171  			test.checkSuccess(c, validatedConfig, err)
   172  		}
   173  	}
   174  }
   175  
   176  func (s *ConfigSuite) TestSetConfig(c *gc.C) {
   177  	for i, test := range changeConfigTests {
   178  		c.Logf("test %d: %s", i, test.info)
   179  
   180  		environ, err := environs.New(environs.OpenParams{
   181  			Cloud:  gce.MakeTestCloudSpec(),
   182  			Config: s.config,
   183  		})
   184  		c.Assert(err, jc.ErrorIsNil)
   185  
   186  		testConfig := test.newConfig(c)
   187  		err = environ.SetConfig(testConfig)
   188  
   189  		// Check the result.
   190  		if test.err != "" {
   191  			test.checkFailure(c, err, "invalid config change")
   192  			test.checkAttrs(c, environ.Config().AllAttrs(), s.config)
   193  		} else {
   194  			test.checkSuccess(c, environ.Config(), err)
   195  		}
   196  	}
   197  }
   198  
   199  func (*ConfigSuite) TestSchema(c *gc.C) {
   200  	fields := gce.Provider.(environs.ProviderSchema).Schema()
   201  	// Check that all the fields defined in environs/config
   202  	// are in the returned schema.
   203  	globalFields, err := config.Schema(nil)
   204  	c.Assert(err, gc.IsNil)
   205  	for name, field := range globalFields {
   206  		c.Check(fields[name], jc.DeepEquals, field)
   207  	}
   208  }