mirror of
https://github.com/fluxcd/flux2.git
synced 2026-05-06 10:04:44 +00:00
test: resolve relative resource references using in-memory build
Signed-off-by: rycli <cyril@ryc.li>
This commit is contained in:
parent
8b7adab83c
commit
0791525969
1 changed files with 65 additions and 0 deletions
|
|
@ -744,3 +744,68 @@ data:
|
||||||
t.Errorf("source directory was modified: (-got +want)%s", diff)
|
t.Errorf("source directory was modified: (-got +want)%s", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_inMemoryBackend_Generate_parentRef(t *testing.T) {
|
||||||
|
// tmpDir/
|
||||||
|
// configmap.yaml (referenced as ../../configmap.yaml)
|
||||||
|
// overlay/sub/kustomization.yaml
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
|
cmYAML := `apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: parent-cm
|
||||||
|
data:
|
||||||
|
key: value
|
||||||
|
`
|
||||||
|
if err := os.WriteFile(filepath.Join(tmpDir, "configmap.yaml"), []byte(cmYAML), 0o644); err != nil {
|
||||||
|
t.Fatalf("write: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
overlayDir := filepath.Join(tmpDir, "overlay", "sub")
|
||||||
|
if err := os.MkdirAll(overlayDir, 0o755); err != nil {
|
||||||
|
t.Fatalf("mkdir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kusYAML := `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
resources:
|
||||||
|
- ../../configmap.yaml
|
||||||
|
`
|
||||||
|
if err := os.WriteFile(filepath.Join(overlayDir, "kustomization.yaml"), []byte(kusYAML), 0o644); err != nil {
|
||||||
|
t.Fatalf("write: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ks := unstructured.Unstructured{Object: map[string]interface{}{
|
||||||
|
"apiVersion": "kustomize.toolkit.fluxcd.io/v1",
|
||||||
|
"kind": "Kustomization",
|
||||||
|
"metadata": map[string]interface{}{"name": "test", "namespace": "default"},
|
||||||
|
"spec": map[string]interface{}{
|
||||||
|
"targetNamespace": "parent-ns",
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
gen := kustomize.NewGenerator(overlayDir, ks)
|
||||||
|
|
||||||
|
backend := inMemoryBackend{}
|
||||||
|
fs, dir, _, err := backend.Generate(gen, overlayDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Generate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ../../configmap.yaml must resolve through the overlay
|
||||||
|
m, err := kustomize.Build(fs, dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("kustomize.Build failed (parent ref not resolved): %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := m.Resources()
|
||||||
|
if len(resources) != 1 {
|
||||||
|
t.Fatalf("expected 1 resource, got %d", len(resources))
|
||||||
|
}
|
||||||
|
if resources[0].GetName() != "parent-cm" {
|
||||||
|
t.Errorf("expected resource name parent-cm, got %s", resources[0].GetName())
|
||||||
|
}
|
||||||
|
if resources[0].GetNamespace() != "parent-ns" {
|
||||||
|
t.Errorf("expected namespace parent-ns, got %s", resources[0].GetNamespace())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue