megactl_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package megactl
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. raiddrivers "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
  20. )
  21. func TestParseLineForStorcli(t *testing.T) {
  22. type testCase struct {
  23. name string
  24. adapter *StorcliAdaptor
  25. lines []string
  26. assertFunc func(t *testing.T, a *StorcliAdaptor)
  27. }
  28. cases := []testCase{
  29. {
  30. name: "Should complete",
  31. adapter: new(StorcliAdaptor),
  32. lines: []string{
  33. "Controller = 0",
  34. "Product Name = SAS3108",
  35. "Serial Number = 1234",
  36. "Bus Number = 3",
  37. "Device Number = 0",
  38. "Function Number = 0",
  39. },
  40. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  41. assert.Equal(t, true, a.isComplete(), a.String())
  42. assert.Equal(t, "SAS3108"+"1234", a.key())
  43. },
  44. },
  45. {
  46. name: "Should complete when no space beside '='",
  47. adapter: new(StorcliAdaptor),
  48. lines: []string{
  49. "Controller=0",
  50. "Product Name = SAS3108",
  51. "Serial Number=1234",
  52. "Bus Number = 3",
  53. "Device Number = 0",
  54. "Function Number = 0",
  55. },
  56. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  57. assert.Equal(t, true, a.isComplete(), a.String())
  58. assert.Equal(t, "SAS3108"+"1234", a.key())
  59. },
  60. },
  61. {
  62. name: "Should parse empty SN",
  63. adapter: new(StorcliAdaptor),
  64. lines: []string{
  65. "Controller = 0",
  66. "Product Name = SAS3108",
  67. "Serial Number =",
  68. "Bus Number = 3",
  69. "Device Number = 0",
  70. "Function Number = 0",
  71. },
  72. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  73. assert.Equal(t, true, a.isComplete(), a.String())
  74. assert.Equal(t, true, a.isSNEmpty, a.String())
  75. assert.Equal(t, "SAS3108", a.key())
  76. },
  77. },
  78. {
  79. name: "Should parse empty SN end with space",
  80. adapter: new(StorcliAdaptor),
  81. lines: []string{
  82. "Controller = 0",
  83. "Product Name = SAS3108",
  84. "Serial Number = ",
  85. "Bus Number = 3",
  86. "Device Number = 0",
  87. "Function Number = 0",
  88. },
  89. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  90. assert.Equal(t, true, a.isComplete(), a.String())
  91. assert.Equal(t, true, a.isSNEmpty, a.String())
  92. assert.Equal(t, "SAS3108", a.key())
  93. },
  94. },
  95. {
  96. name: "Should not complete when no product name",
  97. adapter: new(StorcliAdaptor),
  98. lines: []string{
  99. "Controller = 0",
  100. "Serial Number = 1234",
  101. "Bus Number = 3",
  102. "Device Number = 0",
  103. "Function Number = 0",
  104. },
  105. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  106. assert.Equal(t, false, a.isComplete(), a.String())
  107. },
  108. },
  109. {
  110. name: "Should not complete when no SN",
  111. adapter: new(StorcliAdaptor),
  112. lines: []string{
  113. "Controller = 0",
  114. "Product Name = SAS3108",
  115. "Bus Number = 3",
  116. "Device Number = 0",
  117. "Function Number = 0",
  118. },
  119. assertFunc: func(t *testing.T, a *StorcliAdaptor) {
  120. assert.Equal(t, false, a.isComplete(), a.String())
  121. },
  122. },
  123. }
  124. for _, c := range cases {
  125. t.Run(c.name, func(t *testing.T) {
  126. for _, l := range c.lines {
  127. parseLineForStorcli(c.adapter, l)
  128. }
  129. c.assertFunc(t, c.adapter)
  130. })
  131. }
  132. }
  133. func Test_parseStorcliLogicalVolumes(t *testing.T) {
  134. type args struct {
  135. adapter int
  136. lines []string
  137. }
  138. tests := []struct {
  139. name string
  140. args args
  141. want []*raiddrivers.RaidLogicalVolume
  142. wantErr bool
  143. }{
  144. {
  145. name: "normal parse",
  146. args: args{
  147. adapter: 0,
  148. lines: []string{
  149. "DG/VD TYPE State Access Consist Cache Cac sCC Size Name",
  150. "0/0 RAID10 Optl RW Yes RWBD - ON 3.270 TB sas_data_raid1",
  151. "1/1 RAID5 Optl RW Yes RWBD - ON 7.275 TB sata_data_raid",
  152. },
  153. },
  154. want: []*raiddrivers.RaidLogicalVolume{
  155. {
  156. Index: 0,
  157. Adapter: 0,
  158. },
  159. {
  160. Index: 1,
  161. Adapter: 0,
  162. },
  163. },
  164. wantErr: false,
  165. },
  166. }
  167. for _, tt := range tests {
  168. t.Run(tt.name, func(t *testing.T) {
  169. got, err := parseStorcliLogicalVolumes(tt.args.adapter, tt.args.lines)
  170. if (err != nil) != tt.wantErr {
  171. t.Errorf("parseStorcliLogicalVolumes() error = %v, wantErr %v", err, tt.wantErr)
  172. return
  173. }
  174. if !reflect.DeepEqual(got, tt.want) {
  175. t.Errorf("parseStorcliLogicalVolumes() = %v, want %v", got, tt.want)
  176. }
  177. })
  178. }
  179. }