Skip to content

Commit

Permalink
test: migrate tests to v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgb committed Jun 15, 2024
1 parent e09d427 commit cf1af03
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
22 changes: 14 additions & 8 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"errors"
"os"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
"github.com/cybozu-go/accurate/pkg/constants"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//go:embed testdata/role.yaml
Expand Down Expand Up @@ -245,17 +246,22 @@ var _ = Describe("kubectl accurate", func() {
kubectlSafe(nil, "get", "subnamespaces", "-n", "subroot2", "sn1")
kubectlSafe(nil, "get", "subnamespaces", "-n", "subroot1", "sn1")

Eventually(func() string {
out, err := kubectl(nil, "get", "-n", "subroot2", "subnamespaces.v1.accurate.cybozu.com", "sn1", "-o", "json")
var conditions []metav1.Condition
Eventually(func() ([]metav1.Condition, error) {
out, err := kubectl(nil, "get", "-n", "subroot2", "subnamespaces", "sn1", "-o", "json")
if err != nil {
return ""
return nil, err
}
sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
if err := json.Unmarshal(out, sn); err != nil {
return ""
return nil, err
}
return string(sn.Status)
}).Should(Equal("conflict"))
conditions = sn.Status.Conditions
return conditions, nil
}).Should(HaveLen(1))
Expect(conditions[0].Type).To(Equal("Stalled"))
Expect(conditions[0].Reason).To(Equal("Conflict"))
Expect(conditions[0].Status).To(Equal("True"))

kubectlSafe(nil, "accurate", "sub", "cut", "sn2")
_, err = kubectl(nil, "get", "-n", "sn1", "subnamespaces", "sn2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/constants"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -59,29 +59,29 @@ var _ = Describe("SubNamespace webhook", func() {
})
})

Context("v2", func() {
Context("v1", func() {
It("should deny creation of SubNamespace in a namespace that is neither root nor subnamespace", func() {
ns := &corev1.Namespace{}
ns.Name = "v2-ns1"
ns.Name = "v1-ns1"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev2.SubNamespace{}
sn := &accuratev1.SubNamespace{}
sn.Namespace = ns.Name
sn.Name = "v2-foo"
sn.Name = "v1-foo"
err := k8sClient.Create(ctx, sn)
Expect(err).To(HaveOccurred())
Expect(errors.ReasonForError(err)).Should(Equal(metav1.StatusReasonForbidden))
})

It("should allow creation of SubNamespace in a root namespace", func() {
ns := &corev1.Namespace{}
ns.Name = "v2-ns2"
ns.Name = "v1-ns2"
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev2.SubNamespace{}
sn := &accuratev1.SubNamespace{}
sn.Namespace = ns.Name
sn.Name = "v2-foo"
sn.Name = "v1-foo"
sn.Spec.Labels = map[string]string{"foo": "bar"}
sn.Spec.Annotations = map[string]string{"foo": "bar"}
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -92,8 +92,8 @@ var _ = Describe("SubNamespace webhook", func() {
sn.Finalizers = nil
Expect(k8sClient.Update(ctx, sn)).To(Succeed())

sn = &accuratev2.SubNamespace{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: "v2-ns2", Name: "v2-foo"}, sn)).To(Succeed())
sn = &accuratev1.SubNamespace{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: "v1-ns2", Name: "v1-foo"}, sn)).To(Succeed())
Expect(sn.Finalizers).To(BeEmpty())
})
})
Expand Down
38 changes: 19 additions & 19 deletions hooks/subnamespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
"github.com/cybozu-go/accurate/pkg/constants"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -23,7 +23,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Name = "ns1"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "ns1"
sn.Name = "foo"
err := k8sClient.Create(ctx, sn)
Expand All @@ -37,7 +37,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "ns2"
sn.Name = "foo"
sn.Spec.Labels = map[string]string{"foo": "bar"}
Expand All @@ -50,7 +50,7 @@ var _ = Describe("SubNamespace webhook", func() {
sn.Finalizers = nil
Expect(k8sClient.Update(ctx, sn)).To(Succeed())

sn = &accuratev1.SubNamespace{}
sn = &accuratev2.SubNamespace{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: "ns2", Name: "foo"}, sn)).To(Succeed())
Expect(sn.Finalizers).To(BeEmpty())
})
Expand All @@ -66,7 +66,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelParent: "ns-parent"}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "ns3"
sn.Name = "bar"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -83,7 +83,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "naming-policy-root-1"
sn.Name = "naming-policy-root-1-child"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -95,7 +95,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "root-ns-match-1"
sn.Name = "child-match-1"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -112,7 +112,7 @@ var _ = Describe("SubNamespace webhook", func() {
parent.Labels = map[string]string{constants.LabelParent: "ns-root-1"}
Expect(k8sClient.Create(ctx, parent)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "ns-root-1-parent"
sn.Name = "ns-root-1-parent-child"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -124,7 +124,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "app-team1"
sn.Name = "app-team1-child"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -136,7 +136,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "app-team2-app1"
sn.Name = "app-team2-app1-subapp1"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -148,7 +148,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "unuse-naming-group-team1"
sn.Name = "unuse-naming-group-child1"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Expand All @@ -162,7 +162,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "naming-policy-root-2"
sn.Name = "naming-policy-root-2--child"
err := k8sClient.Create(ctx, sn)
Expand All @@ -176,7 +176,7 @@ var _ = Describe("SubNamespace webhook", func() {
ns.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "root-ns-match-2"
sn.Name = "child-2"
err := k8sClient.Create(ctx, sn)
Expand All @@ -195,7 +195,7 @@ var _ = Describe("SubNamespace webhook", func() {
parent.Labels = map[string]string{constants.LabelParent: "ns-root-1"}
Expect(k8sClient.Create(ctx, parent)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "ns-root-2-parent"
sn.Name = "not-ns-root-2-parent-child"
err := k8sClient.Create(ctx, sn)
Expand All @@ -209,7 +209,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "app-team10"
sn.Name = "app-team20-child"
err := k8sClient.Create(ctx, sn)
Expand All @@ -223,7 +223,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "unuse-naming-group-team2"
sn.Name = "unuse-naming-group-team2-foo"
err := k8sClient.Create(ctx, sn)
Expand All @@ -237,7 +237,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "labels-invalid-1"
sn.Name = "labels-invalid-1-sub"
sn.Spec.Labels = map[string]string{"foo": "~"}
Expand All @@ -252,7 +252,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "annotations-invalid-1"
sn.Name = "annotations-invalid-1-sub"
sn.Spec.Annotations = map[string]string{"foo-": ""}
Expand All @@ -267,7 +267,7 @@ var _ = Describe("SubNamespace webhook", func() {
root.Labels = map[string]string{constants.LabelType: constants.NSTypeRoot}
Expect(k8sClient.Create(ctx, root)).To(Succeed())

sn := &accuratev1.SubNamespace{}
sn := &accuratev2.SubNamespace{}
sn.Namespace = "both-invalid-1"
sn.Name = "both-invalid-1-sub"
sn.Spec.Labels = map[string]string{"foo": "~"}
Expand Down

0 comments on commit cf1af03

Please sign in to comment.