Skip to content

Commit

Permalink
refactor: modify device to interface in network experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
xcaspar committed Apr 11, 2019
1 parent 5e5289d commit 1e594d3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 89 deletions.
8 changes: 4 additions & 4 deletions cli/command_test.go
Expand Up @@ -23,20 +23,20 @@ func Test_baseCommand_recordExpModel(t *testing.T) {
expect expect
}{
{
input{"blade create docker network delay", "--time 3000 --device eth0"},
input{"blade create docker network delay", "--time 3000 --interface eth0"},
expect{&data.ExperimentModel{
Command: "docker",
SubCommand: "network delay",
Flag: "--time 3000 --device eth0",
Flag: "--time 3000 --interface eth0",
Status: "Created",
}, false},
},
{
input{"blade create network delay", "--time 3000 --device eth0"},
input{"blade create network delay", "--time 3000 --interface eth0"},
expect{&data.ExperimentModel{
Command: "network",
SubCommand: "delay",
Flag: "--time 3000 --device eth0",
Flag: "--time 3000 --interface eth0",
Status: "Created",
}, false},
},
Expand Down
8 changes: 4 additions & 4 deletions cli/destroy_test.go
Expand Up @@ -19,19 +19,19 @@ func Test_convertCommandModel(t *testing.T) {
expect expect
}{
{
input{"network delay", "docker", "--time 3000 --device eth0"},
input{"network delay", "docker", "--time 3000 --interface eth0"},
expect{&exec.ExpModel{
Target: "docker",
ActionName: "network delay",
ActionFlags: map[string]string{"time": "3000", "device": "eth0"},
ActionFlags: map[string]string{"time": "3000", "interface": "eth0"},
}},
},
{
input{"delay", "network", "--time 3000 --device eth0"},
input{"delay", "network", "--time 3000 --interface eth0"},
expect{&exec.ExpModel{
Target: "network",
ActionName: "delay",
ActionFlags: map[string]string{"time": "3000", "device": "eth0"},
ActionFlags: map[string]string{"time": "3000", "interface": "eth0"},
}},
},
}
Expand Down
2 changes: 1 addition & 1 deletion cli/query.go
Expand Up @@ -27,5 +27,5 @@ func (qc *QueryCommand) Init() {
}

func (qc *QueryCommand) queryExample() string {
return `query network device`
return `query network interface`
}
4 changes: 2 additions & 2 deletions cli/query_network_test.go
Expand Up @@ -15,7 +15,7 @@ func TestQueryNetworkCommand_queryNetworkInfo(t *testing.T) {
}
qnc := &QueryNetworkCommand{
}
testQueryNetworkDevice(t, command, qnc)
testQueryNetworkInterface(t, command, qnc)

testQueryNetworkUnknownArg(t, command, qnc)
}
Expand All @@ -33,7 +33,7 @@ func testQueryNetworkUnknownArg(t *testing.T, command *cobra.Command, qnc *Query
}
}

