forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
129 lines (120 loc) · 4.48 KB
/
config_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
package main
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/gwconfig"
)
func TestMissingIPConfig(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "noip.json"),
}
err := loadConfig(file)
if err != nil {
t.Fatalf("%v", err)
}
err = config.Validate()
// See https://bugzil.la/1540804 - publicIP is now optional.
if err != nil {
t.Fatalf("Was expecting no error, but got: %v", err)
}
}
func TestValidConfig(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "valid.json"),
}
const ipaddr = "2.1.2.1"
const workerType = "some-worker-type"
err := loadConfig(file)
if err != nil {
t.Fatalf("%v", err)
}
err = config.Validate()
if err != nil {
t.Fatalf("Config should pass validation, but get:\n%s", err)
}
if actualIP := config.PublicIP.String(); actualIP != ipaddr {
t.Fatalf("Was expecting IP address %s but received IP address %s", ipaddr, actualIP)
}
if actualWorkerType := config.WorkerType; actualWorkerType != workerType {
t.Fatalf("Was expecting worker type %s but received worker type %s", workerType, actualWorkerType)
}
}
func TestInvalidIPConfig(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "invalid-ip.json"),
}
err := loadConfig(file)
if err == nil {
t.Fatal("Was expecting to get an error back due to an invalid IP address, but didn't get one!")
}
expectedErrorText := `invalid IP address: 257.1.2.1`
if !strings.Contains(err.Error(), expectedErrorText) {
t.Fatalf("Was expecting error text to include %q but it didn't: %v", expectedErrorText, err)
}
}
func TestInvalidJsonConfig(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "invalid-json.json"),
}
err := loadConfig(file)
if err == nil {
t.Fatal("Was expecting to get an error back due to an invalid JSON config, but didn't get one!")
}
expectedErrorText := `invalid character '"' after object key:value pair`
if !strings.Contains(err.Error(), expectedErrorText) {
t.Fatalf("Was expecting error text to include %q but it didn't: %v", expectedErrorText, err)
}
}
func TestMissingConfigFile(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "non-existent-json.json"),
}
err := loadConfig(file)
if err == nil {
t.Fatal("Was expecting an error when loading non existent config file without --configure-for-{aws,gcp} set")
}
if _, isPathError := err.(*os.PathError); !isPathError {
t.Fatalf("Was expecting an error of type *os.PathError but received error %#v", err)
}
}
func TestWorkerTypeMetadata(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "worker-type-metadata.json"),
}
err := loadConfig(file)
if err != nil {
t.Fatalf("%v", err)
}
err = config.Validate()
if err != nil {
t.Fatalf("Config should pass validation, but get:\n%s", err)
}
// loadConfig function specifies a value, let's check we can't override it in the config file ("fakeos")
if config.WorkerTypeMetadata["generic-worker"].(map[string]interface{})["go-os"] != runtime.GOOS {
t.Fatalf("Was not expecting key 'go-os' from file worker-type-metadata.json to override default value\n%#v", config)
}
// go-version not specified in config file, but should be set in loadConfig, let's check it is
if config.WorkerTypeMetadata["generic-worker"].(map[string]interface{})["go-version"] != runtime.Version() {
t.Fatalf("Was expecting key 'go-version' to be set to go version in worker type metadata\n%#v", config)
}
// machine-setup is not set in loadConfig, but is set in config file, let's check we have it
if config.WorkerTypeMetadata["machine-setup"].(map[string]interface{})["script"] != "https://raw.githubusercontent.com/taskcluster/generic-worker/2d2ad3000787f2c893299e693ea3f59287127f5c/worker_types/win2012r2/userdata" {
t.Fatalf("Was expecting machine-setup to be set properly\n%#v", config)
}
}
func TestBoolAsString(t *testing.T) {
file := &gwconfig.File{
Path: filepath.Join("testdata", "config", "bool-as-string.json"),
}
err := loadConfig(file)
if err == nil {
t.Fatal("Was expecting to get an error back due to a bool being specified as a string, but didn't get one!")
}
expectedErrorText := `cannot unmarshal string into Go struct field Config.shutdownMachineOnIdle of type bool`
if !strings.Contains(err.Error(), expectedErrorText) {
t.Fatalf("Was expecting error text to include %q but it didn't: %v", expectedErrorText, err)
}
}