Browse code

Make pkg/util/ovs and pkg/util/ipcmd tests check the passed-in command line

Dan Winship authored on 2016/08/03 23:34:27
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package ipcmd
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"strings"
5 6
 	"testing"
6 7
 
7 8
 	"k8s.io/kubernetes/pkg/util/exec"
... ...
@@ -27,19 +28,31 @@ func missingSetup() *exec.FakeExec {
27 27
 	}
28 28
 }
29 29
 
30
-func addTestResult(fexec *exec.FakeExec, command string, output string, err error) {
30
+func addTestResult(t *testing.T, fexec *exec.FakeExec, command string, output string, err error) {
31 31
 	fcmd := exec.FakeCmd{
32 32
 		CombinedOutputScript: []exec.FakeCombinedOutputAction{
33 33
 			func() ([]byte, error) { return []byte(output), err },
34 34
 		},
35 35
 	}
36 36
 	fexec.CommandScript = append(fexec.CommandScript,
37
-		func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) })
37
+		func(cmd string, args ...string) exec.Cmd {
38
+			execCommand := strings.Join(append([]string{cmd}, args...), " ")
39
+			if execCommand != command {
40
+				t.Fatalf("Unexpected command: wanted %q got %q", command, execCommand)
41
+			}
42
+			return exec.InitFakeCmd(&fcmd, cmd, args...)
43
+		})
44
+}
45
+
46
+func ensureTestResults(t *testing.T, fexec *exec.FakeExec) {
47
+	if fexec.CommandCalls != len(fexec.CommandScript) {
48
+		t.Fatalf("Only used %d of %d expected commands", fexec.CommandCalls, len(fexec.CommandScript))
49
+	}
38 50
 }
39 51
 
40 52
 func TestGetAddresses(t *testing.T) {
41 53
 	fexec := normalSetup()
42
-	addTestResult(fexec, "/sbin/ip addr show dev lo", `1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
54
+	addTestResult(t, fexec, "/sbin/ip addr show dev lo", `1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
43 55
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
44 56
     inet 127.0.0.1/8 scope host lo
45 57
        valid_lft forever preferred_lft forever
... ...
@@ -61,8 +74,9 @@ func TestGetAddresses(t *testing.T) {
61 61
 	if err != nil {
62 62
 		t.Fatalf("Transaction unexpectedly returned error: %v", err)
63 63
 	}
64
+	ensureTestResults(t, fexec)
64 65
 
65
-	addTestResult(fexec, "/sbin/ip addr show dev eth0", `2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
66
+	addTestResult(t, fexec, "/sbin/ip addr show dev eth0", `2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
66 67
     link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
67 68
     inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
68 69
        valid_lft 81296sec preferred_lft 81296sec
... ...
@@ -84,8 +98,9 @@ func TestGetAddresses(t *testing.T) {
84 84
 	if err != nil {
85 85
 		t.Fatalf("Transaction unexpectedly returned error: %v", err)
86 86
 	}
87
+	ensureTestResults(t, fexec)
87 88
 
88
-	addTestResult(fexec, "/sbin/ip addr show dev wlan0", "", fmt.Errorf("Device \"%s\" does not exist", "wlan0"))
89
+	addTestResult(t, fexec, "/sbin/ip addr show dev wlan0", "", fmt.Errorf("Device \"%s\" does not exist", "wlan0"))
89 90
 	itx = NewTransaction(fexec, "wlan0")
90 91
 	addrs, err = itx.GetAddresses()
91 92
 	if err == nil {
... ...
@@ -95,6 +110,7 @@ func TestGetAddresses(t *testing.T) {
95 95
 	if err == nil {
96 96
 		t.Fatalf("Transaction unexpectedly returned no error")
97 97
 	}
98
+	ensureTestResults(t, fexec)
98 99
 }
99 100
 
100 101
 func TestGetRoutes(t *testing.T) {
... ...
@@ -104,7 +120,7 @@ func TestGetRoutes(t *testing.T) {
104 104
 		l3 = "192.168.1.0/24  proto kernel  scope link  src 192.168.1.15 "
105 105
 	)
106 106
 	fexec := normalSetup()
107
-	addTestResult(fexec, "/sbin/ip route show dev wlp3s0", l1+"\n"+l2+"\n"+l3+"\n", nil)
107
+	addTestResult(t, fexec, "/sbin/ip route show dev wlp3s0", l1+"\n"+l2+"\n"+l3+"\n", nil)
108 108
 	itx := NewTransaction(fexec, "wlp3s0")
109 109
 	routes, err := itx.GetRoutes()
110 110
 	if err != nil {
... ...
@@ -126,8 +142,9 @@ func TestGetRoutes(t *testing.T) {
126 126
 	if err != nil {
127 127
 		t.Fatalf("Transaction unexpectedly returned error: %v", err)
128 128
 	}
129
+	ensureTestResults(t, fexec)
129 130
 
130
-	addTestResult(fexec, "/sbin/ip route show dev wlan0", "", fmt.Errorf("Device \"%s\" does not exist", "wlan0"))
131
+	addTestResult(t, fexec, "/sbin/ip route show dev wlan0", "", fmt.Errorf("Device \"%s\" does not exist", "wlan0"))
131 132
 	itx = NewTransaction(fexec, "wlan0")
132 133
 	routes, err = itx.GetRoutes()
133 134
 	if err == nil {
... ...
@@ -137,20 +154,22 @@ func TestGetRoutes(t *testing.T) {
137 137
 	if err == nil {
138 138
 		t.Fatalf("Transaction unexpectedly returned no error")
139 139
 	}
140
+	ensureTestResults(t, fexec)
140 141
 }
141 142
 
142 143
 func TestErrorHandling(t *testing.T) {
143 144
 	fexec := normalSetup()
144
-	addTestResult(fexec, "/sbin/ip link del dummy0", "", fmt.Errorf("Device \"%s\" does not exist", "dummy0"))
145
+	addTestResult(t, fexec, "/sbin/ip link del dummy0", "", fmt.Errorf("Device \"%s\" does not exist", "dummy0"))
145 146
 	itx := NewTransaction(fexec, "dummy0")
146 147
 	itx.DeleteLink()
147 148
 	err := itx.EndTransaction()
148 149
 	if err == nil {
149 150
 		t.Fatalf("Failed to get expected error")
150 151
 	}
152
+	ensureTestResults(t, fexec)
151 153
 
152
-	addTestResult(fexec, "/sbin/ip link del dummy0", "", fmt.Errorf("Device \"%s\" does not exist", "dummy0"))
153
-	addTestResult(fexec, "/sbin/ip link add dummy0 type dummy", "", nil)
154
+	addTestResult(t, fexec, "/sbin/ip link del dummy0", "", fmt.Errorf("Device \"%s\" does not exist", "dummy0"))
155
+	addTestResult(t, fexec, "/sbin/ip link add dummy0 type dummy", "", nil)
154 156
 	itx = NewTransaction(fexec, "dummy0")
155 157
 	itx.DeleteLink()
156 158
 	itx.IgnoreError()
... ...
@@ -159,8 +178,9 @@ func TestErrorHandling(t *testing.T) {
159 159
 	if err != nil {
160 160
 		t.Fatalf("Unexpectedly got error after IgnoreError(): %v", err)
161 161
 	}
162
+	ensureTestResults(t, fexec)
162 163
 
163
-	addTestResult(fexec, "/sbin/ip link add dummy0 type dummy", "", fmt.Errorf("RTNETLINK answers: Operation not permitted"))
164
+	addTestResult(t, fexec, "/sbin/ip link add dummy0 type dummy", "", fmt.Errorf("RTNETLINK answers: Operation not permitted"))
164 165
 	// other commands do not get run due to previous error
165 166
 	itx = NewTransaction(fexec, "dummy0")
166 167
 	itx.AddLink("type", "dummy")
... ...
@@ -170,6 +190,7 @@ func TestErrorHandling(t *testing.T) {
170 170
 	if err == nil {
171 171
 		t.Fatalf("Failed to get expected error")
172 172
 	}
173
+	ensureTestResults(t, fexec)
173 174
 }
174 175
 
175 176
 func TestIPMissing(t *testing.T) {
... ...
@@ -28,20 +28,32 @@ func missingSetup() *exec.FakeExec {
28 28
 	}
29 29
 }
30 30
 
31
-func addTestResult(fexec *exec.FakeExec, command string, output string, err error) {
31
+func addTestResult(t *testing.T, fexec *exec.FakeExec, command string, output string, err error) {
32 32
 	fcmd := exec.FakeCmd{
33 33
 		CombinedOutputScript: []exec.FakeCombinedOutputAction{
34 34
 			func() ([]byte, error) { return []byte(output), err },
35 35
 		},
36 36
 	}
37 37
 	fexec.CommandScript = append(fexec.CommandScript,
38
-		func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) })
38
+		func(cmd string, args ...string) exec.Cmd {
39
+			execCommand := strings.Join(append([]string{cmd}, args...), " ")
40
+			if execCommand != command {
41
+				t.Fatalf("Unexpected command: wanted %q got %q", command, execCommand)
42
+			}
43
+			return exec.InitFakeCmd(&fcmd, cmd, args...)
44
+		})
45
+}
46
+
47
+func ensureTestResults(t *testing.T, fexec *exec.FakeExec) {
48
+	if fexec.CommandCalls != len(fexec.CommandScript) {
49
+		t.Fatalf("Only used %d of %d expected commands", fexec.CommandCalls, len(fexec.CommandScript))
50
+	}
39 51
 }
40 52
 
41 53
 func TestTransactionSuccess(t *testing.T) {
42 54
 	fexec := normalSetup()
43
-	addTestResult(fexec, "/usr/bin/ovs-ofctl -O OpenFlow13 add-flow br0 flow1", "", nil)
44
-	addTestResult(fexec, "/usr/bin/ovs-ofctl -O OpenFlow13 add-flow br0 flow2", "", nil)
55
+	addTestResult(t, fexec, "/sbin/ovs-ofctl -O OpenFlow13 add-flow br0 flow1", "", nil)
56
+	addTestResult(t, fexec, "/sbin/ovs-ofctl -O OpenFlow13 add-flow br0 flow2", "", nil)
45 57
 
46 58
 	otx := NewTransaction(fexec, "br0")
47 59
 	otx.AddFlow("flow1")
... ...
@@ -50,11 +62,13 @@ func TestTransactionSuccess(t *testing.T) {
50 50
 	if err != nil {
51 51
 		t.Fatalf("Unexpected error from command: %v", err)
52 52
 	}
53
+
54
+	ensureTestResults(t, fexec)
53 55
 }
54 56
 
55 57
 func TestTransactionFailure(t *testing.T) {
56 58
 	fexec := normalSetup()
57
-	addTestResult(fexec, "/usr/bin/ovs-ofctl -O OpenFlow13 add-flow br0 flow1", "", fmt.Errorf("Something bad happened"))
59
+	addTestResult(t, fexec, "/sbin/ovs-ofctl -O OpenFlow13 add-flow br0 flow1", "", fmt.Errorf("Something bad happened"))
58 60
 
59 61
 	otx := NewTransaction(fexec, "br0")
60 62
 	otx.AddFlow("flow1")
... ...
@@ -63,11 +77,13 @@ func TestTransactionFailure(t *testing.T) {
63 63
 	if err == nil {
64 64
 		t.Fatalf("Failed to get expected error")
65 65
 	}
66
+
67
+	ensureTestResults(t, fexec)
66 68
 }
67 69
 
68 70
 func TestDumpFlows(t *testing.T) {
69 71
 	fexec := normalSetup()
70
-	addTestResult(fexec, "/usr/bin/ovs-ofctl -O OpenFlow13 dump-flows br0", `OFPST_FLOW reply (OF1.3) (xid=0x2):
72
+	addTestResult(t, fexec, "/sbin/ovs-ofctl -O OpenFlow13 dump-flows br0", `OFPST_FLOW reply (OF1.3) (xid=0x2):
71 73
  cookie=0x0, duration=13271.779s, table=0, n_packets=0, n_bytes=0, priority=100,ip,nw_dst=192.168.1.0/24 actions=set_field:0a:7b:e6:19:11:cf->eth_dst,output:2
72 74
  cookie=0x0, duration=13271.776s, table=0, n_packets=1, n_bytes=42, priority=100,arp,arp_tpa=192.168.1.0/24 actions=set_field:10.19.17.34->tun_dst,output:1
73 75
  cookie=0x3, duration=13267.277s, table=0, n_packets=788539827, n_bytes=506520926762, priority=100,ip,nw_dst=192.168.2.2 actions=output:3
... ...
@@ -90,6 +106,8 @@ func TestDumpFlows(t *testing.T) {
90 90
 	if len(flows) != 7 {
91 91
 		t.Fatalf("Unexpected number of flows (%d)", len(flows))
92 92
 	}
93
+
94
+	ensureTestResults(t, fexec)
93 95
 }
94 96
 
95 97
 func TestOVSMissing(t *testing.T) {