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