File: network_windows_test.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (201 lines) | stat: -rw-r--r-- 5,450 bytes parent folder | download
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
package libnetwork

import (
	"context"
	"fmt"
	"net"
	"net/netip"
	"testing"

	"github.com/Microsoft/hcsshim"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestAddEpToResolver(t *testing.T) {
	const (
		ep1v4      = "192.0.2.11"
		ep2v4      = "192.0.2.12"
		epFiveDNS  = "192.0.2.13"
		epNoIntDNS = "192.0.2.14"
		ep1v6      = "2001:db8:aaaa::2"
		gw1v4      = "192.0.2.1"
		gw2v4      = "192.0.2.2"
		gw1v6      = "2001:db8:aaaa::1"
		dns1v4     = "198.51.100.1"
		dns2v4     = "198.51.100.2"
		dns3v4     = "198.51.100.3"
	)
	hnsEndpoints := map[string]hcsshim.HNSEndpoint{
		ep1v4: {
			IPAddress:         net.ParseIP(ep1v4),
			GatewayAddress:    gw1v4,
			DNSServerList:     gw1v4 + "," + dns1v4,
			EnableInternalDNS: true,
		},
		ep2v4: {
			IPAddress:         net.ParseIP(ep2v4),
			GatewayAddress:    gw1v4,
			DNSServerList:     gw1v4 + "," + dns2v4,
			EnableInternalDNS: true,
		},
		epFiveDNS: {
			IPAddress:         net.ParseIP(epFiveDNS),
			GatewayAddress:    gw1v4,
			DNSServerList:     gw1v4 + "," + dns1v4 + "," + dns2v4 + "," + dns3v4 + ",198.51.100.4",
			EnableInternalDNS: true,
		},
		epNoIntDNS: {
			IPAddress:      net.ParseIP(epNoIntDNS),
			GatewayAddress: gw1v4,
			DNSServerList:  gw1v4 + "," + dns1v4,
			//EnableInternalDNS: false,
		},
		ep1v6: {
			IPv6Address:       net.ParseIP(ep1v6),
			GatewayAddressV6:  gw1v6,
			DNSServerList:     gw1v6 + "," + dns1v4,
			EnableInternalDNS: true,
		},
	}

	makeIPNet := func(addr, netmask string) *net.IPNet {
		t.Helper()
		ip, ipnet, err := net.ParseCIDR(addr + "/" + netmask)
		assert.NilError(t, err)
		return &net.IPNet{IP: ip, Mask: ipnet.Mask}
	}

	testcases := []struct {
		name           string
		epToAdd        *EndpointInterface
		hnsEndpoints   []hcsshim.HNSEndpoint
		resolverLAs    []string
		expIPToExtDNS  map[netip.Addr][maxExtDNS]extDNSEntry
		expResolverIdx int
	}{
		{
			name: "ipv4",
			epToAdd: &EndpointInterface{
				addr: makeIPNet(ep1v4, "32"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[ep1v4],
			},
			resolverLAs: []string{gw1v4},
			expIPToExtDNS: map[netip.Addr][maxExtDNS]extDNSEntry{
				netip.MustParseAddr(ep1v4): {{IPStr: dns1v4}},
			},
		},
		{
			name: "limit of three dns servers",
			epToAdd: &EndpointInterface{
				addr: makeIPNet(epFiveDNS, "32"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[epFiveDNS],
			},
			resolverLAs: []string{gw1v4},
			// Expect the internal resolver to keep the first three ext-servers.
			expIPToExtDNS: map[netip.Addr][maxExtDNS]extDNSEntry{
				netip.MustParseAddr(epFiveDNS): {
					{IPStr: dns1v4},
					{IPStr: dns2v4},
					{IPStr: dns3v4},
				},
			},
		},
		{
			name: "disabled internal resolver",
			epToAdd: &EndpointInterface{
				addr: makeIPNet(epNoIntDNS, "32"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[epNoIntDNS],
				hnsEndpoints[ep2v4],
			},
			resolverLAs: []string{gw1v4},
		},
		{
			name: "missing internal resolver",
			epToAdd: &EndpointInterface{
				addr: makeIPNet(ep1v4, "32"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[ep1v4],
			},
			// The only resolver is for the gateway on a different network.
			resolverLAs: []string{gw2v4},
		},
		{
			name: "multiple resolvers and endpoints",
			epToAdd: &EndpointInterface{
				addr: makeIPNet(ep2v4, "32"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[ep1v4],
				hnsEndpoints[ep2v4],
			},
			// Put the internal resolver for this network second in the list.
			expResolverIdx: 1,
			resolverLAs:    []string{gw2v4, gw1v4},
			expIPToExtDNS: map[netip.Addr][maxExtDNS]extDNSEntry{
				netip.MustParseAddr(ep2v4): {{IPStr: dns2v4}},
			},
		},
		{
			name: "ipv6",
			epToAdd: &EndpointInterface{
				addrv6: makeIPNet(ep1v6, "80"),
			},
			hnsEndpoints: []hcsshim.HNSEndpoint{
				hnsEndpoints[ep1v6],
			},
			resolverLAs: []string{gw1v6},
			expIPToExtDNS: map[netip.Addr][maxExtDNS]extDNSEntry{
				netip.MustParseAddr(ep1v6): {{IPStr: dns1v4}},
			},
		},
	}

	eMapCmpOpts := []cmp.Option{
		cmpopts.EquateEmpty(),
		cmpopts.EquateComparable(netip.Addr{}),
		cmpopts.IgnoreUnexported(extDNSEntry{}),
	}
	emptyEMap := map[netip.Addr][maxExtDNS]extDNSEntry{}

	for _, tc := range testcases {
		t.Run(tc.name, func(t *testing.T) {
			// Set up resolvers with the required listen-addresses.
			var resolvers []*Resolver
			for _, la := range tc.resolverLAs {
				resolvers = append(resolvers, NewResolver(la, true, nil))
			}

			// Add the endpoint and check expected results.
			err := addEpToResolverImpl(context.TODO(),
				"netname", "epname", tc.epToAdd, resolvers, tc.hnsEndpoints)
			assert.Check(t, err)
			for i, resolver := range resolvers {
				if i == tc.expResolverIdx {
					assert.Check(t, is.DeepEqual(resolver.ipToExtDNS.eMap, tc.expIPToExtDNS,
						eMapCmpOpts...), fmt.Sprintf("resolveridx=%d", i))
				} else {
					assert.Check(t, is.DeepEqual(resolver.ipToExtDNS.eMap, emptyEMap,
						eMapCmpOpts...), fmt.Sprintf("resolveridx=%d", i))
				}
			}

			// Delete the endpoint, check nothing got left behind.
			err = deleteEpFromResolverImpl("epname", tc.epToAdd, resolvers, tc.hnsEndpoints)
			assert.Check(t, err)
			for i, resolver := range resolvers {
				assert.Check(t, is.DeepEqual(resolver.ipToExtDNS.eMap, emptyEMap,
					eMapCmpOpts...), fmt.Sprintf("resolveridx=%d", i))
			}
		})
	}
}