...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 . "github.com/onsi/ginkgo"
20 . "github.com/onsi/gomega"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 )
23
24 var _ = Describe("timechaos_webhook", func() {
25 Context("Defaulter", func() {
26 It("set default namespace selector", func() {
27 timechaos := &TimeChaos{
28 ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
29 }
30 timechaos.Default()
31 Expect(timechaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
32 Expect(timechaos.Spec.ClockIds[0]).To(Equal("CLOCK_REALTIME"))
33 })
34 })
35 Context("webhook.Validator of timechaos", func() {
36 It("Validate", func() {
37
38 type TestCase struct {
39 name string
40 chaos TimeChaos
41 execute func(chaos *TimeChaos) error
42 expect string
43 }
44 tcs := []TestCase{
45 {
46 name: "simple ValidateCreate",
47 chaos: TimeChaos{
48 ObjectMeta: metav1.ObjectMeta{
49 Namespace: metav1.NamespaceDefault,
50 Name: "foo1",
51 },
52 Spec: TimeChaosSpec{TimeOffset: "1s"},
53 },
54 execute: func(chaos *TimeChaos) error {
55 return chaos.ValidateCreate()
56 },
57 expect: "",
58 },
59 {
60 name: "simple ValidateUpdate",
61 chaos: TimeChaos{
62 ObjectMeta: metav1.ObjectMeta{
63 Namespace: metav1.NamespaceDefault,
64 Name: "foo2",
65 },
66 Spec: TimeChaosSpec{TimeOffset: "1s"},
67 },
68 execute: func(chaos *TimeChaos) error {
69 return chaos.ValidateUpdate(chaos)
70 },
71 expect: "",
72 },
73 {
74 name: "simple ValidateDelete",
75 chaos: TimeChaos{
76 ObjectMeta: metav1.ObjectMeta{
77 Namespace: metav1.NamespaceDefault,
78 Name: "foo3",
79 },
80 Spec: TimeChaosSpec{TimeOffset: "1s"},
81 },
82 execute: func(chaos *TimeChaos) error {
83 return chaos.ValidateDelete()
84 },
85 expect: "",
86 },
87 {
88 name: "validate the timeOffset",
89 chaos: TimeChaos{
90 ObjectMeta: metav1.ObjectMeta{
91 Namespace: metav1.NamespaceDefault,
92 Name: "foo6",
93 },
94 Spec: TimeChaosSpec{
95 TimeOffset: "1S",
96 },
97 },
98 execute: func(chaos *TimeChaos) error {
99 return chaos.ValidateCreate()
100 },
101 expect: "error",
102 },
103 }
104
105 for _, tc := range tcs {
106 err := tc.execute(&tc.chaos)
107 if tc.expect == "error" {
108 Expect(err).To(HaveOccurred())
109 } else {
110 Expect(err).NotTo(HaveOccurred())
111 }
112 }
113 })
114 })
115 })
116