device_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 losetup
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. var (
  20. losetupOutput string = `
  21. NAME BACK-FILE
  22. /dev/loop0 /disks/2b917686-2ace-4a57-a4af-44ece2303dd2 (deleted)
  23. /dev/loop1 /disks/033d6bc0-4ce4-48c4-89d3-125077bcc28e
  24. `
  25. )
  26. func Test_parseDevices(t *testing.T) {
  27. type args struct {
  28. output string
  29. }
  30. tests := []struct {
  31. name string
  32. args args
  33. want *Devices
  34. wantErr bool
  35. }{
  36. {
  37. name: "normalOutput",
  38. args: args{losetupOutput},
  39. want: &Devices{
  40. []Device{
  41. {
  42. Name: "/dev/loop0",
  43. BackFile: "/disks/2b917686-2ace-4a57-a4af-44ece2303dd2 (deleted)",
  44. },
  45. {
  46. Name: "/dev/loop1",
  47. BackFile: "/disks/033d6bc0-4ce4-48c4-89d3-125077bcc28e",
  48. },
  49. },
  50. },
  51. wantErr: false,
  52. },
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. got, err := parseDevices(tt.args.output)
  57. if (err != nil) != tt.wantErr {
  58. t.Errorf("parseDevices() error = %v, wantErr %v", err, tt.wantErr)
  59. return
  60. }
  61. if !reflect.DeepEqual(got, tt.want) {
  62. t.Errorf("parseDevices() = %v, want %v", got, tt.want)
  63. }
  64. })
  65. }
  66. }