mount_linux_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // +build linux
  2. /*
  3. Copyright 2014 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 mount
  15. import (
  16. "strings"
  17. "testing"
  18. )
  19. func TestReadProcMountsFrom(t *testing.T) {
  20. successCase :=
  21. `/dev/0 /path/to/0 type0 flags 0 0
  22. /dev/1 /path/to/1 type1 flags 1 1
  23. /dev/2 /path/to/2 type2 flags,1,2=3 2 2
  24. `
  25. hash, err := readProcMountsFrom(strings.NewReader(successCase), nil)
  26. if err != nil {
  27. t.Errorf("expected success")
  28. }
  29. if hash != 0xa3522051 {
  30. t.Errorf("expected 0xa3522051, got %#x", hash)
  31. }
  32. mounts := []MountPoint{}
  33. hash, err = readProcMountsFrom(strings.NewReader(successCase), &mounts)
  34. if err != nil {
  35. t.Errorf("expected success")
  36. }
  37. if hash != 0xa3522051 {
  38. t.Errorf("expected 0xa3522051, got %#x", hash)
  39. }
  40. if len(mounts) != 3 {
  41. t.Fatalf("expected 3 mounts, got %d", len(mounts))
  42. }
  43. mp := MountPoint{"/dev/0", "/path/to/0", "type0", []string{"flags"}, 0, 0}
  44. if !mountPointsEqual(&mounts[0], &mp) {
  45. t.Errorf("got unexpected MountPoint[0]: %#v", mounts[0])
  46. }
  47. mp = MountPoint{"/dev/1", "/path/to/1", "type1", []string{"flags"}, 1, 1}
  48. if !mountPointsEqual(&mounts[1], &mp) {
  49. t.Errorf("got unexpected MountPoint[1]: %#v", mounts[1])
  50. }
  51. mp = MountPoint{"/dev/2", "/path/to/2", "type2", []string{"flags", "1", "2=3"}, 2, 2}
  52. if !mountPointsEqual(&mounts[2], &mp) {
  53. t.Errorf("got unexpected MountPoint[2]: %#v", mounts[2])
  54. }
  55. errorCases := []string{
  56. "/dev/0 /path/to/mount\n",
  57. "/dev/1 /path/to/mount type flags a 0\n",
  58. "/dev/2 /path/to/mount type flags 0 b\n",
  59. }
  60. for _, ec := range errorCases {
  61. _, err := readProcMountsFrom(strings.NewReader(ec), &mounts)
  62. if err == nil {
  63. t.Errorf("expected error")
  64. }
  65. }
  66. }
  67. func mountPointsEqual(a, b *MountPoint) bool {
  68. if a.Device != b.Device || a.Path != b.Path || a.Type != b.Type || !slicesEqual(a.Opts, b.Opts) || a.Pass != b.Pass || a.Freq != b.Freq {
  69. return false
  70. }
  71. return true
  72. }
  73. func slicesEqual(a, b []string) bool {
  74. if len(a) != len(b) {
  75. return false
  76. }
  77. for i := range a {
  78. if a[i] != b[i] {
  79. return false
  80. }
  81. }
  82. return true
  83. }
  84. func TestGetMountRefs(t *testing.T) {
  85. fm := &FakeMounter{
  86. MountPoints: []MountPoint{
  87. {Device: "/dev/sdb", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd"},
  88. {Device: "/dev/sdb", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod"},
  89. {Device: "/dev/sdc", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2"},
  90. {Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod"},
  91. {Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2"},
  92. },
  93. }
  94. tests := []struct {
  95. mountPath string
  96. expectedRefs []string
  97. }{
  98. {
  99. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod",
  100. []string{
  101. "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd",
  102. },
  103. },
  104. {
  105. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod",
  106. []string{
  107. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2",
  108. "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2",
  109. },
  110. },
  111. }
  112. for i, test := range tests {
  113. if refs, err := GetMountRefs(fm, test.mountPath); err != nil || !setEquivalent(test.expectedRefs, refs) {
  114. t.Errorf("%d. getMountRefs(%q) = %v, %v; expected %v, nil", i, test.mountPath, refs, err, test.expectedRefs)
  115. }
  116. }
  117. }
  118. func setEquivalent(set1, set2 []string) bool {
  119. map1 := make(map[string]bool)
  120. map2 := make(map[string]bool)
  121. for _, s := range set1 {
  122. map1[s] = true
  123. }
  124. for _, s := range set2 {
  125. map2[s] = true
  126. }
  127. for s := range map1 {
  128. if !map2[s] {
  129. return false
  130. }
  131. }
  132. for s := range map2 {
  133. if !map1[s] {
  134. return false
  135. }
  136. }
  137. return true
  138. }
  139. func TestGetDeviceNameFromMount(t *testing.T) {
  140. fm := &FakeMounter{
  141. MountPoints: []MountPoint{
  142. {Device: "/dev/disk/by-path/prefix-lun-1",
  143. Path: "/mnt/111"},
  144. {Device: "/dev/disk/by-path/prefix-lun-1",
  145. Path: "/mnt/222"},
  146. },
  147. }
  148. tests := []struct {
  149. mountPath string
  150. expectedDevice string
  151. expectedRefs int
  152. }{
  153. {
  154. "/mnt/222",
  155. "/dev/disk/by-path/prefix-lun-1",
  156. 2,
  157. },
  158. }
  159. for i, test := range tests {
  160. if device, refs, err := GetDeviceNameFromMount(fm, test.mountPath); err != nil || test.expectedRefs != refs || test.expectedDevice != device {
  161. t.Errorf("%d. GetDeviceNameFromMount(%s) = (%s, %d), %v; expected (%s,%d), nil", i, test.mountPath, device, refs, err, test.expectedDevice, test.expectedRefs)
  162. }
  163. }
  164. }