github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/space/add_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package space_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/cmd/juju/space" 13 ) 14 15 type AddSuite struct { 16 BaseSpaceSuite 17 } 18 19 var _ = gc.Suite(&AddSuite{}) 20 21 func (s *AddSuite) SetUpTest(c *gc.C) { 22 s.BaseSpaceSuite.SetUpTest(c) 23 s.newCommand = space.NewAddCommand 24 } 25 26 func (s *AddSuite) TestRunWithoutSubnetsSucceeds(c *gc.C) { 27 s.AssertRunSucceeds(c, 28 `added space "myspace" with no subnets\n`, 29 "", // no stdout, just stderr 30 "myspace", 31 ) 32 33 s.api.CheckCallNames(c, "AddSpace", "Close") 34 s.api.CheckCall(c, 0, "AddSpace", "myspace", []string(nil), true) 35 } 36 37 func (s *AddSuite) TestRunWithSubnetsSucceeds(c *gc.C) { 38 s.AssertRunSucceeds(c, 39 `added space "myspace" with subnets 10.1.2.0/24, 4.3.2.0/28\n`, 40 "", // no stdout, just stderr 41 "myspace", "10.1.2.0/24", "4.3.2.0/28", 42 ) 43 44 s.api.CheckCallNames(c, "AddSpace", "Close") 45 s.api.CheckCall(c, 46 0, "AddSpace", 47 "myspace", s.Strings("10.1.2.0/24", "4.3.2.0/28"), true, 48 ) 49 } 50 51 func (s *AddSuite) TestRunWhenSpacesNotSupported(c *gc.C) { 52 s.api.SetErrors(errors.NewNotSupported(nil, "spaces not supported")) 53 54 err := s.AssertRunSpacesNotSupported(c, 55 `cannot add space "foo": spaces not supported`, 56 "foo", "10.1.2.0/24", 57 ) 58 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 59 60 s.api.CheckCallNames(c, "AddSpace", "Close") 61 s.api.CheckCall(c, 0, "AddSpace", "foo", s.Strings("10.1.2.0/24"), true) 62 } 63 64 func (s *AddSuite) TestRunWhenSpacesAPIFails(c *gc.C) { 65 s.api.SetErrors(errors.New("API error")) 66 67 s.AssertRunFails(c, 68 `cannot add space "foo": API error`, 69 "foo", "10.1.2.0/24", 70 ) 71 72 s.api.CheckCallNames(c, "AddSpace", "Close") 73 s.api.CheckCall(c, 0, "AddSpace", "foo", s.Strings("10.1.2.0/24"), true) 74 } 75 76 func (s *AddSuite) TestRunUnauthorizedMentionsJujuGrant(c *gc.C) { 77 s.api.SetErrors(¶ms.Error{ 78 Message: "permission denied", 79 Code: params.CodeUnauthorized, 80 }) 81 82 s.AssertRunFailsUnauthorized(c, 83 `*.juju grant.*`, 84 "foo", "10.1.2.0/24", 85 ) 86 }