File: resource_test.go

package info (click to toggle)
golang-github-linbit-golinstor 0.57.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 472 kB
  • sloc: makefile: 11
file content (259 lines) | stat: -rw-r--r-- 11,210 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package client_test

import (
	"encoding/json"
	"reflect"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"github.com/LINBIT/golinstor/client"
	"github.com/LINBIT/golinstor/devicelayerkind"
)

func TestParse(t *testing.T) {
	no := false

	testcases := []struct {
		response string
		actual   interface{}
		expected interface{}
	}{
		{
			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}`,
			actual:   &client.Resource{},
			expected: &client.Resource{
				Name:     "pvc-b5be6893-9892-4278-b2da-51a060fc4624",
				NodeName: "demo1.linstor-days.at.linbit.com",
				Props: map[string]string{
					"StorPoolName": "thinpool",
				},
				LayerObject: &client.ResourceLayer{
					Children: []client.ResourceLayer{
						{
							Type: devicelayerkind.Storage,
							Storage: &client.StorageResource{
								StorageVolumes: []client.StorageVolume{
									{
										VolumeNumber:     0,
										DevicePath:       "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
										AllocatedSizeKib: 516096,
										UsableSizeKib:    516096,
										DiskState:        "[]",
									},
								},
							},
						},
					},
					Type: devicelayerkind.Drbd,
					Drbd: &client.DrbdResource{
						DrbdResourceDefinition: client.DrbdResourceDefinitionLayer{
							PeerSlots:     7,
							AlStripes:     1,
							Port:          7000,
							TransportType: "IP",
							Secret:        "bNvYcSbPFPpbHZ9Gtq00",
						},
						DrbdVolumes: []client.DrbdVolume{
							{
								DrbdVolumeDefinition: client.DrbdVolumeDefinition{
									ResourceNameSuffix: "",
									VolumeNumber:       0,
									MinorNumber:        1000,
								},
								DevicePath:       "/dev/drbd1000",
								BackingDevice:    "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
								MetaDisk:         "",
								AllocatedSizeKib: 512148,
								UsableSizeKib:    512000,
								DiskState:        "",
								ExtMetaStorPool:  "",
							},
						},
						Connections: map[string]client.DrbdConnection{
							"demo2.linstor-days.at.linbit.com": {
								Connected: true,
								Message:   "Connected",
							},
							"demo3.linstor-days.at.linbit.com": {
								Connected: false,
								Message:   "Connecting",
							},
						},
						PeerSlots:      7,
						AlStripes:      1,
						AlSize:         32,
						PromotionScore: 10101,
						MayPromote:     true,
					},
				},
				State: &client.ResourceState{
					InUse: &no,
				},
				Uuid:            "78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb",
				CreateTimestamp: &client.TimeStampMs{Time: time.Unix(1622636098, 831_000_000)},
			},
		},
		{
			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"}]}`,
			actual:   &client.ResourceWithVolumes{},
			expected: &client.ResourceWithVolumes{
				Resource: client.Resource{
					Name:     "pvc-b5be6893-9892-4278-b2da-51a060fc4624",
					NodeName: "demo1.linstor-days.at.linbit.com",
					Props: map[string]string{
						"StorPoolName": "thinpool",
					},
					LayerObject: &client.ResourceLayer{
						Children: []client.ResourceLayer{
							{
								Type: devicelayerkind.Storage,
								Storage: &client.StorageResource{
									StorageVolumes: []client.StorageVolume{
										{
											VolumeNumber:     0,
											DevicePath:       "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
											AllocatedSizeKib: 516096,
											UsableSizeKib:    516096,
											DiskState:        "[]",
										},
									},
								},
							},
						},
						Type: devicelayerkind.Drbd,
						Drbd: &client.DrbdResource{
							DrbdResourceDefinition: client.DrbdResourceDefinitionLayer{
								PeerSlots:     7,
								AlStripes:     1,
								Port:          7000,
								TransportType: "IP",
								Secret:        "bNvYcSbPFPpbHZ9Gtq00",
							},
							DrbdVolumes: []client.DrbdVolume{
								{
									DrbdVolumeDefinition: client.DrbdVolumeDefinition{
										ResourceNameSuffix: "",
										VolumeNumber:       0,
										MinorNumber:        1000,
									},
									DevicePath:       "/dev/drbd1000",
									BackingDevice:    "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
									MetaDisk:         "",
									AllocatedSizeKib: 512148,
									UsableSizeKib:    512000,
									DiskState:        "",
									ExtMetaStorPool:  "",
								},
							},
							Connections: map[string]client.DrbdConnection{
								"demo2.linstor-days.at.linbit.com": {
									Connected: true,
									Message:   "Connected",
								},
								"demo3.linstor-days.at.linbit.com": {
									Connected: false,
									Message:   "Connecting",
								},
							},
							PeerSlots:      7,
							AlStripes:      1,
							AlSize:         32,
							PromotionScore: 10101,
							MayPromote:     true,
						},
					},
					Uuid: "78f0d7fe-2b4d-4d5b-afb4-e1b1450c70cb",
				},
				CreateTimestamp: &client.TimeStampMs{Time: time.Unix(1622636098, 831_000_000)},
				Volumes: []client.Volume{
					{
						StoragePoolName:  "thinpool",
						ProviderKind:     client.LVM_THIN,
						DevicePath:       "/dev/drbd1000",
						AllocatedSizeKib: 206,
						State: client.VolumeState{
							DiskState: "UpToDate",
						},
						LayerDataList: []client.VolumeLayer{
							{
								Type: devicelayerkind.Drbd,
								Data: &client.DrbdVolume{
									DrbdVolumeDefinition: client.DrbdVolumeDefinition{
										MinorNumber: 1000,
									},
									DevicePath:       "/dev/drbd1000",
									BackingDevice:    "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
									AllocatedSizeKib: 512148,
									UsableSizeKib:    512000,
								},
							},
							{
								Type: devicelayerkind.Storage,
								Data: &client.StorageVolume{
									DevicePath:       "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
									AllocatedSizeKib: 516096,
									UsableSizeKib:    516096,
									DiskState:        "[]",
								},
							},
						},
						Uuid: "03b8ffd6-dbef-4745-87a0-46b4f8459e1e",
					},
				},
			},
		},
		{
			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"}`,
			actual:   &client.Volume{},
			expected: &client.Volume{
				StoragePoolName:  "thinpool",
				ProviderKind:     client.LVM_THIN,
				DevicePath:       "/dev/drbd1000",
				AllocatedSizeKib: 206,
				State: client.VolumeState{
					DiskState: "UpToDate",
				},
				LayerDataList: []client.VolumeLayer{
					{
						Type: devicelayerkind.Drbd,
						Data: &client.DrbdVolume{
							DrbdVolumeDefinition: client.DrbdVolumeDefinition{
								MinorNumber: 1000,
							},
							DevicePath:       "/dev/drbd1000",
							BackingDevice:    "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
							AllocatedSizeKib: 512148,
							UsableSizeKib:    512000,
						},
					},
					{
						Type: devicelayerkind.Storage,
						Data: &client.StorageVolume{
							DevicePath:       "/dev/linstor_thinpool/pvc-b5be6893-9892-4278-b2da-51a060fc4624_00000",
							AllocatedSizeKib: 516096,
							UsableSizeKib:    516096,
							DiskState:        "[]",
						},
					},
				},
				Uuid: "03b8ffd6-dbef-4745-87a0-46b4f8459e1e",
			},
		},
	}

	t.Parallel()
	for i := range testcases {
		tcase := &testcases[i]
		t.Run(reflect.TypeOf(tcase.expected).Name(), func(t *testing.T) {
			err := json.NewDecoder(strings.NewReader(tcase.response)).Decode(tcase.actual)
			if !assert.NoError(t, err) {
				t.FailNow()
			}

			assert.Equal(t, tcase.expected, tcase.actual)
		})
	}
}