Skip to content

Commit dc2d58f

Browse files
committed
network dns support multiple domain
Signed-off-by: sanxun0325 <bbz17640380550@163.com>
1 parent 318c52d commit dc2d58f

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

exec/bin/changedns/changedns.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
"strings"
2324

25+
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
2426
"github.com/chaosblade-io/chaosblade-spec-go/channel"
2527
"github.com/chaosblade-io/chaosblade-spec-go/spec"
26-
27-
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
2828
)
2929

30+
const sep = ","
31+
3032
var dnsDomain, dnsIp string
3133
var changeDnsStart, changeDnsStop bool
3234

@@ -57,6 +59,7 @@ var cl = channel.NewLocalChannel()
5759
// startChangeDns by the domain and ip
5860
func startChangeDns(domain, ip string) {
5961
ctx := context.Background()
62+
domain = strings.ReplaceAll(domain, sep, " ")
6063
dnsPair := createDnsPair(domain, ip)
6164
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
6265
if response.Success {
@@ -74,6 +77,7 @@ func startChangeDns(domain, ip string) {
7477
// recoverDns
7578
func recoverDns(domain, ip string) {
7679
ctx := context.Background()
80+
domain = strings.ReplaceAll(domain, sep, " ")
7781
dnsPair := createDnsPair(domain, ip)
7882
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
7983
if !response.Success {

exec/bin/changedns/changedns_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,78 @@ func Test_recoverDns_failed(t *testing.T) {
113113
t.Errorf("unexpected commands: %+v, expected commands: %+v", actualCommands, expectedCommands)
114114
}
115115
}
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

Comments
 (0)