diff --git a/api/krusty/remoteloader_test.go b/api/krusty/remoteloader_test.go index b0e456c8c8c..b644bed8c0e 100644 --- a/api/krusty/remoteloader_test.go +++ b/api/krusty/remoteloader_test.go @@ -28,6 +28,7 @@ func TestRemoteLoad_LocalProtocol(t *testing.T) { root string simple string noSuffix string + hash string multiBaseDev string withSubmodule string } @@ -36,9 +37,10 @@ func TestRemoteLoad_LocalProtocol(t *testing.T) { // root/ // simple.git/ - base with just a pod // nosuffix/ - same as simple.git/ without the .git suffix + // hash-xx/ - same as simple.git/ with random hash at xx // multibase.git/ - base with a dev overlay // with-submodule.git/ - includes `simple` as a submodule - // submodule/ - the submodule referencing `simple + // submodule/ - the submodule referencing `simple` createGitRepos := func(t *testing.T) testRepos { t.Helper() @@ -50,10 +52,16 @@ func TestRemoteLoad_LocalProtocol(t *testing.T) { } } root := t.TempDir() + + hashPath, err := os.MkdirTemp(root, "hash-") + require.NoError(t, err) + hashDir := filepath.Base(hashPath) + bash(fmt.Sprintf(` set -eux export ROOT="%s" +export HASH_DIR="%s" export GIT_AUTHOR_EMAIL=nobody@kustomize.io export GIT_AUTHOR_NAME=Nobody export GIT_COMMITTER_EMAIL=nobody@kustomize.io @@ -85,19 +93,27 @@ cp -r testdata/remoteload/multibase $ROOT/multibase.git git add . git commit -m "import" ) +cp -r testdata/remoteload/with-submodule $ROOT/with-submodule.git # see README +cp -r $ROOT/simple.git/. $ROOT/$HASH_DIR ( - mkdir $ROOT/with-submodule.git cd $ROOT/with-submodule.git git init --initial-branch=main - git submodule add $ROOT/simple.git submodule + git add . git commit -m "import" + git checkout -b relative-submodule + git submodule add ../$HASH_DIR submodule + git commit -m "relative submodule" + git checkout main + git submodule add $ROOT/simple.git submodule + git commit -m "submodule" ) -`, root)) +`, root, hashDir)) return testRepos{ root: root, // The strings below aren't currently used, and more serve as documentation. simple: "simple.git", noSuffix: "nosuffix", + hash: hashDir, multiBaseDev: "multibase.git", withSubmodule: "with-submodule.git", } @@ -183,6 +199,15 @@ resources: `, expected: simpleBuild, }, + { + name: "has relative submodule", + kustomization: ` +resources: +- file://$ROOT/with-submodule.git/submodule?ref=relative-submodule +`, + // TODO(annasong): Replace with simpleBuild once #5131 is fixed. + err: `failed to run '\S+/git submodule update --init --recursive'`, + }, { name: "has timeout", kustomization: ` @@ -273,7 +298,7 @@ resources: if test.err != "" { require.Error(t, err) - require.Contains(t, err.Error(), test.err) + require.Regexp(t, test.err, err.Error()) } else { require.NoError(t, err) checkYaml(t, m, strings.ReplaceAll(test.expected, "$ROOT", repos.root)) @@ -430,6 +455,10 @@ func createKustDir(t *testing.T, content string) (filesys.FileSystem, filesys.Co fSys := filesys.MakeFsOnDisk() tmpDir, err := filesys.NewTmpConfirmedDir() require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, fSys.RemoveAll(tmpDir.String())) + }) + require.NoError(t, fSys.WriteFile(filepath.Join(tmpDir.String(), "kustomization.yaml"), []byte(content))) return fSys, tmpDir } diff --git a/api/krusty/testdata/remoteload/with-submodule/README.md b/api/krusty/testdata/remoteload/with-submodule/README.md new file mode 100644 index 00000000000..d0d6022dea7 --- /dev/null +++ b/api/krusty/testdata/remoteload/with-submodule/README.md @@ -0,0 +1,10 @@ +# submodule + +This repo demonstrates kustomize's ability to download git repos +with submodules. The following branches contain +* main: submodule via absolute path +* relative-submodule: submodule via relative path + +For the submodule accessed via a relative path, we include a random hash in the +submodule name to avoid accessing an unintended directory in the case kustomize +contains loader bugs (issue #5131). \ No newline at end of file