File: cli_test.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (251 lines) | stat: -rw-r--r-- 6,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main

import (
	"bytes"
	"context"
	"fmt"
	"os/exec"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/ansible/receptor/tests/utils"
)

func ConfirmListening(pid int, proto string) (bool, error) {
	out := bytes.Buffer{}
	cmd := exec.Command("lsof", "-tap", fmt.Sprint(pid), "-i", proto)
	cmd.Stdout = &out
	cmd.Run()

	if strings.Contains(out.String(), fmt.Sprint(pid)) {
		return true, nil
	}

	return false, nil
}

func TestHelp(t *testing.T) {
	t.Parallel()
	cmd := exec.Command("receptor", "--help")
	if err := cmd.Run(); err != nil {
		t.Fatal(err)
	}
}

func TestListeners(t *testing.T) {
	testTable := []struct {
		listener    string
		listenProto string
	}{
		{"--tcp-listener", "TCP"},
		{"--ws-listener", "TCP"},
		{"--udp-listener", "UDP"},
	}
	for _, data := range testTable {
		listener := data.listener
		listenProto := data.listenProto
		t.Run(listener, func(t *testing.T) {
			receptorStdOut := bytes.Buffer{}
			cmd := exec.Command("receptor", "--node", "id=test", listener, "port=0")
			cmd.Stdout = &receptorStdOut
			err := cmd.Start()
			if err != nil {
				t.Fatal(err)
			}
			defer cmd.Process.Wait()
			defer cmd.Process.Kill()

			ctx1, cancel1 := context.WithTimeout(context.Background(), 2*time.Second)
			defer cancel1()

			success, err := utils.CheckUntilTimeoutWithErr(ctx1, 10*time.Millisecond, func() (bool, error) {
				return ConfirmListening(cmd.Process.Pid, listenProto)
			})
			if err != nil {
				t.Fatal(err)
			}
			if !success {
				t.Fatalf("Timed out while waiting for backend to start:\n%s", receptorStdOut.String())
			}
		})
	}
}

func TestSSLListeners(t *testing.T) {
	testTable := []struct {
		listener string
	}{
		{"--tcp-listener"},
		{"--ws-listener"},
	}
	for _, data := range testTable {
		listener := data.listener
		t.Run(listener, func(t *testing.T) {
			key, crt, err := utils.GenerateCert("test", "test", []string{"test"}, []string{"test"})
			if err != nil {
				t.Fatal(err)
			}

			receptorStdOut := bytes.Buffer{}
			port, err := utils.GetFreeTCPPort()
			if err != nil {
				t.Fatal(err)
			}
			cmd := exec.Command("receptor", "--node", "id=test", "--tls-server", "name=server-tls", fmt.Sprintf("cert=%s", crt), fmt.Sprintf("key=%s", key), listener, fmt.Sprintf("port=%d", port), "tls=server-tls")
			cmd.Stdout = &receptorStdOut
			err = cmd.Start()
			if err != nil {
				t.Fatal(err)
			}
			defer cmd.Process.Wait()
			defer cmd.Process.Kill()

			checkFunc := func() bool {
				opensslStdOut := bytes.Buffer{}
				opensslStdIn := bytes.Buffer{}
				opensslCmd := exec.Command("openssl", "s_client", "-connect", "localhost:"+strconv.Itoa(port))
				opensslCmd.Stdin = &opensslStdIn
				opensslCmd.Stdout = &opensslStdOut
				err = opensslCmd.Run()

				return err == nil
			}

			ctx1, cancel1 := context.WithTimeout(context.Background(), 2*time.Second)
			defer cancel1()

			success := utils.CheckUntilTimeout(ctx1, 10*time.Millisecond, checkFunc)
			if !success {
				t.Fatalf("Timed out while waiting for tls backend to start:\n%s", receptorStdOut.String())
			}
		})
	}
}

