1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package chaosdaemon_test
17
18 import (
19 "context"
20 "os/exec"
21 "testing"
22
23 "github.com/go-logr/logr"
24 . "github.com/onsi/gomega"
25
26 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon"
27 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
28 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
29 pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
30 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
31 )
32
33 func Test_SetDNSServer_Enable(t *testing.T) {
34 g := NewWithT(t)
35
36 type mockCmd struct {
37 cmd string
38 args []string
39 }
40 var executedCommands []mockCmd
41
42 mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
43 executedCommands = append(executedCommands, mockCmd{cmd, args})
44 return exec.Command("echo", "mock command")
45 })
46
47 mock.With("MockContainerdClient", &test.MockClient{})
48
49 crc, err := crclients.CreateContainerRuntimeInfoClient(&crclients.CrClientConfig{
50 Runtime: crclients.ContainerRuntimeContainerd,
51 })
52 g.Expect(err).NotTo(HaveOccurred())
53
54 server := chaosdaemon.NewDaemonServerWithCRClient(crc, nil, logr.Discard())
55
56 res, err := server.SetDNSServer(context.TODO(), &pb.SetDNSServerRequest{
57 ContainerId: "containerd://foo",
58 DnsServer: "8.6.4.2",
59 Enable: true,
60 EnterNS: false,
61 })
62 g.Expect(err).NotTo(HaveOccurred())
63 g.Expect(res).NotTo(BeNil())
64
65 g.Expect(executedCommands).To(Equal([]mockCmd{
66 {cmd: "sh", args: []string{"-c", "ls /etc/resolv.conf.chaos.bak || cp /etc/resolv.conf /etc/resolv.conf.chaos.bak"}},
67 {cmd: "sh", args: []string{"-c", "cp /etc/resolv.conf /etc/resolv_conf_dnschaos_temp && sed -i 's/.*nameserver.*/nameserver 8.6.4.2/' /etc/resolv_conf_dnschaos_temp && cat /etc/resolv_conf_dnschaos_temp > /etc/resolv.conf && rm /etc/resolv_conf_dnschaos_temp"}},
68 }))
69 }
70
71 func Test_SetDNSServer_Enable_InvalidIP(t *testing.T) {
72 g := NewWithT(t)
73
74 cases := []string{"", "127.0.0.b", " 127.0.0.1", "127.0.0.1 ", ":g:1", "127.0.0.1;"}
75
76 mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
77 g.Fail("no process should be executed")
78 return exec.Command("echo", "mock command")
79 })
80
81 mock.With("MockContainerdClient", &test.MockClient{})
82
83 crc, err := crclients.CreateContainerRuntimeInfoClient(&crclients.CrClientConfig{
84 Runtime: crclients.ContainerRuntimeContainerd,
85 })
86 g.Expect(err).NotTo(HaveOccurred())
87
88 server := chaosdaemon.NewDaemonServerWithCRClient(crc, nil, logr.Discard())
89
90 for _, tc := range cases {
91 res, err := server.SetDNSServer(context.TODO(), &pb.SetDNSServerRequest{
92 ContainerId: "containerd://foo",
93 DnsServer: tc,
94 Enable: true,
95 EnterNS: false,
96 })
97 g.Expect(err).To(Equal(chaosdaemon.ErrInvalidDNSServer))
98 g.Expect(res).To(BeNil())
99 }
100 }
101
102 func Test_SetDNSServer_Disable(t *testing.T) {
103 g := NewWithT(t)
104
105 type mockCmd struct {
106 cmd string
107 args []string
108 }
109 var executedCommands []mockCmd
110
111 mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
112 executedCommands = append(executedCommands, mockCmd{cmd, args})
113 return exec.Command("echo", "mock command")
114 })
115
116 mock.With("MockContainerdClient", &test.MockClient{})
117
118 crc, err := crclients.CreateContainerRuntimeInfoClient(&crclients.CrClientConfig{
119 Runtime: crclients.ContainerRuntimeContainerd,
120 })
121 g.Expect(err).NotTo(HaveOccurred())
122
123 server := chaosdaemon.NewDaemonServerWithCRClient(crc, nil, logr.Discard())
124
125 res, err := server.SetDNSServer(context.TODO(), &pb.SetDNSServerRequest{
126 ContainerId: "containerd://foo",
127 DnsServer: "",
128 Enable: false,
129 EnterNS: false,
130 })
131 g.Expect(err).NotTo(HaveOccurred())
132 g.Expect(res).NotTo(BeNil())
133
134 g.Expect(executedCommands).To(Equal([]mockCmd{
135 {cmd: "sh", args: []string{"-c", "ls /etc/resolv.conf.chaos.bak && cat /etc/resolv.conf.chaos.bak > /etc/resolv.conf || true"}},
136 }))
137 }
138