-
Notifications
You must be signed in to change notification settings - Fork 259
added e2e tests for examples #4672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OisinJohnston2005
wants to merge
3
commits into
kptdev:main
Choose a base branch
from
Nordix:add-e2e-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| //go:build docker | ||
|
|
||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package e2e_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/kptdev/kpt/pkg/test/runner" | ||
| ) | ||
|
|
||
| // TestPackageExamples runs e2e tests against the package-examples directory. | ||
| // Test fixtures (expected output) are stored in e2e/testdata/package-examples/ | ||
| // while the actual packages live in the top-level package-examples/ directory. | ||
| // This keeps documentation examples clean of test-specific files. | ||
| // | ||
| // To generate or update expected output: | ||
| // | ||
| // KPT_E2E_UPDATE_EXPECTED=true go test --tags=docker --run=TestPackageExamples ./e2e/ | ||
| func TestPackageExamples(t *testing.T) { | ||
| testdataDir := filepath.Join(".", "testdata", "package-examples") | ||
| pkgExamplesDir := filepath.Join("..", "package-examples") | ||
| updateExpected := strings.ToLower(os.Getenv("KPT_E2E_UPDATE_EXPECTED")) == "true" | ||
|
|
||
| checkOrScaffoldTestdata(t, pkgExamplesDir, testdataDir, updateExpected) | ||
|
|
||
| cases, err := runner.ScanTestCases(testdataDir) | ||
| if err != nil { | ||
| t.Fatalf("failed to scan test cases: %s", err) | ||
| } | ||
|
|
||
| for _, c := range *cases { | ||
| c := c | ||
| name := filepath.Base(c.Path) | ||
| t.Run(name, func(t *testing.T) { | ||
| if !c.Config.Sequential { | ||
| t.Parallel() | ||
| } | ||
| runPackageExampleCase(t, pkgExamplesDir, name, c, updateExpected) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func runPackageExampleCase(t *testing.T, pkgExamplesDir, name string, c runner.TestCase, updateExpected bool) { | ||
| t.Helper() | ||
|
|
||
| pkgSrc := filepath.Join(pkgExamplesDir, name) | ||
| if _, err := os.Stat(filepath.Join(pkgSrc, "Kptfile")); err != nil { | ||
| t.Fatalf("package-example %q does not have a Kptfile: %v", name, err) | ||
| } | ||
|
|
||
| mergedPkg := prepareMergedPackage(t, pkgSrc, name, c.Path) | ||
|
|
||
| mergedCase := runner.TestCase{ | ||
| Path: mergedPkg, | ||
| Config: c.Config, | ||
| } | ||
|
|
||
| r, err := runner.NewRunner(t, mergedCase, c.Config.TestType) | ||
| if err != nil { | ||
| t.Fatalf("failed to create test runner: %v", err) | ||
| } | ||
| if r.Skip() { | ||
| t.Skip() | ||
| } | ||
| if err := r.Run(); err != nil { | ||
| t.Fatalf("failed when running test: %v", err) | ||
| } | ||
|
|
||
| if updateExpected { | ||
| copyUpdatedExpectedOutput(t, mergedPkg, c.Path) | ||
| } | ||
| } | ||
|
|
||
| // prepareMergedPackage creates a temporary directory containing the package | ||
| // content overlaid with test fixtures, and returns the path to the merged package. | ||
| func prepareMergedPackage(t *testing.T, pkgSrc, name, testFixturesDir string) string { | ||
| t.Helper() | ||
|
|
||
| tmpDir, err := os.MkdirTemp("", "kpt-pkg-examples-e2e-*") | ||
| if err != nil { | ||
| t.Fatalf("failed to create temp dir: %v", err) | ||
| } | ||
| t.Cleanup(func() { os.RemoveAll(tmpDir) }) | ||
|
|
||
| mergedPkg := filepath.Join(tmpDir, name) | ||
|
|
||
| // Copy the actual package content | ||
| if out, err := exec.Command("cp", "-r", pkgSrc, mergedPkg).CombinedOutput(); err != nil { | ||
| t.Fatalf("failed to copy package: %v\n%s", err, out) | ||
| } | ||
|
|
||
| // Copy the test fixtures (.expected, .krmignore) on top | ||
| entries, err := os.ReadDir(testFixturesDir) | ||
| if err != nil { | ||
| t.Fatalf("failed to read test fixtures dir: %v", err) | ||
| } | ||
| for _, entry := range entries { | ||
| src := filepath.Join(testFixturesDir, entry.Name()) | ||
| dst := filepath.Join(mergedPkg, entry.Name()) | ||
| if out, err := exec.Command("cp", "-r", src, dst).CombinedOutput(); err != nil { | ||
| t.Fatalf("failed to copy fixture %s: %v\n%s", entry.Name(), err, out) | ||
| } | ||
| } | ||
|
|
||
| return mergedPkg | ||
| } | ||
|
|
||
| // copyUpdatedExpectedOutput copies generated expected output back to the testdata directory. | ||
| func copyUpdatedExpectedOutput(t *testing.T, mergedPkg, testFixturesDir string) { | ||
| t.Helper() | ||
|
|
||
| generatedExpected := filepath.Join(mergedPkg, ".expected") | ||
| targetExpected := filepath.Join(testFixturesDir, ".expected") | ||
| if out, err := exec.Command("cp", "-r", generatedExpected+"/.", targetExpected).CombinedOutput(); err != nil { | ||
| t.Fatalf("failed to copy updated expected output: %v\n%s", err, out) | ||
| } | ||
| t.Logf("updated expected output for %s", filepath.Base(mergedPkg)) | ||
| } | ||
|
|
||
| // checkOrScaffoldTestdata verifies that every package-example directory has a | ||
| // corresponding entry in testdata. When updateExpected is false, missing entries | ||
| // cause the test to fail. When true, the minimum fixture structure is created | ||
| // so that ScanTestCases can pick it up and generate expected output. | ||
| func checkOrScaffoldTestdata(t *testing.T, pkgExamplesDir, testdataDir string, updateExpected bool) { | ||
| t.Helper() | ||
| examples, err := os.ReadDir(pkgExamplesDir) | ||
| if err != nil { | ||
| t.Fatalf("failed to read package-examples directory: %v", err) | ||
| } | ||
| for _, entry := range examples { | ||
| if !entry.IsDir() || strings.HasPrefix(entry.Name(), "_") { | ||
| continue | ||
| } | ||
| fixtureDir := filepath.Join(testdataDir, entry.Name()) | ||
| if _, err := os.Stat(fixtureDir); !os.IsNotExist(err) { | ||
| continue | ||
| } | ||
| if !updateExpected { | ||
| t.Errorf("package-example %q has no corresponding testdata in %s; "+ | ||
| "add a test fixture or run with KPT_E2E_UPDATE_EXPECTED=true to generate one", | ||
| entry.Name(), testdataDir) | ||
| continue | ||
| } | ||
| if err := os.MkdirAll(filepath.Join(fixtureDir, ".expected"), 0755); err != nil { | ||
| t.Fatalf("failed to scaffold testdata for %q: %v", entry.Name(), err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(fixtureDir, ".krmignore"), []byte(".expected\n"), 0644); err != nil { | ||
| t.Fatalf("failed to write .krmignore for %q: %v", entry.Name(), err) | ||
| } | ||
| t.Logf("scaffolded testdata for new example %q", entry.Name()) | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
e2e/testdata/package-examples/cert-manager-basic/.expected/config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| exitCode: 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.