github.com/imran-kn/cilium-fork@v1.6.9/pkg/aws/ec2/mock/mock_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package mock
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/cilium/cilium/pkg/aws/types"
    25  
    26  	"gopkg.in/check.v1"
    27  )
    28  
    29  func Test(t *testing.T) {
    30  	check.TestingT(t)
    31  }
    32  
    33  type MockSuite struct{}
    34  
    35  var _ = check.Suite(&MockSuite{})
    36  
    37  func (e *MockSuite) TestMock(c *check.C) {
    38  	api := NewAPI([]*types.Subnet{{ID: "s-1", AvailableAddresses: 100}}, []*types.Vpc{{ID: "v-1"}})
    39  	c.Assert(api, check.Not(check.IsNil))
    40  
    41  	eniID1, _, err := api.CreateNetworkInterface(8, "s-1", "desc", []string{"sg1", "sg2"})
    42  	c.Assert(err, check.IsNil)
    43  
    44  	eniID2, _, err := api.CreateNetworkInterface(8, "s-1", "desc", []string{"sg1", "sg2"})
    45  	c.Assert(err, check.IsNil)
    46  
    47  	_, err = api.AttachNetworkInterface(0, "i-1", eniID1)
    48  	c.Assert(err, check.IsNil)
    49  
    50  	_, ok := api.enis["i-1"][eniID1]
    51  	c.Assert(ok, check.Equals, true)
    52  
    53  	_, err = api.AttachNetworkInterface(1, "i-1", eniID2)
    54  	c.Assert(err, check.IsNil)
    55  
    56  	_, ok = api.enis["i-1"][eniID1]
    57  	c.Assert(ok, check.Equals, true)
    58  	_, ok = api.enis["i-1"][eniID2]
    59  	c.Assert(ok, check.Equals, true)
    60  
    61  	err = api.DeleteNetworkInterface(eniID1)
    62  	c.Assert(err, check.IsNil)
    63  
    64  	err = api.DeleteNetworkInterface(eniID1)
    65  	c.Assert(err, check.Not(check.IsNil))
    66  
    67  	err = api.DeleteNetworkInterface(eniID2)
    68  	c.Assert(err, check.IsNil)
    69  
    70  	_, ok = api.enis["i-1"][eniID1]
    71  	c.Assert(ok, check.Equals, false)
    72  	_, ok = api.enis["i-1"][eniID2]
    73  	c.Assert(ok, check.Equals, false)
    74  }
    75  
    76  func (e *MockSuite) TestSetMockError(c *check.C) {
    77  	api := NewAPI([]*types.Subnet{}, []*types.Vpc{})
    78  	c.Assert(api, check.Not(check.IsNil))
    79  
    80  	mockError := errors.New("error")
    81  
    82  	api.SetMockError(CreateNetworkInterface, mockError)
    83  	_, _, err := api.CreateNetworkInterface(8, "s-1", "desc", []string{"sg1", "sg2"})
    84  	c.Assert(err, check.Equals, mockError)
    85  
    86  	api.SetMockError(AttachNetworkInterface, mockError)
    87  	_, err = api.AttachNetworkInterface(0, "i-1", "e-1")
    88  	c.Assert(err, check.Equals, mockError)
    89  
    90  	api.SetMockError(DeleteNetworkInterface, mockError)
    91  	err = api.DeleteNetworkInterface("e-1")
    92  	c.Assert(err, check.Equals, mockError)
    93  
    94  	api.SetMockError(AssignPrivateIpAddresses, mockError)
    95  	err = api.AssignPrivateIpAddresses("e-1", 10)
    96  	c.Assert(err, check.Equals, mockError)
    97  
    98  	api.SetMockError(UnassignPrivateIpAddresses, mockError)
    99  	err = api.UnassignPrivateIpAddresses("e-1", []string{"10.0.0.10", "10.0.0.11"})
   100  	c.Assert(err, check.Equals, mockError)
   101  
   102  	api.SetMockError(ModifyNetworkInterface, mockError)
   103  	err = api.ModifyNetworkInterface("e-1", "a-1", true)
   104  	c.Assert(err, check.Equals, mockError)
   105  }
   106  
   107  func (e *MockSuite) TestSetDelay(c *check.C) {
   108  	api := NewAPI([]*types.Subnet{}, []*types.Vpc{})
   109  	c.Assert(api, check.Not(check.IsNil))
   110  
   111  	api.SetDelay(AllOperations, time.Second)
   112  	c.Assert(api.delays[CreateNetworkInterface], check.Equals, time.Second)
   113  	c.Assert(api.delays[DeleteNetworkInterface], check.Equals, time.Second)
   114  	c.Assert(api.delays[ModifyNetworkInterface], check.Equals, time.Second)
   115  	c.Assert(api.delays[AttachNetworkInterface], check.Equals, time.Second)
   116  	c.Assert(api.delays[AssignPrivateIpAddresses], check.Equals, time.Second)
   117  	c.Assert(api.delays[UnassignPrivateIpAddresses], check.Equals, time.Second)
   118  }
   119  
   120  func (e *MockSuite) TestSetLimiter(c *check.C) {
   121  	api := NewAPI([]*types.Subnet{{ID: "s-1", AvailableAddresses: 100}}, []*types.Vpc{{ID: "v-1"}})
   122  	c.Assert(api, check.Not(check.IsNil))
   123  
   124  	api.SetLimiter(10.0, 2)
   125  	_, _, err := api.CreateNetworkInterface(8, "s-1", "desc", []string{"sg1", "sg2"})
   126  	c.Assert(err, check.IsNil)
   127  }