testutil

package
v0.0.0-...-c7d747a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2023 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Upstream = "upstream"
	Local    = "local"
)
View Source
const (
	Dataset1            = "dataset1"
	Dataset2            = "dataset2"
	Dataset3            = "dataset3"
	Dataset4            = "dataset4" // Dataset4 is replica of Dataset2 with different setter values
	Dataset5            = "dataset5" // Dataset5 is replica of Dataset2 with additional non KRM files
	Dataset6            = "dataset6" // Dataset6 contains symlinks
	DatasetMerged       = "datasetmerged"
	DiffOutput          = "diff_output"
	UpdateMergeConflict = "updateMergeConflict"
)
View Source
const TmpDirPrefix = "test-kpt"

Variables

View Source
var AssertNoError = assertnow.NilError
View Source
var EmptyReposInfo = &ReposInfo{}
View Source
var KptfileSet = diffSet(kptfilev1.KptFileName)

Functions

func AddKptfileToWorkspace

func AddKptfileToWorkspace(t *testing.T, w *TestWorkspace, kf *kptfilev1.KptFile)

AddKptfileToWorkspace writes the provided Kptfile to the workspace and makes a commit.

func Chdir

func Chdir(t *testing.T, path string) func()

func ConfigureTestKptCache

func ConfigureTestKptCache(m *testing.M) int

ConfigureTestKptCache sets up a temporary directory for the kpt git cache, sets the env variable so it will be used for tests, and cleans up the directory afterwards.

func Diff

func Diff(sourceDir, destDir string, addMergeCommentsToSource bool) (sets.String, error)

Diff returns a list of files that differ between the source and destination.

Diff is guaranteed to return a non-empty set if any files differ, but this set is not guaranteed to contain all differing files.

func GetTestDataPath

func GetTestDataPath() (string, error)

func KptfileAwarePkgEqual

func KptfileAwarePkgEqual(t *testing.T, pkg1, pkg2 string, addMergeCommentsToSource bool) bool

KptfileAwarePkgEqual compares two packages (including any subpackages) and has special handling of Kptfiles to handle fields that contain values which cannot easily be specified in the golden package.

func PrintFile

func PrintFile(paths ...string) error

func PrintPackage

func PrintPackage(paths ...string) error

func SetupRepoAndWorkspace

func SetupRepoAndWorkspace(t *testing.T, content Content) (*TestGitRepo, *TestWorkspace, func())

SetupRepoAndWorkspace handles setting up a default repo to clone, and a workspace to clone into. returns a cleanup function to remove the git repo and workspace.

func SetupRepos

func SetupRepos(t *testing.T, repoContent map[string][]Content) (map[string]*TestGitRepo, func())

SetupRepos creates repos and returns a mapping from name to TestGitRepos. This only creates the first version of each repo as given by the first item in the repoContent slice.

func UpdateGitDir

func UpdateGitDir(t *testing.T, name string, gitDir GitDirectory, changes []Content, repos map[string]*TestGitRepo) error

func UpdateRepos

func UpdateRepos(t *testing.T, repos map[string]*TestGitRepo, repoContent map[string][]Content) error

UpdateRepos updates the existing repos with any additional Content items in the repoContent slice.

Types

type Content

type Content struct {
	CreateBranch bool
	Branch       string
	Data         string
	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
}

type GitDirectory

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

type ReposInfo

type ReposInfo struct {
	// contains filtered or unexported fields
}

func ToReposInfo

func ToReposInfo(repos map[string]*TestGitRepo) *ReposInfo

func (*ReposInfo) ResolveCommitIndex

func (ri *ReposInfo) ResolveCommitIndex(repoRef string, index int) (string, bool)

func (*ReposInfo) ResolveRepoRef

func (ri *ReposInfo) ResolveRepoRef(repoRef string) (string, bool)

type TestGitRepo

type TestGitRepo struct {
	T *testing.T

	// RepoDirectory is the temp directory of the git repo
	RepoDirectory string

	// DatasetDirectory is the directory of the testdata files
	DatasetDirectory string

	// RepoName is the name of the repository
	RepoName string

	// Commits keeps track of the commit shas for the changes
	// to the repo.
	Commits []string
}

TestGitRepo manages a local git repository for testing

func (*TestGitRepo) AssertEqual

func (g *TestGitRepo) AssertEqual(t *testing.T, sourceDir, destDir string, addMergeCommentsToSource bool) bool

AssertEqual verifies the contents of a source package matches the contents of the destination package it was fetched to. Excludes comparing the .git directory in the source package.

