This repository has been archived by the owner on May 3, 2022. It is now read-only.
/
manifest_test.go
131 lines (106 loc) · 3.11 KB
/
manifest_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
package manifest
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
m := New()
// Testing to make sure maps are initialized
is := assert.New(t)
is.Len(m.InvocationImages, 0)
is.Nil(m.Parameters)
is.Len(m.Credentials, 0)
}
func TestGenerateName(t *testing.T) {
name := generateName()
if name == "" {
t.Error("expected name to be generated")
}
if name != "manifest" {
t.Errorf("expected name to take the form of the current directory, got %s", name)
}
}
func TestLoad(t *testing.T) {
testcases := []string{"", "duffle.json"}
for _, tc := range testcases {
t.Run(tc, func(t *testing.T) {
m, err := Load(tc, "testdata")
if err != nil {
t.Fatal(err)
}
if m == nil {
t.Fatal("manifest should not be nil")
}
wantName := "testbundle"
if m.Name != wantName {
t.Errorf("expected Name to be %q but got %q", wantName, m.Name)
}
if len(m.InvocationImages) != 1 {
t.Fatalf("expected 1 component but got %d", len(m.InvocationImages))
}
if _, ok := m.InvocationImages["cnab"]; !ok {
t.Errorf("expected a component named cnab but got %v", m.InvocationImages)
}
if len(m.Images) != 1 {
t.Errorf("exp 1 but was \"%v\"", len(m.Images))
}
img := m.Images["istio"]
if !strings.EqualFold(img.ImageType, "docker") {
t.Errorf("exp docker but was \"%v\"", img.ImageType)
}
if len(m.Parameters.Fields) != 1 {
t.Fatalf("expected 1 parameter but got %d", len(m.Parameters.Fields))
}
_, ok := m.Parameters.Fields["foo"]
if !ok {
t.Errorf("expected a parameter named foo but got %v", m.Parameters)
}
if m.Definitions["foo"].Type != "string" {
t.Errorf("expected foo parameter to have a type of string but got %v", m.Definitions["foo"].Type)
}
if len(m.Credentials) != 1 {
t.Fatalf("expected 1 credential but got %d", len(m.Credentials))
}
cred, ok := m.Credentials["bar"]
if !ok {
t.Errorf("expected a credential named bar but got %v", m.Credentials)
}
if cred.Path != "/tmp" {
t.Errorf("expected foo credential to have a path of /tmp but got %v", cred.Path)
}
if len(m.Maintainers) != 1 {
t.Fatalf("expected 1 maintainer but got %d", len(m.Maintainers))
}
maintainer := m.Maintainers[0]
if maintainer.Name != "sally" {
t.Errorf("expected maintainer to be sally but got %v", maintainer.Name)
}
})
}
}
func TestInvalidLoad(t *testing.T) {
testcases := []string{"invalid_duffle.json"}
for _, tc := range testcases {
t.Run(tc, func(t *testing.T) {
_, err := Load(tc, "testdata")
if err == nil {
t.Errorf("expected an error to be thrown")
}
if !strings.Contains(err.Error(), "json: cannot unmarshal array into Go struct field") {
t.Errorf("expected err to contain %s but was %s", "json: cannot unmarshal array into Go struct field", err.Error())
}
})
}
}
func TestExamples(t *testing.T) {
testcases := []string{"helloworld/duffle.json"}
for _, tc := range testcases {
t.Run(tc, func(t *testing.T) {
_, err := Load(tc, "../../../examples")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}