Skip to content

Commit f756625

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

File tree

2 files changed

+111
-22
lines changed

2 files changed

+111
-22
lines changed

exec/bin/changedns/changedns.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23-
2423
"github.com/chaosblade-io/chaosblade-spec-go/channel"
2524
"github.com/chaosblade-io/chaosblade-spec-go/spec"
25+
"strings"
2626

2727
"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,13 +59,18 @@ var cl = channel.NewLocalChannel()
5759
// startChangeDns by the domain and ip
5860
func startChangeDns(domain, ip string) {
5961
ctx := context.Background()
60-
dnsPair := createDnsPair(domain, ip)
61-
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
62-
if response.Success {
63-
bin.PrintErrAndExit(fmt.Sprintf("%s has been exist", dnsPair))
64-
return
62+
domains := strings.Split(domain, sep)
63+
var dnsPairs string
64+
for _, v := range domains {
65+
dnsPair := createDnsPair(v, ip)
66+
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
67+
if response.Success {
68+
bin.PrintErrAndExit(fmt.Sprintf("%s has been exist", dnsPair))
69+
return
70+
}
71+
dnsPairs = dnsPairs + "\n" + dnsPair
6572
}
66-
response = cl.Run(ctx, "echo", fmt.Sprintf(`"%s" >> %s`, dnsPair, hosts))
73+
response := cl.Run(ctx, "echo", fmt.Sprintf(`"%s" >> %s`, dnsPairs, hosts))
6774
if !response.Success {
6875
bin.PrintErrAndExit(response.Err)
6976
return
@@ -74,22 +81,24 @@ func startChangeDns(domain, ip string) {
7481
// recoverDns
7582
func recoverDns(domain, ip string) {
7683
ctx := context.Background()
77-
dnsPair := createDnsPair(domain, ip)
78-
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
79-
if !response.Success {
80-
bin.PrintOutputAndExit("nothing to do")
81-
return
82-
}
84+
domains := strings.Split(domain, sep)
85+
for _, v := range domains {
86+
dnsPair := createDnsPair(v, ip)
87+
response := cl.Run(ctx, "grep", fmt.Sprintf(`-q "%s" %s`, dnsPair, hosts))
88+
if !response.Success {
89+
bin.PrintOutputAndExit("nothing to do")
90+
return
91+
}
8392

84-
if !cl.IsCommandAvailable("cat") {
85-
bin.PrintErrAndExit(spec.CommandCatNotFound.Msg)
86-
}
87-
88-
response = cl.Run(ctx, "cat", fmt.Sprintf(`%s | grep -v "%s" > %s && cat %s > %s`,
89-
hosts, dnsPair, tmpHosts, tmpHosts, hosts))
90-
if !response.Success {
91-
bin.PrintErrAndExit(response.Err)
92-
return
93+
if !cl.IsCommandAvailable("cat") {
94+
bin.PrintErrAndExit(spec.CommandCatNotFound.Msg)
95+
}
96+
response = cl.Run(ctx, "cat", fmt.Sprintf(`%s | grep -v "%s" > %s && cat %s > %s`,
97+
hosts, dnsPair, tmpHosts, tmpHosts, hosts))
98+
if !response.Success {
99+
bin.PrintErrAndExit(response.Err)
100+
return
101+
}
93102
}
94103
cl.Run(ctx, "rm", fmt.Sprintf(`-rf %s`, tmpHosts))
95104
}

exec/bin/changedns/changedns_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,83 @@ 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 #chaosblade" /etc/hosts`,
143+
`grep -q "208.80.152.2 bbc.com #chaosblade" /etc/hosts`,
144+
`echo "
145+
208.80.152.2 abc.com #chaosblade
146+
208.80.152.2 bbc.com #chaosblade" >> /etc/hosts`}
147+
148+
startChangeDns(as.domain, as.ip)
149+
if exitCode != 0 {
150+
t.Errorf("unexpected result: %d, expected result: %d", exitCode, 0)
151+
}
152+
if !reflect.DeepEqual(expectedCommands, actualCommands) {
153+
t.Errorf("unexpected commands: %+v, expected commands: %+v", actualCommands, expectedCommands)
154+
}
155+
}
156+
157+
func Test_recoverChangeMultipleDns(t *testing.T) {
158+
type args struct {
159+
domain string
160+
ip string
161+
}
162+
163+
as := &args{
164+
domain: "abc.com,bbc.com",
165+
ip: "208.80.152.2",
166+
}
167+
168+
var exitCode int
169+
bin.ExitFunc = func(code int) {
170+
exitCode = code
171+
}
172+
cl = channel.NewMockLocalChannel()
173+
mockChannel := cl.(*channel.MockLocalChannel)
174+
actualCommands := make([]string, 0)
175+
mockChannel.RunFunc = func(ctx context.Context, script, args string) *spec.Response {
176+
actualCommands = append(actualCommands, fmt.Sprintf("%s %s", script, args))
177+
return spec.ReturnSuccess("")
178+
}
179+
mockChannel.IsCommandAvailableFunc = func(commandName string) bool {
180+
return commandName == "cat"
181+
}
182+
expectedCommands := []string{`grep -q "208.80.152.2 abc.com #chaosblade" /etc/hosts`,
183+
`cat /etc/hosts | grep -v "208.80.152.2 abc.com #chaosblade" > /tmp/chaos-hosts.tmp && cat /tmp/chaos-hosts.tmp > /etc/hosts`,
184+
`grep -q "208.80.152.2 bbc.com #chaosblade" /etc/hosts`,
185+
`cat /etc/hosts | grep -v "208.80.152.2 bbc.com #chaosblade" > /tmp/chaos-hosts.tmp && cat /tmp/chaos-hosts.tmp > /etc/hosts`,
186+
`rm -rf /tmp/chaos-hosts.tmp`}
187+
188+
recoverDns(as.domain, as.ip)
189+
if exitCode != 0 {
190+
t.Errorf("unexpected result: %d, expected result: %d", exitCode, 0)
191+
}
192+
if !reflect.DeepEqual(expectedCommands, actualCommands) {
193+
t.Errorf("unexpected commands: %+v, expected commands: %+v", actualCommands, expectedCommands)
194+
}
195+
}

0 commit comments

Comments
 (0)