launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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 "strings" 10 11 gc "launchpad.net/gocheck" 12 13 "launchpad.net/juju-core/cmd" 14 "launchpad.net/juju-core/testing" 15 ) 16 17 type InitSuite struct { 18 } 19 20 var _ = gc.Suite(&InitSuite{}) 21 22 // The environments.yaml is created by default if it 23 // does not already exist. 24 func (*InitSuite) TestBoilerPlateEnvironment(c *gc.C) { 25 defer testing.MakeEmptyFakeHome(c).Restore() 26 ctx := testing.Context(c) 27 code := cmd.Main(&InitCommand{}, ctx, nil) 28 c.Check(code, gc.Equals, 0) 29 outStr := ctx.Stdout.(*bytes.Buffer).String() 30 strippedOut := strings.Replace(outStr, "\n", "", -1) 31 c.Check(strippedOut, gc.Matches, ".*A boilerplate environment configuration file has been written.*") 32 environpath := testing.HomePath(".juju", "environments.yaml") 33 data, err := ioutil.ReadFile(environpath) 34 c.Assert(err, gc.IsNil) 35 strippedData := strings.Replace(string(data), "\n", "", -1) 36 c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*") 37 } 38 39 // The boilerplate is sent to stdout with --show, and the environments.yaml 40 // is not created. 41 func (*InitSuite) TestBoilerPlatePrinted(c *gc.C) { 42 defer testing.MakeEmptyFakeHome(c).Restore() 43 ctx := testing.Context(c) 44 code := cmd.Main(&InitCommand{}, ctx, []string{"--show"}) 45 c.Check(code, gc.Equals, 0) 46 outStr := ctx.Stdout.(*bytes.Buffer).String() 47 strippedOut := strings.Replace(outStr, "\n", "", -1) 48 c.Check(strippedOut, gc.Matches, ".*# This is the Juju config file, which you can use.*") 49 environpath := testing.HomePath(".juju", "environments.yaml") 50 _, err := ioutil.ReadFile(environpath) 51 c.Assert(err, gc.NotNil) 52 } 53 54 const existingEnv = ` 55 environments: 56 test: 57 type: dummy 58 state-server: false 59 authorized-keys: i-am-a-key 60 ` 61 62 // An existing environments.yaml will not be overwritten without 63 // the explicit -f option. 64 func (*InitSuite) TestExistingEnvironmentNotOverwritten(c *gc.C) { 65 defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() 66 67 ctx := testing.Context(c) 68 code := cmd.Main(&InitCommand{}, ctx, nil) 69 c.Check(code, gc.Equals, 1) 70 errOut := ctx.Stderr.(*bytes.Buffer).String() 71 strippedOut := strings.Replace(errOut, "\n", "", -1) 72 c.Check(strippedOut, gc.Matches, ".*A juju environment configuration already exists.*") 73 environpath := testing.HomePath(".juju", "environments.yaml") 74 data, err := ioutil.ReadFile(environpath) 75 c.Assert(err, gc.IsNil) 76 c.Assert(string(data), gc.Equals, existingEnv) 77 } 78 79 // An existing environments.yaml will be overwritten when -f is 80 // given explicitly. 81 func (*InitSuite) TestExistingEnvironmentOverwritten(c *gc.C) { 82 defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() 83 84 ctx := testing.Context(c) 85 code := cmd.Main(&InitCommand{}, ctx, []string{"-f"}) 86 c.Check(code, gc.Equals, 0) 87 stdOut := ctx.Stdout.(*bytes.Buffer).String() 88 strippedOut := strings.Replace(stdOut, "\n", "", -1) 89 c.Check(strippedOut, gc.Matches, ".*A boilerplate environment configuration file has been written.*") 90 environpath := testing.HomePath(".juju", "environments.yaml") 91 data, err := ioutil.ReadFile(environpath) 92 c.Assert(err, gc.IsNil) 93 strippedData := strings.Replace(string(data), "\n", "", -1) 94 c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*") 95 }