util_windows_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 util
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. )
  20. func TestParseEndpoint(t *testing.T) {
  21. tests := []struct {
  22. endpoint string
  23. expectError bool
  24. expectedProtocol string
  25. expectedAddr string
  26. }{
  27. {
  28. endpoint: "unix:///tmp/s1.sock",
  29. expectedProtocol: "unix",
  30. expectError: true,
  31. },
  32. {
  33. endpoint: "tcp://localhost:15880",
  34. expectedProtocol: "tcp",
  35. expectedAddr: "localhost:15880",
  36. },
  37. {
  38. endpoint: "npipe://./pipe/mypipe",
  39. expectedProtocol: "npipe",
  40. expectedAddr: "//./pipe/mypipe",
  41. },
  42. {
  43. endpoint: "npipe:////./pipe/mypipe2",
  44. expectedProtocol: "npipe",
  45. expectedAddr: "//./pipe/mypipe2",
  46. },
  47. {
  48. endpoint: "npipe:/pipe/mypipe3",
  49. expectedProtocol: "npipe",
  50. expectedAddr: "//./pipe/mypipe3",
  51. },
  52. {
  53. endpoint: "npipe:\\\\.\\pipe\\mypipe4",
  54. expectedProtocol: "npipe",
  55. expectedAddr: "//./pipe/mypipe4",
  56. },
  57. {
  58. endpoint: "npipe:\\pipe\\mypipe5",
  59. expectedProtocol: "npipe",
  60. expectedAddr: "//./pipe/mypipe5",
  61. },
  62. {
  63. endpoint: "tcp1://abc",
  64. expectedProtocol: "tcp1",
  65. expectError: true,
  66. },
  67. {
  68. endpoint: "a b c",
  69. expectError: true,
  70. },
  71. }
  72. for _, test := range tests {
  73. protocol, addr, err := parseEndpoint(test.endpoint)
  74. assert.Equal(t, test.expectedProtocol, protocol)
  75. if test.expectError {
  76. assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
  77. continue
  78. }
  79. require.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
  80. assert.Equal(t, test.expectedAddr, addr)
  81. }
  82. }