Skip to content
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

Update Tester instances to trigger just one test #1898

Merged
merged 48 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8986712
Move installation/uninstallation in system tests
mrodm Jun 6, 2024
5e5d856
Define new methods in testrunner to run always setupRunner, teardownR…
mrodm Jun 6, 2024
0a31346
Update all the commands with the new functions
mrodm Jun 6, 2024
aeed50e
Use RunSuite for policy tests
mrodm Jun 6, 2024
7f8e651
Skip packages using terraform
mrodm Jun 6, 2024
d2cf652
Add debug message
mrodm Jun 6, 2024
123bd62
Skip running suite if there are no tests folders
mrodm Jun 6, 2024
d68820a
Test all packages
mrodm Jun 6, 2024
db25ff3
Rename interfaces
mrodm Jun 7, 2024
61b7ae9
More renames
mrodm Jun 7, 2024
5f43a99
More renames
mrodm Jun 7, 2024
c8ea6c9
Move system tests
mrodm Jun 7, 2024
b8387e9
Move policy tests
mrodm Jun 7, 2024
3e7840c
Migrate asset runner
mrodm Jun 7, 2024
57f5734
Migrate static runner
mrodm Jun 7, 2024
ac39c12
Migrate pipeline runner
mrodm Jun 7, 2024
d925464
Change visibility
mrodm Jun 7, 2024
552900b
Remove unused field
mrodm Jun 7, 2024
2a37def
Create new files for testers
mrodm Jun 7, 2024
6c69ef0
Remove unused var
mrodm Jun 7, 2024
9d1b3ae
Rename test file
mrodm Jun 7, 2024
d9267d4
Rename var
mrodm Jun 7, 2024
7246655
Remove unused function
mrodm Jun 7, 2024
cbe756f
Extract function to get data stream flag
mrodm Jun 10, 2024
8d6f30c
Remove chek fo the existence data stream flag
mrodm Jun 10, 2024
51621bc
Merge upstream/main onto move_tests_to_runners
mrodm Jun 10, 2024
1ab3391
Return tester structs from GetTests
mrodm Jun 10, 2024
b503b7e
Remove parameter from RunSuite
mrodm Jun 10, 2024
5dcec8c
Add TODO
mrodm Jun 10, 2024
43737ca
Move methods and variables related to state file to system runner packge
mrodm Jun 10, 2024
17ca37c
Create a Tester per each variant and config file (system tests)
mrodm Jun 10, 2024
5260264
Remove initRun from system runner
mrodm Jun 10, 2024
d0f9a7f
Create a Tester peach each case file (pipeline tests)
mrodm Jun 10, 2024
3004a3b
Create a Tester peach each case file (policy tests)
mrodm Jun 10, 2024
3d8d1ab
Update test name for elasticsearch logs (pipeline tests)
mrodm Jun 10, 2024
8340468
Show config name in agent logs errors
mrodm Jun 10, 2024
6149f94
Remove debug messages
mrodm Jun 10, 2024
70bc3e1
Remove unused function
mrodm Jun 10, 2024
4a2b149
Remove unused field
mrodm Jun 10, 2024
0a441f4
Move checks for stages into system runner
mrodm Jun 10, 2024
479dee7
Update system runner to use serviceState
mrodm Jun 10, 2024
6a1d4da
Move write service state out of tester struct
mrodm Jun 10, 2024
4a90a84
Rename
mrodm Jun 10, 2024
38f7775
Update tests failed in cisco_asa
mrodm Jun 10, 2024
b630a64
Remove TODO comments
mrodm Jun 11, 2024
cba0d21
Merge upstream/main into change_granularity_tests
mrodm Jun 12, 2024
1685e65
Update policies with service variant and config file name
mrodm Jun 12, 2024
f3fe77c
Ensure TearDownRunner is executed even with errors/signals
mrodm Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions internal/testrunner/runners/pipeline/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package pipeline
import (
"context"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -115,27 +116,51 @@ func (r *runner) GetTests(ctx context.Context) ([]testrunner.Tester, error) {
}
}

// TODO: Return a tester per each configuration file defined in the data stream.
var testers []testrunner.Tester
for _, t := range folders {
t, err := NewPipelineTester(PipelineTesterOptions{
TestFolder: t,
PackageRootPath: r.packageRootPath,
GenerateTestResult: r.generateTestResult,
WithCoverage: r.withCoverage,
CoverageType: r.coverageType,
DeferCleanup: r.deferCleanup,
Profile: r.profile,
API: r.esAPI,
})
for _, folder := range folders {
testCaseFiles, err := r.listTestCaseFiles(folder)
if err != nil {
return nil, fmt.Errorf("failed to create pipeline tester: %w", err)
return nil, fmt.Errorf("listing test case definitions failed: %w", err)
}

for _, caseFile := range testCaseFiles {
t, err := NewPipelineTester(PipelineTesterOptions{
TestFolder: folder,
PackageRootPath: r.packageRootPath,
GenerateTestResult: r.generateTestResult,
WithCoverage: r.withCoverage,
CoverageType: r.coverageType,
DeferCleanup: r.deferCleanup,
Profile: r.profile,
API: r.esAPI,
TestCaseFile: caseFile,
})
if err != nil {
return nil, fmt.Errorf("failed to create pipeline tester: %w", err)
}
testers = append(testers, t)
}
testers = append(testers, t)
}
return testers, nil
}

func (r *runner) Type() testrunner.TestType {
return TestType
}

