Skip to content

Commit

Permalink
Add test for issue kubernetes-sigs#5131
Browse files Browse the repository at this point in the history
  • Loading branch information
annasong20 committed Jun 7, 2023
1 parent 0add0f9 commit cfe8ead
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
39 changes: 34 additions & 5 deletions api/krusty/remoteloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestRemoteLoad_LocalProtocol(t *testing.T) {
root string
simple string
noSuffix string
hash string
multiBaseDev string
withSubmodule string
}
Expand All @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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",
}
Expand Down Expand Up @@ -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: `
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions api/krusty/testdata/remoteload/with-submodule/README.md
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit cfe8ead

Please sign in to comment.