github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/space/package_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 stdtesting "testing" 8 9 "github.com/juju/cmd" 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/utils/featureflag" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cmd/juju/space" 17 "github.com/juju/juju/feature" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 func TestPackage(t *stdtesting.T) { 22 gc.TestingT(t) 23 } 24 25 // BaseSpaceSuite is used for embedding in other suites. 26 type BaseSpaceSuite struct { 27 coretesting.FakeJujuXDGDataHomeSuite 28 coretesting.BaseSuite 29 30 command cmd.Command 31 api *StubAPI 32 } 33 34 var _ = gc.Suite(&BaseSpaceSuite{}) 35 36 func (s *BaseSpaceSuite) SetUpSuite(c *gc.C) { 37 s.FakeJujuXDGDataHomeSuite.SetUpSuite(c) 38 s.BaseSuite.SetUpSuite(c) 39 } 40 41 func (s *BaseSpaceSuite) TearDownSuite(c *gc.C) { 42 s.BaseSuite.TearDownSuite(c) 43 s.FakeJujuXDGDataHomeSuite.TearDownSuite(c) 44 } 45 46 func (s *BaseSpaceSuite) SetUpTest(c *gc.C) { 47 // If any post-MVP command suite enabled the flag, keep it. 48 hasFeatureFlag := featureflag.Enabled(feature.PostNetCLIMVP) 49 50 s.BaseSuite.SetUpTest(c) 51 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 52 53 if hasFeatureFlag { 54 s.BaseSuite.SetFeatureFlags(feature.PostNetCLIMVP) 55 } 56 57 s.api = NewStubAPI() 58 c.Assert(s.api, gc.NotNil) 59 60 // All subcommand suites embedding this one should initialize 61 // s.command immediately after calling this method! 62 } 63 64 func (s *BaseSpaceSuite) TearDownTest(c *gc.C) { 65 s.FakeJujuXDGDataHomeSuite.TearDownTest(c) 66 s.BaseSuite.TearDownTest(c) 67 } 68 69 // RunCommand executes the s.command subcommand passing any args 70 // and returning the stdout and stderr output as strings, as well as 71 // any error. 72 func (s *BaseSpaceSuite) RunCommand(c *gc.C, args ...string) (string, string, error) { 73 if s.command == nil { 74 panic("subcommand is nil") 75 } 76 ctx, err := coretesting.RunCommand(c, s.command, args...) 77 if ctx != nil { 78 return coretesting.Stdout(ctx), coretesting.Stderr(ctx), err 79 } 80 return "", "", err 81 } 82 83 // AssertRunSpacesNotSupported is a shortcut for calling RunCommand with the 84 // passed args then asserting the output is empty and the error is the 85 // spaces not supported, finally returning the error. 86 func (s *BaseSpaceSuite) AssertRunSpacesNotSupported(c *gc.C, expectErr string, args ...string) error { 87 stdout, stderr, err := s.RunCommand(c, args...) 88 c.Assert(err, gc.ErrorMatches, expectErr) 89 c.Assert(stdout, gc.Equals, "") 90 c.Assert(stderr, gc.Equals, expectErr+"\n") 91 return err 92 } 93 94 // AssertRunFails is a shortcut for calling RunCommand with the 95 // passed args then asserting the output is empty and the error is as 96 // expected, finally returning the error. 97 func (s *BaseSpaceSuite) AssertRunFails(c *gc.C, expectErr string, args ...string) error { 98 stdout, stderr, err := s.RunCommand(c, args...) 99 c.Assert(err, gc.ErrorMatches, expectErr) 100 c.Assert(stdout, gc.Equals, "") 101 c.Assert(stderr, gc.Equals, "") 102 return err 103 } 104 105 // AssertRunSucceeds is a shortcut for calling RunSuperCommand with 106 // the passed args then asserting the stderr output matches 107 // expectStderr, stdout is equal to expectStdout, and the error is 108 // nil. 109 func (s *BaseSpaceSuite) AssertRunSucceeds(c *gc.C, expectStderr, expectStdout string, args ...string) { 110 stdout, stderr, err := s.RunCommand(c, args...) 111 c.Assert(err, jc.ErrorIsNil) 112 c.Assert(stdout, gc.Equals, expectStdout) 113 c.Assert(stderr, gc.Matches, expectStderr) 114 } 115 116 // Strings is makes tests taking a slice of strings slightly easier to 117 // write: e.g. s.Strings("foo", "bar") vs. []string{"foo", "bar"}. 118 func (s *BaseSpaceSuite) Strings(values ...string) []string { 119 return values 120 } 121 122 // StubAPI defines a testing stub for the SpaceAPI interface. 123 type StubAPI struct { 124 *testing.Stub 125 126 Spaces []params.Space 127 Subnets []params.Subnet 128 } 129 130 var _ space.SpaceAPI = (*StubAPI)(nil) 131 132 // NewStubAPI creates a StubAPI suitable for passing to 133 // space.New*Command(). 134 func NewStubAPI() *StubAPI { 135 subnets := []params.Subnet{{ 136 // IPv6 subnet. 137 CIDR: "2001:db8::/32", 138 ProviderId: "subnet-public", 139 Life: params.Dying, 140 SpaceTag: "space-space1", 141 Zones: []string{"zone2"}, 142 }, { 143 // Invalid subnet (just for 100% coverage, otherwise it can't happen). 144 CIDR: "invalid", 145 ProviderId: "no-such", 146 SpaceTag: "space-space1", 147 Zones: []string{"zone1"}, 148 }, { 149 // IPv4 subnet. 150 CIDR: "10.1.2.0/24", 151 ProviderId: "subnet-private", 152 Life: params.Alive, 153 SpaceTag: "space-space2", 154 Zones: []string{"zone1", "zone2"}, 155 }, { 156 // IPv4 VLAN subnet. 157 CIDR: "4.3.2.0/28", 158 Life: params.Dead, 159 ProviderId: "vlan-42", 160 SpaceTag: "space-space2", 161 Zones: []string{"zone1"}, 162 VLANTag: 42, 163 }} 164 spaces := []params.Space{{ 165 Name: "space1", 166 Subnets: append([]params.Subnet{}, subnets[:2]...), 167 }, { 168 Name: "space2", 169 Subnets: append([]params.Subnet{}, subnets[2:]...), 170 }} 171 return &StubAPI{ 172 Stub: &testing.Stub{}, 173 Spaces: spaces, 174 Subnets: subnets, 175 } 176 } 177 178 func (sa *StubAPI) Close() error { 179 sa.MethodCall(sa, "Close") 180 return sa.NextErr() 181 } 182 183 func (sa *StubAPI) ListSpaces() ([]params.Space, error) { 184 sa.MethodCall(sa, "ListSpaces") 185 if err := sa.NextErr(); err != nil { 186 return nil, err 187 } 188 return sa.Spaces, nil 189 } 190 191 func (sa *StubAPI) AddSpace(name string, subnetIds []string, public bool) error { 192 sa.MethodCall(sa, "AddSpace", name, subnetIds, public) 193 return sa.NextErr() 194 } 195 196 func (sa *StubAPI) RemoveSpace(name string) error { 197 sa.MethodCall(sa, "RemoveSpace", name) 198 return sa.NextErr() 199 } 200 201 func (sa *StubAPI) UpdateSpace(name string, subnetIds []string) error { 202 sa.MethodCall(sa, "UpdateSpace", name, subnetIds) 203 return sa.NextErr() 204 } 205 206 func (sa *StubAPI) RenameSpace(name, newName string) error { 207 sa.MethodCall(sa, "RenameSpace", name, newName) 208 return sa.NextErr() 209 }