Skip to content

Commit

Permalink
Add additional tests for kpt pkg update (#1848)
Browse files Browse the repository at this point in the history
* Test for manual update of the upstream section in Kptfile

* Test for symlinks in upstream package during updates
  • Loading branch information
mortent committed May 4, 2021
1 parent 9d6733b commit 8719185
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 8 deletions.
13 changes: 13 additions & 0 deletions internal/testutil/setup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Content struct {
Pkg *pkgbuilder.RootPkg
Tag string
Message string

// UpdateFunc is invoked after the repo has been updated with the new
// content, but before it is committed. This allows for making changes
// that isn't supported by the pkgbuilder (like creating symlinks).
UpdateFunc func(path string) error
}

// Init initializes test data
Expand Down Expand Up @@ -139,6 +144,7 @@ type GitDirectory interface {
ReplaceData(data string) error
Commit(message string) (string, error)
Tag(tagName string) error
CustomUpdate(updateFunc func(string) error) error
}

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

if content.UpdateFunc != nil {
err = gitDir.CustomUpdate(content.UpdateFunc)
if !assert.NoError(t, err) {
return err
}
}

sha, err := gitDir.Commit(content.Message)
if !assert.NoError(t, err) {
return err
Expand Down
12 changes: 12 additions & 0 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ func (g *TestGitRepo) ReplaceData(data string) error {
return replaceData(g.RepoDirectory, data)
}

// CustomUpdate executes the provided update function and passes in the
// path to the directory of the repository.
func (g *TestGitRepo) CustomUpdate(f func(string) error) error {
return f(g.RepoDirectory)
}

// SetupTestGitRepo initializes a new git repository and populates it with data from a source
func (g *TestGitRepo) SetupTestGitRepo(name string, data []Content, repos map[string]*TestGitRepo) error {
defaultBranch := "main"
Expand Down Expand Up @@ -775,6 +781,12 @@ func (w *TestWorkspace) ReplaceData(data string) error {
return replaceData(filepath.Join(w.WorkspaceDirectory, w.PackageDir), data)
}

// CustomUpdate executes the provided update function and passes in the
// path to the directory of the repository.
func (w *TestWorkspace) CustomUpdate(f func(string) error) error {
return f(w.WorkspaceDirectory)
}

// Commit performs a git commit
func (w *TestWorkspace) Commit(message string) (string, error) {
return commit(w.WorkspaceDirectory, message)
Expand Down
14 changes: 6 additions & 8 deletions internal/util/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,21 +1351,19 @@ func TestCommand_Run_symlinks(t *testing.T) {
WithKptfile().
WithResource(pkgbuilder.ConfigMapResource),
),
UpdateFunc: func(path string) error {
// Create symlink in the upstream repo.
return os.Symlink(filepath.Join(path, "subpkg"),
filepath.Join(path, "subpkg-sym"))
},
},
},
})
defer clean()
upstreamRepo := repos[testutil.Upstream]

// Create a symlink in the upstream repo
err := os.Symlink(filepath.Join(upstreamRepo.RepoDirectory, "subpkg"),
filepath.Join(upstreamRepo.RepoDirectory, "subpkg-sym"))
if !assert.NoError(t, err) {
t.FailNow()
}

destinationDir := filepath.Join(w.WorkspaceDirectory, upstreamRepo.RepoName)
err = Command{
err := Command{
Git: &kptfilev1alpha2.Git{
Repo: upstreamRepo.RepoDirectory,
Directory: "/",
Expand Down
119 changes: 119 additions & 0 deletions internal/util/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,125 @@ func TestCommand_Run_failInvalidRef(t *testing.T) {
}
}

func TestCommand_Run_manualChange(t *testing.T) {
g := &testutil.TestSetupManager{
T: t,
ReposChanges: map[string][]testutil.Content{
testutil.Upstream: {
{
Pkg: pkgbuilder.NewRootPkg().
WithResource(pkgbuilder.DeploymentResource),
Branch: masterBranch,
Tag: "v1",
},
{
Pkg: pkgbuilder.NewRootPkg().
WithResource(pkgbuilder.ConfigMapResource),
Tag: "v2",
},
{
Pkg: pkgbuilder.NewRootPkg().
WithResource(pkgbuilder.SecretResource),
Tag: "v3",
},
},
},
GetRef: "v1",
LocalChanges: []testutil.Content{
{
Pkg: pkgbuilder.NewRootPkg().
WithKptfile(
pkgbuilder.NewKptfile().
WithUpstreamRef(testutil.Upstream, "/", "v3", "resource-merge").
WithUpstreamLockRef(testutil.Upstream, "/", "v1", 0),
).
WithResource(pkgbuilder.DeploymentResource),
},
},
}
defer g.Clean()
if !g.Init() {
return
}

err := Command{
Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()),
}.Run(fake.CtxWithNilPrinter())
if !assert.NoError(t, err) {
t.FailNow()
}

expLocal := pkgbuilder.NewRootPkg().
WithKptfile(
pkgbuilder.NewKptfile().
WithUpstreamRef(testutil.Upstream, "/", "v3", "resource-merge").
WithUpstreamLockRef(testutil.Upstream, "/", "v3", 2),
).
WithResource(pkgbuilder.SecretResource)
expectedPath := expLocal.ExpandPkgWithName(t,
g.LocalWorkspace.PackageDir, testutil.ToReposInfo(g.Repos))

testutil.KptfileAwarePkgEqual(t, expectedPath, g.LocalWorkspace.FullPackagePath())
}

func TestCommand_Run_symlinks(t *testing.T) {
g := &testutil.TestSetupManager{
T: t,
ReposChanges: map[string][]testutil.Content{
testutil.Upstream: {
{
Branch: masterBranch,
Pkg: pkgbuilder.NewRootPkg().
WithResource(pkgbuilder.DeploymentResource),
},
{
Pkg: pkgbuilder.NewRootPkg().
WithResource(pkgbuilder.SecretResource).
WithSubPackages(
pkgbuilder.NewSubPkg("subpkg").
WithKptfile().
WithResource(pkgbuilder.ConfigMapResource),
),
UpdateFunc: func(path string) error {
// Create symlink in the upstream repo.
return os.Symlink(filepath.Join(path, "subpkg"),
filepath.Join(path, "subpkg-sym"))
},
},
},
},
GetRef: masterBranch,
}
defer g.Clean()
if !g.Init() {
return
}
upstreamRepo := g.Repos[testutil.Upstream]

err := Command{
Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()),
}.Run(fake.CtxWithNilPrinter())
if !assert.NoError(t, err) {
t.FailNow()
}

expectedPkg := pkgbuilder.NewRootPkg().
WithKptfile(
pkgbuilder.NewKptfile().
WithUpstreamRef(testutil.Upstream, "/", "master", "resource-merge").
WithUpstreamLockRef(testutil.Upstream, "/", "master", 1),
).
WithResource(pkgbuilder.SecretResource).
WithSubPackages(
pkgbuilder.NewSubPkg("subpkg").
WithKptfile().
WithResource(pkgbuilder.ConfigMapResource),
)
expectedPath := expectedPkg.ExpandPkgWithName(t, upstreamRepo.RepoName, testutil.ToReposInfo(g.Repos))

testutil.KptfileAwarePkgEqual(t, expectedPath, g.LocalWorkspace.FullPackagePath())
}

func TestCommand_Run_badStrategy(t *testing.T) {
strategy := kptfilev1alpha2.UpdateStrategyType("foo")

Expand Down

0 comments on commit 8719185

Please sign in to comment.