@@ -113,3 +113,78 @@ func Test_recoverDns_failed(t *testing.T) {
113
113
t .Errorf ("unexpected commands: %+v, expected commands: %+v" , actualCommands , expectedCommands )
114
114
}
115
115
}
116
+
117
+ func Test_startChangeMultipleDns (t * testing.T ) {
118
+ type args struct {
119
+ domain string
120
+ ip string
121
+ }
122
+
123
+ as := & args {
124
+ domain : "abc.com,bbc.com" ,
125
+ ip : "208.80.152.2" ,
126
+ }
127
+
128
+ var exitCode int
129
+ bin .ExitFunc = func (code int ) {
130
+ exitCode = code
131
+ }
132
+ cl = channel .NewMockLocalChannel ()
133
+ mockChannel := cl .(* channel.MockLocalChannel )
134
+ actualCommands := make ([]string , 0 )
135
+ mockChannel .RunFunc = func (ctx context.Context , script , args string ) * spec.Response {
136
+ actualCommands = append (actualCommands , fmt .Sprintf ("%s %s" , script , args ))
137
+ if script == "echo" {
138
+ return spec .ReturnSuccess ("" )
139
+ }
140
+ return spec .ReturnFail (spec.CodeType {}, "" )
141
+ }
142
+ expectedCommands := []string {`grep -q "208.80.152.2 abc.com bbc.com #chaosblade" /etc/hosts` ,
143
+ `echo "208.80.152.2 abc.com bbc.com #chaosblade" >> /etc/hosts` }
144
+
145
+ startChangeDns (as .domain , as .ip )
146
+ if exitCode != 0 {
147
+ t .Errorf ("unexpected result: %d, expected result: %d" , exitCode , 0 )
148
+ }
149
+ if ! reflect .DeepEqual (expectedCommands , actualCommands ) {
150
+ t .Errorf ("unexpected commands: %+v, expected commands: %+v" , actualCommands , expectedCommands )
151
+ }
152
+ }
153
+
154
+ func Test_recoverChangeMultipleDns (t * testing.T ) {
155
+ type args struct {
156
+ domain string
157
+ ip string
158
+ }
159
+
160
+ as := & args {
161
+ domain : "abc.com,bbc.com" ,
162
+ ip : "208.80.152.2" ,
163
+ }
164
+
165
+ var exitCode int
166
+ bin .ExitFunc = func (code int ) {
167
+ exitCode = code
168
+ }
169
+ cl = channel .NewMockLocalChannel ()
170
+ mockChannel := cl .(* channel.MockLocalChannel )
171
+ actualCommands := make ([]string , 0 )
172
+ mockChannel .RunFunc = func (ctx context.Context , script , args string ) * spec.Response {
173
+ actualCommands = append (actualCommands , fmt .Sprintf ("%s %s" , script , args ))
174
+ return spec .ReturnSuccess ("" )
175
+ }
176
+ mockChannel .IsCommandAvailableFunc = func (commandName string ) bool {
177
+ return commandName == "cat"
178
+ }
179
+ expectedCommands := []string {`grep -q "208.80.152.2 abc.com bbc.com #chaosblade" /etc/hosts` ,
180
+ `cat /etc/hosts | grep -v "208.80.152.2 abc.com bbc.com #chaosblade" > /tmp/chaos-hosts.tmp && cat /tmp/chaos-hosts.tmp > /etc/hosts` ,
181
+ `rm -rf /tmp/chaos-hosts.tmp` }
182
+
183
+ recoverDns (as .domain , as .ip )
184
+ if exitCode != 0 {
185
+ t .Errorf ("unexpected result: %d, expected result: %d" , exitCode , 0 )
186
+ }
187
+ if ! reflect .DeepEqual (expectedCommands , actualCommands ) {
188
+ t .Errorf ("unexpected commands: %+v, expected commands: %+v" , actualCommands , expectedCommands )
189
+ }
190
+ }
0 commit comments