Documentation
¶
Overview ¶
Package odstasktest implements ODS Pipeline specific functionality to run Tekton tasks in a KinD cluster on top of package tektontaskrun.
odstasktest is intended to be used as a library for testing ODS Pipeline tasks using Go.
Example usage:
package test
import (
"log"
"os"
"path/filepath"
"testing"
ott "github.com/opendevstack/ods-pipeline/pkg/odstasktest"
ttr "github.com/opendevstack/ods-pipeline/pkg/tektontaskrun"
)
var (
namespaceConfig *ttr.NamespaceConfig
rootPath = "../.."
)
func TestMain(m *testing.M) {
cc, err := ttr.StartKinDCluster(
ttr.LoadImage(ttr.ImageBuildConfig{
Dockerfile: "build/images/Dockerfile.my-task",
ContextDir: rootPath,
}),
)
if err != nil {
log.Fatal("Could not start KinD cluster: ", err)
}
nc, cleanup, err := ttr.SetupTempNamespace(
cc,
ott.StartNexus(),
ott.InstallODSPipeline(),
ttr.InstallTaskFromPath(
filepath.Join(rootPath, "build/tasks/my-task.yaml"),
nil,
),
)
if err != nil {
log.Fatal("Could not setup temporary namespace: ", err)
}
defer cleanup()
namespaceConfig = nc
os.Exit(m.Run())
}
func TestMyTask(t *testing.T) {
if err := ttr.RunTask(
ttr.InNamespace(namespaceConfig.Name),
ttr.UsingTask("my-task"),
ttr.WithStringParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
}),
ott.WithGitSourceWorkspace(t, "../testdata/workspaces/go-sample-app"),
ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun) {
ott.AssertFilesExist(
t, config.WorkspaceConfigs["source"].Dir,
"docker/Dockerfile",
"docker/app",
)
}),
); err != nil {
t.Fatal(err)
}
}
// further tests here ...
Index ¶
- func AssertFileContent(t *testing.T, wsDir, filename, want string)
- func AssertFileContentContains(t *testing.T, wsDir, filename string, wantContains ...string)
- func AssertFilesExist(t *testing.T, wsDir string, wantFiles ...string)
- func GetSourceWorkspaceContext(t *testing.T, config *ttr.TaskRunConfig) (dir string, ctxt *pipelinectxt.ODSContext)
- func InitGitRepo(t *testing.T, namespace string) ttr.WorkspaceOpt
- func InstallODSPipeline(opts *InstallOptions) ttr.NamespaceOpt
- func StartBitbucket() ttr.NamespaceOpt
- func StartNexus() ttr.NamespaceOpt
- func StartSonarQube() ttr.NamespaceOpt
- func WithGitSourceWorkspace(t *testing.T, sourceDir, namespace string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt
- func WithSourceWorkspace(t *testing.T, sourceDir string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt
- type InstallOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertFileContent ¶
AssertFileContent checks that the file named by filename in the directory wsDir has the exact context specified by want.
func AssertFileContentContains ¶
AssertFileContentContains checks that the file named by filename in the directory wsDir contains all of wantContains.
func AssertFilesExist ¶
AssertFilesExist checks that all files named by wantFiles exist in wsDir. Any files that do not exist will report a test error.
func GetSourceWorkspaceContext ¶
func GetSourceWorkspaceContext(t *testing.T, config *ttr.TaskRunConfig) (dir string, ctxt *pipelinectxt.ODSContext)
GetSourceWorkspaceContext reads the ODS context from the source workspace.
func InitGitRepo ¶
func InitGitRepo(t *testing.T, namespace string) ttr.WorkspaceOpt
InitGitRepo initialises a Git repository inside the given workspace. The workspace will also be setup with an ODS context directory in .ods with the given namespace.
func InstallODSPipeline ¶
func InstallODSPipeline(opts *InstallOptions) ttr.NamespaceOpt
InstallODSPipeline installs the ODS Pipeline Helm chart in the namespace given in NamespaceConfig.
func StartBitbucket ¶
func StartBitbucket() ttr.NamespaceOpt
StartBitbucket starts a Bitbucket instance in a Docker container (named ods-test-bitbucket-server). If a container of the same name already exists, it will be reused unless -ods-restart-bitbucket is passed.
func StartNexus ¶
func StartNexus() ttr.NamespaceOpt
StartNexus starts a Nexus instance in a Docker container (named ods-test-nexus). If a container of the same name already exists, it will be reused unless -ods-restart-nexus is passed.
func StartSonarQube ¶
func StartSonarQube() ttr.NamespaceOpt
StartSonarQube starts a SonarQube instance in a Docker container (named ods-test-sonarqube). If a container of the same name already exists, it will be reused unless -ods-restart-sonarqube is passed.
func WithGitSourceWorkspace ¶
func WithGitSourceWorkspace(t *testing.T, sourceDir, namespace string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt
WithGitSourceWorkspace configures the task run with a workspace named "source", mapped to the directory sourced from sourceDir. The directory is initialised as a Git repository with an ODS context with the given namespace.
func WithSourceWorkspace ¶
func WithSourceWorkspace(t *testing.T, sourceDir string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt
WithSourceWorkspace configures the task run with a workspace named "source", mapped to the directory sourced from sourceDir.
Types ¶
type InstallOptions ¶
type InstallOptions struct {
// PrivateCert specifies if services should be accessed through TLS
// with a private certificate.
PrivateCert bool
}
InstallOptions configure the installation of ODS Pipeline.