aboutsummaryrefslogtreecommitdiff
path: root/weed/admin/view/app/task_config_schema_test.go
blob: a4e2a8bc4fbb563fb189d334797ace7f75f3bb17 (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
package app

import (
	"testing"
)

// Test structs that mirror the actual configuration structure
type TestBaseConfigForTemplate struct {
	Enabled             bool `json:"enabled"`
	ScanIntervalSeconds int  `json:"scan_interval_seconds"`
	MaxConcurrent       int  `json:"max_concurrent"`
}

type TestTaskConfigForTemplate struct {
	TestBaseConfigForTemplate
	TaskSpecificField    float64 `json:"task_specific_field"`
	AnotherSpecificField string  `json:"another_specific_field"`
}

func TestGetTaskFieldValue_EmbeddedStructFields(t *testing.T) {
	config := &TestTaskConfigForTemplate{
		TestBaseConfigForTemplate: TestBaseConfigForTemplate{
			Enabled:             true,
			ScanIntervalSeconds: 2400,
			MaxConcurrent:       5,
		},
		TaskSpecificField:    0.18,
		AnotherSpecificField: "test_value",
	}

	// Test embedded struct fields
	tests := []struct {
		fieldName     string
		expectedValue interface{}
		description   string
	}{
		{"enabled", true, "BaseConfig boolean field"},
		{"scan_interval_seconds", 2400, "BaseConfig integer field"},
		{"max_concurrent", 5, "BaseConfig integer field"},
		{"task_specific_field", 0.18, "Task-specific float field"},
		{"another_specific_field", "test_value", "Task-specific string field"},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			result := getTaskFieldValue(config, test.fieldName)

			if result != test.expectedValue {
				t.Errorf("Field %s: expected %v (%T), got %v (%T)",
					test.fieldName, test.expectedValue, test.expectedValue, result, result)
			}
		})
	}
}

func TestGetTaskFieldValue_NonExistentField(t *testing.T) {
	config := &TestTaskConfigForTemplate{
		TestBaseConfigForTemplate: TestBaseConfigForTemplate{
			Enabled:             true,
			ScanIntervalSeconds: 1800,
			MaxConcurrent:       3,
		},
	}

	result := getTaskFieldValue(config, "non_existent_field")

	if result != nil {
		t.Errorf("Expected nil for non-existent field, got %v", result)
	}
}

func TestGetTaskFieldValue_NilConfig(t *testing.T) {
	var config *TestTaskConfigForTemplate = nil

	result := getTaskFieldValue(config, "enabled")

	if result != nil {
		t.Errorf("Expected nil for nil config, got %v", result)
	}
}

func TestGetTaskFieldValue_EmptyStruct(t *testing.T) {
	config := &TestTaskConfigForTemplate{}

	// Test that we can extract zero values
	tests := []struct {
		fieldName     string
		expectedValue interface{}
		description   string
	}{
		{"enabled", false, "Zero value boolean"},
		{"scan_interval_seconds", 0, "Zero value integer"},
		{"max_concurrent", 0, "Zero value integer"},
		{"task_specific_field", 0.0, "Zero value float"},
		{"another_specific_field", "", "Zero value string"},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			result := getTaskFieldValue(config, test.fieldName)

			if result != test.expectedValue {
				t.Errorf("Field %s: expected %v (%T), got %v (%T)",
					test.fieldName, test.expectedValue, test.expectedValue, result, result)
			}
		})
	}
}

func TestGetTaskFieldValue_NonStructConfig(t *testing.T) {
	var config interface{} = "not a struct"

	result := getTaskFieldValue(config, "enabled")

	if result != nil {
		t.Errorf("Expected nil for non-struct config, got %v", result)
	}
}

func TestGetTaskFieldValue_PointerToStruct(t *testing.T) {
	config := &TestTaskConfigForTemplate{
		TestBaseConfigForTemplate: TestBaseConfigForTemplate{
			Enabled:             false,
			ScanIntervalSeconds: 900,
			MaxConcurrent:       2,
		},
		TaskSpecificField: 0.35,
	}

	// Test that pointers are handled correctly
	enabledResult := getTaskFieldValue(config, "enabled")
	if enabledResult != false {
		t.Errorf("Expected false for enabled field, got %v", enabledResult)
	}

	intervalResult := getTaskFieldValue(config, "scan_interval_seconds")
	if intervalResult != 900 {
		t.Errorf("Expected 900 for scan_interval_seconds field, got %v", intervalResult)
	}
}

func TestGetTaskFieldValue_FieldsWithJSONOmitempty(t *testing.T) {
	// Test struct with omitempty tags
	type TestConfigWithOmitempty struct {
		TestBaseConfigForTemplate
		OptionalField string `json:"optional_field,omitempty"`
	}

	config := &TestConfigWithOmitempty{
		TestBaseConfigForTemplate: TestBaseConfigForTemplate{
			Enabled:             true,
			ScanIntervalSeconds: 1200,
			MaxConcurrent:       4,
		},
		OptionalField: "optional_value",
	}

	// Test that fields with omitempty are still found
	result := getTaskFieldValue(config, "optional_field")
	if result != "optional_value" {
		t.Errorf("Expected 'optional_value' for optional_field, got %v", result)
	}

	// Test embedded fields still work
	enabledResult := getTaskFieldValue(config, "enabled")
	if enabledResult != true {
		t.Errorf("Expected true for enabled field, got %v", enabledResult)
	}
}

func TestGetTaskFieldValue_DeepEmbedding(t *testing.T) {
	// Test with multiple levels of embedding
	type DeepBaseConfig struct {
		DeepField string `json:"deep_field"`
	}

	type MiddleConfig struct {
		DeepBaseConfig
		MiddleField int `json:"middle_field"`
	}

	type TopConfig struct {
		MiddleConfig
		TopField bool `json:"top_field"`
	}

	config := &TopConfig{
		MiddleConfig: MiddleConfig{
			DeepBaseConfig: DeepBaseConfig{
				DeepField: "deep_value",
			},
			MiddleField: 123,
		},
		TopField: true,
	}

	// Test that deeply embedded fields are found
	deepResult := getTaskFieldValue(config, "deep_field")
	if deepResult != "deep_value" {
		t.Errorf("Expected 'deep_value' for deep_field, got %v", deepResult)
	}

	middleResult := getTaskFieldValue(config, "middle_field")
	if middleResult != 123 {
		t.Errorf("Expected 123 for middle_field, got %v", middleResult)
	}

	topResult := getTaskFieldValue(config, "top_field")
	if topResult != true {
		t.Errorf("Expected true for top_field, got %v", topResult)
	}
}

// Benchmark to ensure performance is reasonable
func BenchmarkGetTaskFieldValue(b *testing.B) {
	config := &TestTaskConfigForTemplate{
		TestBaseConfigForTemplate: TestBaseConfigForTemplate{
			Enabled:             true,
			ScanIntervalSeconds: 1800,
			MaxConcurrent:       3,
		},
		TaskSpecificField:    0.25,
		AnotherSpecificField: "benchmark_test",
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Test both embedded and regular fields
		_ = getTaskFieldValue(config, "enabled")
		_ = getTaskFieldValue(config, "task_specific_field")
	}
}