func testQueryNetworkDevice(t *testing.T, command *cobra.Command, qnc *QueryNetworkCommand) {
func testQueryNetworkInterface(t *testing.T, command *cobra.Command, qnc *QueryNetworkCommand) {
buffer := &bytes.Buffer{}
command.SetOutput(buffer)

Expand Down
56 changes: 28 additions & 28 deletions exec/os/bin/delaynetwork.go
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/chaosblade-io/chaosblade/transport"
)

var delayNetDevice, delayNetTime, delayNetOffset, delayLocalPort, delayRemotePort, delayExcludePort string
var delayNetInterface, delayNetTime, delayNetOffset, delayLocalPort, delayRemotePort, delayExcludePort string
var delayNetStart, delayNetStop bool

func main() {
flag.StringVar(&delayNetDevice, "device", "", "network device")
flag.StringVar(&delayNetInterface, "interface", "", "network interface")
flag.StringVar(&delayNetTime, "time", "", "delay time")
flag.StringVar(&delayNetOffset, "offset", "", "delay offset")
flag.StringVar(&delayLocalPort, "local-port", "", "local port")
Expand All @@ -22,105 +22,105 @@ func main() {
flag.BoolVar(&delayNetStop, "stop", false, "stop delay")
flag.Parse()

if delayNetDevice == "" {
printErrAndExit("less device arg")
if delayNetInterface == "" {
printErrAndExit("less --interface flag")
}

if delayNetStart {
startDelayNet(delayNetDevice, delayNetTime, delayNetOffset, delayLocalPort, delayRemotePort, delayExcludePort)
startDelayNet(delayNetInterface, delayNetTime, delayNetOffset, delayLocalPort, delayRemotePort, delayExcludePort)
} else if delayNetStop {
stopDelayNet(delayNetDevice)
stopDelayNet(delayNetInterface)
} else {
printErrAndExit("less --start or --stop flag")
}
}

func startDelayNet(device, time, offset, localPort, remotePort, excludePort string) {
func startDelayNet(netInterface, time, offset, localPort, remotePort, excludePort string) {
channel := exec.NewLocalChannel()
ctx := context.Background()
// assert localPort and remotePort
if localPort == "" && remotePort == "" && excludePort == "" {
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root netem delay %sms %sms`, device, time, offset))
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root netem delay %sms %sms`, netInterface, time, offset))
if !response.Success {
printErrAndExit(response.Err)
}
printOutputAndExit(response.Result.(string))
return
}
response := addQdiscForDelay(channel, ctx, device, time, offset)
response := addQdiscForDelay(channel, ctx, netInterface, time, offset)
if localPort == "" && remotePort == "" && excludePort != "" {
response = addExcludePortFilterForDelay(excludePort, device, response, channel, ctx)
response = addExcludePortFilterForDelay(excludePort, netInterface, response, channel, ctx)
printOutputAndExit(response.Result.(string))
return
}
response = addLocalOrRemotePortForDelay(localPort, response, channel, ctx, device, remotePort)
response = addLocalOrRemotePortForDelay(localPort, response, channel, ctx, netInterface, remotePort)
printOutputAndExit(response.Result.(string))
}

// addLocalOrRemotePortForDelay
func addLocalOrRemotePortForDelay(localPort string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context, device string, remotePort string) *transport.Response {
func addLocalOrRemotePortForDelay(localPort string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context, netInterface string, remotePort string) *transport.Response {
// local port 0
if localPort != "" {
response = channel.Run(ctx, "tc",
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport eq %s)" flowid 1:4`, device, localPort))
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport eq %s)" flowid 1:4`, netInterface, localPort))
if !response.Success {
stopDelayNet(device)
stopDelayNet(netInterface)
printErrAndExit(response.Err)
}
}
// remote port 2
if remotePort != "" {
response = channel.Run(ctx, "tc",
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 2 layer transport eq %s)" flowid 1:4`, device, remotePort))
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 2 layer transport eq %s)" flowid 1:4`, netInterface, remotePort))
if !response.Success {
stopDelayNet(device)
stopDelayNet(netInterface)
printErrAndExit(response.Err)
}
}
return response
}

// addExcludePortFilterForDelay
func addExcludePortFilterForDelay(excludePort string, device string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context) *transport.Response {
func addExcludePortFilterForDelay(excludePort string, netInterface string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context) *transport.Response {
response = channel.Run(ctx, "tc",
fmt.Sprintf(
`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport gt 0) and cmp(u16 at 0 layer transport lt %s)" flowid 1:4`,
device, excludePort))
netInterface, excludePort))
if !response.Success {
stopDelayNet(device)
stopDelayNet(netInterface)
printErrAndExit(response.Err)
}
response = channel.Run(ctx, "tc",
fmt.Sprintf(
`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport gt %s) and cmp(u16 at 0 layer transport lt 65535)" flowid 1:4`,
device, excludePort))
netInterface, excludePort))
if !response.Success {
stopDelayNet(device)
stopDelayNet(netInterface)
printErrAndExit(response.Err)
}
return response
}

