Skip to content

Commit

Permalink
- Use log package instead of fmt
Browse files Browse the repository at this point in the history
- Use configurable timeout for all timeout types
- Use CommandContext for applying the global timeout
- For preventing memory leak extract the test function

Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed Aug 22, 2024
1 parent aae5026 commit 72c3770
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 209 deletions.
3 changes: 1 addition & 2 deletions cmd/uptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"gopkg.in/alecthomas/kingpin.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/crossplane/uptest/internal"
"github.com/crossplane/uptest/internal/config"
Expand Down Expand Up @@ -93,7 +92,7 @@ func e2eTests() {
SetupScriptPath: setupPath,
TeardownScriptPath: teardownPath,
DefaultConditions: strings.Split(*defaultConditions, ","),
DefaultTimeout: v1.Duration{Duration: *defaultTimeout},
DefaultTimeout: *defaultTimeout,
Directory: *testDir,
SkipDelete: *skipDelete,
OnlyCleanUptestResources: *onlyCleanUptestResources,
Expand Down
5 changes: 2 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package config
import (
"time"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ type AutomatedTest struct {
SetupScriptPath string
TeardownScriptPath string

DefaultTimeout v1.Duration
DefaultTimeout time.Duration
DefaultConditions []string

SkipDelete bool
Expand Down Expand Up @@ -93,7 +92,7 @@ type Resource struct {
APIVersion string
Kind string

Timeout v1.Duration
Timeout time.Duration
Conditions []string
PreAssertScriptPath string
PostAssertScriptPath string
Expand Down
3 changes: 2 additions & 1 deletion internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"fmt"
"io"
"log"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (p *preparer) prepareManifests() ([]config.Manifest, error) {
}
if u != nil {
if v, ok := u.GetAnnotations()["upjet.upbound.io/manual-intervention"]; ok {
fmt.Printf("Skipping %s with name %s since it requires the following manual intervention: %s\n", u.GroupVersionKind().String(), u.GetName(), v)
log.Printf("Skipping %s with name %s since it requires the following manual intervention: %s\n", u.GroupVersionKind().String(), u.GetName(), v)
continue
}
y, err := yaml.Marshal(u)
Expand Down
4 changes: 2 additions & 2 deletions internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package internal

import (
"fmt"
"log"
"os"

"github.com/crossplane/crossplane-runtime/pkg/errors"
Expand All @@ -18,7 +18,7 @@ func RunTest(o *config.AutomatedTest) error {
if !o.RenderOnly {
defer func() {
if err := os.RemoveAll(o.Directory); err != nil {
fmt.Println(fmt.Sprint(err, "cannot clean the test directory"))
log.Printf("Cannot clean the test directory: %s\n", err.Error())
}
}()
}
Expand Down
8 changes: 4 additions & 4 deletions internal/templates/00-apply.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ metadata:
name: apply
spec:
timeouts:
apply: 1m
apply: {{ .TestCase.Timeout }}
assert: {{ .TestCase.Timeout }}
exec: 1m
exec: {{ .TestCase.Timeout }}
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
Expand All @@ -32,8 +32,8 @@ spec:
{{- end }}
- name: Assert Status Conditions
description: |
Assert applied resources. Firstly run the pre-assert script if exists.
Then check the status conditions. Finally run the post-assert script if
Assert applied resources. First, run the pre-assert script if exists.
Then, check the status conditions. Finally run the post-assert script if it
exists.
try:
{{- range $resource := .Resources }}
Expand Down
30 changes: 0 additions & 30 deletions internal/templates/00-assert.yaml.tmpl

This file was deleted.

19 changes: 0 additions & 19 deletions internal/templates/01-assert.yaml.tmpl

This file was deleted.

4 changes: 2 additions & 2 deletions internal/templates/01-update.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ metadata:
name: update
spec:
timeouts:
apply: 1m
apply: {{ .TestCase.Timeout }}
assert: {{ .TestCase.Timeout }}
exec: 1m
exec: {{ .TestCase.Timeout }}
steps:
- name: Update Root Resource
description: |
Expand Down
22 changes: 0 additions & 22 deletions internal/templates/02-assert.yaml.tmpl

This file was deleted.

4 changes: 2 additions & 2 deletions internal/templates/02-import.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ metadata:
name: import
spec:
timeouts:
apply: 1m
apply: {{ .TestCase.Timeout }}
assert: {{ .TestCase.Timeout }}
exec: 3m
exec: {{ .TestCase.Timeout }}
steps:
- name: Remove State
description: |
Expand Down
26 changes: 0 additions & 26 deletions internal/templates/03-assert.yaml.tmpl

This file was deleted.

Loading

0 comments on commit 72c3770

Please sign in to comment.