Skip to content

Commit

Permalink
Add code comments for exported packages
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Jan 28, 2025
1 parent e379bca commit 8ede9cd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
21 changes: 21 additions & 0 deletions internal/config/builder.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,105 @@
// SPDX-FileCopyrightText: 2025 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: CC0-1.0

// Package config contains configuration options for configuring uptest runtime.
package config

import (
"time"
)

// Builder is a struct that helps construct an AutomatedTest instance step-by-step.
type Builder struct {
test AutomatedTest
}

// NewBuilder initializes and returns a new Builder instance.
func NewBuilder() *Builder {
return &Builder{
test: AutomatedTest{},
}
}

// SetDirectory sets the directory path for the AutomatedTest and returns the Builder.
func (b *Builder) SetDirectory(directory string) *Builder {
b.test.Directory = directory
return b
}

// SetManifestPaths sets the paths of the manifest files for the AutomatedTest and returns the Builder.
func (b *Builder) SetManifestPaths(manifestPaths []string) *Builder {
b.test.ManifestPaths = manifestPaths
return b
}

// SetDataSourcePath sets the data source path for the AutomatedTest and returns the Builder.
func (b *Builder) SetDataSourcePath(dataSourcePath string) *Builder {
b.test.DataSourcePath = dataSourcePath
return b
}

// SetSetupScriptPath sets the setup script path for the AutomatedTest and returns the Builder.
func (b *Builder) SetSetupScriptPath(setupScriptPath string) *Builder {
b.test.SetupScriptPath = setupScriptPath
return b
}

// SetTeardownScriptPath sets the teardown script path for the AutomatedTest and returns the Builder.
func (b *Builder) SetTeardownScriptPath(teardownScriptPath string) *Builder {
b.test.TeardownScriptPath = teardownScriptPath
return b
}

// SetDefaultTimeout sets the default timeout duration for the AutomatedTest and returns the Builder.
func (b *Builder) SetDefaultTimeout(defaultTimeout time.Duration) *Builder {
b.test.DefaultTimeout = defaultTimeout
return b
}

// SetDefaultConditions sets the default conditions for the AutomatedTest and returns the Builder.
func (b *Builder) SetDefaultConditions(defaultConditions []string) *Builder {
b.test.DefaultConditions = defaultConditions
return b
}

// SetSkipDelete sets whether the AutomatedTest should skip resource deletion and returns the Builder.
func (b *Builder) SetSkipDelete(skipDelete bool) *Builder {
b.test.SkipDelete = skipDelete
return b
}

// SetSkipUpdate sets whether the AutomatedTest should skip resource updates and returns the Builder.
func (b *Builder) SetSkipUpdate(skipUpdate bool) *Builder {
b.test.SkipUpdate = skipUpdate
return b
}

// SetSkipImport sets whether the AutomatedTest should skip resource imports and returns the Builder.
func (b *Builder) SetSkipImport(skipImport bool) *Builder {
b.test.SkipImport = skipImport
return b
}

// SetOnlyCleanUptestResources sets whether the AutomatedTest should clean up only test-specific resources and returns the Builder.
func (b *Builder) SetOnlyCleanUptestResources(onlyCleanUptestResources bool) *Builder {
b.test.OnlyCleanUptestResources = onlyCleanUptestResources
return b
}

// SetRenderOnly sets whether the AutomatedTest should only render outputs without execution and returns the Builder.
func (b *Builder) SetRenderOnly(renderOnly bool) *Builder {
b.test.RenderOnly = renderOnly
return b
}

// SetLogCollectionInterval sets the interval for log collection during the AutomatedTest and returns the Builder.
func (b *Builder) SetLogCollectionInterval(logCollectionInterval time.Duration) *Builder {
b.test.LogCollectionInterval = logCollectionInterval
return b
}

// Build finalizes and returns the constructed AutomatedTest instance.
func (b *Builder) Build() *AutomatedTest {
return &b.test
}
22 changes: 18 additions & 4 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,51 @@ type injectedManifest struct {
Manifest string
}

// PreparerOption is a functional option type for configuring a Preparer.
type PreparerOption func(*Preparer)

// WithDataSource is a functional option that sets the data source path for the Preparer.
func WithDataSource(path string) PreparerOption {
return func(p *Preparer) {
p.dataSourcePath = path
}
}

// WithTestDirectory is a functional option that sets the test directory for the Preparer.
func WithTestDirectory(path string) PreparerOption {
return func(p *Preparer) {
p.testDirectory = path
}
}

// NewPreparer creates a new Preparer instance with the provided test file paths and optional configurations.
// It applies any provided PreparerOption functions to customize the Preparer.
func NewPreparer(testFilePaths []string, opts ...PreparerOption) *Preparer {
p := &Preparer{
testFilePaths: testFilePaths,
testDirectory: os.TempDir(),
testDirectory: os.TempDir(), // Default test directory is the system's temporary directory.
}
// Apply each provided option to configure the Preparer.
for _, f := range opts {
f(p)
}
return p
}

// Preparer represents a structure used to prepare testing environments or configurations.
type Preparer struct {
testFilePaths []string
dataSourcePath string
testDirectory string
testFilePaths []string // Paths to the test files.
dataSourcePath string // Path to the data source file.
testDirectory string // Directory where tests will be executed.
}

// PrepareManifests prepares and processes manifests from test files.
// It performs the following steps:
// 1. Cleans and recreates the case directory.
// 2. Injects variables into test files.
// 3. Decodes, processes, and validates each manifest file, skipping any that require manual intervention.
// 4. Returns the processed manifests or an error if any step fails.
//
//nolint:gocyclo // This function is not complex, gocyclo threshold was reached due to the error handling.
func (p *Preparer) PrepareManifests() ([]config.Manifest, error) {
caseDirectory := filepath.Join(p.testDirectory, caseDirectory)
Expand Down
4 changes: 4 additions & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ var testFiles = []string{
"03-delete.yaml",
}

// NewTester returns a Tester object.
func NewTester(ms []config.Manifest, opts *config.AutomatedTest) *Tester {
return &Tester{
options: opts,
manifests: ms,
}
}

// Tester is responsible preparing and storing the test data&configurations,
// and executing the tests.
type Tester struct {
options *config.AutomatedTest
manifests []config.Manifest
}

// ExecuteTests execute tests via chainsaw.
func (t *Tester) ExecuteTests() error {
if err := writeTestFile(t.manifests, t.options.Directory); err != nil {
return errors.Wrap(err, "cannot write test manifest files")
Expand Down
6 changes: 6 additions & 0 deletions pkg/runner.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// SPDX-FileCopyrightText: 2025 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: CC0-1.0

// Package pkg contains configuration options for configuring uptest runtime.
package pkg

import (
Expand Down Expand Up @@ -34,6 +39,7 @@ func RunTest(o *config.AutomatedTest) error {
return nil
}

// NewAutomatedTestBuilder returns a Builder for AutomatedTest object
func NewAutomatedTestBuilder() *config.Builder {
return config.NewBuilder()
}

0 comments on commit 8ede9cd

Please sign in to comment.