forked from Venafi/vcert
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_test.go
118 lines (102 loc) · 2.99 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
/*
* Copyright 2018 Venafi, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package vcert
import (
"io/ioutil"
"os"
"testing"
)
const validTestModeConfig = `
test_mode = true`
const invalidTestModeConfig = `
test_mode = false`
const validTPPConfigDeprecated = `# all fine here
tpp_url = https://ha-tpp1.example.com:5008/vedsdk
tpp_user = admin
tpp_password = xxx
tpp_zone = devops\vcert`
const validTPPConfig = `# all fine here
url = https://ha-tpp1.example.com:5008/vedsdk
access_token = ns1dofUPmsdxTLQS2hM1gQ==
tpp_zone = devops\vcert`
const emptyConfig = ``
const invalidTPPConfig = `# cloud zone cannot be used in TPP section
url = https://ha-tpp1.example.com:5008/vedsdk
access_token = ns1dofUPmsdxTLQS2hM1gQ==
tpp_zone = devops\vcert
cloud_zone = Default`
const invalidTPPConfig2 = `# missing password
url = https://ha-tpp1.example.com:5008/vedsdk
tpp_user = admin
#tpp_password = xxx
tpp_zone = devops\vcert`
const invalidTPPConfig3 = `# trust bundle cannot be loaded
url = https://ha-tpp1.example.com:5008/vedsdk
access_token = ns1dofUPmsdxTLQS2hM1gQ==
tpp_zone = devops\vcert
trust_bundle = ~/.vcert/file.does-not-exist`
const validCloudConfig = `
url = https://api.dev12.qa.venafi.io/v1
cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548
cloud_zone = Default`
const validCloudConfig2 = `
cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548`
const invalidCloudConfig = `# tpp user is illegal
url = https://api.dev12.qa.venafi.io/v1
cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548
tpp_user = admin
cloud_zone = Default`
func TestLoadFromFile(t *testing.T) {
var cases = []struct {
valid bool
content string
}{
{true, validTestModeConfig},
{true, validTPPConfig},
{true, validTPPConfigDeprecated},
{true, validCloudConfig},
{true, validCloudConfig},
{true, validCloudConfig2},
{false, emptyConfig},
{false, invalidTestModeConfig},
{false, invalidTPPConfig},
{false, invalidTPPConfig2},
{false, invalidTPPConfig3},
{false, invalidCloudConfig},
}
for _, test_case := range cases {
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
err = ioutil.WriteFile(tmpfile.Name(), []byte(test_case.content), 0644)
if err != nil {
t.Fatal(err)
}
_, err = LoadConfigFromFile(tmpfile.Name(), "")
if test_case.valid {
if err != nil {
t.Logf("config: %s", test_case.content)
t.Fatal(err)
}
} else {
if err == nil {
t.Fatalf("it should fail to load config: \n%s", test_case.content)
}
}
}
}