aboutsummaryrefslogtreecommitdiff
path: root/weed/admin/handlers/maintenance_handlers_test.go
blob: fa5a365f141fd2cab108ffd809c2e2402dfcfef5 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package handlers

import (
	"net/url"
	"testing"

	"github.com/seaweedfs/seaweedfs/weed/admin/config"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks/balance"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks/base"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks/erasure_coding"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks/vacuum"
)

func TestParseTaskConfigFromForm_WithEmbeddedStruct(t *testing.T) {
	// Create a maintenance handlers instance for testing
	h := &MaintenanceHandlers{}

	// Test with balance config
	t.Run("Balance Config", func(t *testing.T) {
		// Simulate form data
		formData := url.Values{
			"enabled":                     {"on"},      // checkbox field
			"scan_interval_seconds_value": {"30"},      // interval field
			"scan_interval_seconds_unit":  {"minutes"}, // interval unit
			"max_concurrent":              {"2"},       // number field
			"imbalance_threshold":         {"0.15"},    // float field
			"min_server_count":            {"3"},       // number field
		}

		// Get schema
		schema := tasks.GetTaskConfigSchema("balance")
		if schema == nil {
			t.Fatal("Failed to get balance schema")
		}

		// Create config instance
		config := &balance.Config{}

		// Parse form data
		err := h.parseTaskConfigFromForm(formData, schema, config)
		if err != nil {
			t.Fatalf("Failed to parse form data: %v", err)
		}

		// Verify embedded struct fields were set correctly
		if !config.Enabled {
			t.Errorf("Expected Enabled=true, got %v", config.Enabled)
		}

		if config.ScanIntervalSeconds != 1800 { // 30 minutes * 60
			t.Errorf("Expected ScanIntervalSeconds=1800, got %v", config.ScanIntervalSeconds)
		}

		if config.MaxConcurrent != 2 {
			t.Errorf("Expected MaxConcurrent=2, got %v", config.MaxConcurrent)
		}

		// Verify balance-specific fields were set correctly
		if config.ImbalanceThreshold != 0.15 {
			t.Errorf("Expected ImbalanceThreshold=0.15, got %v", config.ImbalanceThreshold)
		}

		if config.MinServerCount != 3 {
			t.Errorf("Expected MinServerCount=3, got %v", config.MinServerCount)
		}
	})

	// Test with vacuum config
	t.Run("Vacuum Config", func(t *testing.T) {
		// Simulate form data
		formData := url.Values{
			// "enabled" field omitted to simulate unchecked checkbox
			"scan_interval_seconds_value":  {"4"},     // interval field
			"scan_interval_seconds_unit":   {"hours"}, // interval unit
			"max_concurrent":               {"3"},     // number field
			"garbage_threshold":            {"0.4"},   // float field
			"min_volume_age_seconds_value": {"2"},     // interval field
			"min_volume_age_seconds_unit":  {"days"},  // interval unit
			"min_interval_seconds_value":   {"1"},     // interval field
			"min_interval_seconds_unit":    {"days"},  // interval unit
		}

		// Get schema
		schema := tasks.GetTaskConfigSchema("vacuum")
		if schema == nil {
			t.Fatal("Failed to get vacuum schema")
		}

		// Create config instance
		config := &vacuum.Config{}

		// Parse form data
		err := h.parseTaskConfigFromForm(formData, schema, config)
		if err != nil {
			t.Fatalf("Failed to parse form data: %v", err)
		}

		// Verify embedded struct fields were set correctly
		if config.Enabled {
			t.Errorf("Expected Enabled=false, got %v", config.Enabled)
		}

		if config.ScanIntervalSeconds != 14400 { // 4 hours * 3600
			t.Errorf("Expected ScanIntervalSeconds=14400, got %v", config.ScanIntervalSeconds)
		}

		if config.MaxConcurrent != 3 {
			t.Errorf("Expected MaxConcurrent=3, got %v", config.MaxConcurrent)
		}

		// Verify vacuum-specific fields were set correctly
		if config.GarbageThreshold != 0.4 {
			t.Errorf("Expected GarbageThreshold=0.4, got %v", config.GarbageThreshold)
		}

		if config.MinVolumeAgeSeconds != 172800 { // 2 days * 86400
			t.Errorf("Expected MinVolumeAgeSeconds=172800, got %v", config.MinVolumeAgeSeconds)
		}

		if config.MinIntervalSeconds != 86400 { // 1 day * 86400
			t.Errorf("Expected MinIntervalSeconds=86400, got %v", config.MinIntervalSeconds)
		}
	})

	// Test with erasure coding config
	t.Run("Erasure Coding Config", func(t *testing.T) {
		// Simulate form data
		formData := url.Values{
			"enabled":                     {"on"},              // checkbox field
			"scan_interval_seconds_value": {"2"},               // interval field
			"scan_interval_seconds_unit":  {"hours"},           // interval unit
			"max_concurrent":              {"1"},               // number field
			"quiet_for_seconds_value":     {"10"},              // interval field
			"quiet_for_seconds_unit":      {"minutes"},         // interval unit
			"fullness_ratio":              {"0.85"},            // float field
			"collection_filter":           {"test_collection"}, // string field
			"min_size_mb":                 {"50"},              // number field
		}

		// Get schema
		schema := tasks.GetTaskConfigSchema("erasure_coding")
		if schema == nil {
			t.Fatal("Failed to get erasure_coding schema")
		}

		// Create config instance
		config := &erasure_coding.Config{}

		// Parse form data
		err := h.parseTaskConfigFromForm(formData, schema, config)
		if err != nil {
			t.Fatalf("Failed to parse form data: %v", err)
		}

		// Verify embedded struct fields were set correctly
		if !config.Enabled {
			t.Errorf("Expected Enabled=true, got %v", config.Enabled)
		}

		if config.ScanIntervalSeconds != 7200 { // 2 hours * 3600
			t.Errorf("Expected ScanIntervalSeconds=7200, got %v", config.ScanIntervalSeconds)
		}

		if config.MaxConcurrent != 1 {
			t.Errorf("Expected MaxConcurrent=1, got %v", config.MaxConcurrent)
		}

		// Verify erasure coding-specific fields were set correctly
		if config.QuietForSeconds != 600 { // 10 minutes * 60
			t.Errorf("Expected QuietForSeconds=600, got %v", config.QuietForSeconds)
		}

		if config.FullnessRatio != 0.85 {
			t.Errorf("Expected FullnessRatio=0.85, got %v", config.FullnessRatio)
		}

		if config.CollectionFilter != "test_collection" {
			t.Errorf("Expected CollectionFilter='test_collection', got %v", config.CollectionFilter)
		}

		if config.MinSizeMB != 50 {
			t.Errorf("Expected MinSizeMB=50, got %v", config.MinSizeMB)
		}
	})
}

