forked from proofpoint/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
91 lines (78 loc) · 1.97 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
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package bundle
import (
"fmt"
"testing"
)
func TestConfigValidation(t *testing.T) {
tests := []struct {
input string
wantErr bool
}{
{
input: `{}`,
wantErr: true,
},
{
input: `{"name": "a/b/c", "service": "invalid"}`,
wantErr: true,
},
{
input: `{"name": "a/b/c", "service": "service2"}`,
wantErr: false,
},
{
input: `{"name": "a/b/c", "service": "service2", "prefix": "mybundle"}`,
wantErr: false,
},
{
input: `{"name": "a/b/c", "service": "service2", "prefix": "/"}`,
wantErr: false,
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("TestConfigValidation_case_%d", i), func(t *testing.T) {
_, err := ParseConfig([]byte(test.input), []string{"service1", "service2"})
if err != nil && !test.wantErr {
t.Fail()
}
if err == nil && test.wantErr {
t.Fail()
}
})
}
}
func TestConfigValid(t *testing.T) {
in := `{"name": "a/b/c", "service": "service2", "prefix": "mybundle"}`
config, err := ParseConfig([]byte(in), []string{"service1", "service2"})
if err != nil {
t.Fail()
}
if config.Name != "a/b/c" {
t.Fatalf("want %v got %v", "a/b/c", config.Name)
}
if config.Service != "service2" {
t.Fatalf("want %v got %v", "service2", config.Name)
}
if *(config.Prefix) != "mybundle" {
t.Fatalf("want %v got %v", "mybundle", *(config.Prefix))
}
}
func TestConfigCorrupted(t *testing.T) {
in := `{"name": "a/b/c", "service": "service2", "prefix: mybundle"}`
config, err := ParseConfig([]byte(in), []string{"service1", "service2"})
if err != nil {
t.Fail()
}
if config.Name != "a/b/c" {
t.Fatalf("want %v got %v", "a/b/c", config.Name)
}
if config.Service != "service2" {
t.Fatalf("want %v got %v", "service2", config.Name)
}
if *(config.Prefix) != "bundles" {
t.Fatalf("want %v got %v", "bundles", *(config.Prefix))
}
}