server_others_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // +build !windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package app
  15. import (
  16. "fmt"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/proxy/ipvs"
  19. )
  20. type fakeIPSetVersioner struct {
  21. version string // what to return
  22. err error // what to return
  23. }
  24. func (fake *fakeIPSetVersioner) GetVersion() (string, error) {
  25. return fake.version, fake.err
  26. }
  27. type fakeKernelCompatTester struct {
  28. ok bool
  29. }
  30. func (fake *fakeKernelCompatTester) IsCompatible() error {
  31. if !fake.ok {
  32. return fmt.Errorf("error")
  33. }
  34. return nil
  35. }
  36. // fakeKernelHandler implements KernelHandler.
  37. type fakeKernelHandler struct {
  38. modules []string
  39. kernelVersion string
  40. }
  41. func (fake *fakeKernelHandler) GetModules() ([]string, error) {
  42. return fake.modules, nil
  43. }
  44. func (fake *fakeKernelHandler) GetKernelVersion() (string, error) {
  45. return fake.kernelVersion, nil
  46. }
  47. func Test_getProxyMode(t *testing.T) {
  48. var cases = []struct {
  49. flag string
  50. ipsetVersion string
  51. kmods []string
  52. kernelVersion string
  53. kernelCompat bool
  54. ipsetError error
  55. expected string
  56. }{
  57. { // flag says userspace
  58. flag: "userspace",
  59. expected: proxyModeUserspace,
  60. },
  61. { // flag says iptables, kernel not compatible
  62. flag: "iptables",
  63. kernelCompat: false,
  64. expected: proxyModeUserspace,
  65. },
  66. { // flag says iptables, kernel is compatible
  67. flag: "iptables",
  68. kernelCompat: true,
  69. expected: proxyModeIPTables,
  70. },
  71. { // detect, kernel not compatible
  72. flag: "",
  73. kernelCompat: false,
  74. expected: proxyModeUserspace,
  75. },
  76. { // detect, kernel is compatible
  77. flag: "",
  78. kernelCompat: true,
  79. expected: proxyModeIPTables,
  80. },
  81. { // flag says ipvs, ipset version ok, kernel modules installed for linux kernel before 4.19
  82. flag: "ipvs",
  83. kmods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack_ipv4"},
  84. kernelVersion: "4.18",
  85. ipsetVersion: ipvs.MinIPSetCheckVersion,
  86. expected: proxyModeIPVS,
  87. },
  88. { // flag says ipvs, ipset version ok, kernel modules installed for linux kernel 4.19
  89. flag: "ipvs",
  90. kmods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack"},
  91. kernelVersion: "4.19",
  92. ipsetVersion: ipvs.MinIPSetCheckVersion,
  93. expected: proxyModeIPVS,
  94. },
  95. { // flag says ipvs, ipset version too low, fallback on iptables mode
  96. flag: "ipvs",
  97. kmods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack"},
  98. kernelVersion: "4.19",
  99. ipsetVersion: "0.0",
  100. kernelCompat: true,
  101. expected: proxyModeIPTables,
  102. },
  103. { // flag says ipvs, bad ipset version, fallback on iptables mode
  104. flag: "ipvs",
  105. kmods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack"},
  106. kernelVersion: "4.19",
  107. ipsetVersion: "a.b.c",
  108. kernelCompat: true,
  109. expected: proxyModeIPTables,
  110. },
  111. { // flag says ipvs, required kernel modules are not installed, fallback on iptables mode
  112. flag: "ipvs",
  113. kmods: []string{"foo", "bar", "baz"},
  114. kernelVersion: "4.19",
  115. ipsetVersion: ipvs.MinIPSetCheckVersion,
  116. kernelCompat: true,
  117. expected: proxyModeIPTables,
  118. },
  119. }
  120. for i, c := range cases {
  121. kcompater := &fakeKernelCompatTester{c.kernelCompat}
  122. ipsetver := &fakeIPSetVersioner{c.ipsetVersion, c.ipsetError}
  123. khandler := &fakeKernelHandler{
  124. modules: c.kmods,
  125. kernelVersion: c.kernelVersion,
  126. }
  127. r := getProxyMode(c.flag, khandler, ipsetver, kcompater)
  128. if r != c.expected {
  129. t.Errorf("Case[%d] Expected %q, got %q", i, c.expected, r)
  130. }
  131. }
  132. }