While the sourceDir can be the TestGitRepo, because tests change the TestGitRepo may have been changed after the destDir was copied, it is often better to explicitly use a set of golden files as the sourceDir rather than the original TestGitRepo that was copied.

func (*TestGitRepo) AssertKptfile

func (g *TestGitRepo) AssertKptfile(t *testing.T, cloned string, kpkg kptfilev1.KptFile) bool

AssertKptfile verifies the contents of the KptFile matches the provided value.

func (*TestGitRepo) CheckoutBranch

func (g *TestGitRepo) CheckoutBranch(branch string, create bool) error

CheckoutBranch checks out the git branch in the repo

func (*TestGitRepo) Commit

func (g *TestGitRepo) Commit(message string) (string, error)

Commit performs a git commit and returns the SHA for the newly created commit.

func (*TestGitRepo) CustomUpdate

func (g *TestGitRepo) CustomUpdate(f func(string) error) error

CustomUpdate executes the provided update function and passes in the path to the directory of the repository.

func (*TestGitRepo) DeleteBranch

func (g *TestGitRepo) DeleteBranch(branch string) error

DeleteBranch deletes the git branch in the repo

func (*TestGitRepo) GetCommit

func (g *TestGitRepo) GetCommit() (string, error)

func (*TestGitRepo) RemoveAll

func (g *TestGitRepo) RemoveAll() error

RemoveAll deletes the test git repo

func (*TestGitRepo) ReplaceData

func (g *TestGitRepo) ReplaceData(data string) error

ReplaceData replaces the data with a new source

func (*TestGitRepo) SetupTestGitRepo

func (g *TestGitRepo) SetupTestGitRepo(name string, data []Content, repos map[string]*TestGitRepo) error

SetupTestGitRepo initializes a new git repository and populates it with data from a source

func (*TestGitRepo) Tag

func (g *TestGitRepo) Tag(tagName string) error

Tag initializes tags the git repository

type TestSetupManager

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

	// ReposChanges are content for any repos.
	ReposChanges map[string][]Content

	LocalChanges []Content

	Repos map[string]*TestGitRepo

	LocalWorkspace *TestWorkspace
	// contains filtered or unexported fields
}

func (*TestSetupManager) AssertKptfile

func (g *TestSetupManager) AssertKptfile(name, commit, ref string, strategy kptfilev1.UpdateStrategyType) bool

func (*TestSetupManager) AssertLocalDataEquals

func (g *TestSetupManager) AssertLocalDataEquals(path string, addMergeCommentToSource bool) bool

func (*TestSetupManager) Clean

func (g *TestSetupManager) Clean()

func (*TestSetupManager) GetSubPkg

func (g *TestSetupManager) GetSubPkg(dest, repo, upstreamDir string)

GetSubPkg gets a subpkg specified by repo/upstreamDir to destination in the local workspace

func (*TestSetupManager) Init

func (g *TestSetupManager) Init() bool

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

type TestWorkspace

type TestWorkspace struct {
	WorkspaceDirectory string
	PackageDir         string
}

func SetupReposAndWorkspace

func SetupReposAndWorkspace(t *testing.T, reposContent map[string][]Content) (map[string]*TestGitRepo, *TestWorkspace, func())

SetupReposAndWorkspace handles setting up a set of repos as specified by the reposContent and a workspace to clone into. It returns a cleanup function that will remove the repos.

func SetupWorkspace

func SetupWorkspace(t *testing.T) (*TestWorkspace, func())

SetupWorkspace creates a local workspace which kpt packages can be cloned into. It returns a cleanup function that will remove the workspace.

func (*TestWorkspace) CheckoutBranch

func (w *TestWorkspace) CheckoutBranch(branch string, create bool) error

CheckoutBranch checks out the git branch in the repo

func (*TestWorkspace) Commit

func (w *TestWorkspace) Commit(message string) (string, error)

Commit performs a git commit

func (*TestWorkspace) CustomUpdate

func (w *TestWorkspace) CustomUpdate(f func(string) error) error

CustomUpdate executes the provided update function and passes in the path to the directory of the repository.

func (*TestWorkspace) FullPackagePath

func (w *TestWorkspace) FullPackagePath() string

FullPackagePath returns the full path to the roor package in the local workspace.

func (*TestWorkspace) RemoveAll

func (w *TestWorkspace) RemoveAll() error

func (*TestWorkspace) ReplaceData

func (w *TestWorkspace) ReplaceData(data string) error

ReplaceData replaces the data with a new source

func (*TestWorkspace) SetupTestWorkspace

func (w *TestWorkspace) SetupTestWorkspace() error

func (*TestWorkspace) Tag

func (w *TestWorkspace) Tag(tagName string) error

Tag initializes tags the git repository

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL