Skip to content

Commit

Permalink
Merge pull request tylertreat#59 from aidanhs/aphs-fix-tests
Browse files Browse the repository at this point in the history
Fix tests, add travis, no ips means all ips
  • Loading branch information
tylertreat authored Sep 16, 2017
2 parents 2d6610c + 92d7d27 commit b0d2c70
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: go

go:
- 1.8.x

script:
- go test -v ./...
9 changes: 9 additions & 0 deletions throttler/tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func addNetemRule(cfg *Config, c commander) error {

func addIptablesRules(cfg *Config, c commander) error {
var err error
if len(cfg.TargetIps) == 0 && len(cfg.TargetIps6) == 0 {
if err == nil {
err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps)
}
if err == nil {
err = addIptablesRulesForAddrs(cfg, c, ip6Tables, cfg.TargetIps6)
}
return err
}
if err == nil && len(cfg.TargetIps) > 0 {
err = addIptablesRulesForAddrs(cfg, c, ip4Tables, cfg.TargetIps)
}
Expand Down
33 changes: 26 additions & 7 deletions throttler/tc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *cmdRecorder) verifyCommands(t *testing.T, expected []string) {

for i, cmd := range expected {
if actual := r.commands[i]; actual != cmd {
t.Fatalf("Expected to see command `%s`, got `%s`", i, cmd, actual)
t.Fatalf("Expected to see command `%s`, got `%s`", cmd, actual)
}
}
}
Expand All @@ -76,14 +76,33 @@ func TestTcPacketLossSetup(t *testing.T) {
cfg.PacketLoss = 0.2
th.setup(&cfg)
r.verifyCommands(t, []string{
"sudo tc qdisc add dev eth1 handle 10: root htb",
"sudo tc qdisc add dev eth1 handle 10: root htb default 1",
"sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit",
"sudo tc class add dev eth1 parent 10:1 classid 10:10 htb rate 20000kbit",
"sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit",
"sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%",
"sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10",
})
}

func TestTcWildcardIps(t *testing.T) {
r := newCmdRecorder()
th := &tcThrottler{r}
cfg := defaultTestConfig
cfg.TargetIps = []string{}
cfg.TargetPorts = []string{}
cfg.TargetProtos = []string{}
cfg.PacketLoss = -1
th.setup(&cfg)
r.verifyCommands(t, []string{
"sudo tc qdisc add dev eth0 handle 10: root htb default 1",
"sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit",
"sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit",
"sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem",
"sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10",
"sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10",
})
}

func TestTcMultiplePortsAndIps(t *testing.T) {
r := newCmdRecorder()
th := &tcThrottler{r}
Expand All @@ -93,9 +112,9 @@ func TestTcMultiplePortsAndIps(t *testing.T) {
cfg.TargetProtos = []string{"tcp", "udp"}
th.setup(&cfg)
r.verifyCommands(t, []string{
"sudo tc qdisc add dev eth0 handle 10: root htb",
"sudo tc qdisc add dev eth0 handle 10: root htb default 1",
"sudo tc class add dev eth0 parent 10: classid 10:1 htb rate 20000kbit",
"sudo tc class add dev eth0 parent 10:1 classid 10:10 htb rate 20000kbit",
"sudo tc class add dev eth0 parent 10: classid 10:10 htb rate 1000000kbit",
"sudo tc qdisc add dev eth0 parent 10:10 handle 100: netem loss 0.10%",
"sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --match multiport --dports 80,8080 -d 1.1.1.1",
"sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p udp --match multiport --dports 80,8080 -d 1.1.1.1",
Expand All @@ -113,9 +132,9 @@ func TestTcMixedIPv6Setup(t *testing.T) {
cfg.TargetIps6 = []string{"2001:db8::1"}
th.setup(&cfg)
r.verifyCommands(t, []string{
"sudo tc qdisc add dev eth1 handle 10: root htb",
"sudo tc qdisc add dev eth1 handle 10: root htb default 1",
"sudo tc class add dev eth1 parent 10: classid 10:1 htb rate 20000kbit",
"sudo tc class add dev eth1 parent 10:1 classid 10:10 htb rate 20000kbit",
"sudo tc class add dev eth1 parent 10: classid 10:10 htb rate 1000000kbit",
"sudo tc qdisc add dev eth1 parent 10:10 handle 100: netem loss 0.20%",
"sudo iptables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 10.10.10.10",
"sudo ip6tables -A POSTROUTING -t mangle -j CLASSIFY --set-class 10:10 -p tcp --dport 80 -d 2001:db8::1",
Expand Down

0 comments on commit b0d2c70

Please sign in to comment.