-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpuccini_test.go
169 lines (142 loc) · 4.3 KB
/
puccini_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
package main
import (
contextpkg "context"
"os"
"path"
"path/filepath"
"testing"
"github.com/tliron/exturl"
cloutpkg "github.com/tliron/puccini/clout"
"github.com/tliron/puccini/clout/js"
"github.com/tliron/puccini/normal"
"github.com/tliron/puccini/tosca/parser"
_ "github.com/tliron/commonlog/simple"
)
func TestParse(t *testing.T) {
context := NewContext(t)
defer context.urlContext.Release()
context.compileAll()
}
func BenchmarkParse(b *testing.B) {
context := NewContext(b)
defer context.urlContext.Release()
for i := 0; i < b.N; i++ {
context.compileAll()
}
}
//
// Context
//
type Context struct {
tb testing.TB
root string
urlContext *exturl.Context
parser *parser.Parser
}
func NewContext(tb testing.TB) *Context {
var root string
var ok bool
if root, ok = os.LookupEnv("PUCCINI_TEST_ROOT"); !ok {
var err error
if root, err = os.Getwd(); err != nil {
tb.Errorf("%s", err.Error())
}
}
return &Context{
tb: tb,
root: root,
urlContext: exturl.NewContext(),
parser: parser.NewParser(),
}
}
func (self *Context) compileAll() {
self.compile("legacy/tosca_1_0.yaml", nil)
self.compile("legacy/tosca_1_1.yaml", nil)
self.compile("legacy/tosca_1_2.yaml", nil)
self.compile("1.3/artifacts.yaml", nil)
self.compile("1.3/attributes.yaml", nil)
self.compile("1.3/copy.yaml", nil)
self.compile("1.3/data-types.yaml", nil)
self.compile("1.3/descriptions.yaml", nil)
self.compile("1.3/dsl-definitions.yaml", nil)
self.compile("1.3/functions.yaml", nil)
self.compile("1.3/inputs-and-outputs.yaml", map[string]any{"ram": "1gib"})
self.compile("1.3/interfaces.yaml", nil)
self.compile("1.3/metadata.yaml", nil)
self.compile("1.3/namespaces.yaml", nil)
self.compile("1.3/policies-and-groups.yaml", nil)
self.compile("1.3/requirements-and-capabilities.yaml", nil)
self.compile("1.3/simple-for-nfv.yaml", nil)
self.compile("1.3/source-and-target.yaml", nil)
self.compile("1.3/substitution-mapping-client.yaml", nil)
self.compile("1.3/substitution-mapping.yaml", nil)
self.compile("1.3/unicode.yaml", nil)
self.compile("1.3/workflows.yaml", nil)
self.compile("2.0/artifacts.yaml", nil)
//self.compile("2.0/attributes.yaml", nil)
self.compile("javascript/artifacts.yaml", nil)
self.compile("javascript/constraints.yaml", nil)
self.compile("javascript/converters.yaml", nil)
self.compile("javascript/define.yaml", nil)
self.compile("javascript/exec.yaml", nil)
self.compile("javascript/functions.yaml", nil)
self.compile("openstack/hello-world.yaml", nil)
self.compile("bpmn/open-loop.yaml", nil)
self.compile("cloudify/advanced-blueprint-example.yaml", map[string]any{
"host_ip": "1.2.3.4",
"agent_user": "my_user",
"agent_private_key_path": "my_key",
})
self.compile("cloudify/example.yaml", nil)
self.compile("hot/hello-world.yaml", map[string]any{
"username": "test",
})
}
func (self *Context) compile(url string, inputs map[string]any) {
if t, ok := self.tb.(*testing.T); ok {
t.Run(url, func(t_ *testing.T) {
// Running the tests in parallel is not just for speed;
// it actually helps us to find concurrency bugs
t_.Parallel()
self.compile_(t_, url, inputs)
})
} else {
self.compile_(self.tb, url, inputs)
}
}
func (self *Context) compile_(t testing.TB, url string, inputs map[string]any) {
var normalServiceTemplate *normal.ServiceTemplate
var clout *cloutpkg.Clout
var err error
url_ := self.urlContext.NewFileURL(path.Join(filepath.ToSlash(self.root), "examples", url))
parserContext := self.parser.NewContext()
parserContext.URL = url_
parserContext.Inputs = inputs
if normalServiceTemplate, err = parserContext.Parse(contextpkg.TODO()); err != nil {
t.Errorf("%s\n%s", err.Error(), parserContext.GetProblems().ToString(true))
return
}
problems := parserContext.GetProblems()
if clout, err = normalServiceTemplate.Compile(); err != nil {
t.Errorf("%s\n%s", err.Error(), problems.ToString(true))
return
}
execContext := js.ExecContext{
Clout: clout,
Problems: problems,
URLContext: self.urlContext,
History: true,
Format: "yaml",
Pretty: true,
}
execContext.Resolve()
if !problems.Empty() {
t.Errorf("%s", problems.ToString(true))
return
}
execContext.Coerce()
if !problems.Empty() {
t.Errorf("%s", problems.ToString(true))
return
}
}