iptables_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "fmt"
  18. "net"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "github.com/flannel-io/flannel/pkg/ip"
  23. "github.com/flannel-io/flannel/subnet"
  24. )
  25. func lease() *subnet.Lease {
  26. _, net, _ := net.ParseCIDR("192.168.0.0/16")
  27. return &subnet.Lease{
  28. Subnet: ip.FromIPNet(net),
  29. }
  30. }
  31. type MockIPTables struct {
  32. rules []IPTablesRule
  33. t *testing.T
  34. failures map[string]*MockIPTablesError
  35. }
  36. type MockIPTablesError struct {
  37. notExist bool
  38. }
  39. func (mock *MockIPTablesError) IsNotExist() bool {
  40. return mock.notExist
  41. }
  42. func (mock *MockIPTablesError) Error() string {
  43. return fmt.Sprintf("IsNotExist: %v", !mock.notExist)
  44. }
  45. func (mock *MockIPTables) failDelete(table string, chain string, rulespec []string, notExist bool) {
  46. if mock.failures == nil {
  47. mock.failures = make(map[string]*MockIPTablesError)
  48. }
  49. key := table + chain + strings.Join(rulespec, "")
  50. mock.failures[key] = &MockIPTablesError{
  51. notExist: notExist,
  52. }
  53. }
  54. func (mock *MockIPTables) ruleIndex(table string, chain string, rulespec []string) int {
  55. for i, rule := range mock.rules {
  56. if rule.table == table && rule.chain == chain && reflect.DeepEqual(rule.rulespec, rulespec) {
  57. return i
  58. }
  59. }
  60. return -1
  61. }
  62. func (mock *MockIPTables) Delete(table string, chain string, rulespec ...string) error {
  63. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  64. key := table + chain + strings.Join(rulespec, "")
  65. reason := mock.failures[key]
  66. if reason != nil {
  67. return reason
  68. }
  69. if ruleIndex != -1 {
  70. mock.rules = append(mock.rules[:ruleIndex], mock.rules[ruleIndex+1:]...)
  71. }
  72. return nil
  73. }
  74. func (mock *MockIPTables) Exists(table string, chain string, rulespec ...string) (bool, error) {
  75. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  76. if ruleIndex != -1 {
  77. return true, nil
  78. }
  79. return false, nil
  80. }
  81. func (mock *MockIPTables) AppendUnique(table string, chain string, rulespec ...string) error {
  82. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  83. if ruleIndex == -1 {
  84. mock.rules = append(mock.rules, IPTablesRule{table: table, chain: chain, rulespec: rulespec})
  85. }
  86. return nil
  87. }
  88. func TestDeleteRules(t *testing.T) {
  89. ipt := &MockIPTables{t: t}
  90. setupIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  91. if len(ipt.rules) != 4 {
  92. t.Errorf("Should be 4 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  93. }
  94. teardownIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  95. if len(ipt.rules) != 0 {
  96. t.Errorf("Should be 0 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  97. }
  98. }
  99. func TestEnsureRulesError(t *testing.T) {
  100. // If an error prevents a rule from being deleted, ensureIPTables should leave the rules as is
  101. // rather than potentially re-appending rules in an incorrect order
  102. ipt_correct := &MockIPTables{t: t}
  103. setupIPTables(ipt_correct, MasqRules(ip.IP4Net{}, lease()))
  104. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  105. ipt_recreate := &MockIPTables{t: t}
  106. setupIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  107. ipt_recreate.rules = ipt_recreate.rules[0:2]
  108. rule := ipt_recreate.rules[1]
  109. ipt_recreate.failDelete(rule.table, rule.chain, rule.rulespec, false)
  110. err := ensureIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  111. if err == nil {
  112. t.Errorf("ensureIPTables should have failed but did not.")
  113. }
  114. if len(ipt_recreate.rules) == len(ipt_correct.rules) {
  115. t.Errorf("ensureIPTables should not have completed.")
  116. }
  117. }
  118. func TestEnsureRules(t *testing.T) {
  119. // If any masqRules are missing, they should be all deleted and recreated in the correct order
  120. ipt_correct := &MockIPTables{t: t}
  121. setupIPTables(ipt_correct, MasqRules(ip.IP4Net{}, lease()))
  122. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  123. ipt_recreate := &MockIPTables{t: t}
  124. setupIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  125. ipt_recreate.rules = ipt_recreate.rules[0:2]
  126. // set up a normal error that iptables returns when deleting a rule that is already gone
  127. deletedRule := ipt_correct.rules[3]
  128. ipt_recreate.failDelete(deletedRule.table, deletedRule.chain, deletedRule.rulespec, true)
  129. err := ensureIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  130. if err != nil {
  131. t.Errorf("ensureIPTables should have completed without errors")
  132. }
  133. if !reflect.DeepEqual(ipt_recreate.rules, ipt_correct.rules) {
  134. t.Errorf("iptables masqRules after ensureIPTables are incorrect. Expected: %#v, Actual: %#v", ipt_recreate.rules, ipt_correct.rules)
  135. }
  136. }