github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/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  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/apiserver/spaces"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type SpacesSuite struct {
    20  	coretesting.BaseSuite
    21  	apiservertesting.StubNetwork
    22  
    23  	resources  *common.Resources
    24  	authorizer apiservertesting.FakeAuthorizer
    25  	facade     spaces.API
    26  }
    27  
    28  var _ = gc.Suite(&SpacesSuite{})
    29  
    30  func (s *SpacesSuite) SetUpSuite(c *gc.C) {
    31  	s.StubNetwork.SetUpSuite(c)
    32  	s.BaseSuite.SetUpSuite(c)
    33  }
    34  
    35  func (s *SpacesSuite) TearDownSuite(c *gc.C) {
    36  	s.BaseSuite.TearDownSuite(c)
    37  }
    38  
    39  func (s *SpacesSuite) SetUpTest(c *gc.C) {
    40  	s.BaseSuite.SetUpTest(c)
    41  	apiservertesting.BackingInstance.SetUp(c, apiservertesting.StubZonedEnvironName, apiservertesting.WithZones, apiservertesting.WithSpaces, apiservertesting.WithSubnets)
    42  
    43  	s.resources = common.NewResources()
    44  	s.authorizer = apiservertesting.FakeAuthorizer{
    45  		Tag:            names.NewUserTag("admin"),
    46  		EnvironManager: false,
    47  	}
    48  
    49  	var err error
    50  	s.facade, err = spaces.NewAPIWithBacking(
    51  		apiservertesting.BackingInstance, s.resources, s.authorizer,
    52  	)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(s.facade, gc.NotNil)
    55  }
    56  
    57  func (s *SpacesSuite) TearDownTest(c *gc.C) {
    58  	if s.resources != nil {
    59  		s.resources.StopAll()
    60  	}
    61  	s.BaseSuite.TearDownTest(c)
    62  }
    63  
    64  func (s *SpacesSuite) TestNewAPIWithBacking(c *gc.C) {
    65  	// Clients are allowed.
    66  	facade, err := spaces.NewAPIWithBacking(
    67  		apiservertesting.BackingInstance, s.resources, s.authorizer,
    68  	)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	c.Assert(facade, gc.NotNil)
    71  	// No calls so far.
    72  	apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub)
    73  
    74  	// Agents are not allowed
    75  	agentAuthorizer := s.authorizer
    76  	agentAuthorizer.Tag = names.NewMachineTag("42")
    77  	facade, err = spaces.NewAPIWithBacking(
    78  		apiservertesting.BackingInstance, s.resources, agentAuthorizer,
    79  	)
    80  	c.Assert(err, jc.DeepEquals, common.ErrPerm)
    81  	c.Assert(facade, gc.IsNil)
    82  	// No calls so far.
    83  	apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub)
    84  }
    85  
    86  type checkAddSpacesParams struct {
    87  	Name      string
    88  	Subnets   []string
    89  	Error     string
    90  	MakesCall bool
    91  	Public    bool
    92  }
    93  
    94  func (s *SpacesSuite) checkAddSpaces(c *gc.C, p checkAddSpacesParams) {
    95  	args := params.CreateSpaceParams{}
    96  	if p.Name != "" {
    97  		args.SpaceTag = "space-" + p.Name
    98  	}
    99  	if len(p.Subnets) > 0 {
   100  		for _, cidr := range p.Subnets {
   101  			args.SubnetTags = append(args.SubnetTags, "subnet-"+cidr)
   102  		}
   103  	}
   104  	args.Public = p.Public
   105  
   106  	spaces := params.CreateSpacesParams{}
   107  	spaces.Spaces = append(spaces.Spaces, args)
   108  	results, err := s.facade.CreateSpaces(spaces)
   109  
   110  	c.Assert(len(results.Results), gc.Equals, 1)
   111  	c.Assert(err, gc.IsNil)
   112  	if p.Error == "" {
   113  		c.Assert(results.Results[0].Error, gc.IsNil)
   114  	} else {
   115  		c.Assert(results.Results[0].Error, gc.NotNil)
   116  		c.Assert(results.Results[0].Error, gc.ErrorMatches, p.Error)
   117  	}
   118  
   119  	if p.Error == "" || p.MakesCall {
   120  		apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub,
   121  			apiservertesting.BackingCall("AddSpace", p.Name, p.Subnets, p.Public),
   122  		)
   123  	} else {
   124  		apiservertesting.CheckMethodCalls(c, apiservertesting.SharedStub)
   125  	}
   126  }
   127  
   128  func (s *SpacesSuite) TestAddSpacesOneSubnet(c *gc.C) {
   129  	p := checkAddSpacesParams{
   130  		Name:    "foo",
   131  		Subnets: []string{"10.0.0.0/24"},
   132  	}
   133  	s.checkAddSpaces(c, p)
   134  }
   135  
   136  func (s *SpacesSuite) TestAddSpacesTwoSubnets(c *gc.C) {
   137  	p := checkAddSpacesParams{
   138  		Name:    "foo",
   139  		Subnets: []string{"10.0.0.0/24", "10.0.1.0/24"},
   140  	}
   141  	s.checkAddSpaces(c, p)
   142  }
   143  
   144  func (s *SpacesSuite) TestAddSpacesManySubnets(c *gc.C) {
   145  	p := checkAddSpacesParams{
   146  		Name: "foo",
   147  		Subnets: []string{"10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24",
   148  			"10.0.3.0/24", "10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"},
   149  	}
   150  	s.checkAddSpaces(c, p)
   151  }
   152  
   153  func (s *SpacesSuite) TestAddSpacesAPIError(c *gc.C) {
   154  	apiservertesting.SharedStub.SetErrors(errors.AlreadyExistsf("space-foo"))
   155  	p := checkAddSpacesParams{
   156  		Name:      "foo",
   157  		Subnets:   []string{"10.0.0.0/24"},
   158  		MakesCall: true,
   159  		Error:     "space-foo already exists",
   160  	}
   161  	s.checkAddSpaces(c, p)
   162  }
   163  
   164  func (s *SpacesSuite) TestCreateInvalidSpace(c *gc.C) {
   165  	p := checkAddSpacesParams{
   166  		Name:    "-",
   167  		Subnets: []string{"10.0.0.0/24"},
   168  		Error:   `"space--" is not a valid space tag`,
   169  	}
   170  	s.checkAddSpaces(c, p)
   171  }
   172  
   173  func (s *SpacesSuite) TestCreateInvalidSubnet(c *gc.C) {
   174  	p := checkAddSpacesParams{
   175  		Name:    "foo",
   176  		Subnets: []string{"bar"},
   177  		Error:   `"subnet-bar" is not a valid subnet tag`,
   178  	}
   179  	s.checkAddSpaces(c, p)
   180  }
   181  
   182  func (s *SpacesSuite) TestPublic(c *gc.C) {
   183  	p := checkAddSpacesParams{
   184  		Name:    "foo",
   185  		Subnets: []string{"10.0.0.0/24"},
   186  		Public:  true,
   187  	}
   188  	s.checkAddSpaces(c, p)
   189  }
   190  
   191  func (s *SpacesSuite) TestEmptySpaceName(c *gc.C) {
   192  	p := checkAddSpacesParams{
   193  		Subnets: []string{"10.0.0.0/24"},
   194  		Error:   `"" is not a valid tag`,
   195  	}
   196  	s.checkAddSpaces(c, p)
   197  }
   198  
   199  func (s *SpacesSuite) TestNoSubnets(c *gc.C) {
   200  	p := checkAddSpacesParams{
   201  		Name:    "foo",
   202  		Subnets: nil,
   203  	}
   204  	s.checkAddSpaces(c, p)
   205  }
   206  
   207  func (s *SpacesSuite) TestListSpacesDefault(c *gc.C) {
   208  	expected := []params.Space{{
   209  		Name: "default",
   210  		Subnets: []params.Subnet{{
   211  			CIDR:       "192.168.0.0/24",
   212  			ProviderId: "provider-192.168.0.0/24",
   213  			Zones:      []string{"foo"},
   214  			Status:     "in-use",
   215  			SpaceTag:   "space-default",
   216  		}, {
   217  			CIDR:       "192.168.3.0/24",
   218  			ProviderId: "provider-192.168.3.0/24",
   219  			VLANTag:    23,
   220  			Zones:      []string{"bar", "bam"},
   221  			SpaceTag:   "space-default",
   222  		}},
   223  	}, {
   224  		Name: "dmz",
   225  		Subnets: []params.Subnet{{
   226  			CIDR:       "192.168.1.0/24",
   227  			ProviderId: "provider-192.168.1.0/24",
   228  			VLANTag:    23,
   229  			Zones:      []string{"bar", "bam"},
   230  			SpaceTag:   "space-dmz",
   231  		}},
   232  	}, {
   233  		Name: "private",
   234  		Subnets: []params.Subnet{{
   235  			CIDR:       "192.168.2.0/24",
   236  			ProviderId: "provider-192.168.2.0/24",
   237  			Zones:      []string{"foo"},
   238  			Status:     "in-use",
   239  			SpaceTag:   "space-private",
   240  		}},
   241  	}}
   242  	spaces, err := s.facade.ListSpaces()
   243  	c.Assert(err, jc.ErrorIsNil)
   244  	c.Assert(spaces.Results, jc.DeepEquals, expected)
   245  }
   246  
   247  func (s *SpacesSuite) TestListSpacesAllSpacesError(c *gc.C) {
   248  	boom := errors.New("backing boom")
   249  	apiservertesting.BackingInstance.SetErrors(boom)
   250  	_, err := s.facade.ListSpaces()
   251  	c.Assert(err, gc.ErrorMatches, "backing boom")
   252  }
   253  
   254  func (s *SpacesSuite) TestListSpacesSubnetsError(c *gc.C) {
   255  	boom := errors.New("boom")
   256  	apiservertesting.SharedStub.SetErrors(nil, boom, boom, boom)
   257  	results, err := s.facade.ListSpaces()
   258  	for _, space := range results.Results {
   259  		c.Assert(space.Error, gc.ErrorMatches, "fetching subnets: boom")
   260  	}
   261  	c.Assert(err, jc.ErrorIsNil)
   262  }
   263  
   264  func (s *SpacesSuite) TestListSpacesSubnetsSingleSubnetError(c *gc.C) {
   265  	boom := errors.New("boom")
   266  	apiservertesting.SharedStub.SetErrors(nil, nil, boom)
   267  
   268  	results, err := s.facade.ListSpaces()
   269  	for i, space := range results.Results {
   270  		if i == 1 {
   271  			c.Assert(space.Error, gc.ErrorMatches, "fetching subnets: boom")
   272  		} else {
   273  			c.Assert(space.Error, gc.IsNil)
   274  		}
   275  	}
   276  	c.Assert(err, jc.ErrorIsNil)
   277  }