Skip to content

Commit 7df72e9

Browse files
committed
enhance thirdparty resource interpreter testing framework
Signed-off-by: zhzhuang-zju <[email protected]>
1 parent 25cf101 commit 7df72e9

File tree

180 files changed

+5572
-4089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+5572
-4089
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Thirdparty Resource Interpreter
2+
3+
This directory contains third-party resource interpreters for Karmada. These interpreters define how Karmada should handle custom resources from various third-party applications and operators.
4+
5+
## Files
6+
7+
- `thirdparty.go` - Main implementation of the third-party resource interpreter
8+
- `thirdparty_test.go` - Test suite for validating resource interpreter customizations
9+
- `resourcecustomizations/` - Directory containing resource customization definitions organized by API version and kind
10+
11+
## Directory Structure
12+
13+
The resource customizations are organized in the following structure:
14+
15+
```
16+
resourcecustomizations/
17+
├── <group>/
18+
│ └── <version>/
19+
│ └── <kind>/
20+
│ ├── customizations.yaml # Resource interpreter customization rules
21+
│ └── testdata/ # Test input and expected output files
22+
│ ├── aggregatestatus-test.yaml # test case for AggregateStatus operation
23+
│ ├── interpretcomponent-test.yaml # test case for InterpretComponent operation
24+
│ ├── interprethealth-test.yaml # test case for InterpretHealth operation
25+
│ ├── ...
26+
```
27+
28+
## How to test
29+
30+
### Running Tests
31+
32+
To run all third-party resource interpreter tests:
33+
34+
```bash
35+
cd pkg/resourceinterpreter/default/thirdparty
36+
go test -v
37+
```
38+
39+
### Creating Test Cases
40+
41+
#### 1. Create Test Structure
42+
43+
For a new resource type, create the directory structure:
44+
45+
```bash
46+
mkdir -p resourcecustomizations/<group>/<version>/<kind>/testdata
47+
```
48+
49+
#### 2. Create Test Data Files
50+
51+
Test data files are divided by operation type. Each file contains different test cases for the corresponding operation. The naming convention for test data files is `<operation>-test.yaml`.
52+
53+
For example:
54+
```markdown
55+
aggregatestatus-test.yaml # test case for AggregateStatus operation
56+
interpretcomponent-test.yaml # test case for InterpretComponent operation
57+
interprethealth-test.yaml # test case for InterpretHealth operation
58+
```
59+
60+
#### 3. Add Test Cases
61+
62+
The test case structure is as follows:
63+
```go
64+
type IndividualTest struct {
65+
Name string `yaml:"name"` // the name of individual test
66+
Description string `yaml:"description,omitempty"` // the description of individual test
67+
DesiredObj *unstructured.Unstructured `yaml:"desiredObj,omitempty"` // the desired object
68+
ObservedObj *unstructured.Unstructured `yaml:"observedObj,omitempty"` // the observed object
69+
StatusItems []workv1alpha2.AggregatedStatusItem `yaml:"statusItems,omitempty"` // the status items of aggregated status
70+
InputReplicas int64 `yaml:"inputReplicas,omitempty"` // the input replicas for revise operation
71+
Operation string `yaml:"operation"` // the operation of resource interpreter
72+
Output map[string]interface{} `yaml:"output,omitempty"` // the expected output results
73+
}
74+
```
75+
76+
Where:
77+
- `Output` The output are key-value mapping where the key is the field name of the expected result and the value is the expected result. The keys in output for different operations correspond to the Name field of the results returned by the corresponding resource interpreter operation `RuleResult.Results`.
78+
79+
For example:
80+
```go
81+
func (h *healthInterpretationRule) Run(interpreter *declarative.ConfigurableInterpreter, args RuleArgs) *RuleResult {
82+
obj, err := args.getObjectOrError()
83+
if err != nil {
84+
return newRuleResultWithError(err)
85+
}
86+
healthy, enabled, err := interpreter.InterpretHealth(obj)
87+
if err != nil {
88+
return newRuleResultWithError(err)
89+
}
90+
if !enabled {
91+
return newRuleResultWithError(fmt.Errorf("rule is not enabled"))
92+
}
93+
return newRuleResult().add("healthy", healthy)
94+
}
95+
```
96+
97+
The output for operation `InterpretHealth` should contain the `healthy` key.
98+
99+
For more examples of test cases, refer to the existing test data files in this directory.
100+
101+
### Supported Operations
102+
103+
The test framework supports the following operations:
104+
105+
- `InterpretReplica` - Extract replica count from resource
106+
- `InterpretComponent` - Extract component information from resource
107+
- `ReviseReplica` - Modify replica count in resource
108+
- `InterpretStatus` - Extract status information
109+
- `InterpretHealth` - Determine resource health status
110+
- `InterpretDependency` - Extract resource dependencies
111+
- `AggregateStatus` - Aggregate status from multiple clusters
112+
- `Retain` - Retain the desired resource template.
113+
114+
> NOTE: Not all operations need to be implemented for every resource type. Implement only the operations relevant to your resource.
115+
116+
### Test Validation
117+
118+
The test framework validates:
119+
120+
1. **Lua Script Syntax** - Ensures all Lua scripts are syntactically correct
121+
2. **Execution Results** - Compares actual results with expected results
122+
123+
### Debugging Tests
124+
125+
To debug failing tests:
126+
127+
1. **Check Lua Script Syntax** - Ensure your Lua scripts are valid
128+
2. **Verify Test Data** - Confirm test input files are properly formatted
129+
3. **Review Expected Results** - Make sure expected results match the actual operation output
130+
4. **Use Verbose Output** - Run tests with `-v` flag for detailed output
131+
132+
### Best Practices
133+
134+
1. **Comprehensive Coverage** - Test all supported operations for your resource type
135+
2. **Edge Cases** - Include tests for edge cases and error conditions
136+
3. **Realistic Data** - Use realistic resource definitions in test data
137+
4. **Clear Naming** - Use descriptive names for test files and cases
138+
139+
For more information about resource interpreter customizations, see the [Karmada documentation](https://karmada.io/docs/userguide/globalview/customizing-resource-interpreter/).

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/apps.kruise.io/v1alpha1/AdvancedCronJob/customizations_tests.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# test case for aggregating status of AdvancedCronJob
2+
# case1. AdvancedCronJob with two status items
3+
4+
name: "AdvancedCronJob with two status items"
5+
description: "Test aggregating status of AdvancedCronJob with two status items"
6+
desiredObj:
7+
apiVersion: apps.kruise.io/v1alpha1
8+
kind: AdvancedCronJob
9+
metadata:
10+
labels:
11+
app: sample
12+
name: sample
13+
namespace: test-acj
14+
spec:
15+
schedule: "*/2 * * * *"
16+
template:
17+
broadcastJobTemplate:
18+
spec:
19+
template:
20+
metadata:
21+
labels:
22+
app: sample
23+
spec:
24+
restartPolicy: Never
25+
volumes:
26+
- name: configmap
27+
configMap:
28+
name: my-sample-config
29+
containers:
30+
- name: nginx
31+
image: nginx:alpine
32+
env:
33+
- name: logData
34+
valueFrom:
35+
configMapKeyRef:
36+
name: mysql-config
37+
key: log
38+
- name: lowerData
39+
valueFrom:
40+
configMapKeyRef:
41+
name: mysql-config
42+
key: lower
43+
completionPolicy:
44+
type: Never
45+
statusItems:
46+
- applied: true
47+
clusterName: member1
48+
health: Healthy
49+
status:
50+
active:
51+
- apiVersion: apps.kruise.io/v1alpha1
52+
kind: BroadcastJob
53+
name: sample-1681378080
54+
namespace: test-acj
55+
resourceVersion: "3636404"
56+
uid: f96013ef-2869-49d7-adf3-8f7231cc5e2a
57+
lastScheduleTime: "2023-04-13T09:30:00Z"
58+
type: BroadcastJob
59+
- applied: true
60+
clusterName: member3
61+
health: Healthy
62+
status:
63+
active:
64+
- apiVersion: apps.kruise.io/v1alpha1
65+
kind: BroadcastJob
66+
name: sample-1681378080
67+
namespace: test-acj
68+
resourceVersion: "3635081"
69+
uid: d1f3c194-d650-4cce-b23d-307a445bb92e
70+
lastScheduleTime: "2023-04-13T09:30:00Z"
71+
type: BroadcastJob
72+
operation: AggregateStatus
73+
output:

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/apps.kruise.io/v1alpha1/AdvancedCronJob/testdata/desired-acj-nginx.yaml

Lines changed: 0 additions & 38 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# test case for interpreting dependency of AdvancedCronJob
2+
# case1. AdvancedCronJob with configmap dependency
3+
4+
name: "AdvancedCronJob with configmap dependency"
5+
description: "Test interpreting dependency of AdvancedCronJob with configmap dependency"
6+
desiredObj:
7+
apiVersion: apps.kruise.io/v1alpha1
8+
kind: AdvancedCronJob
9+
metadata:
10+
labels:
11+
app: sample
12+
name: sample
13+
namespace: test-acj
14+
spec:
15+
schedule: "*/2 * * * *"
16+
template:
17+
broadcastJobTemplate:
18+
spec:
19+
template:
20+
metadata:
21+
labels:
22+
app: sample
23+
spec:
24+
restartPolicy: Never
25+
volumes:
26+
- name: configmap
27+
configMap:
28+
name: my-sample-config
29+
containers:
30+
- name: nginx
31+
image: nginx:alpine
32+
env:
33+
- name: logData
34+
valueFrom:
35+
configMapKeyRef:
36+
name: mysql-config
37+
key: log
38+
- name: lowerData
39+
valueFrom:
40+
configMapKeyRef:
41+
name: mysql-config
42+
key: lower
43+
completionPolicy:
44+
type: Never
45+
operation: InterpretDependency
46+
output:

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/apps.kruise.io/v1alpha1/AdvancedCronJob/testdata/observed-acj-nginx.yaml

Lines changed: 0 additions & 48 deletions
This file was deleted.

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/apps.kruise.io/v1alpha1/AdvancedCronJob/testdata/status-file.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)