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