github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/ec2/config_test.go (about)

     1  // Copyright 2011, 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  // TODO: Clean this up so it matches environs/openstack/config_test.go.
     7  
     8  import (
     9  	stdcontext "context"
    10  
    11  	jujutesting "github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cloud"
    16  	"github.com/juju/juju/environs"
    17  	environscloudspec "github.com/juju/juju/environs/cloudspec"
    18  	"github.com/juju/juju/environs/config"
    19  	"github.com/juju/juju/environs/context"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  // Use local suite since this file lives in the ec2 package
    24  // for testing internals.
    25  type ConfigSuite struct {
    26  	jujutesting.IsolationSuite
    27  }
    28  
    29  var _ = gc.Suite(&ConfigSuite{})
    30  
    31  // configTest specifies a config parsing test, checking that env when
    32  // parsed as the ec2 section of a config file matches baseConfigResult
    33  // when mutated by the mutate function, or that the parse matches the
    34  // given error.
    35  type configTest struct {
    36  	config             map[string]interface{}
    37  	change             map[string]interface{}
    38  	expect             map[string]interface{}
    39  	vpcID              string
    40  	forceVPCID         bool
    41  	firewallMode       string
    42  	blockStorageSource string
    43  	err                string
    44  }
    45  
    46  type attrs map[string]interface{}
    47  
    48  func (t configTest) check(c *gc.C) {
    49  	credential := cloud.NewCredential(
    50  		cloud.AccessKeyAuthType,
    51  		map[string]string{
    52  			"access-key": "x",
    53  			"secret-key": "y",
    54  		},
    55  	)
    56  	cloudSpec := environscloudspec.CloudSpec{
    57  		Type:       "ec2",
    58  		Name:       "ec2test",
    59  		Region:     "us-east-1",
    60  		Credential: &credential,
    61  	}
    62  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    63  		"type": "ec2",
    64  	}).Merge(t.config)
    65  	cfg, err := config.New(config.NoDefaults, attrs)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	e, err := environs.New(stdcontext.TODO(), environs.OpenParams{
    68  		Cloud:  cloudSpec,
    69  		Config: cfg,
    70  	})
    71  	if t.change != nil {
    72  		c.Assert(err, jc.ErrorIsNil)
    73  
    74  		// Testing a change in configuration.
    75  		var old, changed, valid *config.Config
    76  		ec2env := e.(*environ)
    77  		old = ec2env.ecfg().Config
    78  		changed, err = old.Apply(t.change)
    79  		c.Assert(err, jc.ErrorIsNil)
    80  
    81  		// Keep err for validation below.
    82  		valid, err = providerInstance.Validate(changed, old)
    83  		if err == nil {
    84  			err = ec2env.SetConfig(valid)
    85  		}
    86  	}
    87  	if t.err != "" {
    88  		c.Check(err, gc.ErrorMatches, t.err)
    89  		return
    90  	}
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	ecfg := e.(*environ).ecfg()
    94  	c.Assert(ecfg.Name(), gc.Equals, "testmodel")
    95  	c.Assert(ecfg.vpcID(), gc.Equals, t.vpcID)
    96  	c.Assert(ecfg.forceVPCID(), gc.Equals, t.forceVPCID)
    97  
    98  	if t.firewallMode != "" {
    99  		c.Assert(ecfg.FirewallMode(), gc.Equals, t.firewallMode)
   100  	}
   101  	for name, expect := range t.expect {
   102  		actual, found := ecfg.UnknownAttrs()[name]
   103  		c.Check(found, jc.IsTrue)
   104  		c.Check(actual, gc.Equals, expect)
   105  	}
   106  }
   107  
   108  var configTests = []configTest{
   109  	{
   110  		config: attrs{},
   111  	}, {
   112  		config:     attrs{},
   113  		vpcID:      "",
   114  		forceVPCID: false,
   115  	}, {
   116  		config: attrs{
   117  			"vpc-id": "invalid",
   118  		},
   119  		err:        `.*vpc-id: "invalid" is not a valid AWS VPC ID`,
   120  		vpcID:      "",
   121  		forceVPCID: false,
   122  	}, {
   123  		config: attrs{
   124  			"vpc-id": vpcIDNone,
   125  		},
   126  		vpcID:      "none",
   127  		forceVPCID: false,
   128  	}, {
   129  		config: attrs{
   130  			"vpc-id": 42,
   131  		},
   132  		err:        `.*expected string, got int\(42\)`,
   133  		vpcID:      "",
   134  		forceVPCID: false,
   135  	}, {
   136  		config: attrs{
   137  			"vpc-id-force": "nonsense",
   138  		},
   139  		err:        `.*expected bool, got string\("nonsense"\)`,
   140  		vpcID:      "",
   141  		forceVPCID: false,
   142  	}, {
   143  		config: attrs{
   144  			"vpc-id":       "vpc-anything",
   145  			"vpc-id-force": 999,
   146  		},
   147  		err:        `.*expected bool, got int\(999\)`,
   148  		vpcID:      "",
   149  		forceVPCID: false,
   150  	}, {
   151  		config: attrs{
   152  			"vpc-id":       "",
   153  			"vpc-id-force": true,
   154  		},
   155  		err:        `.*cannot use vpc-id-force without specifying vpc-id as well`,
   156  		vpcID:      "",
   157  		forceVPCID: true,
   158  	}, {
   159  		config: attrs{
   160  			"vpc-id": "vpc-a1b2c3d4",
   161  		},
   162  		vpcID:      "vpc-a1b2c3d4",
   163  		forceVPCID: false,
   164  	}, {
   165  		config: attrs{
   166  			"vpc-id":       "vpc-some-id",
   167  			"vpc-id-force": true,
   168  		},
   169  		vpcID:      "vpc-some-id",
   170  		forceVPCID: true,
   171  	}, {
   172  		config: attrs{
   173  			"vpc-id":       "vpc-abcd",
   174  			"vpc-id-force": false,
   175  		},
   176  		vpcID:      "vpc-abcd",
   177  		forceVPCID: false,
   178  	}, {
   179  		config: attrs{
   180  			"vpc-id":       "vpc-unchanged",
   181  			"vpc-id-force": true,
   182  		},
   183  		change: attrs{
   184  			"vpc-id":       "vpc-unchanged",
   185  			"vpc-id-force": false,
   186  		},
   187  		err:        `.*cannot change vpc-id-force from true to false`,
   188  		vpcID:      "vpc-unchanged",
   189  		forceVPCID: true,
   190  	}, {
   191  		config: attrs{
   192  			"vpc-id": "",
   193  		},
   194  		change: attrs{
   195  			"vpc-id": "none",
   196  		},
   197  		err:        `.*cannot change vpc-id from "" to "none"`,
   198  		vpcID:      "",
   199  		forceVPCID: false,
   200  	}, {
   201  		config: attrs{
   202  			"vpc-id": "",
   203  		},
   204  		change: attrs{
   205  			"vpc-id": "vpc-changed",
   206  		},
   207  		err:        `.*cannot change vpc-id from "" to "vpc-changed"`,
   208  		vpcID:      "",
   209  		forceVPCID: false,
   210  	}, {
   211  		config: attrs{
   212  			"vpc-id": "vpc-initial",
   213  		},
   214  		change: attrs{
   215  			"vpc-id": "",
   216  		},
   217  		err:        `.*cannot change vpc-id from "vpc-initial" to ""`,
   218  		vpcID:      "vpc-initial",
   219  		forceVPCID: false,
   220  	}, {
   221  		config: attrs{
   222  			"vpc-id": "vpc-old",
   223  		},
   224  		change: attrs{
   225  			"vpc-id": "vpc-new",
   226  		},
   227  		err:        `.*cannot change vpc-id from "vpc-old" to "vpc-new"`,
   228  		vpcID:      "vpc-old",
   229  		forceVPCID: false,
   230  	}, {
   231  		config: attrs{
   232  			"vpc-id":       "vpc-foo",
   233  			"vpc-id-force": true,
   234  		},
   235  		change:     attrs{},
   236  		vpcID:      "vpc-foo",
   237  		forceVPCID: true,
   238  	}, {
   239  		config:       attrs{},
   240  		firewallMode: config.FwInstance,
   241  	}, {
   242  		config:             attrs{},
   243  		blockStorageSource: "ebs",
   244  	}, {
   245  		config: attrs{
   246  			"storage-default-block-source": "ebs-fast",
   247  		},
   248  		blockStorageSource: "ebs-fast",
   249  	}, {
   250  		config: attrs{
   251  			"firewall-mode": "instance",
   252  		},
   253  		firewallMode: config.FwInstance,
   254  	}, {
   255  		config: attrs{
   256  			"firewall-mode": "global",
   257  		},
   258  		firewallMode: config.FwGlobal,
   259  	}, {
   260  		config: attrs{
   261  			"firewall-mode": "none",
   262  		},
   263  		firewallMode: config.FwNone,
   264  	}, {
   265  		config: attrs{
   266  			"ssl-hostname-verification": false,
   267  		},
   268  		err: ".*disabling ssh-hostname-verification is not supported",
   269  	}, {
   270  		config: attrs{
   271  			"future": "hammerstein",
   272  		},
   273  		expect: attrs{
   274  			"future": "hammerstein",
   275  		},
   276  	}, {
   277  		change: attrs{
   278  			"future": "hammerstein",
   279  		},
   280  		expect: attrs{
   281  			"future": "hammerstein",
   282  		},
   283  	},
   284  }
   285  
   286  func (s *ConfigSuite) TestConfig(c *gc.C) {
   287  	for i, t := range configTests {
   288  		c.Logf("test %d: %v", i, t.config)
   289  		t.check(c)
   290  	}
   291  }
   292  
   293  func (s *ConfigSuite) TestPrepareConfigSetsDefaultBlockSource(c *gc.C) {
   294  	s.PatchValue(&verifyCredentials, func(Client, context.ProviderCallContext) error { return nil })
   295  	attrs := testing.FakeConfig().Merge(testing.Attrs{
   296  		"type": "ec2",
   297  	})
   298  	cfg, err := config.New(config.NoDefaults, attrs)
   299  	c.Assert(err, jc.ErrorIsNil)
   300  
   301  	credential := cloud.NewCredential(
   302  		cloud.AccessKeyAuthType,
   303  		map[string]string{
   304  			"access-key": "x",
   305  			"secret-key": "y",
   306  		},
   307  	)
   308  	cfg, err = providerInstance.PrepareConfig(environs.PrepareConfigParams{
   309  		Config: cfg,
   310  		Cloud: environscloudspec.CloudSpec{
   311  			Type:       "ec2",
   312  			Name:       "aws",
   313  			Region:     "test",
   314  			Credential: &credential,
   315  		},
   316  	})
   317  	c.Assert(err, jc.ErrorIsNil)
   318  	source, ok := cfg.StorageDefaultBlockSource()
   319  	c.Assert(ok, jc.IsTrue)
   320  	c.Assert(source, gc.Equals, "ebs")
   321  }
   322  
   323  func (s *ConfigSuite) TestPrepareSetsDefaultBlockSource(c *gc.C) {
   324  	s.PatchValue(&verifyCredentials, func(Client, context.ProviderCallContext) error { return nil })
   325  	attrs := testing.FakeConfig().Merge(testing.Attrs{
   326  		"type": "ec2",
   327  	})
   328  	baseConfig, err := config.New(config.NoDefaults, attrs)
   329  	c.Assert(err, jc.ErrorIsNil)
   330  
   331  	credential := cloud.NewCredential(
   332  		cloud.AccessKeyAuthType,
   333  		map[string]string{
   334  			"access-key": "x",
   335  			"secret-key": "y",
   336  		},
   337  	)
   338  	cfg, err := providerInstance.PrepareConfig(environs.PrepareConfigParams{
   339  		Config: baseConfig,
   340  		Cloud: environscloudspec.CloudSpec{
   341  			Type:       "ec2",
   342  			Name:       "aws",
   343  			Region:     "test",
   344  			Credential: &credential,
   345  		},
   346  	})
   347  	c.Assert(err, jc.ErrorIsNil)
   348  
   349  	source, ok := cfg.StorageDefaultBlockSource()
   350  	c.Assert(ok, jc.IsTrue)
   351  	c.Assert(source, gc.Equals, "ebs")
   352  }
   353  
   354  func (*ConfigSuite) TestSchema(c *gc.C) {
   355  	fields := providerInstance.Schema()
   356  	// Check that all the fields defined in environs/config
   357  	// are in the returned schema.
   358  	globalFields, err := config.Schema(nil)
   359  	c.Assert(err, gc.IsNil)
   360  	for name, field := range globalFields {
   361  		c.Check(fields[name], jc.DeepEquals, field)
   362  	}
   363  }