forked from networkservicemesh/fanout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
141 lines (126 loc) · 4.56 KB
/
setup_test.go
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
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fanout
import (
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
"github.com/caddyserver/caddy"
)
func TestSetup(t *testing.T) {
tests := []struct {
input string
expectedFrom string
expectedTo []string
expectedIgnored []string
expectedFails int
expectedWorkers int
expectedNetwork string
expectedErr string
}{
// positive
{input: "fanout . 127.0.0.1", expectedFrom: ".", expectedFails: 2, expectedWorkers: 1, expectedNetwork: "udp"},
{input: "fanout . 127.0.0.1 {\nexcept a b\nworker-count 3\n}", expectedFrom: ".", expectedFails: 2, expectedWorkers: 1, expectedIgnored: []string{"a.", "b."}, expectedNetwork: "udp"},
{input: "fanout . 127.0.0.1 127.0.0.2 {\nnetwork tcp\n}", expectedFrom: ".", expectedFails: 0, expectedWorkers: 2, expectedNetwork: "tcp", expectedTo: []string{"127.0.0.1:53", "127.0.0.2:53"}},
{input: "fanout . 127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4 {\nworker-count 3\n}", expectedFrom: ".", expectedFails: 2, expectedWorkers: 3, expectedNetwork: "udp"},
// negative
{input: "fanout . aaa", expectedErr: "not an IP address or file"},
{input: "fanout . 127.0.0.1 {\nexcept a b\nworker-count 1\n}", expectedErr: "use Forward plugin"},
{input: "fanout . 127.0.0.1 {\nexcept a b\nworker-count ten\n}", expectedErr: "'ten'"},
{input: "fanout . 127.0.0.1 127.0.0.2 {\nnetwork XXX\n}", expectedErr: "unknown network protocol"},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
f, err := parseFanout(c)
if test.expectedErr != "" && err == nil {
t.Fatalf("Test %d: expected error but not found errors", i)
}
if err != nil {
if !strings.Contains(err.Error(), test.expectedErr) {
t.Fatalf("Test %d: expected error to contain: %v, found error: %v, input: %s", i, test.expectedErr, err, test.input)
}
continue
}
if f.from != test.expectedFrom && test.expectedFrom != "" {
t.Fatalf("Test %d: expected: %s, got: %s", i, test.expectedFrom, f.from)
}
if test.expectedIgnored != nil {
if !reflect.DeepEqual(f.ignored, test.expectedIgnored) {
t.Fatalf("Test %d: expected: %q, actual: %q", i, test.expectedIgnored, f.ignored)
}
}
if test.expectedTo != nil {
var to []string
for j := 0; j < len(f.clients); j++ {
to = append(to, f.clients[j].Endpoint())
}
if !reflect.DeepEqual(to, test.expectedTo) {
t.Fatalf("Test %d: expected: %q, actual: %q", i, test.expectedTo, to)
}
}
if f.workerCount != test.expectedWorkers {
t.Fatalf("Test %d: expected: %d, got: %d", i, test.expectedWorkers, f.workerCount)
}
if f.net != test.expectedNetwork {
t.Fatalf("Test %d: expected: %v, got: %v", i, test.expectedNetwork, f.net)
}
}
}
func TestSetupResolvconf(t *testing.T) {
const resolv = "resolv.conf"
if err := ioutil.WriteFile(resolv,
[]byte(`nameserver 10.10.255.252
nameserver 10.10.255.253`), 0666); err != nil {
t.Fatalf("Failed to write resolv.conf file: %s", err)
}
defer func() {
logErrIfNotNil(os.Remove(resolv))
}()
tests := []struct {
input string
shouldErr bool
expectedErr string
expectedNames []string
}{
{`fanout . ` + resolv, false, "", []string{"10.10.255.252:53", "10.10.255.253:53"}},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
f, err := parseFanout(c)
if test.shouldErr && err == nil {
t.Errorf("Test %d: expected error but found %s for input %s", i, err, test.input)
continue
}
if err != nil {
if !test.shouldErr {
t.Errorf("Test %d: expected no error but found one for input %s, got: %v", i, test.input, err)
}
if !strings.Contains(err.Error(), test.expectedErr) {
t.Errorf("Test %d: expected error to contain: %v, found error: %v, input: %s", i, test.expectedErr, err, test.input)
}
}
if !test.shouldErr {
for j, n := range test.expectedNames {
addr := f.clients[j].Endpoint()
if n != addr {
t.Errorf("Test %d, expected %q, got %q", j, n, addr)
}
}
}
}
}