github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"github.com/juju/cmd/cmdtesting"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	jujucloud "github.com/juju/juju/cloud"
    15  	"github.com/juju/juju/cmd/juju/cloud"
    16  	"github.com/juju/juju/juju/osenv"
    17  	_ "github.com/juju/juju/provider/all"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type listSuite struct {
    22  	testing.FakeJujuXDGDataHomeSuite
    23  }
    24  
    25  var _ = gc.Suite(&listSuite{})
    26  
    27  func (s *listSuite) TestListPublic(c *gc.C) {
    28  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand())
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	out := cmdtesting.Stdout(ctx)
    31  	out = strings.Replace(out, "\n", "", -1)
    32  
    33  	// Check that we are producing the expected fields
    34  	c.Assert(out, gc.Matches, `Cloud +Regions +Default +Type +Description.*`)
    35  	// // Just check couple of snippets of the output to make sure it looks ok.
    36  	c.Assert(out, gc.Matches, `.*aws +[0-9]+ +[a-z0-9-]+ +ec2 +Amazon Web Services.*`)
    37  	c.Assert(out, gc.Matches, `.*azure +[0-9]+ +[a-z0-9-]+ +azure +Microsoft Azure.*`)
    38  	// LXD should be there too.
    39  	c.Assert(out, gc.Matches, `.*localhost[ ]*1[ ]*localhost[ ]*lxd.*`)
    40  }
    41  
    42  func (s *listSuite) TestListPublicAndPersonal(c *gc.C) {
    43  	data := `
    44  clouds:
    45    homestack:
    46      type: openstack
    47      auth-types: [userpass, access-key]
    48      endpoint: http://homestack
    49      regions:
    50        london:
    51          endpoint: http://london/1.0
    52  `[1:]
    53  	err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600)
    54  	c.Assert(err, jc.ErrorIsNil)
    55  
    56  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand())
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	out := cmdtesting.Stdout(ctx)
    59  	out = strings.Replace(out, "\n", "", -1)
    60  	// Just check a snippet of the output to make sure it looks ok.
    61  	// local clouds are last.
    62  	// homestack should abut localhost and hence come last in the output.
    63  	c.Assert(out, jc.Contains, `Hypervisorhomestack             1  london           openstack   Openstack Cloud`)
    64  }
    65  
    66  func (s *listSuite) TestListPublicAndPersonalSameName(c *gc.C) {
    67  	data := `
    68  clouds:
    69    aws:
    70      type: ec2
    71      auth-types: [access-key]
    72      endpoint: http://custom
    73  `[1:]
    74  	err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml")
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	out := cmdtesting.Stdout(ctx)
    80  	out = strings.Replace(out, "\n", "", -1)
    81  	// Just check a snippet of the output to make sure it looks ok.
    82  	// local clouds are last.
    83  	c.Assert(out, gc.Not(gc.Matches), `.*aws:[ ]*defined: public[ ]*type: ec2[ ]*auth-types: \[access-key\].*`)
    84  	c.Assert(out, gc.Matches, `.*aws:[ ]*defined: local[ ]*type: ec2[ ]*description: Amazon Web Services[ ]*auth-types: \[access-key\].*`)
    85  }
    86  
    87  func (s *listSuite) TestListYAML(c *gc.C) {
    88  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml")
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	out := cmdtesting.Stdout(ctx)
    91  	out = strings.Replace(out, "\n", "", -1)
    92  	// Just check a snippet of the output to make sure it looks ok.
    93  	c.Assert(out, gc.Matches, `.*aws:[ ]*defined: public[ ]*type: ec2[ ]*description: Amazon Web Services[ ]*auth-types: \[access-key\].*`)
    94  }
    95  
    96  func (s *listSuite) TestListJSON(c *gc.C) {
    97  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "json")
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	out := cmdtesting.Stdout(ctx)
   100  	out = strings.Replace(out, "\n", "", -1)
   101  	// Just check a snippet of the output to make sure it looks ok.
   102  	c.Assert(out, gc.Matches, `.*{"aws":{"defined":"public","type":"ec2","description":"Amazon Web Services","auth-types":\["access-key"\].*`)
   103  }
   104  
   105  func (s *listSuite) TestListPreservesRegionOrder(c *gc.C) {
   106  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml")
   107  	c.Assert(err, jc.ErrorIsNil)
   108  	lines := strings.Split(cmdtesting.Stdout(ctx), "\n")
   109  	withClouds := "clouds:\n  " + strings.Join(lines, "\n  ")
   110  
   111  	parsedClouds, err := jujucloud.ParseCloudMetadata([]byte(withClouds))
   112  	c.Assert(err, jc.ErrorIsNil)
   113  	parsedCloud, ok := parsedClouds["aws"]
   114  	c.Assert(ok, jc.IsTrue) // aws found in output
   115  
   116  	aws, err := jujucloud.CloudByName("aws")
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	c.Assert(&parsedCloud, jc.DeepEquals, aws)
   119  }