gce_util_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2018 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 gcepd
  14. import "testing"
  15. func TestParseScsiSerial(t *testing.T) {
  16. cases := []struct {
  17. name string
  18. output string
  19. diskName string
  20. expectErr bool
  21. }{
  22. {
  23. name: "valid",
  24. output: "0Google PersistentDisk test-disk",
  25. diskName: "test-disk",
  26. },
  27. {
  28. name: "valid with newline",
  29. output: "0Google PersistentDisk test-disk\n",
  30. diskName: "test-disk",
  31. },
  32. {
  33. name: "invalid prefix",
  34. output: "00Google PersistentDisk test-disk",
  35. expectErr: true,
  36. },
  37. {
  38. name: "invalid suffix",
  39. output: "0Google PersistentDisk test-disk more",
  40. expectErr: true,
  41. },
  42. }
  43. for _, test := range cases {
  44. serial, err := parseScsiSerial(test.output)
  45. if err != nil && !test.expectErr {
  46. t.Errorf("test %v failed: %v", test.name, err)
  47. }
  48. if err == nil && test.expectErr {
  49. t.Errorf("test %q failed: got success", test.name)
  50. }
  51. if serial != test.diskName {
  52. t.Errorf("test %v failed: expected serial %q, got %q", test.name, test.diskName, serial)
  53. }
  54. }
  55. }