ipmasq_test.go 3.0 KB

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