/
makemac_test.go
182 lines (175 loc) · 3.76 KB
/
makemac_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
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
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestHostTypeToVersion(t *testing.T) {
testCases := []struct {
desc string
hostType string
wantVersion *Version
}{
{
desc: "valid original darwin host type",
hostType: "host-darwin-10_14",
wantVersion: &Version{
Major: 10,
Minor: 14,
Arch: "amd64",
},
},
{
desc: "valid original darwin host type",
hostType: "host-darwin-10_15",
wantVersion: &Version{
Major: 10,
Minor: 15,
Arch: "amd64",
},
},
{
desc: "valid newer darwin host ARM64",
hostType: "host-darwin-arm64-11_0",
wantVersion: &Version{
Major: 11,
Minor: 0,
Arch: "arm64",
},
},
{
desc: "valid newer darwin host AMD64",
hostType: "host-darwin-amd64-11_1",
wantVersion: &Version{
Major: 11,
Minor: 1,
Arch: "amd64",
},
},
{
desc: "valid newer darwin host AMD64",
hostType: "host-darwin-amd64-13",
wantVersion: &Version{
Major: 13,
Minor: 0,
Arch: "amd64",
},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got, err := hostTypeToVersion(tc.hostType)
if err != nil {
t.Fatalf("hostTypeToVersion(%q) = %v, %s; want no error", tc.hostType, got, err)
}
if diff := cmp.Diff(tc.wantVersion, got); diff != "" {
t.Errorf("hostTypeToVersion(%q) = (-want +got):\n%s", tc.hostType, diff)
}
})
}
}
func TestHostTypeToVersionError(t *testing.T) {
testCases := []struct {
desc string
hostType string
wantErr error
}{
{
desc: "empty string",
hostType: "",
},
{
desc: "invalid newer darwin host ARM64",
hostType: "host-darwin-amd64-11_1-toothrot",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got, gotErr := hostTypeToVersion(tc.hostType); got != nil || gotErr == nil {
t.Errorf("hostTypeToVersion(%q) = %+v, %s; want nil, error", tc.hostType, got, gotErr)
}
})
}
}
func TestHostOnMacStadium(t *testing.T) {
testCases := []struct {
desc string
hostType string
want bool
}{
{
desc: "empty string",
hostType: "",
want: false,
},
{
desc: "valid original darwin host",
hostType: "host-darwin-10_14",
want: true,
},
{
desc: "valid newer darwin host",
hostType: "host-darwin-amd64-11_0",
want: true,
},
{
desc: "invalid newer darwin host type",
hostType: "host-darwin-arm64-11_0-toothrot",
want: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got := hostOnMacStadium(tc.hostType); got != tc.want {
t.Errorf("hostOnMacStadium(%q) = %t; want %t", tc.hostType, got, tc.want)
}
})
}
}
func TestVMNameReg(t *testing.T) {
testCases := []struct {
desc string
vmName string
want bool
}{
{
desc: "empty string",
vmName: "",
want: false,
},
{
desc: "invalid original darwin host",
vmName: "mac_10_11_host01b",
want: false,
},
{
desc: "valid newer darwin host",
vmName: "mac_11_12_amd64_host01b",
want: true,
},
{
desc: "valid newer darwin host",
vmName: "mac_11_20_amd64_host05a",
want: true,
},
{
desc: "invalid newer darwin host type",
vmName: "host-darwin-arm64-11_0-toothrot",
want: false,
},
{
desc: "invalid bastion host",
vmName: "dns_server",
want: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got := vmNameReg.MatchString(tc.vmName); got != tc.want {
t.Errorf("vmNameReg.MatchString(%q) = %t; want %t", tc.vmName, got, tc.want)
}
})
}
}