func TestNegativeCost(t *testing.T) {
	testTable := []struct {
		listener string
	}{
		{"--tcp-listener"},
		{"--ws-listener"},
		{"--udp-listener"},
	}
	for _, data := range testTable {
		listener := data.listener
		t.Run(listener, func(t *testing.T) {
			receptorStdOut := bytes.Buffer{}
			cmd := exec.Command("receptor", "--node", "id=test", listener, "port=0", "cost=-1")
			cmd.Stdout = &receptorStdOut
			err := cmd.Start()
			if err != nil {
				t.Fatal(err)
			}

			// Wait for our process to hopefully run and quit
			time.Sleep(1500 * time.Millisecond)

			cmd.Process.Kill()
			cmd.Wait()
			if !strings.Contains(receptorStdOut.String(), "Error: connection cost must be positive") {
				t.Fatalf("Expected stdout: Error: connection cost must be positive, actual stdout: %v", receptorStdOut.String())
			}
		})
	}
}

func TestCostMap(t *testing.T) {
	testTable := []struct {
		listener    string
		listenProto string
		costMaps    []string
	}{
		{"--tcp-listener", "TCP", []string{"{}", "{\"a\": 1}", "{\"a\": 1.1}", "{\"a\": 1.3, \"b\": 5.6, \"c\": 0.2}"}},
		{"--ws-listener", "TCP", []string{"{}", "{\"a\": 1}", "{\"a\": 1.1}", "{\"a\": 1.3, \"b\": 5.6, \"c\": 0.2}"}},
		{"--udp-listener", "UDP", []string{"{}", "{\"a\": 1}", "{\"a\": 1.1}", "{\"a\": 1.3, \"b\": 5.6, \"c\": 0.2}"}},
	}
	for _, data := range testTable {
		listener := data.listener
		listenProto := data.listenProto
		costMaps := make([]string, len(data.costMaps))
		copy(costMaps, data.costMaps)
		t.Run(listener, func(t *testing.T) {
			for _, costMap := range costMaps {
				costMapCopy := costMap
				t.Run(costMapCopy, func(t *testing.T) {
					receptorStdOut := bytes.Buffer{}
					cmd := exec.Command("receptor", "--node", "id=test", listener, "port=0", fmt.Sprintf("nodecost=%s", costMapCopy))
					cmd.Stdout = &receptorStdOut
					err := cmd.Start()
					if err != nil {
						t.Fatal(err)
					}
					defer cmd.Process.Wait()
					defer cmd.Process.Kill()

					ctx1, cancel1 := context.WithTimeout(context.Background(), 2*time.Second)
					defer cancel1()

					success, err := utils.CheckUntilTimeoutWithErr(ctx1, 10*time.Millisecond, func() (bool, error) {
						return ConfirmListening(cmd.Process.Pid, listenProto)
					})
					if err != nil {
						t.Fatal(err)
					}
					if !success {
						t.Fatalf("Timed out while waiting for backend to start:\n%s", receptorStdOut.String())
					}
				})
			}
		})
	}
}

func TestCosts(t *testing.T) {
	testTable := []struct {
		listener    string
		listenProto string
		costs       []string
	}{
		{"--tcp-listener", "TCP", []string{"1", "1.5", "1.0", "0.2", "52", "23"}},
		{"--ws-listener", "TCP", []string{"1", "1.5", "1.0", "0.2", "52", "23"}},
		{"--udp-listener", "UDP", []string{"1", "1.5", "1.0", "0.2", "52", "23"}},
	}
	for _, data := range testTable {
		listener := data.listener
		listenProto := data.listenProto
		costs := make([]string, len(data.costs))
		copy(costs, data.costs)
		t.Run(listener, func(t *testing.T) {
			for _, cost := range costs {
				costCopy := cost
				t.Run(costCopy, func(t *testing.T) {
					t.Parallel()
					receptorStdOut := bytes.Buffer{}
					cmd := exec.Command("receptor", "--node", "id=test", listener, "port=0", fmt.Sprintf("cost=%s", costCopy))
					cmd.Stdout = &receptorStdOut
					err := cmd.Start()
					if err != nil {
						t.Fatal(err)
					}
					defer cmd.Process.Wait()
					defer cmd.Process.Kill()

					ctx1, cancel1 := context.WithTimeout(context.Background(), 2*time.Second)
					defer cancel1()

					success, err := utils.CheckUntilTimeoutWithErr(ctx1, 10*time.Millisecond, func() (bool, error) {
						return ConfirmListening(cmd.Process.Pid, listenProto)
					})
					if err != nil {
						t.Fatal(err)
					}
					if !success {
						t.Fatalf("Timed out while waiting for backend to start:\n%s", receptorStdOut.String())
					}
				})
			}
		})
	}
}