// addQdiscForDelay
func addQdiscForDelay(channel *exec.LocalChannel, ctx context.Context, device string, time string, offset string) *transport.Response {
func addQdiscForDelay(channel *exec.LocalChannel, ctx context.Context, netInterface string, time string, offset string) *transport.Response {
// add tc filter for delay specify port
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root handle 1: prio bands 4`, device))
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root handle 1: prio bands 4`, netInterface))
if !response.Success {
printErrAndExit(response.Err)
}
response = channel.Run(ctx, "tc",
fmt.Sprintf(`qdisc add dev %s parent 1:4 handle 40: netem delay %sms %sms`, device, time, offset))
fmt.Sprintf(`qdisc add dev %s parent 1:4 handle 40: netem delay %sms %sms`, netInterface, time, offset))
if !response.Success {
stopDelayNet(device)
stopDelayNet(netInterface)
printErrAndExit(response.Err)
}
return response
}

// stopDelayNet, no need to add os.Exit
func stopDelayNet(device string) {
func stopDelayNet(netInterface string) {
channel := exec.NewLocalChannel()
ctx := context.Background()
channel.Run(ctx, "tc", fmt.Sprintf(`filter del dev %s parent 1: prio 4 basic`, device))
channel.Run(ctx, "tc", fmt.Sprintf(`qdisc del dev %s root`, device))
channel.Run(ctx, "tc", fmt.Sprintf(`filter del dev %s parent 1: prio 4 basic`, netInterface))
channel.Run(ctx, "tc", fmt.Sprintf(`qdisc del dev %s root`, netInterface))
}
56 changes: 28 additions & 28 deletions exec/os/bin/lossnetwork.go
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/chaosblade-io/chaosblade/transport"
)

var lossNetDevice, lossNetPercent, lossNetLocalPort, lossNetRemotePort, lossNetExcludePort string
var lossNetInterface, lossNetPercent, lossNetLocalPort, lossNetRemotePort, lossNetExcludePort string
var lossNetStart, lossNetStop bool

func main() {
flag.StringVar(&lossNetDevice, "device", "", "network device")
flag.StringVar(&lossNetInterface, "interface", "", "network interface")
flag.StringVar(&lossNetPercent, "percent", "", "loss percent")
flag.StringVar(&lossNetLocalPort, "local-port", "", "local port")
flag.StringVar(&lossNetRemotePort, "remote-port", "", "remote port")
Expand All @@ -25,101 +25,101 @@ func main() {
printErrAndExit("must add --start or --stop flag")
}
if lossNetStart {
startLossNet(lossNetDevice, lossNetPercent, lossNetLocalPort, lossNetRemotePort, lossNetExcludePort)
startLossNet(lossNetInterface, lossNetPercent, lossNetLocalPort, lossNetRemotePort, lossNetExcludePort)
} else if lossNetStop {
stopLossNet(lossNetDevice)
stopLossNet(lossNetInterface)
} else {
printErrAndExit("less --start or --stop flag")
}
}

func startLossNet(device, percent, localPort, remotePort, excludePort string) {
func startLossNet(netInterface, percent, localPort, remotePort, excludePort string) {
// invoke stop
stopLossNet(device)
stopLossNet(netInterface)
channel := exec.NewLocalChannel()
ctx := context.Background()

if localPort == "" && remotePort == "" && excludePort == "" {
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root netem loss %s%%`, device, percent))
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root netem loss %s%%`, netInterface, percent))
if !response.Success {
printErrAndExit(response.Err)
}
printOutputAndExit(response.Result.(string))
return
}
response := addQdiscForLoss(channel, ctx, device, percent)
response := addQdiscForLoss(channel, ctx, netInterface, percent)
if localPort == "" && remotePort == "" && excludePort != "" {
response = addExcludePortFilterForLoss(excludePort, device, response, channel, ctx)
response = addExcludePortFilterForLoss(excludePort, netInterface, response, channel, ctx)
printOutputAndExit(response.Result.(string))
return
}
response = addLocalOrRemotePortFilterForLoss(localPort, response, channel, ctx, device, remotePort)
response = addLocalOrRemotePortFilterForLoss(localPort, response, channel, ctx, netInterface, remotePort)
printOutputAndExit(response.Result.(string))
}

