Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

[Enhancement] Comprehensive support for asserting contents of lists/arrays #36

Closed
wants to merge 17 commits into from
28 changes: 27 additions & 1 deletion pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const (
MatchWildcard MatchType = "Wildcard"
)

type Strategy string

const (
StrategyAnywhere Strategy = "Anywhere"
StrategyExact Strategy = "Exact"
)

// Create embedded struct to implement custom DeepCopyInto method
type RestConfig struct {
RC *rest.Config
Expand Down Expand Up @@ -133,7 +140,7 @@ type TestStep struct {
// Useful to reuse a number of applies across tests / test steps.
// all relative paths are relative to the folder the TestStep is defined in.
Apply []Apply `json:"apply,omitempty"`
Assert []string `json:"assert,omitempty"`
Assert []Assert `json:"assert,omitempty"`
Error []string `json:"error,omitempty"`

// Objects to delete at the beginning of the test step.
Expand All @@ -152,6 +159,25 @@ type TestStep struct {
Kubeconfig string `json:"kubeconfig,omitempty"`
}

type Assert struct {
// File specifies the relative or full path to the YAML containing the expected content.
File string `json:"file"`
ShouldFail bool `json:"shouldFail,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't we need ShouldFail here

Options *Options `json:"options,omitempty"`
}
Comment on lines +162 to +167
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a custom UnmarshalJSON func


type Options struct {
AssertArray []AssertArray `json:"arrays,omitempty"`
}

// AssertArray specifies conditions for verifying content within a YAML against a Kubernetes resource.
type AssertArray struct {
// Path indicates the location within the YAML file to extract data for verification.
Path string `json:"path"`
// Strategy defines how the extracted data should be compared against the Kubernetes resource.
Strategy Strategy `json:"strategy"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// TestAssert represents the settings needed to verify the result of a test step.
Expand Down
64 changes: 62 additions & 2 deletions pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (t *Case) LoadTestSteps() error {
Index: int(index),
SkipDelete: t.SkipDelete,
Dir: t.Dir,
Asserts: []client.Object{},
Asserts: []assertArray{},
Apply: []apply{},
Errors: []client.Object{},
}
Expand Down
38 changes: 21 additions & 17 deletions pkg/test/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ func TestLoadTestSteps(t *testing.T) {
}),
},
},
Asserts: []client.Object{
testutils.WithStatus(t, testutils.NewPod("test", ""), map[string]interface{}{
"qosClass": "BestEffort",
}),
Asserts: []assertArray{
{
object: testutils.WithStatus(t, testutils.NewPod("test", ""), map[string]interface{}{
"qosClass": "BestEffort",
}),
options: nil,
},
},
Errors: []client.Object{},
},
Expand Down Expand Up @@ -95,10 +98,10 @@ func TestLoadTestSteps(t *testing.T) {
}),
},
},
Asserts: []client.Object{
testutils.WithStatus(t, testutils.NewPod("test2", ""), map[string]interface{}{
Asserts: []assertArray{
{object: testutils.WithStatus(t, testutils.NewPod("test2", ""), map[string]interface{}{
"qosClass": "BestEffort",
}),
})},
},
Errors: []client.Object{},
},
Expand Down Expand Up @@ -127,10 +130,11 @@ func TestLoadTestSteps(t *testing.T) {
}),
},
},
Asserts: []client.Object{
testutils.WithStatus(t, testutils.NewPod("test3", ""), map[string]interface{}{
"qosClass": "BestEffort",
}),
Asserts: []assertArray{
{
object: testutils.WithStatus(t, testutils.NewPod("test3", ""), map[string]interface{}{
"qosClass": "BestEffort",
})},
},
Errors: []client.Object{},
},
Expand Down Expand Up @@ -171,10 +175,10 @@ func TestLoadTestSteps(t *testing.T) {
}),
},
},
Asserts: []client.Object{
testutils.WithSpec(t, testutils.NewPod("test5", ""), map[string]interface{}{
Asserts: []assertArray{
{object: testutils.WithSpec(t, testutils.NewPod("test5", ""), map[string]interface{}{
"restartPolicy": "Never",
}),
})},
},
Errors: []client.Object{},
},
Expand Down Expand Up @@ -210,8 +214,8 @@ func TestLoadTestSteps(t *testing.T) {
},
},
},
Asserts: []client.Object{
&unstructured.Unstructured{
Asserts: []assertArray{
{object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
Expand All @@ -229,7 +233,7 @@ func TestLoadTestSteps(t *testing.T) {
},
},
},
},
}},
},
Errors: []client.Object{},
},
Expand Down
Loading
Loading