File: utils_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (135 lines) | stat: -rw-r--r-- 2,830 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
//go:build !integration
// +build !integration

package dns

import (
	"errors"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"

	"gitlab.com/gitlab-org/gitlab-runner/helpers/dns/test"
)

func TestMakeRFC1123Compatible(t *testing.T) {
	examples := []struct {
		name     string
		expected string
	}{
		{name: "tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
		{name: "----tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
		{
			name:     "very-long-token-----------------------------------------------end",
			expected: "very-long-token-----------------------------------------------e",
		},
	}

	for _, example := range examples {
		t.Run(example.name, func(t *testing.T) {
			name := MakeRFC1123Compatible(example.name)

			assert.Equal(t, example.expected, name)
			test.AssertRFC1123Compatibility(t, name)
		})
	}
}

func TestValidateDNS1123Subdomain(t *testing.T) {
	examples := []struct {
		name  string
		valid bool
	}{
		{name: "valid-dns", valid: true},
		{name: "1.1.1.1", valid: true},
		{name: "a.b.c", valid: true},
		{name: "c-1.p", valid: true},
		{name: "a---b", valid: true},

		{name: "__invalid", valid: false},
		{name: "long-" + strings.Repeat("a", 300), valid: false},
		{name: "A.B", valid: false},
		{name: "A.2---C", valid: false},
		{name: "A_B--C", valid: false},
	}

	for _, example := range examples {
		t.Run(example.name, func(t *testing.T) {
			err := ValidateDNS1123Subdomain(example.name)

			if example.valid {
				assert.NoError(t, err)
				return
			}

			assert.NotNil(t, err)
		})
	}

	// A separate test for empty subdomain value since otherwise it's rendered as
	// TestValidateDNS1123Subdomain/#00 which is less clear
	t.Run("empty", func(t *testing.T) {
		assert.NotNil(t, ValidateDNS1123Subdomain(""))
	})
}

func TestRFC1123SubdomainError(t *testing.T) {
	tests := map[string]struct {
		err *RFC1123SubdomainError

		expected string
	}{
		"one inner message": {
			err: &RFC1123SubdomainError{errs: []string{"one"}},

			expected: "one",
		},
		"two inner messages": {
			err: &RFC1123SubdomainError{errs: []string{"one", "two"}},

			expected: "one, two",
		},
		"empty inner err": {
			err:      &RFC1123SubdomainError{},
			expected: emptyRFC1123SubdomainErrorMessage,
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			assert.Equal(t, tt.expected, tt.err.Error())
		})
	}
}

func TestRFC1123SubdomainErrorIs(t *testing.T) {
	tests := map[string]struct {
		is error

		expected bool
	}{
		"is": {
			is: &RFC1123SubdomainError{},

			expected: true,
		},
		"is not": {
			is: errors.New("is not"),

			expected: false,
		},
		"is not - nil": {
			is: nil,

			expected: false,
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			err := &RFC1123SubdomainError{}
			assert.Equal(t, tt.expected, err.Is(tt.is))
		})
	}
}