github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/subnet/package_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package subnet_test 5 6 import ( 7 stdtesting "testing" 8 9 "github.com/juju/cmd" 10 "github.com/juju/cmd/cmdtesting" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 "github.com/juju/utils/featureflag" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/names.v2" 16 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/cmd/juju/subnet" 19 "github.com/juju/juju/cmd/modelcmd" 20 "github.com/juju/juju/feature" 21 "github.com/juju/juju/jujuclient/jujuclienttesting" 22 "github.com/juju/juju/network" 23 coretesting "github.com/juju/juju/testing" 24 ) 25 26 func TestPackage(t *stdtesting.T) { 27 gc.TestingT(t) 28 } 29 30 // BaseSubnetSuite is used for embedding in other suites. 31 type BaseSubnetSuite struct { 32 coretesting.FakeJujuXDGDataHomeSuite 33 34 newCommand func() modelcmd.ModelCommand 35 api *StubAPI 36 } 37 38 var _ = gc.Suite(&BaseSubnetSuite{}) 39 40 func (s *BaseSubnetSuite) SetUpSuite(c *gc.C) { 41 s.FakeJujuXDGDataHomeSuite.SetUpSuite(c) 42 } 43 44 func (s *BaseSubnetSuite) TearDownSuite(c *gc.C) { 45 s.FakeJujuXDGDataHomeSuite.TearDownSuite(c) 46 } 47 48 func (s *BaseSubnetSuite) SetUpTest(c *gc.C) { 49 // If any post-MVP command suite enabled the flag, keep it. 50 hasFeatureFlag := featureflag.Enabled(feature.PostNetCLIMVP) 51 52 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 53 54 if hasFeatureFlag { 55 s.FakeJujuXDGDataHomeSuite.SetFeatureFlags(feature.PostNetCLIMVP) 56 } 57 58 s.api = NewStubAPI() 59 c.Assert(s.api, gc.NotNil) 60 61 // All subcommand suites embedding this one should initialize 62 // s.newCommand immediately after calling this method! 63 } 64 65 func (s *BaseSubnetSuite) TearDownTest(c *gc.C) { 66 s.FakeJujuXDGDataHomeSuite.TearDownTest(c) 67 } 68 69 // InitCommand creates a command with s.newCommand and runs its 70 // Init method only. It returns the inner command and any error. 71 func (s *BaseSubnetSuite) InitCommand(c *gc.C, args ...string) (cmd.Command, error) { 72 cmd := s.newCommandForTest() 73 err := cmdtesting.InitCommand(cmd, args) 74 return modelcmd.InnerCommand(cmd), err 75 } 76 77 // RunCommand creates a command with s.newCommand and executes it, 78 // passing any args and returning the stdout and stderr output as 79 // strings, as well as any error. 80 func (s *BaseSubnetSuite) RunCommand(c *gc.C, args ...string) (string, string, error) { 81 cmd := s.newCommandForTest() 82 ctx, err := cmdtesting.RunCommand(c, cmd, args...) 83 return cmdtesting.Stdout(ctx), cmdtesting.Stderr(ctx), err 84 } 85 86 func (s *BaseSubnetSuite) newCommandForTest() modelcmd.ModelCommand { 87 cmd := s.newCommand() 88 cmd.SetClientStore(jujuclienttesting.MinimalStore()) 89 cmd1 := modelcmd.InnerCommand(cmd).(interface { 90 SetAPI(subnet.SubnetAPI) 91 }) 92 cmd1.SetAPI(s.api) 93 return cmd 94 } 95 96 // AssertRunFails is a shortcut for calling RunCommand with the 97 // passed args then asserting the output is empty and the error is as 98 // expected, finally returning the error. 99 func (s *BaseSubnetSuite) AssertRunFails(c *gc.C, expectErr string, args ...string) error { 100 stdout, stderr, err := s.RunCommand(c, args...) 101 c.Assert(err, gc.ErrorMatches, expectErr) 102 c.Assert(stdout, gc.Equals, "") 103 c.Assert(stderr, gc.Equals, "") 104 return err 105 } 106 107 // AssertRunSucceeds is a shortcut for calling RunCommand with 108 // the passed args then asserting the stderr output matches 109 // expectStderr, stdout is equal to expectStdout, and the error is 110 // nil. 111 func (s *BaseSubnetSuite) AssertRunSucceeds(c *gc.C, expectStderr, expectStdout string, args ...string) { 112 stdout, stderr, err := s.RunCommand(c, args...) 113 c.Assert(err, jc.ErrorIsNil) 114 c.Assert(stdout, gc.Equals, expectStdout) 115 c.Assert(stderr, gc.Matches, expectStderr) 116 } 117 118 // Strings makes tests taking a slice of strings slightly easier to 119 // write: e.g. s.Strings("foo", "bar") vs. []string{"foo", "bar"}. 120 func (s *BaseSubnetSuite) Strings(values ...string) []string { 121 return values 122 } 123 124 // StubAPI defines a testing stub for the SubnetAPI interface. 125 type StubAPI struct { 126 *testing.Stub 127 128 Subnets []params.Subnet 129 Spaces []names.Tag 130 Zones []string 131 } 132 133 var _ subnet.SubnetAPI = (*StubAPI)(nil) 134 135 // NewStubAPI creates a StubAPI suitable for passing to 136 // subnet.New*Command(). 137 func NewStubAPI() *StubAPI { 138 subnets := []params.Subnet{{ 139 // IPv4 subnet. 140 CIDR: "10.20.0.0/24", 141 ProviderId: "subnet-foo", 142 Life: params.Alive, 143 SpaceTag: "space-public", 144 Zones: []string{"zone1", "zone2"}, 145 }, { 146 // IPv6 subnet. 147 CIDR: "2001:db8::/32", 148 ProviderId: "subnet-bar", 149 ProviderNetworkId: "network-yay", 150 Life: params.Dying, 151 SpaceTag: "space-dmz", 152 Zones: []string{"zone2"}, 153 }, { 154 // IPv4 VLAN subnet. 155 CIDR: "10.10.0.0/16", 156 Life: params.Dead, 157 SpaceTag: "space-vlan-42", 158 Zones: []string{"zone1"}, 159 VLANTag: 42, 160 }} 161 return &StubAPI{ 162 Stub: &testing.Stub{}, 163 Zones: []string{"zone1", "zone2"}, 164 Subnets: subnets, 165 Spaces: []names.Tag{ 166 names.NewSpaceTag("default"), 167 names.NewSpaceTag("public"), 168 names.NewSpaceTag("dmz"), 169 names.NewSpaceTag("vlan-42"), 170 }, 171 } 172 } 173 174 func (sa *StubAPI) Close() error { 175 sa.MethodCall(sa, "Close") 176 return sa.NextErr() 177 } 178 179 func (sa *StubAPI) AllZones() ([]string, error) { 180 sa.MethodCall(sa, "AllZones") 181 if err := sa.NextErr(); err != nil { 182 return nil, err 183 } 184 return sa.Zones, nil 185 } 186 187 func (sa *StubAPI) AllSpaces() ([]names.Tag, error) { 188 sa.MethodCall(sa, "AllSpaces") 189 if err := sa.NextErr(); err != nil { 190 return nil, err 191 } 192 return sa.Spaces, nil 193 } 194 195 func (sa *StubAPI) CreateSubnet(subnetCIDR names.SubnetTag, spaceTag names.SpaceTag, zones []string, isPublic bool) error { 196 sa.MethodCall(sa, "CreateSubnet", subnetCIDR, spaceTag, zones, isPublic) 197 return sa.NextErr() 198 } 199 200 func (sa *StubAPI) AddSubnet(cidr names.SubnetTag, id network.Id, spaceTag names.SpaceTag, zones []string) error { 201 sa.MethodCall(sa, "AddSubnet", cidr, id, spaceTag, zones) 202 return sa.NextErr() 203 } 204 205 func (sa *StubAPI) RemoveSubnet(subnetCIDR names.SubnetTag) error { 206 sa.MethodCall(sa, "RemoveSubnet", subnetCIDR) 207 return sa.NextErr() 208 } 209 210 func (sa *StubAPI) ListSubnets(withSpace *names.SpaceTag, withZone string) ([]params.Subnet, error) { 211 if withSpace == nil { 212 // Due to the way CheckCall works (using jc.DeepEquals 213 // internally), we need to pass an explicit nil here, rather 214 // than a pointer to a names.SpaceTag pointing to nil. 215 sa.MethodCall(sa, "ListSubnets", nil, withZone) 216 } else { 217 sa.MethodCall(sa, "ListSubnets", withSpace, withZone) 218 } 219 if err := sa.NextErr(); err != nil { 220 return nil, err 221 } 222 return sa.Subnets, nil 223 }