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