github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cloud/list_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloud_test 5 6 import ( 7 "io/ioutil" 8 "strings" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 jujucloud "github.com/juju/juju/cloud" 14 "github.com/juju/juju/cmd/juju/cloud" 15 "github.com/juju/juju/juju/osenv" 16 _ "github.com/juju/juju/provider/all" 17 "github.com/juju/juju/testing" 18 ) 19 20 type listSuite struct { 21 testing.FakeJujuXDGDataHomeSuite 22 } 23 24 var _ = gc.Suite(&listSuite{}) 25 26 func (s *listSuite) TestListPublic(c *gc.C) { 27 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand()) 28 c.Assert(err, jc.ErrorIsNil) 29 out := testing.Stdout(ctx) 30 out = strings.Replace(out, "\n", "", -1) 31 // Just check couple of snippets of the output to make sure it looks ok. 32 c.Assert(out, gc.Matches, `.*aws-china[ ]*ec2[ ]*cn-north-1.*`) 33 // TODO(wallyworld) - uncomment when we build with go 1.3 or greater 34 // LXD should be there too. 35 // c.Assert(out, gc.Matches, `.*localhost[ ]*lxd[ ]*localhost.*`) 36 } 37 38 func (s *listSuite) TestListPublicAndPersonal(c *gc.C) { 39 data := ` 40 clouds: 41 homestack: 42 type: openstack 43 auth-types: [userpass, access-key] 44 endpoint: http://homestack 45 regions: 46 london: 47 endpoint: http://london/1.0 48 `[1:] 49 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600) 50 c.Assert(err, jc.ErrorIsNil) 51 52 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand()) 53 c.Assert(err, jc.ErrorIsNil) 54 out := testing.Stdout(ctx) 55 out = strings.Replace(out, "\n", "", -1) 56 // Just check a snippet of the output to make sure it looks ok. 57 // local clouds are last. 58 // homestack should abut localhost and hence come last in the output. 59 c.Assert(out, jc.Contains, `localhosthomestack openstack london`) 60 } 61 62 func (s *listSuite) TestListPublicAndPersonalSameName(c *gc.C) { 63 data := ` 64 clouds: 65 aws: 66 type: ec2 67 auth-types: [access-key] 68 endpoint: http://custom 69 `[1:] 70 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600) 71 c.Assert(err, jc.ErrorIsNil) 72 73 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml") 74 c.Assert(err, jc.ErrorIsNil) 75 out := testing.Stdout(ctx) 76 out = strings.Replace(out, "\n", "", -1) 77 // Just check a snippet of the output to make sure it looks ok. 78 // local clouds are last. 79 c.Assert(out, gc.Not(gc.Matches), `.*aws:[ ]*defined: public[ ]*type: ec2[ ]*auth-types: \[access-key\].*`) 80 c.Assert(out, gc.Matches, `.*aws:[ ]*defined: local[ ]*type: ec2[ ]*auth-types: \[access-key\].*`) 81 } 82 83 func (s *listSuite) TestListYAML(c *gc.C) { 84 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml") 85 c.Assert(err, jc.ErrorIsNil) 86 out := testing.Stdout(ctx) 87 out = strings.Replace(out, "\n", "", -1) 88 // Just check a snippet of the output to make sure it looks ok. 89 c.Assert(out, gc.Matches, `.*aws:[ ]*defined: public[ ]*type: ec2[ ]*auth-types: \[access-key\].*`) 90 } 91 92 func (s *listSuite) TestListJSON(c *gc.C) { 93 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "json") 94 c.Assert(err, jc.ErrorIsNil) 95 out := testing.Stdout(ctx) 96 out = strings.Replace(out, "\n", "", -1) 97 // Just check a snippet of the output to make sure it looks ok. 98 c.Assert(out, gc.Matches, `.*{"aws":{"defined":"public","type":"ec2","auth-types":\["access-key"\].*`) 99 } 100 101 func (s *listSuite) TestListPreservesRegionOrder(c *gc.C) { 102 ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml") 103 c.Assert(err, jc.ErrorIsNil) 104 lines := strings.Split(testing.Stdout(ctx), "\n") 105 withClouds := "clouds:\n " + strings.Join(lines, "\n ") 106 107 parsedClouds, err := jujucloud.ParseCloudMetadata([]byte(withClouds)) 108 c.Assert(err, jc.ErrorIsNil) 109 parsedCloud, ok := parsedClouds["aws"] 110 c.Assert(ok, jc.IsTrue) // aws found in output 111 112 aws, err := jujucloud.CloudByName("aws") 113 c.Assert(err, jc.ErrorIsNil) 114 c.Assert(&parsedCloud, jc.DeepEquals, aws) 115 }