File: utils_test.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (148 lines) | stat: -rw-r--r-- 4,125 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
package build

import (
	"context"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestToBuildkitExtraHosts(t *testing.T) {
	tests := []struct {
		doc         string
		input       []string
		expectedOut string // Expect output==input if not set.
		expectedErr string // Expect success if not set.
	}{
		{
			doc:         "IPv4, colon sep",
			input:       []string{`myhost:192.168.0.1`},
			expectedOut: `myhost=192.168.0.1`,
		},
		{
			doc:   "IPv4, eq sep",
			input: []string{`myhost=192.168.0.1`},
		},
		{
			doc:         "Weird but permitted, IPv4 with brackets",
			input:       []string{`myhost=[192.168.0.1]`},
			expectedOut: `myhost=192.168.0.1`,
		},
		{
			doc:         "Host and domain",
			input:       []string{`host.and.domain.invalid:10.0.2.1`},
			expectedOut: `host.and.domain.invalid=10.0.2.1`,
		},
		{
			doc:         "IPv6, colon sep",
			input:       []string{`anipv6host:2003:ab34:e::1`},
			expectedOut: `anipv6host=2003:ab34:e::1`,
		},
		{
			doc:         "IPv6, colon sep, brackets",
			input:       []string{`anipv6host:[2003:ab34:e::1]`},
			expectedOut: `anipv6host=2003:ab34:e::1`,
		},
		{
			doc:         "IPv6, eq sep, brackets",
			input:       []string{`anipv6host=[2003:ab34:e::1]`},
			expectedOut: `anipv6host=2003:ab34:e::1`,
		},
		{
			doc:         "IPv6 localhost, colon sep",
			input:       []string{`ipv6local:::1`},
			expectedOut: `ipv6local=::1`,
		},
		{
			doc:   "IPv6 localhost, eq sep",
			input: []string{`ipv6local=::1`},
		},
		{
			doc:         "IPv6 localhost, eq sep, brackets",
			input:       []string{`ipv6local=[::1]`},
			expectedOut: `ipv6local=::1`,
		},
		{
			doc:         "IPv6 localhost, non-canonical, colon sep",
			input:       []string{`ipv6local:0:0:0:0:0:0:0:1`},
			expectedOut: `ipv6local=0:0:0:0:0:0:0:1`,
		},
		{
			doc:   "IPv6 localhost, non-canonical, eq sep",
			input: []string{`ipv6local=0:0:0:0:0:0:0:1`},
		},
		{
			doc:         "IPv6 localhost, non-canonical, eq sep, brackets",
			input:       []string{`ipv6local=[0:0:0:0:0:0:0:1]`},
			expectedOut: `ipv6local=0:0:0:0:0:0:0:1`,
		},
		{
			doc:         "Bad address, colon sep",
			input:       []string{`myhost:192.notanipaddress.1`},
			expectedErr: `invalid IP address in add-host: "192.notanipaddress.1"`,
		},
		{
			doc:         "Bad address, eq sep",
			input:       []string{`myhost=192.notanipaddress.1`},
			expectedErr: `invalid IP address in add-host: "192.notanipaddress.1"`,
		},
		{
			doc:         "No sep",
			input:       []string{`thathost-nosemicolon10.0.0.1`},
			expectedErr: `bad format for add-host: "thathost-nosemicolon10.0.0.1"`,
		},
		{
			doc:         "Bad IPv6",
			input:       []string{`anipv6host:::::1`},
			expectedErr: `invalid IP address in add-host: "::::1"`,
		},
		{
			doc:         "Bad IPv6, trailing colons",
			input:       []string{`ipv6local:::0::`},
			expectedErr: `invalid IP address in add-host: "::0::"`,
		},
		{
			doc:         "Bad IPv6, missing close bracket",
			input:       []string{`ipv6addr=[::1`},
			expectedErr: `invalid IP address in add-host: "[::1"`,
		},
		{
			doc:         "Bad IPv6, missing open bracket",
			input:       []string{`ipv6addr=::1]`},
			expectedErr: `invalid IP address in add-host: "::1]"`,
		},
		{
			doc:         "Missing address, colon sep",
			input:       []string{`myhost.invalid:`},
			expectedErr: `invalid IP address in add-host: ""`,
		},
		{
			doc:         "Missing address, eq sep",
			input:       []string{`myhost.invalid=`},
			expectedErr: `invalid IP address in add-host: ""`,
		},
		{
			doc:         "No input",
			input:       []string{``},
			expectedErr: `bad format for add-host: ""`,
		},
	}

	for _, tc := range tests {
		tc := tc
		if tc.expectedOut == "" {
			tc.expectedOut = strings.Join(tc.input, ",")
		}
		t.Run(tc.doc, func(t *testing.T) {
			actualOut, actualErr := toBuildkitExtraHosts(context.TODO(), tc.input, nil)
			if tc.expectedErr == "" {
				require.Equal(t, tc.expectedOut, actualOut)
				require.Nil(t, actualErr)
			} else {
				require.Zero(t, actualOut)
				require.Error(t, actualErr, tc.expectedErr)
			}
		})
	}
}