github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/spaces/spaces_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package spaces_test
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"math/rand"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	apitesting "github.com/juju/juju/api/base/testing"
    16  	"github.com/juju/juju/api/spaces"
    17  	"github.com/juju/juju/apiserver/params"
    18  	coretesting "github.com/juju/juju/testing"
    19  )
    20  
    21  type SpacesSuite struct {
    22  	coretesting.BaseSuite
    23  
    24  	apiCaller *apitesting.CallChecker
    25  	api       *spaces.API
    26  }
    27  
    28  var _ = gc.Suite(&SpacesSuite{})
    29  
    30  func (s *SpacesSuite) init(c *gc.C, args apitesting.APICall) {
    31  	s.apiCaller = apitesting.APICallChecker(c, args)
    32  	s.api = spaces.NewAPI(s.apiCaller)
    33  	c.Check(s.api, gc.NotNil)
    34  	c.Check(s.apiCaller.CallCount, gc.Equals, 0)
    35  }
    36  
    37  func (s *SpacesSuite) TestNewAPISuccess(c *gc.C) {
    38  	apiCaller := apitesting.APICallChecker(c)
    39  	api := spaces.NewAPI(apiCaller)
    40  	c.Check(api, gc.NotNil)
    41  	c.Check(apiCaller.CallCount, gc.Equals, 0)
    42  }
    43  
    44  func (s *SpacesSuite) TestNewAPIWithNilCaller(c *gc.C) {
    45  	panicFunc := func() { spaces.NewAPI(nil) }
    46  	c.Assert(panicFunc, gc.PanicMatches, "caller is nil")
    47  }
    48  
    49  func makeArgs(name string, subnets []string) (string, []string, apitesting.APICall) {
    50  	spaceTag := names.NewSpaceTag(name).String()
    51  	subnetTags := []string{}
    52  
    53  	for _, s := range subnets {
    54  		subnetTags = append(subnetTags, names.NewSubnetTag(s).String())
    55  	}
    56  
    57  	expectArgs := params.CreateSpacesParams{
    58  		Spaces: []params.CreateSpaceParams{
    59  			{
    60  				SpaceTag:   spaceTag,
    61  				SubnetTags: subnetTags,
    62  				Public:     true,
    63  			}}}
    64  
    65  	expectResults := params.ErrorResults{
    66  		Results: []params.ErrorResult{{}},
    67  	}
    68  
    69  	args := apitesting.APICall{
    70  		Facade:  "Spaces",
    71  		Method:  "CreateSpaces",
    72  		Args:    expectArgs,
    73  		Results: expectResults,
    74  	}
    75  	return name, subnets, args
    76  }
    77  
    78  func (s *SpacesSuite) testCreateSpace(c *gc.C, name string, subnets []string) {
    79  	_, _, args := makeArgs(name, subnets)
    80  	s.init(c, args)
    81  	err := s.api.CreateSpace(name, subnets, true)
    82  	c.Assert(s.apiCaller.CallCount, gc.Equals, 1)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  }
    85  
    86  func (s *SpacesSuite) TestCreateSpace(c *gc.C) {
    87  	name := "foo"
    88  	subnets := []string{}
    89  	r := rand.New(rand.NewSource(0xdeadbeef))
    90  	for i := 0; i < 100; i++ {
    91  		for j := 0; j < 10; j++ {
    92  			n := r.Uint32()
    93  			newSubnet := fmt.Sprintf("%d.%d.%d.0/24", uint8(n>>16), uint8(n>>8), uint8(n))
    94  			subnets = append(subnets, newSubnet)
    95  		}
    96  		s.testCreateSpace(c, name, subnets)
    97  	}
    98  }
    99  
   100  func (s *SpacesSuite) TestCreateSpaceEmptyResults(c *gc.C) {
   101  	_, _, args := makeArgs("foo", nil)
   102  	args.Results = params.ErrorResults{}
   103  	s.init(c, args)
   104  	err := s.api.CreateSpace("foo", nil, true)
   105  	c.Assert(s.apiCaller.CallCount, gc.Equals, 1)
   106  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0")
   107  }
   108  
   109  func (s *SpacesSuite) TestCreateSpaceFails(c *gc.C) {
   110  	name, subnets, args := makeArgs("foo", []string{"1.1.1.0/24"})
   111  	args.Error = errors.New("bang")
   112  	s.init(c, args)
   113  	err := s.api.CreateSpace(name, subnets, true)
   114  	c.Check(s.apiCaller.CallCount, gc.Equals, 1)
   115  	c.Assert(err, gc.ErrorMatches, "bang")
   116  }
   117  
   118  func (s *SpacesSuite) testListSpaces(c *gc.C, results []params.Space, err error, expectErr string) {
   119  	var expectResults params.ListSpacesResults
   120  	if results != nil {
   121  		expectResults = params.ListSpacesResults{
   122  			Results: results,
   123  		}
   124  	}
   125  
   126  	s.init(c, apitesting.APICall{
   127  		Facade:  "Spaces",
   128  		Method:  "ListSpaces",
   129  		Results: expectResults,
   130  		Error:   err,
   131  	})
   132  	gotResults, gotErr := s.api.ListSpaces()
   133  	c.Assert(s.apiCaller.CallCount, gc.Equals, 1)
   134  	c.Assert(gotResults, jc.DeepEquals, results)
   135  	if expectErr != "" {
   136  		c.Assert(gotErr, gc.ErrorMatches, expectErr)
   137  		return
   138  	}
   139  	if err != nil {
   140  		c.Assert(gotErr, jc.DeepEquals, err)
   141  	} else {
   142  		c.Assert(gotErr, jc.ErrorIsNil)
   143  	}
   144  }
   145  
   146  func (s *SpacesSuite) TestListSpacesEmptyResults(c *gc.C) {
   147  	s.testListSpaces(c, []params.Space{}, nil, "")
   148  }
   149  
   150  func (s *SpacesSuite) TestListSpacesManyResults(c *gc.C) {
   151  	spaces := []params.Space{{
   152  		Name: "space1",
   153  		Subnets: []params.Subnet{{
   154  			CIDR: "foo",
   155  		}, {
   156  			CIDR: "bar",
   157  		}},
   158  	}, {
   159  		Name: "space2",
   160  	}, {
   161  		Name:    "space3",
   162  		Subnets: []params.Subnet{},
   163  	}}
   164  	s.testListSpaces(c, spaces, nil, "")
   165  }
   166  
   167  func (s *SpacesSuite) TestListSpacesServerError(c *gc.C) {
   168  	s.testListSpaces(c, nil, errors.New("boom"), "boom")
   169  }