github.com/LINBIT/golinstor@v0.52.0/client/resource_test.go (about) 1 package client_test 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/LINBIT/golinstor/client" 13 "github.com/LINBIT/golinstor/devicelayerkind" 14 ) 15 16 func TestParse(t *testing.T) { 17 no := false 18 19 testcases := []struct { 20 response string 21 actual interface{} 22 expected interface{} 23 }{ 24 { 25 response: `{"name":"pvc-b5be6893-9892-4278-b2da-51a060fc4624","node_name":"demo1.linstor-days.at.linbit.com","props":{"StorPoolName":"thinpool"},"layer_object":{"children":[{"type":"STORAGE","storage":{"storage_volumes":[{"volume_number":0,"device_path":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":516096,"usable_size_kib":516096,"disk_state":"[]"}]}}],"type":"DRBD","drbd":{"drbd_resource_definition":{"peer_slots":7,"al_stripes":1,"al_stripe_size_kib":32,"port":7000,"transport_type":"IP","secret":"bNvYcSbPFPpbHZ9Gtq00","down":false},"node_id":0,"peer_slots":7,"al_stripes":1,"al_size":32,"drbd_volumes":[{"drbd_volume_definition":{"volume_number":0,"minor_number":1000},"device_path":"/dev/drbd1000","backing_device":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":512148,"usable_size_kib":512000}],"connections":{"demo2.linstor-days.at.linbit.com":{"connected":true,"message":"Connected"},"demo3.linstor-days.at.linbit.com":{"connected":false,"message":"Connecting"}},"promotion_score":10101,"may_promote":true}},"state":{"in_use":false},"uuid":"78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb","create_timestamp":1622636098831}`, 26 actual: &client.Resource{}, 27 expected: &client.Resource{ 28 Name: "pvc-b5be6893-9892-4278-b2da-51a060fc4624", 29 NodeName: "demo1.linstor-days.at.linbit.com", 30 Props: map[string]string{ 31 "StorPoolName": "thinpool", 32 }, 33 LayerObject: &client.ResourceLayer{ 34 Children: []client.ResourceLayer{ 35 { 36 Type: devicelayerkind.Storage, 37 Storage: &client.StorageResource{ 38 StorageVolumes: []client.StorageVolume{ 39 { 40 VolumeNumber: 0, 41 DevicePath: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 42 AllocatedSizeKib: 516096, 43 UsableSizeKib: 516096, 44 DiskState: "[]", 45 }, 46 }, 47 }, 48 }, 49 }, 50 Type: devicelayerkind.Drbd, 51 Drbd: &client.DrbdResource{ 52 DrbdResourceDefinition: client.DrbdResourceDefinitionLayer{ 53 PeerSlots: 7, 54 AlStripes: 1, 55 Port: 7000, 56 TransportType: "IP", 57 Secret: "bNvYcSbPFPpbHZ9Gtq00", 58 }, 59 DrbdVolumes: []client.DrbdVolume{ 60 { 61 DrbdVolumeDefinition: client.DrbdVolumeDefinition{ 62 ResourceNameSuffix: "", 63 VolumeNumber: 0, 64 MinorNumber: 1000, 65 }, 66 DevicePath: "/dev/drbd1000", 67 BackingDevice: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 68 MetaDisk: "", 69 AllocatedSizeKib: 512148, 70 UsableSizeKib: 512000, 71 DiskState: "", 72 ExtMetaStorPool: "", 73 }, 74 }, 75 Connections: map[string]client.DrbdConnection{ 76 "demo2.linstor-days.at.linbit.com": { 77 Connected: true, 78 Message: "Connected", 79 }, 80 "demo3.linstor-days.at.linbit.com": { 81 Connected: false, 82 Message: "Connecting", 83 }, 84 }, 85 PeerSlots: 7, 86 AlStripes: 1, 87 AlSize: 32, 88 PromotionScore: 10101, 89 MayPromote: true, 90 }, 91 }, 92 State: &client.ResourceState{ 93 InUse: &no, 94 }, 95 Uuid: "78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb", 96 CreateTimestamp: &client.TimeStampMs{Time: time.Unix(1622636098, 831_000_000)}, 97 }, 98 }, 99 { 100 response: `{"name":"pvc-b5be6893-9892-4278-b2da-51a060fc4624","node_name":"demo1.linstor-days.at.linbit.com","props":{"StorPoolName":"thinpool"},"layer_object":{"children":[{"type":"STORAGE","storage":{"storage_volumes":[{"volume_number":0,"device_path":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":516096,"usable_size_kib":516096,"disk_state":"[]"}]}}],"type":"DRBD","drbd":{"drbd_resource_definition":{"peer_slots":7,"al_stripes":1,"al_stripe_size_kib":32,"port":7000,"transport_type":"IP","secret":"bNvYcSbPFPpbHZ9Gtq00","down":false},"node_id":0,"peer_slots":7,"al_stripes":1,"al_size":32,"drbd_volumes":[{"drbd_volume_definition":{"volume_number":0,"minor_number":1000},"device_path":"/dev/drbd1000","backing_device":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":512148,"usable_size_kib":512000}],"connections":{"demo2.linstor-days.at.linbit.com":{"connected":true,"message":"Connected"},"demo3.linstor-days.at.linbit.com":{"connected":false,"message":"Connecting"}},"promotion_score":10101,"may_promote":true}},"uuid":"78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb","create_timestamp":1622636098831,"volumes":[{"volume_number":0,"storage_pool_name":"thinpool","provider_kind":"LVM_THIN","device_path":"/dev/drbd1000","allocated_size_kib":206,"state":{"disk_state":"UpToDate"},"layer_data_list":[{"type":"DRBD","data":{"drbd_volume_definition":{"volume_number":0,"minor_number":1000},"device_path":"/dev/drbd1000","backing_device":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":512148,"usable_size_kib":512000}},{"type":"STORAGE","data":{"volume_number":0,"device_path":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":516096,"usable_size_kib":516096,"disk_state":"[]"}}],"uuid":"03b8ffd6-dbef-4745-87a0-46b4f8459e1e"}]}`, 101 actual: &client.ResourceWithVolumes{}, 102 expected: &client.ResourceWithVolumes{ 103 Resource: client.Resource{ 104 Name: "pvc-b5be6893-9892-4278-b2da-51a060fc4624", 105 NodeName: "demo1.linstor-days.at.linbit.com", 106 Props: map[string]string{ 107 "StorPoolName": "thinpool", 108 }, 109 LayerObject: &client.ResourceLayer{ 110 Children: []client.ResourceLayer{ 111 { 112 Type: devicelayerkind.Storage, 113 Storage: &client.StorageResource{ 114 StorageVolumes: []client.StorageVolume{ 115 { 116 VolumeNumber: 0, 117 DevicePath: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 118 AllocatedSizeKib: 516096, 119 UsableSizeKib: 516096, 120 DiskState: "[]", 121 }, 122 }, 123 }, 124 }, 125 }, 126 Type: devicelayerkind.Drbd, 127 Drbd: &client.DrbdResource{ 128 DrbdResourceDefinition: client.DrbdResourceDefinitionLayer{ 129 PeerSlots: 7, 130 AlStripes: 1, 131 Port: 7000, 132 TransportType: "IP", 133 Secret: "bNvYcSbPFPpbHZ9Gtq00", 134 }, 135 DrbdVolumes: []client.DrbdVolume{ 136 { 137 DrbdVolumeDefinition: client.DrbdVolumeDefinition{ 138 ResourceNameSuffix: "", 139 VolumeNumber: 0, 140 MinorNumber: 1000, 141 }, 142 DevicePath: "/dev/drbd1000", 143 BackingDevice: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 144 MetaDisk: "", 145 AllocatedSizeKib: 512148, 146 UsableSizeKib: 512000, 147 DiskState: "", 148 ExtMetaStorPool: "", 149 }, 150 }, 151 Connections: map[string]client.DrbdConnection{ 152 "demo2.linstor-days.at.linbit.com": { 153 Connected: true, 154 Message: "Connected", 155 }, 156 "demo3.linstor-days.at.linbit.com": { 157 Connected: false, 158 Message: "Connecting", 159 }, 160 }, 161 PeerSlots: 7, 162 AlStripes: 1, 163 AlSize: 32, 164 PromotionScore: 10101, 165 MayPromote: true, 166 }, 167 }, 168 Uuid: "78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb", 169 }, 170 CreateTimestamp: &client.TimeStampMs{Time: time.Unix(1622636098, 831_000_000)}, 171 Volumes: []client.Volume{ 172 { 173 StoragePoolName: "thinpool", 174 ProviderKind: client.LVM_THIN, 175 DevicePath: "/dev/drbd1000", 176 AllocatedSizeKib: 206, 177 State: client.VolumeState{ 178 DiskState: "UpToDate", 179 }, 180 LayerDataList: []client.VolumeLayer{ 181 { 182 Type: devicelayerkind.Drbd, 183 Data: &client.DrbdVolume{ 184 DrbdVolumeDefinition: client.DrbdVolumeDefinition{ 185 MinorNumber: 1000, 186 }, 187 DevicePath: "/dev/drbd1000", 188 BackingDevice: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 189 AllocatedSizeKib: 512148, 190 UsableSizeKib: 512000, 191 }, 192 }, 193 { 194 Type: devicelayerkind.Storage, 195 Data: &client.StorageVolume{ 196 DevicePath: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 197 AllocatedSizeKib: 516096, 198 UsableSizeKib: 516096, 199 DiskState: "[]", 200 }, 201 }, 202 }, 203 Uuid: "03b8ffd6-dbef-4745-87a0-46b4f8459e1e", 204 }, 205 }, 206 }, 207 }, 208 { 209 response: `{"volume_number":0,"storage_pool_name":"thinpool","provider_kind":"LVM_THIN","device_path":"/dev/drbd1000","allocated_size_kib":206,"state":{"disk_state":"UpToDate"},"layer_data_list":[{"type":"DRBD","data":{"drbd_volume_definition":{"volume_number":0,"minor_number":1000},"device_path":"/dev/drbd1000","backing_device":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":512148,"usable_size_kib":512000}},{"type":"STORAGE","data":{"volume_number":0,"device_path":"/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000","allocated_size_kib":516096,"usable_size_kib":516096,"disk_state":"[]"}}],"uuid":"03b8ffd6-dbef-4745-87a0-46b4f8459e1e"}`, 210 actual: &client.Volume{}, 211 expected: &client.Volume{ 212 StoragePoolName: "thinpool", 213 ProviderKind: client.LVM_THIN, 214 DevicePath: "/dev/drbd1000", 215 AllocatedSizeKib: 206, 216 State: client.VolumeState{ 217 DiskState: "UpToDate", 218 }, 219 LayerDataList: []client.VolumeLayer{ 220 { 221 Type: devicelayerkind.Drbd, 222 Data: &client.DrbdVolume{ 223 DrbdVolumeDefinition: client.DrbdVolumeDefinition{ 224 MinorNumber: 1000, 225 }, 226 DevicePath: "/dev/drbd1000", 227 BackingDevice: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 228 AllocatedSizeKib: 512148, 229 UsableSizeKib: 512000, 230 }, 231 }, 232 { 233 Type: devicelayerkind.Storage, 234 Data: &client.StorageVolume{ 235 DevicePath: "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000", 236 AllocatedSizeKib: 516096, 237 UsableSizeKib: 516096, 238 DiskState: "[]", 239 }, 240 }, 241 }, 242 Uuid: "03b8ffd6-dbef-4745-87a0-46b4f8459e1e", 243 }, 244 }, 245 } 246 247 t.Parallel() 248 for i := range testcases { 249 tcase := &testcases[i] 250 t.Run(reflect.TypeOf(tcase.expected).Name(), func(t *testing.T) { 251 err := json.NewDecoder(strings.NewReader(tcase.response)).Decode(tcase.actual) 252 if !assert.NoError(t, err) { 253 t.FailNow() 254 } 255 256 assert.Equal(t, tcase.expected, tcase.actual) 257 }) 258 } 259 }