launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/azure/environprovider_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azure
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/environs/config"
    13  )
    14  
    15  type environProviderSuite struct {
    16  	providerSuite
    17  }
    18  
    19  var _ = gc.Suite(&environProviderSuite{})
    20  
    21  func (*environProviderSuite) TestOpen(c *gc.C) {
    22  	prov := azureEnvironProvider{}
    23  	attrs := makeAzureConfigMap(c)
    24  	attrs["name"] = "my-shiny-new-env"
    25  	cfg, err := config.New(config.NoDefaults, attrs)
    26  	c.Assert(err, gc.IsNil)
    27  
    28  	env, err := prov.Open(cfg)
    29  	c.Assert(err, gc.IsNil)
    30  
    31  	c.Check(env.Name(), gc.Equals, attrs["name"])
    32  }
    33  
    34  func (environProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) {
    35  	prov := azureEnvironProvider{}
    36  	attrs := makeAzureConfigMap(c)
    37  	// Make the config invalid.
    38  	attrs["location"] = ""
    39  	cfg, err := config.New(config.NoDefaults, attrs)
    40  	c.Assert(err, gc.IsNil)
    41  
    42  	env, err := prov.Open(cfg)
    43  	// When Open() fails (i.e. returns a non-nil error), it returns an
    44  	// environs.Environ interface object with a nil value and a nil
    45  	// type.
    46  	c.Check(env, gc.Equals, nil)
    47  	c.Check(err, gc.ErrorMatches, ".*environment has no location; you need to set one.*")
    48  }
    49  
    50  // writeWALASharedConfig creates a temporary file with a valid WALinux config
    51  // built using the given parameters. The file will be cleaned up at the end
    52  // of the test calling this method.
    53  func writeWALASharedConfig(c *gc.C, deploymentId string, deploymentName string, internalAddress string) string {
    54  	configTemplateXML := `
    55  	<SharedConfig version="1.0.0.0" goalStateIncarnation="1">
    56  	  <Deployment name="%s" guid="{495985a8-8e5a-49aa-826f-d1f7f51045b6}" incarnation="0">
    57  	    <Service name="%s" guid="{00000000-0000-0000-0000-000000000000}" />
    58  	    <ServiceInstance name="%s" guid="{9806cac7-e566-42b8-9ecb-de8da8f69893}" />
    59  	  </Deployment>
    60  	  <Instances>
    61              <Instance id="gwaclroleldc1o5p" address="%s">
    62              </Instance>
    63            </Instances>
    64          </SharedConfig>`
    65  	config := fmt.Sprintf(configTemplateXML, deploymentId, deploymentName, deploymentId, internalAddress)
    66  	file, err := ioutil.TempFile(c.MkDir(), "")
    67  	c.Assert(err, gc.IsNil)
    68  	filename := file.Name()
    69  	err = ioutil.WriteFile(filename, []byte(config), 0644)
    70  	c.Assert(err, gc.IsNil)
    71  	return filename
    72  }
    73  
    74  // overrideWALASharedConfig:
    75  // - creates a temporary file with a valid WALinux config built using the
    76  // given parameters.  The file will be cleaned up at the end of the test
    77  // calling this method.
    78  // - monkey patches the value of '_WALAConfigPath' (the path to the WALA
    79  // configuration file) so that it contains the path to the temporary file.
    80  // overrideWALASharedConfig returns a cleanup method that the caller *must*
    81  // call in order to restore the original value of '_WALAConfigPath'
    82  func overrideWALASharedConfig(c *gc.C, deploymentId, deploymentName, internalAddress string) func() {
    83  	filename := writeWALASharedConfig(c, deploymentId, deploymentName,
    84  		internalAddress)
    85  	oldConfigPath := _WALAConfigPath
    86  	_WALAConfigPath = filename
    87  	// Return cleanup method to restore the original value of
    88  	// '_WALAConfigPath'.
    89  	return func() {
    90  		_WALAConfigPath = oldConfigPath
    91  	}
    92  }
    93  
    94  func (*environProviderSuite) TestParseWALASharedConfig(c *gc.C) {
    95  	deploymentId := "b6de4c4c7d4a49c39270e0c57481fd9b"
    96  	deploymentName := "gwaclmachineex95rsek"
    97  	internalAddress := "10.76.200.59"
    98  
    99  	cleanup := overrideWALASharedConfig(c, deploymentId, deploymentName, internalAddress)
   100  	defer cleanup()
   101  
   102  	config, err := parseWALAConfig()
   103  	c.Assert(err, gc.IsNil)
   104  	c.Check(config.Deployment.Name, gc.Equals, deploymentId)
   105  	c.Check(config.Deployment.Service.Name, gc.Equals, deploymentName)
   106  	c.Check(config.Instances[0].Address, gc.Equals, internalAddress)
   107  }
   108  
   109  func (*environProviderSuite) TestConfigGetDeploymentFQDN(c *gc.C) {
   110  	deploymentId := "b6de4c4c7d4a49c39270e0c57481fd9b"
   111  	serviceName := "gwaclr12slechtstschrijvende5"
   112  	config := WALASharedConfig{
   113  		Deployment: WALADeployment{
   114  			Name:    deploymentId,
   115  			Service: WALADeploymentService{Name: serviceName},
   116  		},
   117  	}
   118  
   119  	c.Check(config.getDeploymentFQDN(), gc.Equals, serviceName+".cloudapp.net")
   120  }
   121  
   122  func (*environProviderSuite) TestConfigGetDeploymentHostname(c *gc.C) {
   123  	deploymentName := "gwaclmachineex95rsek"
   124  	config := WALASharedConfig{Deployment: WALADeployment{Name: "id", Service: WALADeploymentService{Name: deploymentName}}}
   125  
   126  	c.Check(config.getDeploymentName(), gc.Equals, deploymentName)
   127  }
   128  
   129  func (*environProviderSuite) TestConfigGetInternalIP(c *gc.C) {
   130  	internalAddress := "10.76.200.59"
   131  	config := WALASharedConfig{Instances: []WALAInstance{{Address: internalAddress}}}
   132  
   133  	c.Check(config.getInternalIP(), gc.Equals, internalAddress)
   134  }
   135  
   136  func (*environProviderSuite) TestPublicAddress(c *gc.C) {
   137  	deploymentName := "b6de4c4c7d4a49c39270e0c57481fd9b"
   138  	cleanup := overrideWALASharedConfig(c, "deploymentid", deploymentName, "10.76.200.59")
   139  	defer cleanup()
   140  
   141  	expectedAddress := deploymentName + ".cloudapp.net"
   142  	prov := azureEnvironProvider{}
   143  	pubAddress, err := prov.PublicAddress()
   144  	c.Assert(err, gc.IsNil)
   145  	c.Check(pubAddress, gc.Equals, expectedAddress)
   146  }
   147  
   148  func (*environProviderSuite) TestPrivateAddress(c *gc.C) {
   149  	internalAddress := "10.76.200.59"
   150  	cleanup := overrideWALASharedConfig(c, "deploy-id", "name", internalAddress)
   151  	defer cleanup()
   152  
   153  	prov := azureEnvironProvider{}
   154  	privAddress, err := prov.PrivateAddress()
   155  	c.Assert(err, gc.IsNil)
   156  	c.Check(privAddress, gc.Equals, internalAddress)
   157  }