hairpin_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package hairpin
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "os"
  19. "strings"
  20. "testing"
  21. "k8s.io/utils/exec"
  22. fakeexec "k8s.io/utils/exec/testing"
  23. )
  24. func TestFindPairInterfaceOfContainerInterface(t *testing.T) {
  25. // there should be at least "lo" on any system
  26. interfaces, _ := net.Interfaces()
  27. validOutput := fmt.Sprintf("garbage\n peer_ifindex: %d", interfaces[0].Index)
  28. invalidOutput := fmt.Sprintf("garbage\n unknown: %d", interfaces[0].Index)
  29. tests := []struct {
  30. output string
  31. err error
  32. expectedName string
  33. expectErr bool
  34. }{
  35. {
  36. output: validOutput,
  37. expectedName: interfaces[0].Name,
  38. },
  39. {
  40. output: invalidOutput,
  41. expectErr: true,
  42. },
  43. {
  44. output: validOutput,
  45. err: errors.New("error"),
  46. expectErr: true,
  47. },
  48. }
  49. for _, test := range tests {
  50. fcmd := fakeexec.FakeCmd{
  51. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  52. func() ([]byte, error) { return []byte(test.output), test.err },
  53. },
  54. }
  55. fexec := fakeexec.FakeExec{
  56. CommandScript: []fakeexec.FakeCommandAction{
  57. func(cmd string, args ...string) exec.Cmd {
  58. return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
  59. },
  60. },
  61. LookPathFunc: func(file string) (string, error) {
  62. return fmt.Sprintf("/fake-bin/%s", file), nil
  63. },
  64. }
  65. nsenterArgs := []string{"-t", "123", "-n"}
  66. name, err := findPairInterfaceOfContainerInterface(&fexec, "eth0", "123", nsenterArgs)
  67. if test.expectErr {
  68. if err == nil {
  69. t.Errorf("unexpected non-error")
  70. }
  71. } else {
  72. if err != nil {
  73. t.Errorf("unexpected error: %v", err)
  74. }
  75. }
  76. if name != test.expectedName {
  77. t.Errorf("unexpected name: %s (expected: %s)", name, test.expectedName)
  78. }
  79. }
  80. }
  81. func TestSetUpInterfaceNonExistent(t *testing.T) {
  82. err := setUpInterface("non-existent")
  83. if err == nil {
  84. t.Errorf("unexpected non-error")
  85. }
  86. deviceDir := fmt.Sprintf("%s/%s", sysfsNetPath, "non-existent")
  87. if !strings.Contains(fmt.Sprintf("%v", err), deviceDir) {
  88. t.Errorf("should have tried to open %s", deviceDir)
  89. }
  90. }
  91. func TestSetUpInterfaceNotBridged(t *testing.T) {
  92. err := setUpInterface("lo")
  93. if err != nil {
  94. if os.IsNotExist(err) {
  95. t.Skipf("'lo' device does not exist??? (%v)", err)
  96. }
  97. t.Errorf("unexpected error: %v", err)
  98. }
  99. }