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

     1  // +build !windows
     2  
     3  package provider
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/magiconair/properties/assert"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  const (
    13  	mangle      = "mangle"
    14  	inputChain  = "INPUT"
    15  	outputChain = "OUTPUT"
    16  )
    17  
    18  func NewTestProvider(batchTables []string, quote bool) *BatchProvider {
    19  	batchTablesMap := map[string]bool{}
    20  	for _, t := range batchTables {
    21  		batchTablesMap[t] = true
    22  	}
    23  	return &BatchProvider{
    24  		rules:       map[string]map[string][]string{},
    25  		batchTables: batchTablesMap,
    26  		quote:       quote,
    27  	}
    28  }
    29  
    30  func TestAppend(t *testing.T) {
    31  	Convey("Given a valid batch provider", t, func() {
    32  		p := NewTestProvider([]string{mangle}, true)
    33  		Convey("When I append a first rule, it should create the table", func() {
    34  			err := p.Append(mangle, inputChain, "val1")
    35  			So(err, ShouldBeNil)
    36  			So(len(p.rules[mangle]), ShouldEqual, 1)
    37  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
    38  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
    39  		})
    40  
    41  		Convey("When I append two rules in the array, the values should be ordered ", func() {
    42  			err := p.Append(mangle, inputChain, "val1")
    43  			So(err, ShouldBeNil)
    44  			err = p.Append(mangle, inputChain, "val2")
    45  			So(err, ShouldBeNil)
    46  			So(len(p.rules[mangle]), ShouldEqual, 1)
    47  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
    48  			rules := p.rules[mangle][inputChain]
    49  			So(rules[0], ShouldResemble, "\"val1\"")
    50  			So(rules[1], ShouldResemble, "\"val2\"")
    51  		})
    52  
    53  		Convey("When I append two rules in different chains, there should be two chains", func() {
    54  			err := p.Append(mangle, inputChain, "val1")
    55  			So(err, ShouldBeNil)
    56  			err = p.Append(mangle, outputChain, "val2")
    57  			So(err, ShouldBeNil)
    58  			So(len(p.rules[mangle]), ShouldEqual, 2)
    59  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
    60  			So(len(p.rules[mangle][outputChain]), ShouldEqual, 1)
    61  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
    62  			So(p.rules[mangle][outputChain][0], ShouldResemble, "\"val2\"")
    63  		})
    64  	})
    65  }
    66  
    67  func TestInsert(t *testing.T) {
    68  	Convey("Given a valid batch provider", t, func() {
    69  		p := NewTestProvider([]string{mangle}, true)
    70  		Convey("When I insert a first rule, it should create the table", func() {
    71  			err := p.Insert(mangle, inputChain, 1, "val1")
    72  			So(err, ShouldBeNil)
    73  			So(len(p.rules[mangle]), ShouldEqual, 1)
    74  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
    75  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
    76  		})
    77  
    78  		Convey("When I insert two rules in the first position of the array, the values should be reverse ordered ", func() {
    79  			err := p.Insert(mangle, inputChain, 1, "val1")
    80  			So(err, ShouldBeNil)
    81  			err = p.Insert(mangle, inputChain, 1, "val2")
    82  			So(err, ShouldBeNil)
    83  			So(len(p.rules[mangle]), ShouldEqual, 1)
    84  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
    85  			rules := p.rules[mangle][inputChain]
    86  			So(rules[1], ShouldResemble, "\"val1\"")
    87  			So(rules[0], ShouldResemble, "\"val2\"")
    88  		})
    89  
    90  		Convey("When I insert two rules in the first and last position of the array ", func() {
    91  			err := p.Insert(mangle, inputChain, 1, "val1")
    92  			So(err, ShouldBeNil)
    93  			err = p.Insert(mangle, inputChain, 2, "val2")
    94  			So(err, ShouldBeNil)
    95  			So(len(p.rules[mangle]), ShouldEqual, 1)
    96  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
    97  			rules := p.rules[mangle][inputChain]
    98  			So(rules[0], ShouldResemble, "\"val1\"")
    99  			So(rules[1], ShouldResemble, "\"val2\"")
   100  		})
   101  
   102  		Convey("When I insert two rules in the first and a bad position in the array, the last one should be last ", func() {
   103  			err := p.Insert(mangle, inputChain, 1, "val1")
   104  			So(err, ShouldBeNil)
   105  			err = p.Insert(mangle, inputChain, 6, "val2")
   106  			So(err, ShouldBeNil)
   107  			So(len(p.rules[mangle]), ShouldEqual, 1)
   108  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   109  			rules := p.rules[mangle][inputChain]
   110  			So(rules[0], ShouldResemble, "\"val1\"")
   111  			So(rules[1], ShouldResemble, "\"val2\"")
   112  		})
   113  
   114  		Convey("When I insert a rule in the midle of the array, it should go in the right place ", func() {
   115  			err := p.Insert(mangle, inputChain, 1, "val3")
   116  			So(err, ShouldBeNil)
   117  			err = p.Insert(mangle, inputChain, 1, "val2")
   118  			So(err, ShouldBeNil)
   119  			err = p.Insert(mangle, inputChain, 1, "val1")
   120  			So(err, ShouldBeNil)
   121  			err = p.Insert(mangle, inputChain, 2, "val1-2")
   122  			So(err, ShouldBeNil)
   123  			err = p.Insert(mangle, inputChain, 4, "val2-3")
   124  			So(err, ShouldBeNil)
   125  			So(len(p.rules[mangle]), ShouldEqual, 1)
   126  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 5)
   127  			rules := p.rules[mangle][inputChain]
   128  			So(rules[0], ShouldResemble, "\"val1\"")
   129  			So(rules[1], ShouldResemble, "\"val1-2\"")
   130  			So(rules[2], ShouldResemble, "\"val2\"")
   131  			So(rules[3], ShouldResemble, "\"val2-3\"")
   132  			So(rules[4], ShouldResemble, "\"val3\"")
   133  		})
   134  
   135  		Convey("When I Insert two rules in different chains, there should be two chains", func() {
   136  			err := p.Insert(mangle, inputChain, 1, "val1")
   137  			So(err, ShouldBeNil)
   138  			err = p.Insert(mangle, outputChain, 1, "val2")
   139  			So(err, ShouldBeNil)
   140  			So(len(p.rules[mangle]), ShouldEqual, 2)
   141  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   142  			So(len(p.rules[mangle][outputChain]), ShouldEqual, 1)
   143  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   144  			So(p.rules[mangle][outputChain][0], ShouldResemble, "\"val2\"")
   145  		})
   146  	})
   147  }
   148  
   149  func TestInsertWithQuoteFalse(t *testing.T) {
   150  	Convey("Given a valid batch provider", t, func() {
   151  		p := NewTestProvider([]string{mangle}, false)
   152  		Convey("When I insert a first rule, it should create the table", func() {
   153  			err := p.Insert(mangle, inputChain, 1, "val1")
   154  			So(err, ShouldBeNil)
   155  			So(len(p.rules[mangle]), ShouldEqual, 1)
   156  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   157  			So(p.rules[mangle][inputChain][0], ShouldResemble, "val1")
   158  		})
   159  
   160  		Convey("When I insert two rules in the first position of the array, the values should be reverse ordered ", func() {
   161  			err := p.Insert(mangle, inputChain, 1, "val1")
   162  			So(err, ShouldBeNil)
   163  			err = p.Insert(mangle, inputChain, 1, "val2")
   164  			So(err, ShouldBeNil)
   165  			So(len(p.rules[mangle]), ShouldEqual, 1)
   166  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   167  			rules := p.rules[mangle][inputChain]
   168  			So(rules[1], ShouldResemble, "val1")
   169  			So(rules[0], ShouldResemble, "val2")
   170  		})
   171  
   172  		Convey("When I insert two rules in the first and last position of the array ", func() {
   173  			err := p.Insert(mangle, inputChain, 1, "val1")
   174  			So(err, ShouldBeNil)
   175  			err = p.Insert(mangle, inputChain, 2, "val2")
   176  			So(err, ShouldBeNil)
   177  			So(len(p.rules[mangle]), ShouldEqual, 1)
   178  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   179  			rules := p.rules[mangle][inputChain]
   180  			So(rules[0], ShouldResemble, "val1")
   181  			So(rules[1], ShouldResemble, "val2")
   182  		})
   183  
   184  		Convey("When I insert two rules in the first and a bad position in the array, the last one should be last ", func() {
   185  			err := p.Insert(mangle, inputChain, 1, "val1")
   186  			So(err, ShouldBeNil)
   187  			err = p.Insert(mangle, inputChain, 6, "val2")
   188  			So(err, ShouldBeNil)
   189  			So(len(p.rules[mangle]), ShouldEqual, 1)
   190  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   191  			rules := p.rules[mangle][inputChain]
   192  			So(rules[0], ShouldResemble, "val1")
   193  			So(rules[1], ShouldResemble, "val2")
   194  		})
   195  
   196  		Convey("When I insert a rule in the midle of the array, it should go in the right place ", func() {
   197  			err := p.Insert(mangle, inputChain, 1, "val3")
   198  			So(err, ShouldBeNil)
   199  			err = p.Insert(mangle, inputChain, 1, "val2")
   200  			So(err, ShouldBeNil)
   201  			err = p.Insert(mangle, inputChain, 1, "val1")
   202  			So(err, ShouldBeNil)
   203  			err = p.Insert(mangle, inputChain, 2, "val1-2")
   204  			So(err, ShouldBeNil)
   205  			err = p.Insert(mangle, inputChain, 4, "val2-3")
   206  			So(err, ShouldBeNil)
   207  			So(len(p.rules[mangle]), ShouldEqual, 1)
   208  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 5)
   209  			rules := p.rules[mangle][inputChain]
   210  			So(rules[0], ShouldResemble, "val1")
   211  			So(rules[1], ShouldResemble, "val1-2")
   212  			So(rules[2], ShouldResemble, "val2")
   213  			So(rules[3], ShouldResemble, "val2-3")
   214  			So(rules[4], ShouldResemble, "val3")
   215  		})
   216  
   217  		Convey("When I Insert two rules in different chains, there should be two chains", func() {
   218  			err := p.Insert(mangle, inputChain, 1, "val1")
   219  			So(err, ShouldBeNil)
   220  			err = p.Insert(mangle, outputChain, 1, "val2")
   221  			So(err, ShouldBeNil)
   222  			So(len(p.rules[mangle]), ShouldEqual, 2)
   223  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   224  			So(len(p.rules[mangle][outputChain]), ShouldEqual, 1)
   225  			So(p.rules[mangle][inputChain][0], ShouldResemble, "val1")
   226  			So(p.rules[mangle][outputChain][0], ShouldResemble, "val2")
   227  		})
   228  	})
   229  }
   230  
   231  func TestDelete(t *testing.T) {
   232  	Convey("Given a valid batch provider", t, func() {
   233  		p := NewTestProvider([]string{mangle}, true)
   234  		Convey("When I have one rule, I should be able to delete it", func() {
   235  			err := p.Append(mangle, inputChain, "val1")
   236  			So(err, ShouldBeNil)
   237  			So(len(p.rules[mangle]), ShouldEqual, 1)
   238  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   239  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   240  			err = p.Delete(mangle, inputChain, "val1")
   241  			So(err, ShouldBeNil)
   242  			So(len(p.rules[mangle]), ShouldEqual, 1)
   243  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 0)
   244  		})
   245  
   246  		Convey("When I have two rules, I should be able to delete the second one", func() {
   247  			err := p.Append(mangle, inputChain, "val1")
   248  			So(err, ShouldBeNil)
   249  			err = p.Append(mangle, inputChain, "val2")
   250  			So(err, ShouldBeNil)
   251  			So(len(p.rules[mangle]), ShouldEqual, 1)
   252  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   253  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   254  			So(p.rules[mangle][inputChain][1], ShouldResemble, "\"val2\"")
   255  			err = p.Delete(mangle, inputChain, "val2")
   256  			So(err, ShouldBeNil)
   257  			So(len(p.rules[mangle]), ShouldEqual, 1)
   258  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   259  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   260  		})
   261  
   262  		Convey("When I have two rules, I should be able to delete the first one", func() {
   263  			err := p.Append(mangle, inputChain, "val1")
   264  			So(err, ShouldBeNil)
   265  			err = p.Append(mangle, inputChain, "val2")
   266  			So(err, ShouldBeNil)
   267  			So(len(p.rules[mangle]), ShouldEqual, 1)
   268  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   269  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   270  			So(p.rules[mangle][inputChain][1], ShouldResemble, "\"val2\"")
   271  			err = p.Delete(mangle, inputChain, "val1")
   272  			So(err, ShouldBeNil)
   273  			So(len(p.rules[mangle]), ShouldEqual, 1)
   274  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   275  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val2\"")
   276  		})
   277  
   278  		Convey("When I have three rules, I should be able to delete the middle one", func() {
   279  			err := p.Append(mangle, inputChain, "val1")
   280  			So(err, ShouldBeNil)
   281  			err = p.Append(mangle, inputChain, "val2")
   282  			So(err, ShouldBeNil)
   283  			err = p.Append(mangle, inputChain, "val3")
   284  			So(err, ShouldBeNil)
   285  			So(len(p.rules[mangle]), ShouldEqual, 1)
   286  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 3)
   287  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   288  			So(p.rules[mangle][inputChain][1], ShouldResemble, "\"val2\"")
   289  			So(p.rules[mangle][inputChain][2], ShouldResemble, "\"val3\"")
   290  			err = p.Delete(mangle, inputChain, "val2")
   291  			So(err, ShouldBeNil)
   292  			So(len(p.rules[mangle]), ShouldEqual, 1)
   293  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 2)
   294  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   295  			So(p.rules[mangle][inputChain][1], ShouldResemble, "\"val3\"")
   296  		})
   297  	})
   298  }
   299  
   300  func TestClearChain(t *testing.T) {
   301  	Convey("Given a valid batch provider", t, func() {
   302  		p := NewTestProvider([]string{mangle}, true)
   303  		Convey("If a clear an empty chain, I should get no error", func() {
   304  			err := p.ClearChain(mangle, inputChain)
   305  			So(err, ShouldBeNil)
   306  			So(len(p.rules[mangle]), ShouldEqual, 0)
   307  		})
   308  		Convey("After I append a rule, I should be able to delete the chain", func() {
   309  			err := p.Append(mangle, inputChain, "val1")
   310  			So(err, ShouldBeNil)
   311  			So(len(p.rules[mangle]), ShouldEqual, 1)
   312  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 1)
   313  			So(p.rules[mangle][inputChain][0], ShouldResemble, "\"val1\"")
   314  			err = p.ClearChain(mangle, inputChain)
   315  			So(err, ShouldBeNil)
   316  			So(len(p.rules[mangle]), ShouldEqual, 1)
   317  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 0)
   318  		})
   319  	})
   320  }
   321  
   322  func TestDeleteChain(t *testing.T) {
   323  	Convey("Given a valid batch provider", t, func() {
   324  		p := NewTestProvider([]string{mangle}, true)
   325  		Convey("If a delete an empty chain, I should get no error", func() {
   326  			err := p.ClearChain(mangle, inputChain)
   327  			So(err, ShouldBeNil)
   328  			So(len(p.rules[mangle]), ShouldEqual, 0)
   329  		})
   330  		Convey("After I append a rule, I should be able to delete the chain", func() {
   331  			err := p.NewChain(mangle, inputChain)
   332  			So(err, ShouldBeNil)
   333  			So(len(p.rules[mangle]), ShouldEqual, 1)
   334  			So(len(p.rules[mangle][inputChain]), ShouldEqual, 0)
   335  
   336  			err = p.DeleteChain(mangle, inputChain)
   337  			So(err, ShouldBeNil)
   338  			So(len(p.rules[mangle]), ShouldEqual, 0)
   339  		})
   340  	})
   341  }
   342  
   343  func TestProvider(t *testing.T) {
   344  	b, err := NewGoIPTablesProviderV4([]string{}, "")
   345  	assert.Equal(t, b != nil, true, "go iptables should not be nil")
   346  	assert.Equal(t, err == nil, true, "error should be nil")
   347  	b, err = NewGoIPTablesProviderV6([]string{}, "")
   348  	assert.Equal(t, b != nil, true, "go iptables should not be nil")
   349  	assert.Equal(t, err == nil, true, "error should be nil")
   350  }