// addLocalOrRemotePortFilterForLoss
func addLocalOrRemotePortFilterForLoss(localPort string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context, device string, remotePort string) *transport.Response {
func addLocalOrRemotePortFilterForLoss(localPort string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context, netInterface string, remotePort string) *transport.Response {
if localPort != "" {
response = channel.Run(ctx, "tc",
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport eq %s)" flowid 1:4`, device, localPort))
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport eq %s)" flowid 1:4`, netInterface, localPort))
if !response.Success {
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
}
if remotePort != "" {
response = channel.Run(ctx, "tc",
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 2 layer transport eq %s)" flowid 1:4`, device, remotePort))
fmt.Sprintf(`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 2 layer transport eq %s)" flowid 1:4`, netInterface, remotePort))
if !response.Success {
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
}
return response
}

// addExcludePortFilterForLoss
func addExcludePortFilterForLoss(excludePort string, device string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context) *transport.Response {
func addExcludePortFilterForLoss(excludePort string, netInterface string, response *transport.Response, channel *exec.LocalChannel, ctx context.Context) *transport.Response {
response = channel.Run(ctx, "tc",
fmt.Sprintf(
`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport gt 0) and cmp(u16 at 0 layer transport lt %s)" flowid 1:4`,
device, excludePort))
netInterface, excludePort))
if !response.Success {
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
response = channel.Run(ctx, "tc",
fmt.Sprintf(
`filter add dev %s parent 1: protocol ip prio 4 basic match "cmp(u16 at 0 layer transport gt %s) and cmp(u16 at 0 layer transport lt 65535)" flowid 1:4`,
device, excludePort))
netInterface, excludePort))
if !response.Success {
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
return response
}

// addQdiscForLoss
func addQdiscForLoss(channel *exec.LocalChannel, ctx context.Context, device string, percent string) *transport.Response {
func addQdiscForLoss(channel *exec.LocalChannel, ctx context.Context, netInterface string, percent string) *transport.Response {
// invoke tc qdisc add dev ${networkPort} root handle 1: prio bands 4
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root handle 1: prio bands 4`, device))
response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root handle 1: prio bands 4`, netInterface))
if !response.Success {
// invoke stop
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
response = channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s parent 1:4 handle 40: netem loss %s%%`, device, percent))
response = channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s parent 1:4 handle 40: netem loss %s%%`, netInterface, percent))
if !response.Success {
// invoke stop
stopLossNet(device)
stopLossNet(netInterface)
printErrAndExit(response.Err)
}
return response
}

func stopLossNet(device string) {
func stopLossNet(netInterface string) {
channel := exec.NewLocalChannel()
ctx := context.Background()
channel.Run(ctx, "tc", fmt.Sprintf(`filter del dev %s parent 1: prio 4 basic`, device))
channel.Run(ctx, "tc", fmt.Sprintf(`qdisc del dev %s root`, device))
channel.Run(ctx, "tc", fmt.Sprintf(`filter del dev %s parent 1: prio 4 basic`, netInterface))
channel.Run(ctx, "tc", fmt.Sprintf(`qdisc del dev %s root`, netInterface))
}
2 changes: 1 addition & 1 deletion exec/os/network.go
Expand Up @@ -18,7 +18,7 @@ func (*NetworkCommandSpec) LongDesc() string {
}

func (*NetworkCommandSpec) Example() string {
return "network delay --device eth0 --time 3000"
return "network delay --interface eth0 --time 3000"
}

func (*NetworkCommandSpec) Actions() []exec.ExpActionCommandSpec {
Expand Down

0 comments on commit 1e594d3

Please sign in to comment.