-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
f121618
commit d8845f1
Showing
4 changed files
with
176 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package names | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1" | ||
) | ||
|
||
func Step(step v1alpha1.TestStep, i int) string { | ||
if step.Name != "" { | ||
return step.Name | ||
} | ||
return fmt.Sprintf("step-%d", i+1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package names | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStep(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
step v1alpha1.TestStep | ||
i int | ||
want string | ||
}{{ | ||
name: "no name", | ||
step: v1alpha1.TestStep{ | ||
Name: "", | ||
}, | ||
i: 10, | ||
want: "step-11", | ||
}, { | ||
name: "with name", | ||
step: v1alpha1.TestStep{ | ||
Name: "foo", | ||
}, | ||
i: 10, | ||
want: "foo", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := Step(tt.step, tt.i) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/kyverno/chainsaw/pkg/apis" | ||
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1" | ||
"github.com/kyverno/kyverno-json/pkg/core/compilers" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
func Test_buildNamespace(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
compilers compilers.Compilers | ||
nsName string | ||
nsTemplate *v1alpha1.Projection | ||
bindings apis.Bindings | ||
want *corev1.Namespace | ||
wantErr bool | ||
}{{ | ||
name: "simple", | ||
nsName: "foo", | ||
want: &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: corev1.SchemeGroupVersion.String(), | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, { | ||
name: "with template", | ||
compilers: apis.DefaultCompilers, | ||
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")), | ||
nsName: "foo", | ||
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{ | ||
"metadata": map[string]any{ | ||
"labels": map[string]any{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
})), | ||
want: &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: corev1.SchemeGroupVersion.String(), | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
}, | ||
}, { | ||
name: "with nil template", | ||
compilers: apis.DefaultCompilers, | ||
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")), | ||
nsName: "foo", | ||
nsTemplate: ptr.To(v1alpha1.NewProjection(nil)), | ||
want: &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: corev1.SchemeGroupVersion.String(), | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, { | ||
name: "with template", | ||
compilers: apis.DefaultCompilers, | ||
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")), | ||
nsName: "foo", | ||
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{ | ||
"metadata": map[string]any{ | ||
"labels": map[string]any{ | ||
"foo": "($bar)", | ||
}, | ||
}, | ||
})), | ||
want: &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: corev1.SchemeGroupVersion.String(), | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
}, | ||
}, { | ||
name: "with bad template", | ||
compilers: apis.DefaultCompilers, | ||
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")), | ||
nsName: "foo", | ||
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{ | ||
"metadata": map[string]any{ | ||
"labels": map[string]any{ | ||
"foo": "($flop)", | ||
}, | ||
}, | ||
})), | ||
wantErr: true, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := buildNamespace(context.Background(), tt.compilers, tt.nsName, tt.nsTemplate, tt.bindings) | ||
assert.Equal(t, tt.want, got) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |