Skip to content

Commit

Permalink
first stab
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Owsiany <porridge@redhat.com>
  • Loading branch information
porridge committed Aug 21, 2023
1 parent f367579 commit e003d4f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
9 changes: 9 additions & 0 deletions pkg/kuttlctl/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"log"
"os"
"strings"
Expand Down Expand Up @@ -61,6 +62,7 @@ func newTestCmd() *cobra.Command { //nolint:gocyclo
reportName := "kuttl-report"
namespace := ""
suppress := []string{}
runLabels := ""

options := harness.TestSuite{}

Expand Down Expand Up @@ -226,9 +228,15 @@ For more detailed documentation, visit: https://kuttl.dev`,
},
Run: func(cmd *cobra.Command, args []string) {
testutils.RunTests("kuttl", testToRun, options.Parallel, func(t *testing.T) {
l, err := labels.ConvertSelectorToLabelsMap(runLabels)
fmt.Printf("using labels %q %q", runLabels, l.String())
if err != nil {
panic(err)
}
harness := test.Harness{
TestSuite: options,
T: t,
RunLabels: l,
}

harness.Run()
Expand Down Expand Up @@ -257,6 +265,7 @@ For more detailed documentation, visit: https://kuttl.dev`,
testCmd.Flags().StringVar(&reportName, "report-name", "kuttl-report", "Name for the report. Report location determined by --artifacts-dir and report file type determined by --report.")
testCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace to use for tests. Provided namespaces must exist prior to running tests.")
testCmd.Flags().StringSliceVar(&suppress, "suppress-log", []string{}, "Suppress logging for these kinds of logs (events).")
testCmd.Flags().StringVar(&runLabels, "test-run-labels", "", "Test run labels to use.")
// This cannot be a global flag because pkg/test/utils.RunTests calls flag.Parse which barfs on unknown top-level flags.
// Putting it here at least does not advertise it on a level where using it is impossible.
test.SetFlags(testCmd.Flags())
Expand Down
17 changes: 10 additions & 7 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -40,6 +41,7 @@ type Case struct {
SkipDelete bool
Timeout int
PreferredNamespace string
RunLabels labels.Set

Client func(forceNew bool) (client.Client, error)
DiscoveryClient func() (discovery.DiscoveryInterface, error)
Expand Down Expand Up @@ -458,13 +460,14 @@ func (t *Case) LoadTestSteps() error {

for index, files := range testStepFiles {
testStep := &Step{
Timeout: t.Timeout,
Index: int(index),
SkipDelete: t.SkipDelete,
Dir: t.Dir,
Asserts: []client.Object{},
Apply: []client.Object{},
Errors: []client.Object{},
Timeout: t.Timeout,
Index: int(index),
SkipDelete: t.SkipDelete,
Dir: t.Dir,
TestRunLabels: t.RunLabels,
Asserts: []client.Object{},
Apply: []client.Object{},
Errors: []client.Object{},
}

for _, file := range files {
Expand Down
3 changes: 3 additions & 0 deletions pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"math/rand"
"os"
"os/exec"
Expand Down Expand Up @@ -53,6 +54,7 @@ type Harness struct {
stopping bool
bgProcesses []*exec.Cmd
report *report.Testsuites
RunLabels labels.Set
}

// LoadTests loads all of the tests in a given directory.
Expand Down Expand Up @@ -85,6 +87,7 @@ func (h *Harness) LoadTests(dir string) ([]*Case, error) {
Dir: filepath.Join(dir, file.Name()),
SkipDelete: h.TestSuite.SkipDelete,
Suppress: h.TestSuite.Suppress,
RunLabels: h.RunLabels,
})
}

Expand Down
30 changes: 28 additions & 2 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -36,7 +38,8 @@ type Step struct {
Index int
SkipDelete bool

Dir string
Dir string
TestRunLabels labels.Set

Step *harness.TestStep
Assert *harness.TestAssert
Expand Down Expand Up @@ -495,11 +498,34 @@ func (s *Step) String() string {
// if seen, mark a test immediately failed.
// - All other YAML files are considered resources to create.
func (s *Step) LoadYAML(file string) error {
objects, err := testutils.LoadYAMLFromFile(file)
loadedObjects, err := testutils.LoadYAMLFromFile(file)
if err != nil {
return fmt.Errorf("loading %s: %s", file, err)
}

var objects []client.Object

for i, object := range loadedObjects {
if testFileObject, ok := object.(*harness.TestFile); ok {
// TODO: abort on multiple TestFile objects in a file
fmt.Printf("file %q has raw test run selector %q\n", file, testFileObject.TestRunSelector.String())
selector, err := metav1.LabelSelectorAsSelector(testFileObject.TestRunSelector)
if err != nil {
return fmt.Errorf("unrecognized test run selector in object %d of %q: %w", i, file, err)
}
fmt.Printf("file %q has test run selector %q\n", file, selector.String())
if selector.Empty() || selector.Matches(s.TestRunLabels) {
fmt.Printf("file %q selector empty=%s matches=%t\n", file, selector.Empty(), selector.Matches(s.TestRunLabels))
continue
} else {
fmt.Printf("Skipping file %q, label selector does not match test run.\n", file)
return nil
}
} else {
objects = append(objects, object)
}
}

if err = s.populateObjectsByFileName(filepath.Base(file), objects); err != nil {
return fmt.Errorf("populating step: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ func ConvertUnstructured(in client.Object) (client.Object, error) {
return in, nil
}
switch {
case kind == "TestFile":
converted = &harness.TestFile{}
case kind == "TestStep":
converted = &harness.TestStep{}
case kind == "TestAssert":
Expand Down

0 comments on commit e003d4f

Please sign in to comment.