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

     1  package provider
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  type iptablesProviderMockedMethods struct {
     9  	appendMock        func(table, chain string, rulespec ...string) error
    10  	insertMock        func(table, chain string, pos int, rulespec ...string) error
    11  	deleteMock        func(table, chain string, rulespec ...string) error
    12  	listChainsMock    func(table string) ([]string, error)
    13  	clearChainMock    func(table, chain string) error
    14  	deleteChainMock   func(table, chain string) error
    15  	newChainMock      func(table, chain string) error
    16  	commitMock        func() error
    17  	retrieveTableMock func() map[string]map[string][]string
    18  	resetMock         func(subs string) error
    19  	listRulesMock     func(table, chain string) ([]string, error)
    20  }
    21  
    22  // TestIptablesProvider is a test implementation for IptablesProvider
    23  type TestIptablesProvider interface {
    24  	IptablesProvider
    25  	MockAppend(t *testing.T, impl func(table, chain string, rulespec ...string) error)
    26  	MockInsert(t *testing.T, impl func(table, chain string, pos int, rulespec ...string) error)
    27  	MockDelete(t *testing.T, impl func(table, chain string, rulespec ...string) error)
    28  	MockListChains(t *testing.T, impl func(table string) ([]string, error))
    29  	MockClearChain(t *testing.T, impl func(table, chain string) error)
    30  	MockDeleteChain(t *testing.T, impl func(table, chain string) error)
    31  	MockNewChain(t *testing.T, impl func(table, chain string) error)
    32  	MockCommit(t *testing.T, impl func() error)
    33  	MockReset(t *testing.T, impl func(subs string) error)
    34  	MockListRules(t *testing.T, impl func(table, chain string) ([]string, error))
    35  }
    36  
    37  // A testIptablesProvider is an empty TransactionalManipulator that can be easily mocked.
    38  type testIptablesProvider struct {
    39  	mocks       map[*testing.T]*iptablesProviderMockedMethods
    40  	lock        *sync.Mutex
    41  	currentTest *testing.T
    42  }
    43  
    44  // NewTestIptablesProvider returns a new TestManipulator.
    45  func NewTestIptablesProvider() TestIptablesProvider {
    46  	return &testIptablesProvider{lock: &sync.Mutex{}, mocks: map[*testing.T]*iptablesProviderMockedMethods{}}
    47  }
    48  
    49  func (m *testIptablesProvider) MockListRules(t *testing.T, impl func(table, chain string) ([]string, error)) {
    50  	m.currentMocks(t).listRulesMock = impl
    51  }
    52  func (m *testIptablesProvider) MockAppend(t *testing.T, impl func(table, chain string, rulespec ...string) error) {
    53  
    54  	m.currentMocks(t).appendMock = impl
    55  }
    56  
    57  func (m *testIptablesProvider) MockReset(t *testing.T, impl func(subs string) error) {
    58  	m.currentMocks(t).resetMock = impl
    59  }
    60  
    61  func (m *testIptablesProvider) MockInsert(t *testing.T, impl func(table, chain string, pos int, rulespec ...string) error) {
    62  
    63  	m.currentMocks(t).insertMock = impl
    64  }
    65  
    66  func (m *testIptablesProvider) MockDelete(t *testing.T, impl func(table, chain string, rulespec ...string) error) {
    67  
    68  	m.currentMocks(t).deleteMock = impl
    69  }
    70  
    71  func (m *testIptablesProvider) MockListChains(t *testing.T, impl func(table string) ([]string, error)) {
    72  
    73  	m.currentMocks(t).listChainsMock = impl
    74  }
    75  
    76  func (m *testIptablesProvider) MockClearChain(t *testing.T, impl func(table, chain string) error) {
    77  
    78  	m.currentMocks(t).clearChainMock = impl
    79  }
    80  
    81  func (m *testIptablesProvider) MockDeleteChain(t *testing.T, impl func(table, chain string) error) {
    82  
    83  	m.currentMocks(t).deleteChainMock = impl
    84  }
    85  
    86  func (m *testIptablesProvider) MockNewChain(t *testing.T, impl func(table, chain string) error) {
    87  
    88  	m.currentMocks(t).newChainMock = impl
    89  }
    90  
    91  func (m *testIptablesProvider) MockCommit(t *testing.T, impl func() error) {
    92  	m.currentMocks(t).commitMock = impl
    93  }
    94  
    95  func (m *testIptablesProvider) Append(table, chain string, rulespec ...string) error {
    96  
    97  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.appendMock != nil {
    98  		return mock.appendMock(table, chain, rulespec...)
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func (m *testIptablesProvider) ListRules(table, chain string) ([]string, error) {
   105  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.listRulesMock != nil {
   106  		return mock.listRulesMock(table, chain)
   107  	}
   108  	return []string{}, nil
   109  }
   110  
   111  func (m *testIptablesProvider) Insert(table, chain string, pos int, rulespec ...string) error {
   112  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.insertMock != nil {
   113  		return mock.insertMock(table, chain, pos, rulespec...)
   114  
   115  	}
   116  
   117  	return nil
   118  }
   119  
   120  func (m *testIptablesProvider) ResetRules(subs string) error {
   121  
   122  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.resetMock != nil {
   123  		return mock.resetMock(subs)
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func (m *testIptablesProvider) Delete(table, chain string, rulespec ...string) error {
   130  
   131  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.deleteMock != nil {
   132  		return mock.deleteMock(table, chain, rulespec...)
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  func (m *testIptablesProvider) ListChains(table string) ([]string, error) {
   139  
   140  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.listChainsMock != nil {
   141  		return mock.listChainsMock(table)
   142  	}
   143  
   144  	return nil, nil
   145  }
   146  
   147  func (m *testIptablesProvider) ClearChain(table, chain string) error {
   148  
   149  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.clearChainMock != nil {
   150  		return mock.clearChainMock(table, chain)
   151  	}
   152  
   153  	return nil
   154  }
   155  
   156  func (m *testIptablesProvider) DeleteChain(table, chain string) error {
   157  
   158  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.deleteChainMock != nil {
   159  		return mock.deleteChainMock(table, chain)
   160  	}
   161  
   162  	return nil
   163  }
   164  
   165  func (m *testIptablesProvider) NewChain(table, chain string) error {
   166  
   167  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.newChainMock != nil {
   168  		return mock.newChainMock(table, chain)
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  func (m *testIptablesProvider) Commit() error {
   175  
   176  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.commitMock != nil {
   177  		return mock.commitMock()
   178  	}
   179  
   180  	return nil
   181  }
   182  
   183  func (m *testIptablesProvider) RetrieveTable() map[string]map[string][]string {
   184  
   185  	if mock := m.currentMocks(m.currentTest); mock != nil && mock.retrieveTableMock != nil {
   186  		return mock.retrieveTableMock()
   187  	}
   188  
   189  	return nil
   190  }
   191  
   192  func (m *testIptablesProvider) currentMocks(t *testing.T) *iptablesProviderMockedMethods {
   193  	m.lock.Lock()
   194  	defer m.lock.Unlock()
   195  
   196  	mocks := m.mocks[t]
   197  
   198  	if mocks == nil {
   199  		mocks = &iptablesProviderMockedMethods{}
   200  		m.mocks[t] = mocks
   201  	}
   202  
   203  	m.currentTest = t
   204  	return mocks
   205  }