func TestConfigurationValidation(t *testing.T) {
	// Test that config structs can be validated and converted to protobuf format
	taskTypes := []struct {
		name   string
		config interface{}
	}{
		{
			"balance",
			&balance.Config{
				BaseConfig: base.BaseConfig{
					Enabled:             true,
					ScanIntervalSeconds: 2400,
					MaxConcurrent:       3,
				},
				ImbalanceThreshold: 0.18,
				MinServerCount:     4,
			},
		},
		{
			"vacuum",
			&vacuum.Config{
				BaseConfig: base.BaseConfig{
					Enabled:             false,
					ScanIntervalSeconds: 7200,
					MaxConcurrent:       2,
				},
				GarbageThreshold:    0.35,
				MinVolumeAgeSeconds: 86400,
				MinIntervalSeconds:  604800,
			},
		},
		{
			"erasure_coding",
			&erasure_coding.Config{
				BaseConfig: base.BaseConfig{
					Enabled:             true,
					ScanIntervalSeconds: 3600,
					MaxConcurrent:       1,
				},
				QuietForSeconds:  900,
				FullnessRatio:    0.9,
				CollectionFilter: "important",
				MinSizeMB:        100,
			},
		},
	}

	for _, test := range taskTypes {
		t.Run(test.name, func(t *testing.T) {
			// Test that configs can be converted to protobuf TaskPolicy
			switch cfg := test.config.(type) {
			case *balance.Config:
				policy := cfg.ToTaskPolicy()
				if policy == nil {
					t.Fatal("ToTaskPolicy returned nil")
				}
				if policy.Enabled != cfg.Enabled {
					t.Errorf("Expected Enabled=%v, got %v", cfg.Enabled, policy.Enabled)
				}
				if policy.MaxConcurrent != int32(cfg.MaxConcurrent) {
					t.Errorf("Expected MaxConcurrent=%v, got %v", cfg.MaxConcurrent, policy.MaxConcurrent)
				}
			case *vacuum.Config:
				policy := cfg.ToTaskPolicy()
				if policy == nil {
					t.Fatal("ToTaskPolicy returned nil")
				}
				if policy.Enabled != cfg.Enabled {
					t.Errorf("Expected Enabled=%v, got %v", cfg.Enabled, policy.Enabled)
				}
				if policy.MaxConcurrent != int32(cfg.MaxConcurrent) {
					t.Errorf("Expected MaxConcurrent=%v, got %v", cfg.MaxConcurrent, policy.MaxConcurrent)
				}
			case *erasure_coding.Config:
				policy := cfg.ToTaskPolicy()
				if policy == nil {
					t.Fatal("ToTaskPolicy returned nil")
				}
				if policy.Enabled != cfg.Enabled {
					t.Errorf("Expected Enabled=%v, got %v", cfg.Enabled, policy.Enabled)
				}
				if policy.MaxConcurrent != int32(cfg.MaxConcurrent) {
					t.Errorf("Expected MaxConcurrent=%v, got %v", cfg.MaxConcurrent, policy.MaxConcurrent)
				}
			default:
				t.Fatalf("Unknown config type: %T", test.config)
			}

			// Test that configs can be validated
			switch cfg := test.config.(type) {
			case *balance.Config:
				if err := cfg.Validate(); err != nil {
					t.Errorf("Validation failed: %v", err)
				}
			case *vacuum.Config:
				if err := cfg.Validate(); err != nil {
					t.Errorf("Validation failed: %v", err)
				}
			case *erasure_coding.Config:
				if err := cfg.Validate(); err != nil {
					t.Errorf("Validation failed: %v", err)
				}
			}
		})
	}
}

