losetup_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. func Test_parseJsonOutput(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. content string
  23. want *Devices
  24. wantErr bool
  25. }{
  26. {
  27. name: "normal input",
  28. content: `{
  29. "loopdevices": [
  30. {
  31. "name": "/dev/loop1",
  32. "sizelimit": 0,
  33. "offset": 0,
  34. "autoclear": true,
  35. "ro": false,
  36. "back-file": "/opt/cloud/workspace/disks/recycle_bin/20240911160650/194767a9-556f-4072-8a20-7ab1086f22ff.1726070810",
  37. "dio": false,
  38. "log-sec": 512
  39. },{
  40. "name": "/dev/loop57",
  41. "sizelimit": 0,
  42. "offset": 0,
  43. "autoclear": false,
  44. "ro": false,
  45. "back-file": "/opt/cloud/workspace/disks/2c10325f-7109-4cf0-8a9f-4c3724920245 (deleted)",
  46. "dio": false,
  47. "log-sec": 512
  48. }]}`,
  49. want: &Devices{
  50. LoopDevs: []Device{
  51. {
  52. Name: "/dev/loop1",
  53. BackFile: "/opt/cloud/workspace/disks/recycle_bin/20240911160650/194767a9-556f-4072-8a20-7ab1086f22ff.1726070810",
  54. SizeLimit: false,
  55. ReadOnly: false,
  56. },
  57. {
  58. Name: "/dev/loop57",
  59. BackFile: "/opt/cloud/workspace/disks/2c10325f-7109-4cf0-8a9f-4c3724920245 (deleted)",
  60. SizeLimit: false,
  61. ReadOnly: false,
  62. },
  63. },
  64. },
  65. wantErr: false,
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. got, err := parseJsonOutput(tt.content)
  71. if (err != nil) != tt.wantErr {
  72. t.Errorf("parseJsonOutput() error = %v, wantErr %v", err, tt.wantErr)
  73. return
  74. }
  75. if !reflect.DeepEqual(got, tt.want) {
  76. t.Errorf("parseJsonOutput() got = %v, want %v", got, tt.want)
  77. }
  78. })
  79. }
  80. }