func (r *runner) listTestCaseFiles(folder testrunner.TestFolder) ([]string, error) {
fis, err := os.ReadDir(folder.Path)
if err != nil {
return nil, fmt.Errorf("reading pipeline tests failed (path: %s): %w", folder.Path, err)
}

var files []string
for _, fi := range fis {
if strings.HasSuffix(fi.Name(), expectedTestResultSuffix) ||
strings.HasSuffix(fi.Name(), configTestSuffixYAML) {
continue
}
files = append(files, fi.Name())
}
return files, nil
}
67 changes: 24 additions & 43 deletions internal/testrunner/runners/pipeline/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type tester struct {
withCoverage bool
coverageType string

testCaseFile string

pipelines []ingest.Pipeline

runCompareResults bool
Expand All @@ -62,6 +64,7 @@ type PipelineTesterOptions struct {
GenerateTestResult bool
WithCoverage bool
CoverageType string
TestCaseFile string
}

func NewPipelineTester(options PipelineTesterOptions) (*tester, error) {
Expand All @@ -71,6 +74,7 @@ func NewPipelineTester(options PipelineTesterOptions) (*tester, error) {
esAPI: options.API,
deferCleanup: options.DeferCleanup,
testFolder: options.TestFolder,
testCaseFile: options.TestCaseFile,
generateTestResult: options.GenerateTestResult,
withCoverage: options.WithCoverage,
coverageType: options.CoverageType,
Expand All @@ -96,6 +100,11 @@ func NewPipelineTester(options PipelineTesterOptions) (*tester, error) {
r.runCompareResults = false
}
}

if r.esAPI == nil {
return nil, errors.New("missing Elasticsearch client")
}

return &r, nil
}

Expand Down Expand Up @@ -140,15 +149,6 @@ func (r *tester) TearDown(ctx context.Context) error {
}

func (r *tester) run(ctx context.Context) ([]testrunner.TestResult, error) {
testCaseFiles, err := r.listTestCaseFiles()
if err != nil {
return nil, fmt.Errorf("listing test case definitions failed: %w", err)
}

if r.esAPI == nil {
return nil, errors.New("missing Elasticsearch client")
}

dataStreamPath, found, err := packages.FindDataStreamRootForPath(r.testFolder.Path)
if err != nil {
return nil, fmt.Errorf("locating data_stream root failed: %w", err)
Expand All @@ -157,7 +157,7 @@ func (r *tester) run(ctx context.Context) ([]testrunner.TestResult, error) {
return nil, errors.New("data stream root not found")
}

startTime := time.Now()
startTesting := time.Now()
var entryPipeline string
entryPipeline, r.pipelines, err = ingest.InstallDataStreamPipelines(r.esAPI, dataStreamPath)
if err != nil {
Expand Down Expand Up @@ -198,23 +198,21 @@ func (r *tester) run(ctx context.Context) ([]testrunner.TestResult, error) {
}

results := make([]testrunner.TestResult, 0)
for _, testCaseFile := range testCaseFiles {
validatorOptions := []fields.ValidatorOption{
fields.WithSpecVersion(pkgManifest.SpecVersion),
// explicitly enabled for pipeline tests only
// since system tests can have dynamic public IPs
fields.WithEnabledAllowedIPCheck(),
fields.WithExpectedDatasets(expectedDatasets),
fields.WithEnabledImportAllECSSChema(true),
}
result, err := r.runTestCase(ctx, testCaseFile, dataStreamPath, dsManifest.Type, entryPipeline, validatorOptions)
if err != nil {
return nil, err
}
results = append(results, result)
validatorOptions := []fields.ValidatorOption{
fields.WithSpecVersion(pkgManifest.SpecVersion),
// explicitly enabled for pipeline tests only
// since system tests can have dynamic public IPs
fields.WithEnabledAllowedIPCheck(),
fields.WithExpectedDatasets(expectedDatasets),
fields.WithEnabledImportAllECSSChema(true),
}
result, err := r.runTestCase(ctx, r.testCaseFile, dataStreamPath, dsManifest.Type, entryPipeline, validatorOptions)
if err != nil {
return nil, err
}
results = append(results, result)

esLogs, err := r.checkElasticsearchLogs(ctx, startTime)
esLogs, err := r.checkElasticsearchLogs(ctx, startTesting)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -278,7 +276,7 @@ func (r *tester) checkElasticsearchLogs(ctx context.Context, startTesting time.T

tr := testrunner.TestResult{
TestType: TestType,
Name: "(ingest pipeline warnings)",
Name: fmt.Sprintf("(ingest pipeline warnings %s)", r.testCaseFile),
Package: r.testFolder.Package,
DataStream: r.testFolder.DataStream,
TimeElapsed: time.Since(startTime),
Expand Down Expand Up @@ -364,23 +362,6 @@ func (r *tester) runTestCase(ctx context.Context, testCaseFile string, dsPath st
return tr, nil
}

func (r *tester) listTestCaseFiles() ([]string, error) {
fis, err := os.ReadDir(r.testFolder.Path)
if err != nil {
return nil, fmt.Errorf("reading pipeline tests failed (path: %s): %w", r.testFolder.Path, err)
}

var files []string
for _, fi := range fis {
if strings.HasSuffix(fi.Name(), expectedTestResultSuffix) ||
strings.HasSuffix(fi.Name(), configTestSuffixYAML) {
continue
}
files = append(files, fi.Name())
}
return files, nil
}

func loadTestCaseFile(testFolderPath, testCaseFile string) (*testCase, error) {
testCasePath := filepath.Join(testFolderPath, testCaseFile)
testCaseData, err := os.ReadFile(testCasePath)
Expand Down
53 changes: 45 additions & 8 deletions internal/testrunner/runners/policy/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package policy
import (
"context"
"fmt"
"path/filepath"
"strings"

"github.com/elastic/elastic-package/internal/kibana"
Expand Down Expand Up @@ -117,19 +118,55 @@ func (r *runner) GetTests(ctx context.Context) ([]testrunner.Tester, error) {
}
}

// TODO: Return a tester per each configuration file defined in the data stream folder
var testers []testrunner.Tester
for _, t := range folders {
testers = append(testers, NewPolicyTester(PolicyTesterOptions{
PackageRootPath: r.packageRootPath,
TestFolder: t,
KibanaClient: r.kibanaClient,
GenerateTestResult: r.generateTestResult,
}))
for _, folder := range folders {
tests, err := filepath.Glob(filepath.Join(folder.Path, "test-*.yml"))
if err != nil {
return nil, fmt.Errorf("failed to look for test files in %s: %w", folder.Path, err)
}
for _, test := range tests {
testers = append(testers, NewPolicyTester(PolicyTesterOptions{
PackageRootPath: r.packageRootPath,
TestFolder: folder,
KibanaClient: r.kibanaClient,
GenerateTestResult: r.generateTestResult,
TestPath: test,
}))

}
}
return testers, nil
}

func (r *runner) Type() testrunner.TestType {
return TestType
}

func (r *runner) setupSuite(ctx context.Context, manager *resources.Manager) (cleanup func(ctx context.Context) error, err error) {
packageResource := resources.FleetPackage{
RootPath: r.packageRootPath,
}
setupResources := resources.Resources{
&packageResource,
}

cleanup = func(ctx context.Context) error {
packageResource.Absent = true
_, err := manager.ApplyCtx(ctx, setupResources)
return err
}

logger.Debugf("Installing package...")
_, err = manager.ApplyCtx(ctx, setupResources)
if err != nil {
if ctx.Err() == nil {
cleanupErr := cleanup(ctx)
if cleanupErr != nil {
return nil, fmt.Errorf("setup failed: %w (with cleanup error: %w)", err, cleanupErr)
}
}
return nil, fmt.Errorf("setup failed: %w", err)
}

return cleanup, err
}
46 changes: 7 additions & 39 deletions internal/testrunner/runners/policy/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package policy

import (
"context"
"fmt"
"path/filepath"
"strings"

Expand All @@ -23,6 +22,7 @@ type tester struct {
packageRootPath string
generateTestResult bool
kibanaClient *kibana.Client
testPath string

resourcesManager *resources.Manager
}
Expand All @@ -32,6 +32,7 @@ var _ testrunner.Tester = new(tester)

type PolicyTesterOptions struct {
TestFolder testrunner.TestFolder
TestPath string
KibanaClient *kibana.Client
PackageRootPath string
GenerateTestResult bool
Expand All @@ -43,6 +44,7 @@ func NewPolicyTester(options PolicyTesterOptions) *tester {
testFolder: options.TestFolder,
packageRootPath: options.PackageRootPath,
generateTestResult: options.GenerateTestResult,
testPath: options.TestPath,
}
tester.resourcesManager = resources.NewManager()
tester.resourcesManager.RegisterProvider(resources.DefaultKibanaProviderName, &resources.KibanaProvider{Client: tester.kibanaClient})
Expand All @@ -59,18 +61,13 @@ func (r *tester) String() string {

func (r *tester) Run(ctx context.Context) ([]testrunner.TestResult, error) {
var results []testrunner.TestResult
tests, err := filepath.Glob(filepath.Join(r.testFolder.Path, "test-*.yml"))

result, err := r.runTest(ctx, r.resourcesManager, r.testPath)
if err != nil {
return nil, fmt.Errorf("failed to look for test files in %s: %w", r.testFolder.Path, err)
}
for _, test := range tests {
result, err := r.runTest(ctx, r.resourcesManager, test)
if err != nil {
logger.Error(err)
}
results = append(results, result...)
logger.Error(err)
}

results = append(results, result...)
return results, nil
}

Expand Down Expand Up @@ -133,35 +130,6 @@ func testNameFromPath(path string) string {
return strings.TrimSuffix(filepath.Base(path), ext)
}

func (r *runner) setupSuite(ctx context.Context, manager *resources.Manager) (cleanup func(ctx context.Context) error, err error) {
packageResource := resources.FleetPackage{
RootPath: r.packageRootPath,
}
setupResources := resources.Resources{
&packageResource,
}

cleanup = func(ctx context.Context) error {
packageResource.Absent = true
_, err := manager.ApplyCtx(ctx, setupResources)
return err
}

logger.Debugf("Installing package...")
_, err = manager.ApplyCtx(ctx, setupResources)
if err != nil {
if ctx.Err() == nil {
cleanupErr := cleanup(ctx)
if cleanupErr != nil {
return nil, fmt.Errorf("setup failed: %w (with cleanup error: %w)", err, cleanupErr)
}
}
return nil, fmt.Errorf("setup failed: %w", err)
}

return cleanup, err
}

func (r *tester) TearDown(ctx context.Context) error {
return nil
}
Loading