github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/add_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloud_test 5 6 import ( 7 "io/ioutil" 8 "path/filepath" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/juju/cloud" 14 "github.com/juju/juju/juju/osenv" 15 "github.com/juju/juju/testing" 16 ) 17 18 type addSuite struct { 19 testing.BaseSuite 20 } 21 22 var _ = gc.Suite(&addSuite{}) 23 24 func (s *addSuite) SetUpTest(c *gc.C) { 25 origHome := osenv.SetJujuXDGDataHome(c.MkDir()) 26 s.AddCleanup(func(*gc.C) { osenv.SetJujuXDGDataHome(origHome) }) 27 } 28 29 func (s *addSuite) TestAddBadArgs(c *gc.C) { 30 addCmd := cloud.NewAddCloudCommand() 31 _, err := testing.RunCommand(c, addCmd) 32 c.Assert(err, gc.ErrorMatches, "Usage: juju add-cloud <cloud name> <cloud definition file>") 33 _, err = testing.RunCommand(c, addCmd, "cloud", "cloud.yaml", "extra") 34 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`) 35 } 36 37 func (s *addSuite) createTestCloudData(c *gc.C) string { 38 current := ` 39 clouds: 40 homestack: 41 type: openstack 42 auth-types: [userpass, access-key] 43 endpoint: http://homestack 44 regions: 45 london: 46 endpoint: http://london/1.0 47 `[1:] 48 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(current), 0600) 49 c.Assert(err, jc.ErrorIsNil) 50 51 sourceDir := c.MkDir() 52 sourceFile := filepath.Join(sourceDir, "someclouds.yaml") 53 source := ` 54 clouds: 55 homestack: 56 type: openstack 57 auth-types: [userpass, access-key] 58 endpoint: http://homestack 59 regions: 60 london: 61 endpoint: http://london/1.0 62 new-york: 63 endpoint: http://newyork/1.0 64 garage-maas: 65 type: mass 66 auth-types: [oauth] 67 endpoint: http://garagemaas 68 `[1:] 69 err = ioutil.WriteFile(sourceFile, []byte(source), 0600) 70 c.Assert(err, jc.ErrorIsNil) 71 return sourceFile 72 } 73 74 func (s *addSuite) TestAddBadFilename(c *gc.C) { 75 addCmd := cloud.NewAddCloudCommand() 76 _, err := testing.RunCommand(c, addCmd, "cloud", "somefile.yaml") 77 c.Assert(err, gc.ErrorMatches, "open somefile.yaml: .*") 78 } 79 80 func (s *addSuite) TestAddBadCloudName(c *gc.C) { 81 sourceFile := s.createTestCloudData(c) 82 addCmd := cloud.NewAddCloudCommand() 83 _, err := testing.RunCommand(c, addCmd, "cloud", sourceFile) 84 c.Assert(err, gc.ErrorMatches, `cloud "cloud" not found in file .*`) 85 } 86 87 func (s *addSuite) TestAddExisting(c *gc.C) { 88 sourceFile := s.createTestCloudData(c) 89 _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "homestack", sourceFile) 90 c.Assert(err, gc.ErrorMatches, `cloud called \"homestack\" already exists; use --replace to replace this existing cloud`) 91 } 92 93 func (s *addSuite) TestAddExistingReplace(c *gc.C) { 94 sourceFile := s.createTestCloudData(c) 95 _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "homestack", sourceFile, "--replace") 96 c.Assert(err, jc.ErrorIsNil) 97 data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) 98 c.Assert(string(data), gc.Equals, ` 99 clouds: 100 homestack: 101 type: openstack 102 auth-types: [userpass, access-key] 103 endpoint: http://homestack 104 regions: 105 london: 106 endpoint: http://london/1.0 107 new-york: 108 endpoint: http://newyork/1.0 109 `[1:]) 110 } 111 112 func (s *addSuite) TestAddNew(c *gc.C) { 113 sourceFile := s.createTestCloudData(c) 114 _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "garage-maas", sourceFile) 115 c.Assert(err, jc.ErrorIsNil) 116 data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) 117 c.Assert(string(data), gc.Equals, ` 118 clouds: 119 garage-maas: 120 type: mass 121 auth-types: [oauth] 122 endpoint: http://garagemaas 123 homestack: 124 type: openstack 125 auth-types: [userpass, access-key] 126 endpoint: http://homestack 127 regions: 128 london: 129 endpoint: http://london/1.0 130 `[1:]) 131 }