Skip to content

Commit

Permalink
Move TestSetupManager into a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Dec 4, 2020
1 parent 0a78525 commit 9f85223
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 373 deletions.
6 changes: 3 additions & 3 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func TestKptGetSet(t *testing.T) {
// diff requires a kptfile exists
testutil.CopyKptfile(t, localDir, upstreamGit.RepoDirectory)

testutil.AssertEqual(t, upstreamGit,
testutil.AssertPkgEqual(t, upstreamGit,
filepath.Join(expected, test.subdir), localDir)
return
}

testutil.AssertEqual(t, upstreamGit,
testutil.AssertPkgEqual(t, upstreamGit,
filepath.Join(expected, test.subdir), localDir)

// Run Set
Expand All @@ -212,7 +212,7 @@ func TestKptGetSet(t *testing.T) {
testutil.Compare(t,
filepath.Join(expected, test.subdir, "Kptfile"),
filepath.Join(localDir, "Kptfile"))
testutil.AssertEqual(t, upstreamGit,
testutil.AssertPkgEqual(t, upstreamGit,
filepath.Join(expected, test.subdir),
localDir)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/cmdexport/orchestrators/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"

"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
"github.com/GoogleContainerTools/kpt/internal/util/testutil"
"github.com/stretchr/testify/assert"
)

func TestGenerateJenkinsStageStep(t *testing.T) {
Expand All @@ -39,7 +39,7 @@ fn run /app/resources \
--fn-path /app/function2.yaml`
script := step.Generate()

testutil.AssertEqual(t, script, strings.TrimLeft(expected, "\n"))
assert.Equal(t, script, strings.TrimLeft(expected, "\n"))
}

var jenkinsTestCases = []testCase{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmdexport/orchestrators/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"gotest.tools/assert"

"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
"github.com/GoogleContainerTools/kpt/internal/util/testutil"
"github.com/GoogleContainerTools/kpt/internal/testutil"
)

type testCase struct {
Expand Down
247 changes: 247 additions & 0 deletions internal/testutil/setup_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package testutil

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/kpt/internal/gitutil"
"github.com/GoogleContainerTools/kpt/internal/testutil/pkgbuilder"
"github.com/GoogleContainerTools/kpt/internal/util/get"
"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/copyutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

type TestSetupManager struct {
T *testing.T

// GetRef is the git ref to fetch
GetRef string

// GetSubDirectory is the repo subdirectory containing the package
GetSubDirectory string

// UpstreamInit are made before fetching the repo
UpstreamInit []Content

// UpstreamChanges are upstream content changes made after cloning the repo
UpstreamChanges []Content

LocalChanges []Content

UpstreamRepo *TestGitRepo

LocalWorkspace *TestWorkspace

cleanTestRepo func()
cacheDir string
targetDir string
}

type Content struct {
CreateBranch bool
Branch string
Data string
Pkg *pkgbuilder.Pkg
Tag string
Message string
}

// Init initializes test data
// - Setup a new upstream repo in a tmp directory
// - Set the initial upstream content to Dataset1
// - Setup a new cache location for git repos and update the environment variable
// - Setup fetch the upstream package to a local package
// - Verify the local package contains the upstream content
func (g *TestSetupManager) Init(dataset string) bool {
// Default optional values
if g.GetRef == "" {
g.GetRef = "master"
}
if g.GetSubDirectory == "" {
g.GetSubDirectory = "/"
}

// Configure the cache location for cloning repos
cacheDir, err := ioutil.TempDir("", "kpt-test-cache-repos-")
if !assert.NoError(g.T, err) {
return false
}
g.cacheDir = cacheDir
os.Setenv(gitutil.RepoCacheDirEnv, g.cacheDir)

// Setup a "remote" source repo, and a "local" destination repo
g.UpstreamRepo, g.LocalWorkspace, g.cleanTestRepo = SetupDefaultRepoAndWorkspace(g.T, dataset)
if g.GetSubDirectory == "/" {
g.targetDir = filepath.Base(g.UpstreamRepo.RepoName)
} else {
g.targetDir = filepath.Base(g.GetSubDirectory)
}
if !assert.NoError(g.T, os.Chdir(g.UpstreamRepo.RepoDirectory)) {
return false
}

if err := updateGitDir(g.T, g.UpstreamRepo, g.UpstreamInit); err != nil {
return false
}

// Fetch the source repo
if !assert.NoError(g.T, os.Chdir(g.LocalWorkspace.WorkspaceDirectory)) {
return false
}

if !assert.NoError(g.T, get.Command{
Destination: g.targetDir,
Git: kptfile.Git{
Repo: g.UpstreamRepo.RepoDirectory,
Ref: g.GetRef,
Directory: g.GetSubDirectory,
}}.Run()) {
return false
}
localGit := gitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory)
if !assert.NoError(g.T, localGit.Run("add", ".")) {
return false
}
if !assert.NoError(g.T, localGit.Run("commit", "-m", "add files")) {
return false
}

// Modify source repository state after fetching it
if err := updateGitDir(g.T, g.UpstreamRepo, g.UpstreamChanges); err != nil {
return false
}

// Verify the local package has the correct dataset
if same := g.AssertLocalDataEquals(filepath.Join(dataset, g.GetSubDirectory)); !same {
return same
}

if err := updateGitDir(g.T, g.LocalWorkspace, g.LocalChanges); err != nil {
return false
}

return true
}

type GitDirectory interface {
CheckoutBranch(branch string, create bool) error
ReplaceData(data string) error
Commit(message string) error
Tag(tagName string) error
}

func updateGitDir(t *testing.T, gitDir GitDirectory, changes []Content) error {
for _, content := range changes {
if content.Message == "" {
content.Message = "initializing data"
}
if len(content.Branch) > 0 {
err := gitDir.CheckoutBranch(content.Branch, content.CreateBranch)
if !assert.NoError(t, err) {
return err
}
}

var pkgData string
if content.Pkg != nil {
pkgData = pkgbuilder.ExpandPkg(t, content.Pkg)
} else {
pkgData = content.Data
}

err := gitDir.ReplaceData(pkgData)
if !assert.NoError(t, err) {
return err
}

err = gitDir.Commit(content.Message)
if !assert.NoError(t, err) {
return err
}
if len(content.Tag) > 0 {
err = gitDir.Tag(content.Tag)
if !assert.NoError(t, err) {
return err
}
}
}
return nil
}

func (g *TestSetupManager) AssertKptfile(name, commit, ref string) bool {
expectedKptfile := kptfile.KptFile{
ResourceMeta: yaml.ResourceMeta{
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{
Name: name,
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfile.TypeMeta.APIVersion,
Kind: kptfile.TypeMeta.Kind},
},
PackageMeta: kptfile.PackageMeta{},
Upstream: kptfile.Upstream{
Type: "git",
Git: kptfile.Git{
Directory: g.GetSubDirectory,
Repo: g.UpstreamRepo.RepoDirectory,
Ref: ref,
Commit: commit,
},
},
}

return g.UpstreamRepo.AssertKptfile(
g.T, filepath.Join(g.LocalWorkspace.WorkspaceDirectory, g.targetDir), expectedKptfile)
}

func (g *TestSetupManager) AssertKptfileEquals(path, commit, ref string) bool {
kf, err := kptfileutil.ReadFile(path)
if !assert.NoError(g.T, err) {
g.T.FailNow()
}
kf.Upstream.Type = kptfile.GitOrigin
kf.Upstream.Git.Directory = g.GetSubDirectory
kf.Upstream.Git.Commit = commit
kf.Upstream.Git.Ref = ref
kf.Upstream.Git.Repo = g.UpstreamRepo.RepoDirectory
return g.UpstreamRepo.AssertKptfile(g.T, g.LocalWorkspace.FullPackagePath(), kf)
}

func (g *TestSetupManager) AssertLocalDataEquals(path string) bool {
var sourceDir string
if filepath.IsAbs(path) {
sourceDir = path
} else {
sourceDir = filepath.Join(g.UpstreamRepo.DatasetDirectory, path)
}
destDir := filepath.Join(g.LocalWorkspace.WorkspaceDirectory, g.targetDir)
return g.UpstreamRepo.AssertEqual(g.T, sourceDir, destDir)
}

func (g *TestSetupManager) SetLocalData(path string) bool {
if !assert.NoError(g.T, copyutil.CopyDir(
filepath.Join(g.UpstreamRepo.DatasetDirectory, path),
filepath.Join(g.LocalWorkspace.WorkspaceDirectory, g.UpstreamRepo.RepoName))) {
return false
}
localGit := gitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory)
if !assert.NoError(g.T, localGit.Run("add", ".")) {
return false
}
if !assert.NoError(g.T, localGit.Run("commit", "-m", "add files")) {
return false
}
return true
}

func (g *TestSetupManager) Clean() {
g.cleanTestRepo()
os.RemoveAll(g.cacheDir)
}
38 changes: 30 additions & 8 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ type TestGitRepo struct {

// RepoName is the name of the repository
RepoName string

Updater string
}

var AssertNoError = assertnow.NilError
Expand All @@ -88,16 +86,16 @@ func (g *TestGitRepo) AssertEqual(t *testing.T, sourceDir, destDir string) bool
return false
}
diff = diff.Difference(KptfileSet)
return assert.Empty(t, diff.List(), g.Updater)
return assert.Empty(t, diff.List())
}

func AssertEqual(t *testing.T, g *TestGitRepo, sourceDir, destDir string) {
func AssertPkgEqual(t *testing.T, g *TestGitRepo, sourceDir, destDir string) {
diff, err := Diff(sourceDir, destDir)
if !assert.NoError(t, err) {
t.FailNow()
}
diff = diff.Difference(KptfileSet)
if !assert.Empty(t, diff.List(), g.Updater) {
if !assert.Empty(t, diff.List()) {
t.FailNow()
}
}
Expand Down Expand Up @@ -243,16 +241,16 @@ func Compare(t *testing.T, a, b string) {
func (g *TestGitRepo) AssertKptfile(t *testing.T, cloned string, kpkg kptfile.KptFile) bool {
// read the actual generated KptFile
b, err := ioutil.ReadFile(filepath.Join(cloned, kptfile.KptFileName))
if !assert.NoError(t, err, g.Updater) {
if !assert.NoError(t, err) {
return false
}
actual := kptfile.KptFile{}
d := yaml.NewDecoder(bytes.NewBuffer(b))
d.KnownFields(true)
if !assert.NoError(t, d.Decode(&actual), g.Updater) {
if !assert.NoError(t, d.Decode(&actual)) {
return false
}
return assert.Equal(t, kpkg, actual, g.Updater)
return assert.Equal(t, kpkg, actual)
}

// CheckoutBranch checks out the git branch in the repo
Expand Down Expand Up @@ -708,3 +706,27 @@ func (w *TestWorkspace) Commit(message string) error {
func (w *TestWorkspace) Tag(tagName string) error {
return tag(w.WorkspaceDirectory, tagName)
}

func PrintPackage(paths ...string) error {
path := filepath.Join(paths...)
return filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.Contains(path, "/.git") {
return nil
}
fmt.Println(path)
return nil
})
}

func PrintFile(paths ...string) error {
path := filepath.Join(paths...)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
Loading

0 comments on commit 9f85223

Please sign in to comment.