Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Conversation

michaelsauter
Copy link
Member

@michaelsauter michaelsauter commented Jul 28, 2023

Provide Go package(s) allowing to develop tasks easily in external Git repositories. Eventually, the goal is to extract the build / package / deploy tasks from this repo and have them in separate Git repositories with independent lifecycle.

Package tektontaskrun provides ODS Pipeline independent functionality to run Tekton tasks in a KinD cluster. The code is mostly copied from test/tasks/common_test.go and pkg/tasktesting. Package odstasktest builds on top of tektontaskrun and provides ODS Pipeline specific functionality to run ODS Pipeline tasks (e.g. installing ODS Pipeline, starting Nexus, etc.).

As a side-effect, the new approach allows to just run the Go tests without requiring to prepare any state upfront. All dependencies will be taken care of by the Go packages. As an example, here is how an external repo can use this:

// main_test.go
import (
	ott "github.com/opendevstack/ods-pipeline/pkg/odstasktest"
	ttr "github.com/opendevstack/ods-pipeline/pkg/tektontaskrun"
)

var namespaceConfig *ttr.NamespaceConfig

func TestMain(m *testing.M) {
	cc, err := ttr.StartKinDCluster(
		ttr.LoadImage(ttr.ImageBuildConfig{
			Dockerfile: "build/images/Dockerfile.go-toolset",
			ContextDir: rootPath,
		}),
	)
	if err != nil {
		log.Fatal("Could not start KinD cluster: ", err)
	}
	nc, cleanup, err := ttr.SetupTempNamespace(
		cc,
		ott.InstallODSPipeline(),
		ttr.InstallTaskFromPath(
			filepath.Join(rootPath, "build/tasks/ods-pipeline-v1-go-build.yaml"),
			nil,
		),
	)
	if err != nil {
		log.Fatal("Could not setup temporary namespace: ", err)
	}
	defer cleanup()
	namespaceConfig = nc
	os.Exit(m.Run())
}
// go_build_test.go
import (
	ott "github.com/opendevstack/ods-pipeline/pkg/odstasktest"
	ttr "github.com/opendevstack/ods-pipeline/pkg/tektontaskrun"
)
func TestBuildGoTask(t *testing.T) {
	if err := ttr.RunTask(
                ttr.InNamespace(namespaceConfig.Name),
		ttr.UsingTask("ods-pipeline-v1-go-build"),
		ttr.WithStringParams(map[string]string{
			"go-os":       runtime.GOOS,
			"go-arch":     runtime.GOARCH,
			"cache-build": "false",
		}),
		withGoWorkspace(t, "go-sample-app"),
		ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun) {
			wd := config.WorkspaceConfigs["source"].Dir
			cmd := exec.Command(wd + "/docker/app")
			b, err := cmd.Output()
			if err != nil {
				t.Fatal(err)
			}
			goProverb := "Don't communicate by sharing memory, share memory by communicating."
			if string(b) != goProverb {
				t.Fatalf("Got: %+v, want: %+v.", string(b), goProverb)
			}

			ott.AssertFilesExist(t, wd,
				"docker/Dockerfile",
				"docker/app",
				filepath.Join(pipelinectxt.LintReportsPath, "report.txt"),
				filepath.Join(pipelinectxt.XUnitReportsPath, "report.xml"),
				filepath.Join(pipelinectxt.CodeCoveragesPath, "coverage.out"),
			)
		}),
	); err != nil {
		t.Fatal(err)
	}
}

...

This is still very much in progress.

For two examples using this PR, see https://github.com/BIX-Digital/ods-pipeline-go and https://github.com/BIX-Digital/ods-pipeline-sonar.

Closes #163

Tasks:

  • Updated design documents in docs/design directory or not applicable
  • Updated user-facing documentation in docs directory or not applicable
  • Ran tests (e.g. make test) or not applicable
  • Updated changelog or not applicable

@michaelsauter michaelsauter self-assigned this Jul 28, 2023
@michaelsauter michaelsauter changed the title WIP Refactor to ease development of tasks in external Git repositories Jul 28, 2023
@michaelsauter michaelsauter force-pushed the feature/pkg-test branch 15 times, most recently from c99cead to 3368333 Compare July 28, 2023 13:56
@michaelsauter michaelsauter force-pushed the feature/pkg-test branch 13 times, most recently from de9883d to dbbcfcc Compare August 11, 2023 09:06
@michaelsauter michaelsauter merged commit 7a8f50c into master Sep 29, 2023
@michaelsauter michaelsauter deleted the feature/pkg-test branch September 29, 2023 07:22
This was referenced Sep 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor test setup so that it can be used outside this repo
2 participants