@@ -23,6 +23,8 @@ import (
2323 "bytes"
2424 "os"
2525 "path/filepath"
26+ "strconv"
27+ "strings"
2628 "testing"
2729 "text/template"
2830)
@@ -53,8 +55,8 @@ func TestBuildKustomization(t *testing.T) {
5355 assertFunc : "assertGoldenTemplateFile" ,
5456 },
5557 {
56- name : "build podinfo (on-disk )" ,
57- args : "build kustomization podinfo --path ./testdata/build-kustomization/podinfo --in-memory-build=false " ,
58+ name : "build podinfo (in-memory )" ,
59+ args : "build kustomization podinfo --path ./testdata/build-kustomization/podinfo --in-memory-build=true " ,
5860 resultFile : "./testdata/build-kustomization/podinfo-result.yaml" ,
5961 assertFunc : "assertGoldenTemplateFile" ,
6062 },
@@ -77,8 +79,8 @@ func TestBuildKustomization(t *testing.T) {
7779 assertFunc : "assertGoldenTemplateFile" ,
7880 },
7981 {
80- name : "build ignore (on-disk )" ,
81- args : "build kustomization podinfo --path ./testdata/build-kustomization/ignore --ignore-paths \" !configmap.yaml,!secret.yaml\" --in-memory-build=false " ,
82+ name : "build ignore (in-memory )" ,
83+ args : "build kustomization podinfo --path ./testdata/build-kustomization/ignore --ignore-paths \" !configmap.yaml,!secret.yaml\" --in-memory-build=true " ,
8284 resultFile : "./testdata/build-kustomization/podinfo-with-ignore-result.yaml" ,
8385 assertFunc : "assertGoldenTemplateFile" ,
8486 },
@@ -89,8 +91,8 @@ func TestBuildKustomization(t *testing.T) {
8991 assertFunc : "assertGoldenTemplateFile" ,
9092 },
9193 {
92- name : "build with recursive (on-disk )" ,
93- args : "build kustomization podinfo --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=false " ,
94+ name : "build with recursive (in-memory )" ,
95+ args : "build kustomization podinfo --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=true " ,
9496 resultFile : "./testdata/build-kustomization/podinfo-with-my-app-result.yaml" ,
9597 assertFunc : "assertGoldenTemplateFile" ,
9698 },
@@ -164,8 +166,8 @@ spec:
164166 assertFunc : "assertGoldenTemplateFile" ,
165167 },
166168 {
167- name : "build podinfo (on-disk )" ,
168- args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo --in-memory-build=false " ,
169+ name : "build podinfo (in-memory )" ,
170+ args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo --in-memory-build=true " ,
169171 resultFile : "./testdata/build-kustomization/podinfo-result.yaml" ,
170172 assertFunc : "assertGoldenTemplateFile" ,
171173 },
@@ -200,14 +202,14 @@ spec:
200202 assertFunc : "assertGoldenTemplateFile" ,
201203 },
202204 {
203- name : "build with recursive (on-disk )" ,
204- args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=false " ,
205+ name : "build with recursive (in-memory )" ,
206+ args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=true " ,
205207 resultFile : "./testdata/build-kustomization/podinfo-with-my-app-result.yaml" ,
206208 assertFunc : "assertGoldenTemplateFile" ,
207209 },
208210 {
209- name : "build with recursive in dry-run mode (on-disk )" ,
210- args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=false --dry-run" ,
211+ name : "build with recursive in dry-run mode (in-memory )" ,
212+ args : "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --in-memory-build=true --dry-run" ,
211213 resultFile : "./testdata/build-kustomization/podinfo-with-my-app-result.yaml" ,
212214 assertFunc : "assertGoldenTemplateFile" ,
213215 },
@@ -255,6 +257,104 @@ spec:
255257 }
256258}
257259
260+ func TestBuildKustomizationDefaultBuildsAbsolutePathOutsideCwd (t * testing.T ) {
261+ cwdDir , sourceDir , kustomizationFile := newOutsideCwdKustomization (t , "default" , "" )
262+
263+ restore := chdirForTest (t , cwdDir )
264+ defer restore ()
265+
266+ flag := buildKsCmd .Flags ().Lookup ("in-memory-build" )
267+ if flag == nil {
268+ t .Fatal ("missing in-memory-build flag" )
269+ }
270+ defaultValue , err := strconv .ParseBool (flag .DefValue )
271+ if err != nil {
272+ t .Fatal (err )
273+ }
274+ buildKsArgs .inMemoryBuild = defaultValue
275+
276+ output , err := executeCommand ("build kustomization app --path " + sourceDir +
277+ " --kustomization-file " + kustomizationFile +
278+ " --namespace default --dry-run" )
279+ if err != nil {
280+ t .Fatalf ("expected build to succeed with default backend, got: %v" , err )
281+ }
282+ if ! strings .Contains (output , "name: outside-cwd" ) {
283+ t .Fatalf ("expected rendered ConfigMap in output, got:\n %s" , output )
284+ }
285+ }
286+
287+ func chdirForTest (t * testing.T , dir string ) func () {
288+ t .Helper ()
289+ orig , err := os .Getwd ()
290+ if err != nil {
291+ t .Fatal (err )
292+ }
293+ if err := os .Chdir (dir ); err != nil {
294+ t .Fatal (err )
295+ }
296+ return func () {
297+ if err := os .Chdir (orig ); err != nil {
298+ t .Fatal (err )
299+ }
300+ }
301+ }
302+
303+ func newOutsideCwdKustomization (t * testing.T , namespace , targetNamespace string ) (string , string , string ) {
304+ t .Helper ()
305+ parentDir := t .TempDir ()
306+
307+ cwdDir := filepath .Join (parentDir , "cwd" )
308+ if err := os .MkdirAll (cwdDir , 0o755 ); err != nil {
309+ t .Fatal (err )
310+ }
311+
312+ sourceDir := filepath .Join (parentDir , "source" )
313+ if err := os .MkdirAll (sourceDir , 0o755 ); err != nil {
314+ t .Fatal (err )
315+ }
316+
317+ if err := os .WriteFile (filepath .Join (sourceDir , "kustomization.yaml" ), []byte (`apiVersion: kustomize.config.k8s.io/v1beta1
318+ kind: Kustomization
319+ resources:
320+ - configmap.yaml
321+ ` ), 0o644 ); err != nil {
322+ t .Fatal (err )
323+ }
324+
325+ if err := os .WriteFile (filepath .Join (sourceDir , "configmap.yaml" ), []byte (`apiVersion: v1
326+ kind: ConfigMap
327+ metadata:
328+ name: outside-cwd
329+ ` ), 0o644 ); err != nil {
330+ t .Fatal (err )
331+ }
332+
333+ targetNamespaceField := ""
334+ if targetNamespace != "" {
335+ targetNamespaceField = "\n targetNamespace: " + targetNamespace
336+ }
337+
338+ kustomizationFile := filepath .Join (parentDir , "app.yaml" )
339+ if err := os .WriteFile (kustomizationFile , []byte (`apiVersion: kustomize.toolkit.fluxcd.io/v1
340+ kind: Kustomization
341+ metadata:
342+ name: app
343+ namespace: ` + namespace + `
344+ spec:
345+ interval: 5m
346+ path: ./source
347+ prune: true` + targetNamespaceField + `
348+ sourceRef:
349+ kind: GitRepository
350+ name: app
351+ ` ), 0o644 ); err != nil {
352+ t .Fatal (err )
353+ }
354+
355+ return cwdDir , sourceDir , kustomizationFile
356+ }
357+
258358// TestBuildKustomizationPathNormalization verifies that absolute and complex
259359// paths are normalized to prevent path concatenation bugs (issue #5673).
260360// Without normalization, paths could be duplicated like: /path/test/path/test/file
@@ -278,8 +378,8 @@ func TestBuildKustomizationPathNormalization(t *testing.T) {
278378 assertFunc : "assertGoldenTemplateFile" ,
279379 },
280380 {
281- name : "build with absolute path (on-disk )" ,
282- args : "build kustomization podinfo --path " + absTestDataPath + " --in-memory-build=false " ,
381+ name : "build with absolute path (in-memory )" ,
382+ args : "build kustomization podinfo --path " + absTestDataPath + " --in-memory-build=true " ,
283383 resultFile : "./testdata/build-kustomization/podinfo-result.yaml" ,
284384 assertFunc : "assertGoldenTemplateFile" ,
285385 },
0 commit comments