github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	jujucloud "github.com/juju/juju/cloud"
    17  	jujutesting "github.com/juju/juju/testing"
    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 := jujutesting.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 := jujutesting.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 := jujutesting.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 := jujutesting.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 := jujutesting.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 := jujutesting.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) TestQueryRegion(c *gc.C) {
   106  	input := "mars-west1\n"
   107  
   108  	scanner := bufio.NewScanner(strings.NewReader(input))
   109  	regions := []jujucloud.Region{
   110  		{Name: "mars-east1"},
   111  		{Name: "mars-west1"},
   112  		{Name: "jupiter-central"},
   113  	}
   114  
   115  	buf := bytes.Buffer{}
   116  	region, err := queryRegion("goggles", regions, scanner, &buf)
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	c.Assert(region, gc.Equals, "mars-west1")
   119  
   120  	// regions should be alphabetized, and the first one in the original list
   121  	// should be the default.
   122  	expected := `
   123  Regions in goggles:
   124  jupiter-central
   125  mars-east1
   126  mars-west1
   127  
   128  Select a region in goggles [mars-east1]: 
   129  `[1:]
   130  	c.Assert(buf.String(), gc.Equals, expected)
   131  }
   132  
   133  func (BSInteractSuite) TestQueryRegionDefault(c *gc.C) {
   134  	input := "\n"
   135  
   136  	scanner := bufio.NewScanner(strings.NewReader(input))
   137  	regions := []jujucloud.Region{
   138  		{Name: "mars-east1"},
   139  		{Name: "jupiter-central"},
   140  	}
   141  
   142  	region, err := queryRegion("goggles", regions, scanner, ioutil.Discard)
   143  	c.Assert(err, jc.ErrorIsNil)
   144  	c.Assert(region, gc.Equals, regions[0].Name)
   145  }
   146  
   147  func (BSInteractSuite) TestQueryName(c *gc.C) {
   148  	input := "awesome-cloud\n"
   149  
   150  	scanner := bufio.NewScanner(strings.NewReader(input))
   151  	buf := bytes.Buffer{}
   152  	name, err := queryName("default-cloud", scanner, &buf)
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(name, gc.Equals, "awesome-cloud")
   155  
   156  	expected := `
   157  Enter a name for the Controller [default-cloud]: 
   158  `[1:]
   159  	c.Assert(buf.String(), gc.Equals, expected)
   160  }
   161  
   162  func (BSInteractSuite) TestQueryNameDefault(c *gc.C) {
   163  	input := "\n"
   164  
   165  	scanner := bufio.NewScanner(strings.NewReader(input))
   166  	name, err := queryName("default-cloud", scanner, ioutil.Discard)
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	c.Assert(name, gc.Equals, "default-cloud")
   169  }