Debian Codegraph @

debian/vendor/github.com/czerwonk/bird_socket/bird_socket_test.go

raw
1 package birdsocket
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 // TestBirdSocketConnection simulate a scenario in
10 // which the BirdSocket read buffer contains the
11 // string sent in output by Bird whenever a new
12 // connection is initiated
13 func TestBirdSocketConnection(t *testing.T) {
14 out := "0001 BIRD 1.6.4 ready.\n"
15 completed := containsActionCompletedCode([]byte(out))
16
17 assert.True(t, completed, "'connect' successfully completed")
18 }
19
20 // TestBirdShowProtocols simulate a scenario in which
21 // the full output of the 'show protocols' command is
22 // saved in the BirdSocket read buffer
23 func TestBirdShowProtocols(t *testing.T) {
24 out := "2002-name proto table state since info\n" +
25 "1002-device1 Device master up 2018-12-21 12:35:11\n" +
26 " direct1 Direct master up 2018-12-21 12:35:11\n" +
27 " kernel1 Kernel master up 2018-12-21 12:35:11\n" +
28 " SAR2 BGP master down 2018-12-21 12:35:11 Error: Invalid next hop\n" +
29 " SAR1 BGP master down 2018-12-21 12:35:11 Error: Invalid next hop\n" +
30 "0000\n"
31 completed := containsActionCompletedCode([]byte(out))
32
33 assert.True(t, completed, "'show protocols' successfully completed")
34 }
35
36 // TestIncompleteBirdShowProtocols simulate a scenario in which
37 // the 'action successfully completed' Bird response code
38 // is not present in the output of the 'show protocols' command
39 // saved in the BirdSocket read buffer
40 func TestIncompleteBirdShowProtocols(t *testing.T) {
41 out := "1002-device1 Device master up 2018-12-21 12:35:11\n" +
42 " direct1 Direct master up 2018-12-21 12:35:11\n" +
43 " kernel1 Kernel master up 2018-12-21 12:35:11\n"
44 completed := containsActionCompletedCode([]byte(out))
45
46 assert.False(t, completed, "'show protocols' successfully completed")
47 }
48
49 // TestTruncatedBirdShowProtocols simulate a scenario in which
50 // the 'action successfully completed' Bird response code
51 // is present in the output of the 'show protocols' command
52 // saved in the BirdSocket read buffer,
53 // however the response of the Bird deamon is not yet completed
54 // (the final new line is not present in the buffer)
55 func TestTruncatedBirdShowProtocols(t *testing.T) {
56 out := "1002-device1 Device master up 2018-12-21 12:35:11\n" +
57 " direct1 Direct master up 2018-12-21 12:35:11\n" +
58 " kernel1 Kernel master up 2018-12-21 12:35:11\n" +
59 " SAR2 BGP master down 2018-12-21 12:35:11 Error: Invalid next hop\n" +
60 " SAR1 BGP master down 2018-12-21 12:35:11 Error: Invalid next hop\n" +
61 "0000"
62 completed := containsActionCompletedCode([]byte(out))
63
64 assert.True(t, completed, "'show protocols' successfully completed")
65 }
66
67 // TestBirdShowStatus simulate a scenario in which
68 // the full output of the 'show status' command is
69 // saved in the BirdSocket read buffer
70 func TestBirdShowStatus(t *testing.T) {
71 out := "1000-BIRD 1.6.4\n" +
72 "1011-Router ID is 192.168.1.9\n" +
73 " Current server time is 2018-12-27 12:15:01\n" +
74 " Last reboot on 2018-12-21 12:35:11\n" +
75 " Last reconfiguration on 2018-12-21 12:35:11\n" +
76 "0013 Daemon is up and running\n"
77 completed := containsActionCompletedCode([]byte(out))
78
79 assert.True(t, completed, "'show status' successfully completed")
80 }
81
82 // TestIncompleteBirdShowStatus simulate a scenario in which
83 // the 'action successfully completed' Bird response code
84 // is not present in the output of the 'show status' command
85 // saved in the BirdSocket read buffer
86 func TestIncompleteBirdShowStatus(t *testing.T) {
87 out := "1011-Router ID is 192.168.1.9\n" +
88 " Current server time is 2018-12-27 12:15:01\n" +
89 " Last reboot on 2018-12-21 12:35:11\n"
90 completed := containsActionCompletedCode([]byte(out))
91
92 assert.False(t, completed, "'show status' successfully completed")
93 }
94
95 // TestTruncatedBirdShowStatus simulate a scenario in which
96 // the 'action successfully completed' Bird response code
97 // is present in the output of the 'show status' command
98 // saved in the BirdSocket read buffer,
99 // however the response of the Bird deamon is not yet completed
100 // (the final new line is not present in the buffer)
101 func TestTruncatedBirdShowStatus(t *testing.T) {
102 out := "1011-Router ID is 192.168.1.9\n" +
103 " Current server time is 2018-12-27 12:15:01\n" +
104 " Last reboot on 2018-12-21 12:35:11\n" +
105 " Last reconfiguration on 2018-12-21 12:35:11\n" +
106 "0013 Daemon is up and running"
107 completed := containsActionCompletedCode([]byte(out))
108
109 assert.True(t, completed, "'show status' successfully completed")
110 }
111
112 func TestExitCodes(t *testing.T) {
113 testCases := []struct {
114 out string
115 }{
116 {
117 out: "9000 Command too long",
118 },
119 {
120 out: "8000 Reply too long",
121 },
122 }
123 for _, tc := range testCases {
124 completed := containsActionCompletedCode([]byte(tc.out))
125 assert.True(t, completed, "exit codes completed")
126 }
127 }