-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: Verify field immutability for all CRDs (#1755)
* fix: AlertRuleGroup editable only testing value, not presence * test: AlertRuleGroup Editable * fix: NotificationPolicy editable only testing value, not presence * test: NotificationPolicy editable * test: Dashboard uid * test: ContactPoint uid * test: Folder uid * test: Datasource uid * Chore: Disable Chainsaw test after being replaced with envtest
- Loading branch information
1 parent
73ef281
commit 1ac8d8d
Showing
16 changed files
with
371 additions
and
3 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
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,71 @@ | ||
package v1beta1 | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func newAlertRuleGroup(name string, editable *bool) *GrafanaAlertRuleGroup { | ||
return &GrafanaAlertRuleGroup{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: APIVersion, | ||
Kind: "GrafanaAlertRuleGroup", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: name, | ||
Namespace: "default", | ||
}, | ||
Spec: GrafanaAlertRuleGroupSpec{ | ||
Name: name, | ||
Editable: editable, | ||
FolderRef: "DummyFolderRef", | ||
InstanceSelector: &v1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"test": "alertrulegroup", | ||
}, | ||
}, | ||
Rules: []AlertRule{}, | ||
}, | ||
} | ||
} | ||
|
||
var _ = Describe("AlertRuleGroup type", func() { | ||
Context("Ensure AlertRuleGroup spec.editable is immutable", func() { | ||
ctx := context.Background() | ||
refTrue := true | ||
refFalse := false | ||
|
||
It("Should block adding editable field when missing", func() { | ||
arg := newAlertRuleGroup("missing-editable", nil) | ||
By("Create new AlertRuleGroup without editable") | ||
Expect(k8sClient.Create(ctx, arg)).To(Succeed()) | ||
|
||
By("Adding a editable") | ||
arg.Spec.Editable = &refTrue | ||
Expect(k8sClient.Update(ctx, arg)).To(HaveOccurred()) | ||
}) | ||
|
||
It("Should block removing editable field when set", func() { | ||
arg := newAlertRuleGroup("existing-editable", &refTrue) | ||
By("Creating AlertRuleGroup with existing editable") | ||
Expect(k8sClient.Create(ctx, arg)).To(Succeed()) | ||
|
||
By("And setting editable to ''") | ||
arg.Spec.Editable = nil | ||
Expect(k8sClient.Update(ctx, arg)).To(HaveOccurred()) | ||
}) | ||
|
||
It("Should block changing value of editable", func() { | ||
arg := newAlertRuleGroup("removing-editable", &refTrue) | ||
By("Create new AlertRuleGroup with existing editable") | ||
Expect(k8sClient.Create(ctx, arg)).To(Succeed()) | ||
|
||
By("Changing the existing editable") | ||
arg.Spec.Editable = &refFalse | ||
Expect(k8sClient.Update(ctx, arg)).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
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,74 @@ | ||
package v1beta1 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
// apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func newContactPoint(name string, uid string) *GrafanaContactPoint { | ||
settings := new(apiextensionsv1.JSON) | ||
json.Unmarshal([]byte("{}"), settings) //nolint:errcheck | ||
|
||
return &GrafanaContactPoint{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: APIVersion, | ||
Kind: "GrafanaContactPoint", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: name, | ||
Namespace: "default", | ||
}, | ||
Spec: GrafanaContactPointSpec{ | ||
CustomUID: uid, | ||
InstanceSelector: &v1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"test": "datasource", | ||
}, | ||
}, | ||
Settings: settings, | ||
}, | ||
} | ||
} | ||
|
||
var _ = Describe("ContactPoint type", func() { | ||
Context("Ensure ContactPoint spec.uid is immutable", func() { | ||
ctx := context.Background() | ||
|
||
It("Should block adding uid field when missing", func() { | ||
contactpoint := newContactPoint("missing-uid", "") | ||
By("Create new ContactPoint without uid") | ||
Expect(k8sClient.Create(ctx, contactpoint)).To(Succeed()) | ||
|
||
By("Adding a uid") | ||
contactpoint.Spec.CustomUID = "new-contactpoint-uid" | ||
Expect(k8sClient.Update(ctx, contactpoint)).To(HaveOccurred()) | ||
}) | ||
|
||
It("Should block removing uid field when set", func() { | ||
contactpoint := newContactPoint("existing-uid", "existing-uid") | ||
By("Creating ContactPoint with existing UID") | ||
Expect(k8sClient.Create(ctx, contactpoint)).To(Succeed()) | ||
|
||
By("And setting UID to ''") | ||
contactpoint.Spec.CustomUID = "" | ||
Expect(k8sClient.Update(ctx, contactpoint)).To(HaveOccurred()) | ||
}) | ||
|
||
It("Should block changing value of uid", func() { | ||
contactpoint := newContactPoint("removing-uid", "existing-uid") | ||
By("Create new ContactPoint with existing UID") | ||
Expect(k8sClient.Create(ctx, contactpoint)).To(Succeed()) | ||
|
||
By("Changing the existing UID") | ||
contactpoint.Spec.CustomUID = "new-contactpoint-uid" | ||
Expect(k8sClient.Update(ctx, contactpoint)).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
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
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
Oops, something went wrong.