github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cloud/add_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cmd/juju/cloud"
    14  	"github.com/juju/juju/juju/osenv"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  type addSuite struct {
    19  	testing.FakeJujuXDGDataHomeSuite
    20  }
    21  
    22  var _ = gc.Suite(&addSuite{})
    23  
    24  func (s *addSuite) TestAddBadArgs(c *gc.C) {
    25  	addCmd := cloud.NewAddCloudCommand()
    26  	_, err := testing.RunCommand(c, addCmd)
    27  	c.Assert(err, gc.ErrorMatches, "Usage: juju add-cloud <cloud name> <cloud definition file>")
    28  	_, err = testing.RunCommand(c, addCmd, "cloud", "cloud.yaml", "extra")
    29  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`)
    30  }
    31  
    32  func (s *addSuite) createTestCloudData(c *gc.C) string {
    33  	current := `
    34  clouds:
    35    homestack:
    36      type: openstack
    37      auth-types: [userpass, access-key]
    38      endpoint: http://homestack
    39      regions:
    40        london:
    41          endpoint: http://london/1.0
    42  `[1:]
    43  	err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(current), 0600)
    44  	c.Assert(err, jc.ErrorIsNil)
    45  
    46  	sourceDir := c.MkDir()
    47  	sourceFile := filepath.Join(sourceDir, "someclouds.yaml")
    48  	source := `
    49  clouds:
    50    aws:
    51      type: ec2
    52      auth-types: [ access-key ]
    53      regions:
    54        us-east-1:
    55          endpoint: https://us-east-1.aws.amazon.com/v1.2/
    56    localhost:
    57      type: lxd
    58    homestack:
    59      type: openstack
    60      auth-types: [userpass, access-key]
    61      endpoint: http://homestack
    62      regions:
    63        london:
    64          endpoint: http://london/1.0
    65        new-york:
    66          endpoint: http://newyork/1.0
    67    garage-maas:
    68      type: mass
    69      auth-types: [oauth]
    70      endpoint: http://garagemaas
    71  `[1:]
    72  	err = ioutil.WriteFile(sourceFile, []byte(source), 0600)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	return sourceFile
    75  }
    76  
    77  func (s *addSuite) TestAddBadFilename(c *gc.C) {
    78  	addCmd := cloud.NewAddCloudCommand()
    79  	_, err := testing.RunCommand(c, addCmd, "cloud", "somefile.yaml")
    80  	c.Assert(err, gc.ErrorMatches, "open somefile.yaml: .*")
    81  }
    82  
    83  func (s *addSuite) TestAddBadCloudName(c *gc.C) {
    84  	sourceFile := s.createTestCloudData(c)
    85  	addCmd := cloud.NewAddCloudCommand()
    86  	_, err := testing.RunCommand(c, addCmd, "cloud", sourceFile)
    87  	c.Assert(err, gc.ErrorMatches, `cloud "cloud" not found in file .*`)
    88  }
    89  
    90  func (s *addSuite) TestAddExisting(c *gc.C) {
    91  	sourceFile := s.createTestCloudData(c)
    92  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "homestack", sourceFile)
    93  	c.Assert(err, gc.ErrorMatches, `\"homestack\" already exists; use --replace to replace this existing cloud`)
    94  }
    95  
    96  func (s *addSuite) TestAddExistingReplace(c *gc.C) {
    97  	sourceFile := s.createTestCloudData(c)
    98  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "homestack", sourceFile, "--replace")
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml"))
   101  	c.Assert(string(data), gc.Equals, `
   102  clouds:
   103    homestack:
   104      type: openstack
   105      auth-types: [userpass, access-key]
   106      endpoint: http://homestack
   107      regions:
   108        london:
   109          endpoint: http://london/1.0
   110        new-york:
   111          endpoint: http://newyork/1.0
   112  `[1:])
   113  }
   114  
   115  func (s *addSuite) TestAddExistingPublic(c *gc.C) {
   116  	sourceFile := s.createTestCloudData(c)
   117  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "aws", sourceFile)
   118  	c.Assert(err, gc.ErrorMatches, `\"aws\" is the name of a public cloud; use --replace to use your cloud definition instead`)
   119  }
   120  
   121  func (s *addSuite) TestAddExistingBuiltin(c *gc.C) {
   122  	sourceFile := s.createTestCloudData(c)
   123  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "localhost", sourceFile)
   124  	c.Assert(err, gc.ErrorMatches, `\"localhost\" is the name of a built-in cloud; use --replace to use your cloud definition instead`)
   125  }
   126  
   127  func (s *addSuite) TestAddExistingPublicReplace(c *gc.C) {
   128  	sourceFile := s.createTestCloudData(c)
   129  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "aws", sourceFile, "--replace")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml"))
   132  	c.Assert(string(data), gc.Equals, `
   133  clouds:
   134    aws:
   135      type: ec2
   136      auth-types: [access-key]
   137      regions:
   138        us-east-1:
   139          endpoint: https://us-east-1.aws.amazon.com/v1.2/
   140    homestack:
   141      type: openstack
   142      auth-types: [userpass, access-key]
   143      endpoint: http://homestack
   144      regions:
   145        london:
   146          endpoint: http://london/1.0
   147  `[1:])
   148  }
   149  
   150  func (s *addSuite) TestAddNew(c *gc.C) {
   151  	sourceFile := s.createTestCloudData(c)
   152  	_, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "garage-maas", sourceFile)
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml"))
   155  	c.Assert(string(data), gc.Equals, `
   156  clouds:
   157    garage-maas:
   158      type: mass
   159      auth-types: [oauth]
   160      endpoint: http://garagemaas
   161    homestack:
   162      type: openstack
   163      auth-types: [userpass, access-key]
   164      endpoint: http://homestack
   165      regions:
   166        london:
   167          endpoint: http://london/1.0
   168  `[1:])
   169  }