Skip to content

Commit 437a1ea

Browse files
authored
direct: add test for redundant resources.yml lifecycle entries (#5267)
Adds a unit test that fails when `bundle/direct/dresources/resources.yml` has redundant lifecycle rules: a field listed twice in the same category of a resource, or a hand-written field that `resources.generated.yml` already produces. The hand-written and generated configs are merged at runtime by consulting both in series (`bundle/direct/bundle_plan.go`), so duplicates were silently tolerated. Also removes one existing bug the test surfaced: a duplicated block of `gateway_definition.*` and `ingestion_definition.ingest_from_uc_foreign_catalog` entries under `pipelines.recreate_on_changes`. This pull request and its description were written by Isaac.
1 parent 6327c33 commit 437a1ea

2 files changed

Lines changed: 57 additions & 12 deletions

File tree

bundle/direct/dresources/config_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,60 @@ func TestGetResourceConfig(t *testing.T) {
1515
assert.NotEmpty(t, GetResourceConfig("volumes").RecreateOnChanges)
1616
assert.Empty(t, GetResourceConfig("nonexistent").RecreateOnChanges)
1717
}
18+
19+
// categoryRules projects ResourceLifecycleConfig's five categories onto a
20+
// uniform [name, []FieldRule] shape so the redundancy check can iterate them.
21+
func categoryRules(c ResourceLifecycleConfig) []struct {
22+
name string
23+
rules []FieldRule
24+
} {
25+
backendAsFieldRules := make([]FieldRule, len(c.BackendDefaults))
26+
for i, r := range c.BackendDefaults {
27+
backendAsFieldRules[i] = FieldRule{Field: r.Field}
28+
}
29+
return []struct {
30+
name string
31+
rules []FieldRule
32+
}{
33+
{"ignore_remote_changes", c.IgnoreRemoteChanges},
34+
{"ignore_local_changes", c.IgnoreLocalChanges},
35+
{"recreate_on_changes", c.RecreateOnChanges},
36+
{"update_id_on_changes", c.UpdateIDOnChanges},
37+
{"backend_defaults", backendAsFieldRules},
38+
}
39+
}
40+
41+
// TestResourcesYMLNoRedundantRules guards against two redundancy classes in
42+
// resources.yml: duplicate field entries within the same category of a
43+
// resource, and entries that the autogenerated resources.generated.yml already
44+
// produces from the OpenAPI schema.
45+
func TestResourcesYMLNoRedundantRules(t *testing.T) {
46+
handWritten := MustLoadConfig()
47+
generated := MustLoadGeneratedConfig()
48+
49+
for resourceType, rc := range handWritten.Resources {
50+
genCats := categoryRules(generated.Resources[resourceType])
51+
genFields := make(map[string]map[string]bool, len(genCats))
52+
for _, c := range genCats {
53+
fields := make(map[string]bool, len(c.rules))
54+
for _, r := range c.rules {
55+
fields[r.Field.String()] = true
56+
}
57+
genFields[c.name] = fields
58+
}
59+
60+
for _, c := range categoryRules(rc) {
61+
seen := make(map[string]bool, len(c.rules))
62+
for _, r := range c.rules {
63+
field := r.Field.String()
64+
if seen[field] {
65+
t.Errorf("bundle/direct/dresources/resources.yml: %s.%s lists %q twice; remove the duplicate entry", resourceType, c.name, field)
66+
}
67+
seen[field] = true
68+
if genFields[c.name][field] {
69+
t.Errorf("bundle/direct/dresources/resources.yml: %s.%s entry %q is already produced by resources.generated.yml; remove it from resources.yml", resourceType, c.name, field)
70+
}
71+
}
72+
}
73+
}
74+
}

bundle/direct/dresources/resources.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,6 @@ resources:
117117
# https://github.com/databricks/terraform-provider-databricks/blob/4eba541abe1a9f50993ea7b9dd83874207e224a1/pipelines/resource_pipeline.go#L209
118118
- field: ingestion_definition.ingest_from_uc_foreign_catalog
119119
reason: immutable
120-
# https://github.com/databricks/terraform-provider-databricks/blob/4eba541abe1a9f50993ea7b9dd83874207e224a1/pipelines/resource_pipeline.go#L204
121-
- field: gateway_definition.connection_id
122-
reason: immutable
123-
- field: gateway_definition.connection_name
124-
reason: immutable
125-
- field: gateway_definition.gateway_storage_catalog
126-
reason: immutable
127-
- field: gateway_definition.gateway_storage_schema
128-
reason: immutable
129-
# https://github.com/databricks/terraform-provider-databricks/blob/4eba541abe1a9f50993ea7b9dd83874207e224a1/pipelines/resource_pipeline.go#L209
130-
- field: ingestion_definition.ingest_from_uc_foreign_catalog
131-
reason: immutable
132120

133121
ignore_remote_changes:
134122
# "id" is handled in a special way before any fields changed

0 commit comments

Comments
 (0)