iptables_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 flannel authors
  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. // +build !windows
  15. package network
  16. import (
  17. "net"
  18. "reflect"
  19. "testing"
  20. "github.com/coreos/flannel/pkg/ip"
  21. "github.com/coreos/flannel/subnet"
  22. )
  23. func lease() *subnet.Lease {
  24. _, net, _ := net.ParseCIDR("192.168.0.0/16")
  25. return &subnet.Lease{
  26. Subnet: ip.FromIPNet(net),
  27. }
  28. }
  29. type MockIPTables struct {
  30. rules []IPTablesRule
  31. }
  32. func (mock *MockIPTables) ruleIndex(table string, chain string, rulespec []string) int {
  33. for i, rule := range mock.rules {
  34. if rule.table == table && rule.chain == chain && reflect.DeepEqual(rule.rulespec, rulespec) {
  35. return i
  36. }
  37. }
  38. return -1
  39. }
  40. func (mock *MockIPTables) Delete(table string, chain string, rulespec ...string) error {
  41. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  42. if ruleIndex != -1 {
  43. mock.rules = append(mock.rules[:ruleIndex], mock.rules[ruleIndex+1:]...)
  44. }
  45. return nil
  46. }
  47. func (mock *MockIPTables) Exists(table string, chain string, rulespec ...string) (bool, error) {
  48. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  49. if ruleIndex != -1 {
  50. return true, nil
  51. }
  52. return false, nil
  53. }
  54. func (mock *MockIPTables) AppendUnique(table string, chain string, rulespec ...string) error {
  55. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  56. if ruleIndex == -1 {
  57. mock.rules = append(mock.rules, IPTablesRule{table: table, chain: chain, rulespec: rulespec})
  58. }
  59. return nil
  60. }
  61. func TestDeleteRules(t *testing.T) {
  62. ipt := &MockIPTables{}
  63. setupIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  64. if len(ipt.rules) != 4 {
  65. t.Errorf("Should be 4 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  66. }
  67. teardownIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  68. if len(ipt.rules) != 0 {
  69. t.Errorf("Should be 0 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  70. }
  71. }
  72. func TestEnsureRules(t *testing.T) {
  73. // If any masqRules are missing, they should be all deleted and recreated in the correct order
  74. ipt_correct := &MockIPTables{}
  75. setupIPTables(ipt_correct, MasqRules(ip.IP4Net{}, lease()))
  76. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  77. ipt_recreate := &MockIPTables{}
  78. setupIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  79. ipt_recreate.rules = ipt_recreate.rules[0:2]
  80. ensureIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  81. if !reflect.DeepEqual(ipt_recreate.rules, ipt_correct.rules) {
  82. t.Errorf("iptables masqRules after ensureIPTables are incorrected. Expected: %#v, Actual: %#v", ipt_recreate.rules, ipt_correct.rules)
  83. }
  84. }