iptables_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. _, ipv6Net, _ := net.ParseCIDR("fc00::/48")
  27. _, net, _ := net.ParseCIDR("192.168.0.0/16")
  28. return &subnet.Lease{
  29. Subnet: ip.FromIPNet(net),
  30. IPv6Subnet: ip.FromIP6Net(ipv6Net),
  31. }
  32. }
  33. type MockIPTables struct {
  34. rules []IPTablesRule
  35. t *testing.T
  36. failures map[string]*MockIPTablesError
  37. }
  38. type MockIPTablesError struct {
  39. notExist bool
  40. }
  41. func (mock *MockIPTablesError) IsNotExist() bool {
  42. return mock.notExist
  43. }
  44. func (mock *MockIPTablesError) Error() string {
  45. return fmt.Sprintf("IsNotExist: %v", !mock.notExist)
  46. }
  47. func (mock *MockIPTables) failDelete(table string, chain string, rulespec []string, notExist bool) {
  48. if mock.failures == nil {
  49. mock.failures = make(map[string]*MockIPTablesError)
  50. }
  51. key := table + chain + strings.Join(rulespec, "")
  52. mock.failures[key] = &MockIPTablesError{
  53. notExist: notExist,
  54. }
  55. }
  56. func (mock *MockIPTables) ruleIndex(table string, chain string, rulespec []string) int {
  57. for i, rule := range mock.rules {
  58. if rule.table == table && rule.chain == chain && reflect.DeepEqual(rule.rulespec, rulespec) {
  59. return i
  60. }
  61. }
  62. return -1
  63. }
  64. func (mock *MockIPTables) Delete(table string, chain string, rulespec ...string) error {
  65. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  66. key := table + chain + strings.Join(rulespec, "")
  67. reason := mock.failures[key]
  68. if reason != nil {
  69. return reason
  70. }
  71. if ruleIndex != -1 {
  72. mock.rules = append(mock.rules[:ruleIndex], mock.rules[ruleIndex+1:]...)
  73. }
  74. return nil
  75. }
  76. func (mock *MockIPTables) Exists(table string, chain string, rulespec ...string) (bool, error) {
  77. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  78. if ruleIndex != -1 {
  79. return true, nil
  80. }
  81. return false, nil
  82. }
  83. func (mock *MockIPTables) AppendUnique(table string, chain string, rulespec ...string) error {
  84. var ruleIndex = mock.ruleIndex(table, chain, rulespec)
  85. if ruleIndex == -1 {
  86. mock.rules = append(mock.rules, IPTablesRule{table: table, chain: chain, rulespec: rulespec})
  87. }
  88. return nil
  89. }
  90. func TestDeleteRules(t *testing.T) {
  91. ipt := &MockIPTables{t: t}
  92. setupIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  93. if len(ipt.rules) != 4 {
  94. t.Errorf("Should be 4 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  95. }
  96. teardownIPTables(ipt, MasqRules(ip.IP4Net{}, lease()))
  97. if len(ipt.rules) != 0 {
  98. t.Errorf("Should be 0 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  99. }
  100. }
  101. func TestDeleteIP6Rules(t *testing.T) {
  102. ipt := &MockIPTables{t: t}
  103. setupIPTables(ipt, MasqIP6Rules(ip.IP6Net{}, lease()))
  104. if len(ipt.rules) != 4 {
  105. t.Errorf("Should be 4 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  106. }
  107. teardownIPTables(ipt, MasqIP6Rules(ip.IP6Net{}, lease()))
  108. if len(ipt.rules) != 0 {
  109. t.Errorf("Should be 0 masqRules, there are actually %d: %#v", len(ipt.rules), ipt.rules)
  110. }
  111. }
  112. func TestEnsureRulesError(t *testing.T) {
  113. // If an error prevents a rule from being deleted, ensureIPTables should leave the rules as is
  114. // rather than potentially re-appending rules in an incorrect order
  115. ipt_correct := &MockIPTables{t: t}
  116. setupIPTables(ipt_correct, MasqRules(ip.IP4Net{}, lease()))
  117. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  118. ipt_recreate := &MockIPTables{t: t}
  119. setupIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  120. ipt_recreate.rules = ipt_recreate.rules[0:2]
  121. rule := ipt_recreate.rules[1]
  122. ipt_recreate.failDelete(rule.table, rule.chain, rule.rulespec, false)
  123. err := ensureIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  124. if err == nil {
  125. t.Errorf("ensureIPTables should have failed but did not.")
  126. }
  127. if len(ipt_recreate.rules) == len(ipt_correct.rules) {
  128. t.Errorf("ensureIPTables should not have completed.")
  129. }
  130. }
  131. func TestEnsureIP6RulesError(t *testing.T) {
  132. // If an error prevents a rule from being deleted, ensureIPTables should leave the rules as is
  133. // rather than potentially re-appending rules in an incorrect order
  134. ipt_correct := &MockIPTables{t: t}
  135. setupIPTables(ipt_correct, MasqIP6Rules(ip.IP6Net{}, lease()))
  136. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  137. ipt_recreate := &MockIPTables{t: t}
  138. setupIPTables(ipt_recreate, MasqIP6Rules(ip.IP6Net{}, lease()))
  139. ipt_recreate.rules = ipt_recreate.rules[0:2]
  140. rule := ipt_recreate.rules[1]
  141. ipt_recreate.failDelete(rule.table, rule.chain, rule.rulespec, false)
  142. err := ensureIPTables(ipt_recreate, MasqIP6Rules(ip.IP6Net{}, lease()))
  143. if err == nil {
  144. t.Errorf("ensureIPTables should have failed but did not.")
  145. }
  146. if len(ipt_recreate.rules) == len(ipt_correct.rules) {
  147. t.Errorf("ensureIPTables should not have completed.")
  148. }
  149. }
  150. func TestEnsureRules(t *testing.T) {
  151. // If any masqRules are missing, they should be all deleted and recreated in the correct order
  152. ipt_correct := &MockIPTables{t: t}
  153. setupIPTables(ipt_correct, MasqRules(ip.IP4Net{}, lease()))
  154. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  155. ipt_recreate := &MockIPTables{t: t}
  156. setupIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  157. ipt_recreate.rules = ipt_recreate.rules[0:2]
  158. // set up a normal error that iptables returns when deleting a rule that is already gone
  159. deletedRule := ipt_correct.rules[3]
  160. ipt_recreate.failDelete(deletedRule.table, deletedRule.chain, deletedRule.rulespec, true)
  161. err := ensureIPTables(ipt_recreate, MasqRules(ip.IP4Net{}, lease()))
  162. if err != nil {
  163. t.Errorf("ensureIPTables should have completed without errors")
  164. }
  165. if !reflect.DeepEqual(ipt_recreate.rules, ipt_correct.rules) {
  166. t.Errorf("iptables masqRules after ensureIPTables are incorrect. Expected: %#v, Actual: %#v", ipt_recreate.rules, ipt_correct.rules)
  167. }
  168. }
  169. func TestEnsureIP6Rules(t *testing.T) {
  170. // If any masqRules are missing, they should be all deleted and recreated in the correct order
  171. ipt_correct := &MockIPTables{t: t}
  172. setupIPTables(ipt_correct, MasqIP6Rules(ip.IP6Net{}, lease()))
  173. // setup a mock instance where we delete some masqRules and run `ensureIPTables`
  174. ipt_recreate := &MockIPTables{t: t}
  175. setupIPTables(ipt_recreate, MasqIP6Rules(ip.IP6Net{}, lease()))
  176. ipt_recreate.rules = ipt_recreate.rules[0:2]
  177. // set up a normal error that iptables returns when deleting a rule that is already gone
  178. deletedRule := ipt_correct.rules[3]
  179. ipt_recreate.failDelete(deletedRule.table, deletedRule.chain, deletedRule.rulespec, true)
  180. err := ensureIPTables(ipt_recreate, MasqIP6Rules(ip.IP6Net{}, lease()))
  181. if err != nil {
  182. t.Errorf("ensureIPTables should have completed without errors")
  183. }
  184. if !reflect.DeepEqual(ipt_recreate.rules, ipt_correct.rules) {
  185. t.Errorf("iptables masqIP6Rules after ensureIPTables are incorrect. Expected: %#v, Actual: %#v", ipt_recreate.rules, ipt_correct.rules)
  186. }
  187. }