fc_util_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 fc
  14. import (
  15. "os"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "k8s.io/kubernetes/pkg/volume/util"
  20. )
  21. type fakeFileInfo struct {
  22. name string
  23. }
  24. func (fi *fakeFileInfo) Name() string {
  25. return fi.name
  26. }
  27. func (fi *fakeFileInfo) Size() int64 {
  28. return 0
  29. }
  30. func (fi *fakeFileInfo) Mode() os.FileMode {
  31. return 777
  32. }
  33. func (fi *fakeFileInfo) ModTime() time.Time {
  34. return time.Now()
  35. }
  36. func (fi *fakeFileInfo) IsDir() bool {
  37. return false
  38. }
  39. func (fi *fakeFileInfo) Sys() interface{} {
  40. return nil
  41. }
  42. type fakeIOHandler struct{}
  43. func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
  44. switch dirname {
  45. case "/dev/disk/by-path/":
  46. f := &fakeFileInfo{
  47. name: "pci-0000:41:00.0-fc-0x500a0981891b8dc5-lun-0",
  48. }
  49. return []os.FileInfo{f}, nil
  50. case "/sys/block/":
  51. f := &fakeFileInfo{
  52. name: "dm-1",
  53. }
  54. return []os.FileInfo{f}, nil
  55. case "/dev/disk/by-id/":
  56. f := &fakeFileInfo{
  57. name: "scsi-3600508b400105e210000900000490000",
  58. }
  59. return []os.FileInfo{f}, nil
  60. }
  61. return nil, nil
  62. }
  63. func (handler *fakeIOHandler) Lstat(name string) (os.FileInfo, error) {
  64. return nil, nil
  65. }
  66. func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
  67. return "/dev/sda", nil
  68. }
  69. func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.FileMode) error {
  70. return nil
  71. }
  72. func TestSearchDisk(t *testing.T) {
  73. fakeMounter := fcDiskMounter{
  74. fcDisk: &fcDisk{
  75. wwns: []string{"500a0981891b8dc5"},
  76. lun: "0",
  77. io: &fakeIOHandler{},
  78. },
  79. deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
  80. }
  81. devicePath, error := searchDisk(fakeMounter)
  82. // if no disk matches input wwn and lun, exit
  83. if devicePath == "" || error != nil {
  84. t.Errorf("no fc disk found")
  85. }
  86. }
  87. func TestSearchDiskWWID(t *testing.T) {
  88. fakeMounter := fcDiskMounter{
  89. fcDisk: &fcDisk{
  90. wwids: []string{"3600508b400105e210000900000490000"},
  91. io: &fakeIOHandler{},
  92. },
  93. deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
  94. }
  95. devicePath, error := searchDisk(fakeMounter)
  96. // if no disk matches input wwid, exit
  97. if devicePath == "" || error != nil {
  98. t.Errorf("no fc disk found")
  99. }
  100. }
  101. func TestParsePDName(t *testing.T) {
  102. tests := []struct {
  103. name string
  104. path string
  105. wwns []string
  106. lun int32
  107. wwids []string
  108. expectError bool
  109. }{
  110. {
  111. name: "single WWID",
  112. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/60050763008084e6e0000000000001ae",
  113. wwids: []string{"60050763008084e6e0000000000001ae"},
  114. },
  115. {
  116. name: "multiple WWID",
  117. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/60050763008084e6e0000000000001ae-60050763008084e6e0000000000001af",
  118. wwids: []string{"60050763008084e6e0000000000001ae", "60050763008084e6e0000000000001af"},
  119. },
  120. {
  121. name: "single WWN",
  122. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/50050768030539b6-lun-0",
  123. wwns: []string{"50050768030539b6"},
  124. lun: 0,
  125. },
  126. {
  127. name: "multiple WWNs",
  128. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/50050768030539b6-50050768030539b7-lun-0",
  129. wwns: []string{"50050768030539b6", "50050768030539b7"},
  130. lun: 0,
  131. },
  132. {
  133. name: "no WWNs",
  134. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/lun-0",
  135. expectError: true,
  136. },
  137. {
  138. name: "invalid lun",
  139. path: "/var/lib/kubelet/plugins/kubernetes.io/fc/50050768030539b6-lun-x",
  140. expectError: true,
  141. },
  142. }
  143. for _, test := range tests {
  144. t.Run(test.name, func(t *testing.T) {
  145. wwns, lun, wwids, err := parsePDName(test.path)
  146. if test.expectError && err == nil {
  147. t.Errorf("expected error but got none")
  148. }
  149. if !test.expectError && err != nil {
  150. t.Errorf("got unexpected error: %s", err)
  151. }
  152. if !reflect.DeepEqual(wwns, test.wwns) {
  153. t.Errorf("expected WWNs %+v, got %+v", test.wwns, wwns)
  154. }
  155. if lun != test.lun {
  156. t.Errorf("expected lun %d, got %d", test.lun, lun)
  157. }
  158. if !reflect.DeepEqual(wwids, test.wwids) {
  159. t.Errorf("expected WWIDs %+v, got %+v", test.wwids, wwids)
  160. }
  161. })
  162. }
  163. }