azure_common_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // +build !providerless
  2. /*
  3. Copyright 2017 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 azure_dd
  15. import (
  16. "fmt"
  17. "os"
  18. "runtime"
  19. "strings"
  20. "testing"
  21. "time"
  22. "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
  23. "github.com/stretchr/testify/assert"
  24. "k8s.io/utils/exec"
  25. )
  26. type fakeFileInfo struct {
  27. name string
  28. }
  29. func (fi *fakeFileInfo) Name() string {
  30. return fi.name
  31. }
  32. func (fi *fakeFileInfo) Size() int64 {
  33. return 0
  34. }
  35. func (fi *fakeFileInfo) Mode() os.FileMode {
  36. return 777
  37. }
  38. func (fi *fakeFileInfo) ModTime() time.Time {
  39. return time.Now()
  40. }
  41. func (fi *fakeFileInfo) IsDir() bool {
  42. return false
  43. }
  44. func (fi *fakeFileInfo) Sys() interface{} {
  45. return nil
  46. }
  47. var (
  48. lun = 1
  49. lunStr = "1"
  50. diskPath = "4:0:0:" + lunStr
  51. devName = "sdd"
  52. lunStr1 = "2"
  53. diskPath1 = "3:0:0:" + lunStr1
  54. devName1 = "sde"
  55. )
  56. type fakeIOHandler struct{}
  57. func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
  58. switch dirname {
  59. case "/sys/bus/scsi/devices":
  60. f1 := &fakeFileInfo{
  61. name: "3:0:0:1",
  62. }
  63. f2 := &fakeFileInfo{
  64. name: "4:0:0:0",
  65. }
  66. f3 := &fakeFileInfo{
  67. name: diskPath,
  68. }
  69. f4 := &fakeFileInfo{
  70. name: "host1",
  71. }
  72. f5 := &fakeFileInfo{
  73. name: "target2:0:0",
  74. }
  75. return []os.FileInfo{f1, f2, f3, f4, f5}, nil
  76. case "/sys/bus/scsi/devices/" + diskPath + "/block":
  77. n := &fakeFileInfo{
  78. name: devName,
  79. }
  80. return []os.FileInfo{n}, nil
  81. case "/sys/bus/scsi/devices/" + diskPath1 + "/block":
  82. n := &fakeFileInfo{
  83. name: devName1,
  84. }
  85. return []os.FileInfo{n}, nil
  86. }
  87. return nil, fmt.Errorf("bad dir")
  88. }
  89. func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.FileMode) error {
  90. return nil
  91. }
  92. func (handler *fakeIOHandler) Readlink(name string) (string, error) {
  93. return "/dev/azure/disk/sda", nil
  94. }
  95. func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) {
  96. if strings.HasSuffix(filename, "vendor") {
  97. return []byte("Msft \n"), nil
  98. }
  99. if strings.HasSuffix(filename, "model") {
  100. return []byte("Virtual Disk \n"), nil
  101. }
  102. return nil, fmt.Errorf("unknown file")
  103. }
  104. func TestIoHandler(t *testing.T) {
  105. if runtime.GOOS != "windows" && runtime.GOOS != "linux" {
  106. t.Skipf("TestIoHandler not supported on GOOS=%s", runtime.GOOS)
  107. }
  108. disk, err := findDiskByLun(lun, &fakeIOHandler{}, exec.New())
  109. if runtime.GOOS == "windows" {
  110. if err != nil {
  111. t.Errorf("no data disk found: disk %v err %v", disk, err)
  112. }
  113. } else {
  114. // if no disk matches lun, exit
  115. if disk != "/dev/"+devName || err != nil {
  116. t.Errorf("no data disk found: disk %v err %v", disk, err)
  117. }
  118. }
  119. }
  120. func TestNormalizeStorageAccountType(t *testing.T) {
  121. tests := []struct {
  122. storageAccountType string
  123. expectedAccountType compute.DiskStorageAccountTypes
  124. expectError bool
  125. }{
  126. {
  127. storageAccountType: "",
  128. expectedAccountType: compute.StandardLRS,
  129. expectError: false,
  130. },
  131. {
  132. storageAccountType: "NOT_EXISTING",
  133. expectedAccountType: "",
  134. expectError: true,
  135. },
  136. {
  137. storageAccountType: "Standard_LRS",
  138. expectedAccountType: compute.StandardLRS,
  139. expectError: false,
  140. },
  141. {
  142. storageAccountType: "Premium_LRS",
  143. expectedAccountType: compute.PremiumLRS,
  144. expectError: false,
  145. },
  146. {
  147. storageAccountType: "StandardSSD_LRS",
  148. expectedAccountType: compute.StandardSSDLRS,
  149. expectError: false,
  150. },
  151. {
  152. storageAccountType: "UltraSSD_LRS",
  153. expectedAccountType: compute.UltraSSDLRS,
  154. expectError: false,
  155. },
  156. }
  157. for _, test := range tests {
  158. result, err := normalizeStorageAccountType(test.storageAccountType)
  159. assert.Equal(t, result, test.expectedAccountType)
  160. assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
  161. }
  162. }
  163. func TestGetDiskNum(t *testing.T) {
  164. tests := []struct {
  165. deviceInfo string
  166. expectedNum string
  167. expectError bool
  168. }{
  169. {
  170. deviceInfo: "/dev/disk0",
  171. expectedNum: "0",
  172. expectError: false,
  173. },
  174. {
  175. deviceInfo: "/dev/disk99",
  176. expectedNum: "99",
  177. expectError: false,
  178. },
  179. {
  180. deviceInfo: "",
  181. expectedNum: "",
  182. expectError: true,
  183. },
  184. {
  185. deviceInfo: "/dev/disk",
  186. expectedNum: "",
  187. expectError: true,
  188. },
  189. {
  190. deviceInfo: "999",
  191. expectedNum: "",
  192. expectError: true,
  193. },
  194. }
  195. for _, test := range tests {
  196. result, err := getDiskNum(test.deviceInfo)
  197. assert.Equal(t, result, test.expectedNum)
  198. assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
  199. }
  200. }