github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cloud/remove_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  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	jujucloud "github.com/juju/juju/cloud"
    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 removeSuite struct {
    19  	testing.FakeJujuXDGDataHomeSuite
    20  }
    21  
    22  var _ = gc.Suite(&removeSuite{})
    23  
    24  func (s *removeSuite) TestRemoveBadArgs(c *gc.C) {
    25  	cmd := cloud.NewRemoveCloudCommand()
    26  	_, err := testing.RunCommand(c, cmd)
    27  	c.Assert(err, gc.ErrorMatches, "Usage: juju remove-cloud <cloud name>")
    28  	_, err = testing.RunCommand(c, cmd, "cloud", "extra")
    29  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`)
    30  }
    31  
    32  func (s *removeSuite) TestRemoveNotFound(c *gc.C) {
    33  	cmd := cloud.NewRemoveCloudCommand()
    34  	ctx, err := testing.RunCommand(c, cmd, "fnord")
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	c.Assert(testing.Stderr(ctx), gc.Equals, "No personal cloud called \"fnord\" exists\n")
    37  }
    38  
    39  func (s *removeSuite) createTestCloudData(c *gc.C) {
    40  	err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("public-clouds.yaml"), []byte(`
    41  clouds:
    42    prodstack:
    43      type: openstack
    44      auth-types: [userpass, access-key]
    45      endpoint: http://homestack
    46  `[1:]), 0600)
    47  	c.Assert(err, jc.ErrorIsNil)
    48  
    49  	err = ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(`
    50  clouds:
    51    homestack:
    52      type: openstack
    53      auth-types: [userpass, access-key]
    54      endpoint: http://homestack
    55    homestack2:
    56      type: openstack
    57      auth-types: [userpass, access-key]
    58      endpoint: http://homestack2
    59  `[1:]), 0600)
    60  	c.Assert(err, jc.ErrorIsNil)
    61  }
    62  
    63  func (s *removeSuite) TestRemoveCloud(c *gc.C) {
    64  	s.createTestCloudData(c)
    65  	assertPersonalClouds(c, "homestack", "homestack2")
    66  	ctx, err := testing.RunCommand(c, cloud.NewRemoveCloudCommand(), "homestack")
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(testing.Stderr(ctx), gc.Equals, "Removed details of personal cloud \"homestack\"\n")
    69  	assertPersonalClouds(c, "homestack2")
    70  }
    71  
    72  func (s *removeSuite) TestCannotRemovePublicCloud(c *gc.C) {
    73  	s.createTestCloudData(c)
    74  	ctx, err := testing.RunCommand(c, cloud.NewRemoveCloudCommand(), "prodstack")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(testing.Stderr(ctx), gc.Equals, "No personal cloud called \"prodstack\" exists\n")
    77  }
    78  
    79  func assertPersonalClouds(c *gc.C, names ...string) {
    80  	personalClouds, err := jujucloud.PersonalCloudMetadata()
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	actual := make([]string, 0, len(personalClouds))
    83  	for name := range personalClouds {
    84  		actual = append(actual, name)
    85  	}
    86  	c.Assert(actual, jc.SameContents, names)
    87  }