github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/commands/bootstrap_interactive_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"bufio"
     8  	"bytes"
     9  	"io/ioutil"
    10  	"strings"
    11  
    12  	"github.com/juju/cmd/cmdtesting"
    13  	"github.com/juju/testing"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	jujucloud "github.com/juju/juju/cloud"
    18  	"github.com/juju/juju/version"
    19  )
    20  
    21  type BSInteractSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(BSInteractSuite{})
    26  
    27  func (BSInteractSuite) TestInitEmpty(c *gc.C) {
    28  	cmd := &bootstrapCommand{}
    29  	err := cmdtesting.InitCommand(cmd, nil)
    30  	c.Assert(err, jc.ErrorIsNil)
    31  	c.Assert(cmd.interactive, jc.IsTrue)
    32  }
    33  
    34  func (BSInteractSuite) TestInitBuildAgent(c *gc.C) {
    35  	cmd := &bootstrapCommand{}
    36  	err := cmdtesting.InitCommand(cmd, []string{"--build-agent"})
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	c.Assert(cmd.interactive, jc.IsTrue)
    39  	c.Assert(cmd.BuildAgent, jc.IsTrue)
    40  }
    41  
    42  func (BSInteractSuite) TestInitArg(c *gc.C) {
    43  	cmd := &bootstrapCommand{}
    44  	err := cmdtesting.InitCommand(cmd, []string{"foo"})
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(cmd.interactive, jc.IsFalse)
    47  }
    48  
    49  func (BSInteractSuite) TestInitTwoArgs(c *gc.C) {
    50  	cmd := &bootstrapCommand{}
    51  	err := cmdtesting.InitCommand(cmd, []string{"foo", "bar"})
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	c.Assert(cmd.interactive, jc.IsFalse)
    54  }
    55  
    56  func (BSInteractSuite) TestInitInfoOnlyFlag(c *gc.C) {
    57  	cmd := &bootstrapCommand{}
    58  	err := cmdtesting.InitCommand(cmd, []string{"--clouds"})
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(cmd.interactive, jc.IsFalse)
    61  }
    62  
    63  func (BSInteractSuite) TestInitVariousFlags(c *gc.C) {
    64  	cmd := &bootstrapCommand{}
    65  	err := cmdtesting.InitCommand(cmd, []string{"--keep-broken", "--agent-version", version.Current.String()})
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(cmd.interactive, jc.IsTrue)
    68  }
    69  
    70  func (BSInteractSuite) TestQueryCloud(c *gc.C) {
    71  	input := "search\n"
    72  
    73  	scanner := bufio.NewScanner(strings.NewReader(input))
    74  	clouds := []string{"books", "books-china", "search", "local"}
    75  
    76  	buf := bytes.Buffer{}
    77  	cloud, err := queryCloud(clouds, "local", scanner, &buf)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(cloud, gc.Equals, "search")
    80  
    81  	// clouds should be printed out in the same order as they're given.
    82  	expected := `
    83  Clouds
    84  books
    85  books-china
    86  search
    87  local
    88  
    89  Select a cloud [local]: 
    90  `[1:]
    91  	c.Assert(buf.String(), gc.Equals, expected)
    92  }
    93  
    94  func (BSInteractSuite) TestQueryCloudDefault(c *gc.C) {
    95  	input := "\n"
    96  
    97  	scanner := bufio.NewScanner(strings.NewReader(input))
    98  	clouds := []string{"books", "local"}
    99  
   100  	cloud, err := queryCloud(clouds, "local", scanner, ioutil.Discard)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(cloud, gc.Equals, "local")
   103  }
   104  
   105  func (BSInteractSuite) TestInvalidCloud(c *gc.C) {
   106  	input := "bad^cloud\n"
   107  
   108  	scanner := bufio.NewScanner(strings.NewReader(input))
   109  	clouds := []string{"books", "local", "bad^cloud"}
   110  
   111  	_, err := queryCloud(clouds, "local", scanner, ioutil.Discard)
   112  	c.Assert(err, gc.ErrorMatches, `cloud name "bad\^cloud" not valid`)
   113  }
   114  
   115  func (BSInteractSuite) TestQueryRegion(c *gc.C) {
   116  	input := "mars-west1\n"
   117  
   118  	scanner := bufio.NewScanner(strings.NewReader(input))
   119  	regions := []jujucloud.Region{
   120  		{Name: "mars-east1"},
   121  		{Name: "mars-west1"},
   122  		{Name: "jupiter-central"},
   123  	}
   124  
   125  	buf := bytes.Buffer{}
   126  	region, err := queryRegion("goggles", regions, scanner, &buf)
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(region, gc.Equals, "mars-west1")
   129  
   130  	// regions should be alphabetized, and the first one in the original list
   131  	// should be the default.
   132  	expected := `
   133  Regions in goggles:
   134  jupiter-central
   135  mars-east1
   136  mars-west1
   137  
   138  Select a region in goggles [mars-east1]: 
   139  `[1:]
   140  	c.Assert(buf.String(), gc.Equals, expected)
   141  }
   142  
   143  func (BSInteractSuite) TestQueryRegionDefault(c *gc.C) {
   144  	input := "\n"
   145  
   146  	scanner := bufio.NewScanner(strings.NewReader(input))
   147  	regions := []jujucloud.Region{
   148  		{Name: "mars-east1"},
   149  		{Name: "jupiter-central"},
   150  	}
   151  
   152  	region, err := queryRegion("goggles", regions, scanner, ioutil.Discard)
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(region, gc.Equals, regions[0].Name)
   155  }
   156  
   157  func (BSInteractSuite) TestQueryName(c *gc.C) {
   158  	input := "awesome-cloud\n"
   159  
   160  	scanner := bufio.NewScanner(strings.NewReader(input))
   161  	buf := bytes.Buffer{}
   162  	name, err := queryName("default-cloud", scanner, &buf)
   163  	c.Assert(err, jc.ErrorIsNil)
   164  	c.Assert(name, gc.Equals, "awesome-cloud")
   165  
   166  	expected := `
   167  Enter a name for the Controller [default-cloud]: 
   168  `[1:]
   169  	c.Assert(buf.String(), gc.Equals, expected)
   170  }
   171  
   172  func (BSInteractSuite) TestQueryNameDefault(c *gc.C) {
   173  	input := "\n"
   174  
   175  	scanner := bufio.NewScanner(strings.NewReader(input))
   176  	name, err := queryName("default-cloud", scanner, ioutil.Discard)
   177  	c.Assert(err, jc.ErrorIsNil)
   178  	c.Assert(name, gc.Equals, "default-cloud")
   179  }