containers_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 compute
  15. import (
  16. "reflect"
  17. "testing"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. )
  20. func Test_parseContainerVolumeMount(t *testing.T) {
  21. index0 := 0
  22. tests := []struct {
  23. args string
  24. want *apis.ContainerVolumeMount
  25. wantErr bool
  26. }{
  27. {
  28. args: "readonly=true,mount_path=/data,disk_index=0,overlay=/abc:/bca",
  29. want: &apis.ContainerVolumeMount{
  30. ReadOnly: true,
  31. MountPath: "/data",
  32. Disk: &apis.ContainerVolumeMountDisk{
  33. Index: &index0,
  34. Overlay: &apis.ContainerVolumeMountDiskOverlay{
  35. LowerDir: []string{"/abc", "/bca"},
  36. },
  37. },
  38. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  39. },
  40. },
  41. {
  42. args: "readonly=true,mount_path=/data,disk_index=0,disk_subdir=data",
  43. want: &apis.ContainerVolumeMount{
  44. ReadOnly: true,
  45. MountPath: "/data",
  46. Disk: &apis.ContainerVolumeMountDisk{Index: &index0, SubDirectory: "data"},
  47. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  48. },
  49. },
  50. {
  51. args: "readonly=true,mount_path=/data,disk_index=0,disk_subdir=data,overlay_disk_image=true",
  52. want: &apis.ContainerVolumeMount{
  53. ReadOnly: true,
  54. MountPath: "/data",
  55. Disk: &apis.ContainerVolumeMountDisk{
  56. Index: &index0,
  57. SubDirectory: "data",
  58. Overlay: &apis.ContainerVolumeMountDiskOverlay{
  59. UseDiskImage: true,
  60. },
  61. },
  62. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  63. },
  64. },
  65. {
  66. args: "readonly=true,mount_path=/storage_size,disk_index=0,disk_ssf=storage_size",
  67. want: &apis.ContainerVolumeMount{
  68. ReadOnly: true,
  69. MountPath: "/storage_size",
  70. Disk: &apis.ContainerVolumeMountDisk{
  71. Index: &index0,
  72. StorageSizeFile: "storage_size",
  73. },
  74. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  75. },
  76. },
  77. {
  78. args: "disk_id=abc,mount_path=/data",
  79. wantErr: false,
  80. want: &apis.ContainerVolumeMount{
  81. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  82. Disk: &apis.ContainerVolumeMountDisk{
  83. Id: "abc",
  84. },
  85. MountPath: "/data",
  86. },
  87. },
  88. {
  89. args: "host_path=/hostpath/abc,mount_path=/data",
  90. want: &apis.ContainerVolumeMount{
  91. Type: apis.CONTAINER_VOLUME_MOUNT_TYPE_HOST_PATH,
  92. HostPath: &apis.ContainerVolumeMountHostPath{Path: "/hostpath/abc"},
  93. MountPath: "/data",
  94. },
  95. },
  96. {
  97. args: "read_only=True,mount_path=/test",
  98. want: &apis.ContainerVolumeMount{
  99. ReadOnly: true,
  100. MountPath: "/test",
  101. },
  102. },
  103. {
  104. args: "vm1,read_only=True,mount_path=/test",
  105. want: nil,
  106. wantErr: true,
  107. },
  108. {
  109. args: "read_only=True,mount_path=/test,disk_index=one",
  110. want: nil,
  111. wantErr: true,
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.args, func(t *testing.T) {
  116. got, err := parseContainerVolumeMount(tt.args)
  117. if (err != nil) != tt.wantErr {
  118. t.Errorf("parseContainerVolumeMount() error = %v, wantErr %v", err, tt.wantErr)
  119. return
  120. }
  121. if !reflect.DeepEqual(got, tt.want) {
  122. t.Errorf("parseContainerVolumeMount() got = %v, want %v", got, tt.want)
  123. }
  124. })
  125. }
  126. }