iptables_test.go 3.0 KB

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