func TestParseFieldFromForm_EdgeCases(t *testing.T) {
	h := &MaintenanceHandlers{}

	// Test checkbox parsing (boolean fields)
	t.Run("Checkbox Fields", func(t *testing.T) {
		tests := []struct {
			name          string
			formData      url.Values
			expectedValue bool
		}{
			{"Checked checkbox", url.Values{"test_field": {"on"}}, true},
			{"Unchecked checkbox", url.Values{}, false},
			{"Empty value checkbox", url.Values{"test_field": {""}}, true}, // Present but empty means checked
		}

		for _, test := range tests {
			t.Run(test.name, func(t *testing.T) {
				schema := &tasks.TaskConfigSchema{
					Schema: config.Schema{
						Fields: []*config.Field{
							{
								JSONName:  "test_field",
								Type:      config.FieldTypeBool,
								InputType: "checkbox",
							},
						},
					},
				}

				type TestConfig struct {
					TestField bool `json:"test_field"`
				}

				config := &TestConfig{}
				err := h.parseTaskConfigFromForm(test.formData, schema, config)
				if err != nil {
					t.Fatalf("parseTaskConfigFromForm failed: %v", err)
				}

				if config.TestField != test.expectedValue {
					t.Errorf("Expected %v, got %v", test.expectedValue, config.TestField)
				}
			})
		}
	})

	// Test interval parsing
	t.Run("Interval Fields", func(t *testing.T) {
		tests := []struct {
			name         string
			value        string
			unit         string
			expectedSecs int
		}{
			{"Minutes", "30", "minutes", 1800},
			{"Hours", "2", "hours", 7200},
			{"Days", "1", "days", 86400},
		}

		for _, test := range tests {
			t.Run(test.name, func(t *testing.T) {
				formData := url.Values{
					"test_field_value": {test.value},
					"test_field_unit":  {test.unit},
				}

				schema := &tasks.TaskConfigSchema{
					Schema: config.Schema{
						Fields: []*config.Field{
							{
								JSONName:  "test_field",
								Type:      config.FieldTypeInterval,
								InputType: "interval",
							},
						},
					},
				}

				type TestConfig struct {
					TestField int `json:"test_field"`
				}

				config := &TestConfig{}
				err := h.parseTaskConfigFromForm(formData, schema, config)
				if err != nil {
					t.Fatalf("parseTaskConfigFromForm failed: %v", err)
				}

				if config.TestField != test.expectedSecs {
					t.Errorf("Expected %d seconds, got %d", test.expectedSecs, config.TestField)
				}
			})
		}
	})
}