1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package controllers
17
18 import (
19 "context"
20 "fmt"
21 "strings"
22 "time"
23
24 . "github.com/onsi/ginkgo/v2"
25 . "github.com/onsi/gomega"
26 corev1 "k8s.io/api/core/v1"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "k8s.io/apimachinery/pkg/types"
29 "sigs.k8s.io/controller-runtime/pkg/client"
30
31 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
32 )
33
34
35 var _ = Describe("Workflow", func() {
36 var ns string
37 BeforeEach(func() {
38 ctx := context.TODO()
39 newNs := corev1.Namespace{
40 ObjectMeta: metav1.ObjectMeta{
41 GenerateName: "chaos-mesh-",
42 },
43 Spec: corev1.NamespaceSpec{},
44 }
45 Expect(kubeClient.Create(ctx, &newNs)).To(Succeed())
46 ns = newNs.Name
47 By(fmt.Sprintf("create new namespace %s", ns))
48 })
49
50 AfterEach(func() {
51 ctx := context.TODO()
52 nsToDelete := corev1.Namespace{}
53 Expect(kubeClient.Get(ctx, types.NamespacedName{Name: ns}, &nsToDelete)).To(Succeed())
54 Expect(kubeClient.Delete(ctx, &nsToDelete)).To(Succeed())
55 By(fmt.Sprintf("cleanup namespace %s", ns))
56 })
57
58 Context("one chaos node", func() {
59 It("could spawn one chaos", func() {
60 ctx := context.TODO()
61 now := time.Now()
62 duration := 5 * time.Second
63
64 By("create simple chaos node with pod chaos")
65 startTime := metav1.NewTime(now)
66 deadline := metav1.NewTime(now.Add(duration))
67 workflowNode := v1alpha1.WorkflowNode{
68 ObjectMeta: metav1.ObjectMeta{
69 Namespace: ns,
70 GenerateName: "chaos-node-with-chaos-",
71 },
72 Spec: v1alpha1.WorkflowNodeSpec{
73 TemplateName: "",
74 WorkflowName: "",
75 Type: v1alpha1.TypePodChaos,
76 StartTime: &startTime,
77 Deadline: &deadline,
78 EmbedChaos: &v1alpha1.EmbedChaos{
79 PodChaos: &v1alpha1.PodChaosSpec{
80 ContainerSelector: v1alpha1.ContainerSelector{
81 PodSelector: v1alpha1.PodSelector{
82 Selector: v1alpha1.PodSelectorSpec{
83 GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
84 Namespaces: []string{ns},
85 },
86 },
87 Mode: v1alpha1.AllMode,
88 },
89 },
90 Action: v1alpha1.PodKillAction,
91 },
92 },
93 },
94 }
95 Expect(kubeClient.Create(ctx, &workflowNode)).To(Succeed())
96 Eventually(func() bool {
97 podChaosList := v1alpha1.PodChaosList{}
98 Expect(kubeClient.List(ctx, &podChaosList, &client.ListOptions{Namespace: ns})).To(Succeed())
99 if len(podChaosList.Items) == 0 {
100 return false
101 }
102 return strings.HasPrefix(podChaosList.Items[0].Name, "chaos-node-with-chaos-")
103 }, 10*time.Second, time.Second).Should(BeTrue())
104 })
105
106 It("could spawn one schedule", func() {
107 ctx := context.TODO()
108 now := time.Now()
109 duration := 5 * time.Second
110
111 By("create simple chaos node with schedule")
112 startTime := metav1.NewTime(now)
113 deadline := metav1.NewTime(now.Add(duration))
114 node := v1alpha1.WorkflowNode{
115 ObjectMeta: metav1.ObjectMeta{
116 Namespace: ns,
117 GenerateName: "chaos-node-schedule-",
118 },
119 Spec: v1alpha1.WorkflowNodeSpec{
120 WorkflowName: "",
121 Type: v1alpha1.TypeSchedule,
122 StartTime: &startTime,
123 Deadline: &deadline,
124 Schedule: &v1alpha1.ScheduleSpec{
125 Schedule: "@every 1s",
126 StartingDeadlineSeconds: nil,
127 ConcurrencyPolicy: v1alpha1.AllowConcurrent,
128 HistoryLimit: 5,
129 Type: v1alpha1.ScheduleTypePodChaos,
130 ScheduleItem: v1alpha1.ScheduleItem{
131 EmbedChaos: v1alpha1.EmbedChaos{
132 PodChaos: &v1alpha1.PodChaosSpec{
133 ContainerSelector: v1alpha1.ContainerSelector{
134 PodSelector: v1alpha1.PodSelector{
135 Selector: v1alpha1.PodSelectorSpec{
136 GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
137 Namespaces: []string{ns},
138 LabelSelectors: map[string]string{
139 "app": "not-actually-exist",
140 },
141 },
142 },
143 Mode: v1alpha1.AllMode,
144 },
145 ContainerNames: nil,
146 },
147 Action: v1alpha1.PodKillAction,
148 },
149 },
150 },
151 },
152 },
153 }
154 Expect(kubeClient.Create(ctx, &node)).To(Succeed())
155 Eventually(func() bool {
156 scheduleList := v1alpha1.ScheduleList{}
157 Expect(kubeClient.List(ctx, &scheduleList, &client.ListOptions{Namespace: ns})).To(Succeed())
158 if len(scheduleList.Items) == 0 {
159 return false
160 }
161 return strings.HasPrefix(scheduleList.Items[0].Name, "chaos-node-schedule-")
162 }, 10*time.Second, time.Second).Should(BeTrue())
163 })
164 })
165 })
166