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