github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/aclprovider/ipsetprovidermock.go (about)

     1  package provider
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/aporeto-inc/go-ipset/ipset"
     8  )
     9  
    10  type ipsetProviderMockedMethods struct {
    11  	newMockIPset   func(name string, hasht string, p *ipset.Params) (Ipset, error)
    12  	getMockIPset   func(name string) Ipset
    13  	destroyAllMock func(prefix string) error
    14  	listIPSetsMock func() ([]string, error)
    15  }
    16  
    17  // TestIpsetProvider is a test implementation for IpsetProvider
    18  type TestIpsetProvider interface {
    19  	IpsetProvider
    20  	MockNewIpset(t *testing.T, impl func(name string, hasht string, p *ipset.Params) (Ipset, error))
    21  	MockGetIpset(t *testing.T, impl func(name string) Ipset)
    22  	MockDestroyAll(t *testing.T, impl func(string) error)
    23  	MockListIPSets(t *testing.T, impl func() ([]string, error))
    24  }
    25  
    26  type testIpsetProvider struct {
    27  	mocks       map[*testing.T]*ipsetProviderMockedMethods
    28  	lock        *sync.Mutex
    29  	currentTest *testing.T
    30  }
    31  
    32  // NewTestIpsetProvider returns a new TestManipulator.
    33  func NewTestIpsetProvider() TestIpsetProvider {
    34  	return &testIpsetProvider{
    35  		lock:  &sync.Mutex{},
    36  		mocks: map[*testing.T]*ipsetProviderMockedMethods{},
    37  	}
    38  }
    39  
    40  func (m *testIpsetProvider) MockNewIpset(t *testing.T, impl func(name string, hasht string, p *ipset.Params) (Ipset, error)) {
    41  
    42  	m.currentMocks(t).newMockIPset = impl
    43  }
    44  
    45  func (m *testIpsetProvider) MockGetIpset(t *testing.T, impl func(name string) Ipset) {
    46  	m.currentMocks(t).getMockIPset = impl
    47  }
    48  
    49  func (m *testIpsetProvider) MockDestroyAll(t *testing.T, impl func(string) error) {
    50  
    51  	m.currentMocks(t).destroyAllMock = impl
    52  }
    53  
    54  func (m *testIpsetProvider) MockListIPSets(t *testing.T, impl func() ([]string, error)) {
    55  
    56  	m.currentMocks(t).listIPSetsMock = impl
    57  }
    58  
    59  func (m *testIpsetProvider) NewIpset(name string, hasht string, p *ipset.Params) (Ipset, error) {
    60  
    61  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.newMockIPset != nil {
    62  		return mock.newMockIPset(name, hasht, p)
    63  	}
    64  
    65  	return NewTestIpset(), nil
    66  }
    67  
    68  func (m *testIpsetProvider) GetIpset(name string) Ipset {
    69  
    70  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.newMockIPset != nil {
    71  		return mock.getMockIPset(name)
    72  	}
    73  
    74  	return NewTestIpset()
    75  }
    76  
    77  func (m *testIpsetProvider) DestroyAll(prefix string) error {
    78  
    79  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.destroyAllMock != nil {
    80  		return mock.destroyAllMock(prefix)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  func (m *testIpsetProvider) ListIPSets() ([]string, error) {
    87  
    88  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.listIPSetsMock != nil {
    89  		return mock.listIPSetsMock()
    90  	}
    91  
    92  	return nil, nil
    93  }
    94  
    95  func (m *testIpsetProvider) currentMocks(t *testing.T) *ipsetProviderMockedMethods {
    96  	m.lock.Lock()
    97  	defer m.lock.Unlock()
    98  
    99  	mocks := m.mocks[t]
   100  
   101  	if mocks == nil {
   102  		mocks = &ipsetProviderMockedMethods{}
   103  		m.mocks[t] = mocks
   104  	}
   105  
   106  	m.currentTest = t
   107  	return mocks
   108  }
   109  
   110  type ipsetMockedMethods struct {
   111  	addMock       func(entry string, timeout int) error
   112  	addOptionMock func(entry string, option string, timeout int) error
   113  	delMock       func(entry string) error
   114  	destroyMock   func() error
   115  	flushMock     func() error
   116  	testMock      func(entry string) (bool, error)
   117  }
   118  
   119  // TestIpset is a test implementation for Ipset
   120  type TestIpset interface {
   121  	Ipset
   122  	MockAdd(t *testing.T, impl func(entry string, timeout int) error)
   123  	MockAddOption(t *testing.T, impl func(entry string, option string, timeout int) error)
   124  	MockDel(t *testing.T, impl func(entry string) error)
   125  	MockDestroy(t *testing.T, impl func() error)
   126  	MockFlush(t *testing.T, impl func() error)
   127  	MockTest(t *testing.T, impl func(entry string) (bool, error))
   128  }
   129  
   130  type testIpset struct {
   131  	mocks       map[*testing.T]*ipsetMockedMethods
   132  	lock        *sync.Mutex
   133  	currentTest *testing.T
   134  }
   135  
   136  // NewTestIpset returns a new TestManipulator.
   137  func NewTestIpset() TestIpset {
   138  	return &testIpset{
   139  		lock:  &sync.Mutex{},
   140  		mocks: map[*testing.T]*ipsetMockedMethods{},
   141  	}
   142  }
   143  
   144  func (m *testIpset) MockAdd(t *testing.T, impl func(entry string, timeout int) error) {
   145  
   146  	m.currentMocks(t).addMock = impl
   147  }
   148  
   149  func (m *testIpset) MockAddOption(t *testing.T, impl func(entry string, option string, timeout int) error) {
   150  
   151  	m.currentMocks(t).addOptionMock = impl
   152  }
   153  
   154  func (m *testIpset) MockDel(t *testing.T, impl func(entry string) error) {
   155  
   156  	m.currentMocks(t).delMock = impl
   157  }
   158  
   159  func (m *testIpset) MockDestroy(t *testing.T, impl func() error) {
   160  
   161  	m.currentMocks(t).destroyMock = impl
   162  }
   163  
   164  func (m *testIpset) MockFlush(t *testing.T, impl func() error) {
   165  
   166  	m.currentMocks(t).flushMock = impl
   167  }
   168  
   169  func (m *testIpset) MockTest(t *testing.T, impl func(entry string) (bool, error)) {
   170  
   171  	m.currentMocks(t).testMock = impl
   172  }
   173  
   174  func (m *testIpset) Add(entry string, timeout int) error {
   175  
   176  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.addMock != nil {
   177  		return mock.addMock(entry, timeout)
   178  	}
   179  
   180  	return nil
   181  }
   182  
   183  func (m *testIpset) AddOption(entry string, option string, timeout int) error {
   184  
   185  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.addOptionMock != nil {
   186  		return mock.addOptionMock(entry, option, timeout)
   187  	}
   188  
   189  	return nil
   190  }
   191  
   192  func (m *testIpset) Del(entry string) error {
   193  
   194  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.delMock != nil {
   195  		return mock.delMock(entry)
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  func (m *testIpset) Destroy() error {
   202  
   203  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.destroyMock != nil {
   204  		return mock.destroyMock()
   205  	}
   206  	return nil
   207  
   208  }
   209  
   210  func (m *testIpset) Flush() error {
   211  
   212  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.flushMock != nil {
   213  		return mock.flushMock()
   214  	}
   215  
   216  	return nil
   217  }
   218  
   219  func (m *testIpset) Test(entry string) (bool, error) {
   220  
   221  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.testMock != nil {
   222  		return mock.testMock(entry)
   223  	}
   224  
   225  	return false, nil
   226  }
   227  
   228  func (m *testIpset) currentMocks(t *testing.T) *ipsetMockedMethods {
   229  	m.lock.Lock()
   230  	defer m.lock.Unlock()
   231  
   232  	mocks := m.mocks[t]
   233  
   234  	if mocks == nil {
   235  		mocks = &ipsetMockedMethods{}
   236  		m.mocks[t] = mocks
   237  	}
   238  
   239  	m.currentTest = t
   240  	return mocks
   241  }