github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/init_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/juju/cmd"
    13  	gitjujutesting "github.com/juju/testing"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type InitSuite struct {
    21  	testing.FakeJujuHomeSuite
    22  }
    23  
    24  var _ = gc.Suite(&InitSuite{})
    25  
    26  // The environments.yaml is created by default if it
    27  // does not already exist.
    28  func (*InitSuite) TestBoilerPlateEnvironment(c *gc.C) {
    29  	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
    30  	err := os.Remove(envPath)
    31  	c.Assert(err, jc.ErrorIsNil)
    32  	ctx := testing.Context(c)
    33  	code := cmd.Main(&InitCommand{}, ctx, nil)
    34  	c.Check(code, gc.Equals, 0)
    35  	outStr := ctx.Stdout.(*bytes.Buffer).String()
    36  	strippedOut := strings.Replace(outStr, "\n", "", -1)
    37  	c.Check(strippedOut, gc.Matches, ".*A boilerplate environment configuration file has been written.*")
    38  	environpath := gitjujutesting.HomePath(".juju", "environments.yaml")
    39  	data, err := ioutil.ReadFile(environpath)
    40  	c.Assert(err, jc.ErrorIsNil)
    41  	strippedData := strings.Replace(string(data), "\n", "", -1)
    42  	c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*")
    43  }
    44  
    45  // The boilerplate is sent to stdout with --show, and the environments.yaml
    46  // is not created.
    47  func (*InitSuite) TestBoilerPlatePrinted(c *gc.C) {
    48  	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
    49  	err := os.Remove(envPath)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	ctx := testing.Context(c)
    52  	code := cmd.Main(&InitCommand{}, ctx, []string{"--show"})
    53  	c.Check(code, gc.Equals, 0)
    54  	outStr := ctx.Stdout.(*bytes.Buffer).String()
    55  	strippedOut := strings.Replace(outStr, "\n", "", -1)
    56  	c.Check(strippedOut, gc.Matches, ".*# This is the Juju config file, which you can use.*")
    57  	environpath := gitjujutesting.HomePath(".juju", "environments.yaml")
    58  	_, err = ioutil.ReadFile(environpath)
    59  	c.Assert(err, gc.NotNil)
    60  }
    61  
    62  const existingEnv = `
    63  environments:
    64      test:
    65          type: dummy
    66          state-server: false
    67          authorized-keys: i-am-a-key
    68  `
    69  
    70  // An existing environments.yaml will not be overwritten without
    71  // the explicit -f option.
    72  func (*InitSuite) TestExistingEnvironmentNotOverwritten(c *gc.C) {
    73  	testing.WriteEnvironments(c, existingEnv)
    74  
    75  	ctx := testing.Context(c)
    76  	code := cmd.Main(&InitCommand{}, ctx, nil)
    77  	c.Check(code, gc.Equals, 1)
    78  	errOut := ctx.Stderr.(*bytes.Buffer).String()
    79  	strippedOut := strings.Replace(errOut, "\n", "", -1)
    80  	c.Check(strippedOut, gc.Matches, ".*A juju environment configuration already exists.*")
    81  	environpath := gitjujutesting.HomePath(".juju", "environments.yaml")
    82  	data, err := ioutil.ReadFile(environpath)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(string(data), gc.Equals, existingEnv)
    85  }
    86  
    87  // An existing environments.yaml will be overwritten when -f is
    88  // given explicitly.
    89  func (*InitSuite) TestExistingEnvironmentOverwritten(c *gc.C) {
    90  	testing.WriteEnvironments(c, existingEnv)
    91  
    92  	ctx := testing.Context(c)
    93  	code := cmd.Main(&InitCommand{}, ctx, []string{"-f"})
    94  	c.Check(code, gc.Equals, 0)
    95  	stdOut := ctx.Stdout.(*bytes.Buffer).String()
    96  	strippedOut := strings.Replace(stdOut, "\n", "", -1)
    97  	c.Check(strippedOut, gc.Matches, ".*A boilerplate environment configuration file has been written.*")
    98  	environpath := gitjujutesting.HomePath(".juju", "environments.yaml")
    99  	data, err := ioutil.ReadFile(environpath)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	strippedData := strings.Replace(string(data), "\n", "", -1)
   102  	c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*")
   103  }