util_unix_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // +build freebsd linux darwin
  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 util
  15. import (
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. )
  23. func TestParseEndpoint(t *testing.T) {
  24. tests := []struct {
  25. endpoint string
  26. expectError bool
  27. expectedProtocol string
  28. expectedAddr string
  29. }{
  30. {
  31. endpoint: "unix:///tmp/s1.sock",
  32. expectedProtocol: "unix",
  33. expectedAddr: "/tmp/s1.sock",
  34. },
  35. {
  36. endpoint: "tcp://localhost:15880",
  37. expectedProtocol: "tcp",
  38. expectedAddr: "localhost:15880",
  39. },
  40. {
  41. endpoint: "npipe://./pipe/mypipe",
  42. expectedProtocol: "npipe",
  43. expectError: true,
  44. },
  45. {
  46. endpoint: "tcp1://abc",
  47. expectedProtocol: "tcp1",
  48. expectError: true,
  49. },
  50. {
  51. endpoint: "a b c",
  52. expectError: true,
  53. },
  54. }
  55. for _, test := range tests {
  56. protocol, addr, err := parseEndpoint(test.endpoint)
  57. assert.Equal(t, test.expectedProtocol, protocol)
  58. if test.expectError {
  59. assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
  60. continue
  61. }
  62. assert.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
  63. assert.Equal(t, test.expectedAddr, addr)
  64. }
  65. }
  66. func TestIsUnixDomainSocket(t *testing.T) {
  67. tests := []struct {
  68. label string
  69. listenOnSocket bool
  70. expectSocket bool
  71. expectError bool
  72. invalidFile bool
  73. }{
  74. {
  75. label: "Domain Socket file",
  76. listenOnSocket: true,
  77. expectSocket: true,
  78. expectError: false,
  79. },
  80. {
  81. label: "Non Existent file",
  82. invalidFile: true,
  83. expectError: true,
  84. },
  85. {
  86. label: "Regular file",
  87. listenOnSocket: false,
  88. expectSocket: false,
  89. expectError: false,
  90. },
  91. }
  92. for _, test := range tests {
  93. f, err := ioutil.TempFile("", "test-domain-socket")
  94. require.NoErrorf(t, err, "Failed to create file for test purposes: %v while setting up: %s", err, test.label)
  95. addr := f.Name()
  96. f.Close()
  97. var ln *net.UnixListener
  98. if test.listenOnSocket {
  99. os.Remove(addr)
  100. ta, err := net.ResolveUnixAddr("unix", addr)
  101. require.NoErrorf(t, err, "Failed to ResolveUnixAddr: %v while setting up: %s", err, test.label)
  102. ln, err = net.ListenUnix("unix", ta)
  103. require.NoErrorf(t, err, "Failed to ListenUnix: %v while setting up: %s", err, test.label)
  104. }
  105. fileToTest := addr
  106. if test.invalidFile {
  107. fileToTest = fileToTest + ".invalid"
  108. }
  109. result, err := IsUnixDomainSocket(fileToTest)
  110. if test.listenOnSocket {
  111. // this takes care of removing the file associated with the domain socket
  112. ln.Close()
  113. } else {
  114. // explicitly remove regular file
  115. os.Remove(addr)
  116. }
  117. if test.expectError {
  118. assert.NotNil(t, err, "Unexpected nil error from IsUnixDomainSocket for %s", test.label)
  119. } else {
  120. assert.Nil(t, err, "Unexpected error invoking IsUnixDomainSocket for %s", test.label)
  121. }
  122. assert.Equal(t, result, test.expectSocket, "Unexpected result from IsUnixDomainSocket: %v for %s", result, test.label)
  123. }
  124. }