Skip to content

Commit 8719185

Browse files
authored
Add additional tests for kpt pkg update (#1848)
* Test for manual update of the upstream section in Kptfile * Test for symlinks in upstream package during updates
1 parent 9d6733b commit 8719185

File tree

4 files changed

+150
-8
lines changed

4 files changed

+150
-8
lines changed

internal/testutil/setup_manager.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ type Content struct {
6565
Pkg *pkgbuilder.RootPkg
6666
Tag string
6767
Message string
68+
69+
// UpdateFunc is invoked after the repo has been updated with the new
70+
// content, but before it is committed. This allows for making changes
71+
// that isn't supported by the pkgbuilder (like creating symlinks).
72+
UpdateFunc func(path string) error
6873
}
6974

7075
// Init initializes test data
@@ -139,6 +144,7 @@ type GitDirectory interface {
139144
ReplaceData(data string) error
140145
Commit(message string) (string, error)
141146
Tag(tagName string) error
147+
CustomUpdate(updateFunc func(string) error) error
142148
}
143149

144150
func UpdateGitDir(t *testing.T, name string, gitDir GitDirectory, changes []Content, repos map[string]*TestGitRepo) error {
@@ -165,6 +171,13 @@ func UpdateGitDir(t *testing.T, name string, gitDir GitDirectory, changes []Cont
165171
return err
166172
}
167173

174+
if content.UpdateFunc != nil {
175+
err = gitDir.CustomUpdate(content.UpdateFunc)
176+
if !assert.NoError(t, err) {
177+
return err
178+
}
179+
}
180+
168181
sha, err := gitDir.Commit(content.Message)
169182
if !assert.NoError(t, err) {
170183
return err

internal/testutil/testutil.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ func (g *TestGitRepo) ReplaceData(data string) error {
319319
return replaceData(g.RepoDirectory, data)
320320
}
321321

322+
// CustomUpdate executes the provided update function and passes in the
323+
// path to the directory of the repository.
324+
func (g *TestGitRepo) CustomUpdate(f func(string) error) error {
325+
return f(g.RepoDirectory)
326+
}
327+
322328
// SetupTestGitRepo initializes a new git repository and populates it with data from a source
323329
func (g *TestGitRepo) SetupTestGitRepo(name string, data []Content, repos map[string]*TestGitRepo) error {
324330
defaultBranch := "main"
@@ -775,6 +781,12 @@ func (w *TestWorkspace) ReplaceData(data string) error {
775781
return replaceData(filepath.Join(w.WorkspaceDirectory, w.PackageDir), data)
776782
}
777783

784+
// CustomUpdate executes the provided update function and passes in the
785+
// path to the directory of the repository.
786+
func (w *TestWorkspace) CustomUpdate(f func(string) error) error {
787+
return f(w.WorkspaceDirectory)
788+
}
789+
778790
// Commit performs a git commit
779791
func (w *TestWorkspace) Commit(message string) (string, error) {
780792
return commit(w.WorkspaceDirectory, message)

internal/util/get/get_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,21 +1351,19 @@ func TestCommand_Run_symlinks(t *testing.T) {
13511351
WithKptfile().
13521352
WithResource(pkgbuilder.ConfigMapResource),
13531353
),
1354+
UpdateFunc: func(path string) error {
1355+
// Create symlink in the upstream repo.
1356+
return os.Symlink(filepath.Join(path, "subpkg"),
1357+
filepath.Join(path, "subpkg-sym"))
1358+
},
13541359
},
13551360
},
13561361
})
13571362
defer clean()
13581363
upstreamRepo := repos[testutil.Upstream]
13591364

1360-
// Create a symlink in the upstream repo
1361-
err := os.Symlink(filepath.Join(upstreamRepo.RepoDirectory, "subpkg"),
1362-
filepath.Join(upstreamRepo.RepoDirectory, "subpkg-sym"))
1363-
if !assert.NoError(t, err) {
1364-
t.FailNow()
1365-
}
1366-
13671365
destinationDir := filepath.Join(w.WorkspaceDirectory, upstreamRepo.RepoName)
1368-
err = Command{
1366+
err := Command{
13691367
Git: &kptfilev1alpha2.Git{
13701368
Repo: upstreamRepo.RepoDirectory,
13711369
Directory: "/",

internal/util/update/update_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,125 @@ func TestCommand_Run_failInvalidRef(t *testing.T) {
815815
}
816816
}
817817

818+
func TestCommand_Run_manualChange(t *testing.T) {
819+
g := &testutil.TestSetupManager{
820+
T: t,
821+
ReposChanges: map[string][]testutil.Content{
822+
testutil.Upstream: {
823+
{
824+
Pkg: pkgbuilder.NewRootPkg().
825+
WithResource(pkgbuilder.DeploymentResource),
826+
Branch: masterBranch,
827+
Tag: "v1",
828+
},
829+
{
830+
Pkg: pkgbuilder.NewRootPkg().
831+
WithResource(pkgbuilder.ConfigMapResource),
832+
Tag: "v2",
833+
},
834+
{
835+
Pkg: pkgbuilder.NewRootPkg().
836+
WithResource(pkgbuilder.SecretResource),
837+
Tag: "v3",
838+
},
839+
},
840+
},
841+
GetRef: "v1",
842+
LocalChanges: []testutil.Content{
843+
{
844+
Pkg: pkgbuilder.NewRootPkg().
845+
WithKptfile(
846+
pkgbuilder.NewKptfile().
847+
WithUpstreamRef(testutil.Upstream, "/", "v3", "resource-merge").
848+
WithUpstreamLockRef(testutil.Upstream, "/", "v1", 0),
849+
).
850+
WithResource(pkgbuilder.DeploymentResource),
851+
},
852+
},
853+
}
854+
defer g.Clean()
855+
if !g.Init() {
856+
return
857+
}
858+
859+
err := Command{
860+
Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()),
861+
}.Run(fake.CtxWithNilPrinter())
862+
if !assert.NoError(t, err) {
863+
t.FailNow()
864+
}
865+
866+
expLocal := pkgbuilder.NewRootPkg().
867+
WithKptfile(
868+
pkgbuilder.NewKptfile().
869+
WithUpstreamRef(testutil.Upstream, "/", "v3", "resource-merge").
870+
WithUpstreamLockRef(testutil.Upstream, "/", "v3", 2),
871+
).
872+
WithResource(pkgbuilder.SecretResource)
873+
expectedPath := expLocal.ExpandPkgWithName(t,
874+
g.LocalWorkspace.PackageDir, testutil.ToReposInfo(g.Repos))
875+
876+
testutil.KptfileAwarePkgEqual(t, expectedPath, g.LocalWorkspace.FullPackagePath())
877+
}
878+
879+
func TestCommand_Run_symlinks(t *testing.T) {
880+
g := &testutil.TestSetupManager{
881+
T: t,
882+
ReposChanges: map[string][]testutil.Content{
883+
testutil.Upstream: {
884+
{
885+
Branch: masterBranch,
886+
Pkg: pkgbuilder.NewRootPkg().
887+
WithResource(pkgbuilder.DeploymentResource),
888+
},
889+
{
890+
Pkg: pkgbuilder.NewRootPkg().
891+
WithResource(pkgbuilder.SecretResource).
892+
WithSubPackages(
893+
pkgbuilder.NewSubPkg("subpkg").
894+
WithKptfile().
895+
WithResource(pkgbuilder.ConfigMapResource),
896+
),
897+
UpdateFunc: func(path string) error {
898+
// Create symlink in the upstream repo.
899+
return os.Symlink(filepath.Join(path, "subpkg"),
900+
filepath.Join(path, "subpkg-sym"))
901+
},
902+
},
903+
},
904+
},
905+
GetRef: masterBranch,
906+
}
907+
defer g.Clean()
908+
if !g.Init() {
909+
return
910+
}
911+
upstreamRepo := g.Repos[testutil.Upstream]
912+
913+
err := Command{
914+
Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()),
915+
}.Run(fake.CtxWithNilPrinter())
916+
if !assert.NoError(t, err) {
917+
t.FailNow()
918+
}
919+
920+
expectedPkg := pkgbuilder.NewRootPkg().
921+
WithKptfile(
922+
pkgbuilder.NewKptfile().
923+
WithUpstreamRef(testutil.Upstream, "/", "master", "resource-merge").
924+
WithUpstreamLockRef(testutil.Upstream, "/", "master", 1),
925+
).
926+
WithResource(pkgbuilder.SecretResource).
927+
WithSubPackages(
928+
pkgbuilder.NewSubPkg("subpkg").
929+
WithKptfile().
930+
WithResource(pkgbuilder.ConfigMapResource),
931+
)
932+
expectedPath := expectedPkg.ExpandPkgWithName(t, upstreamRepo.RepoName, testutil.ToReposInfo(g.Repos))
933+
934+
testutil.KptfileAwarePkgEqual(t, expectedPath, g.LocalWorkspace.FullPackagePath())
935+
}
936+
818937
func TestCommand_Run_badStrategy(t *testing.T) {
819938
strategy := kptfilev1alpha2.UpdateStrategyType("foo")
820939

0 commit comments

Comments
 (0)