-
Notifications
You must be signed in to change notification settings - Fork 2k
/
testing.go
133 lines (111 loc) · 3.49 KB
/
testing.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
package hclutils
import (
"testing"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"
)
type HCLParser struct {
spec *hclspec.Spec
vars map[string]cty.Value
}
// NewConfigParser return a helper for parsing drivers TaskConfig
// Parser is an immutable object can be used in multiple tests
func NewConfigParser(spec *hclspec.Spec) *HCLParser {
return &HCLParser{
spec: spec,
}
}
// WithVars returns a new parser that uses passed vars when interpolated strings in config
func (b *HCLParser) WithVars(vars map[string]cty.Value) *HCLParser {
return &HCLParser{
spec: b.spec,
vars: vars,
}
}
// ParseJson parses the json config string and decode it into the `out` parameter.
// out parameter should be a golang reference to a driver specific TaskConfig reference.
// The function terminates and reports errors if any is found during conversion.
//
// Sample invocation would be
//
// ```
// var tc *TaskConfig
// hclutils.NewConfigParser(spec).ParseJson(t, configString, &tc)
// ```
func (b *HCLParser) ParseJson(t *testing.T, configStr string, out interface{}) {
config := JsonConfigToInterface(t, configStr)
b.parse(t, config, out)
}
// ParseHCL parses the hcl config string and decode it into the `out` parameter.
// out parameter should be a golang reference to a driver specific TaskConfig reference.
// The function terminates and reports errors if any is found during conversion.
//
// Sample invocation would be
//
// ```
// var tc *TaskConfig
// hclutils.NewConfigParser(spec).ParseHCL(t, configString, &tc)
// ```
func (b *HCLParser) ParseHCL(t *testing.T, configStr string, out interface{}) {
config := HclConfigToInterface(t, configStr)
b.parse(t, config, out)
}
func (b *HCLParser) parse(t *testing.T, config, out interface{}) {
decSpec, diags := hclspecutils.Convert(b.spec)
require.Empty(t, diags)
ctyValue, diag, errs := ParseHclInterface(config, decSpec, b.vars)
if len(errs) > 1 {
t.Error("unexpected errors parsing file")
for _, err := range errs {
t.Errorf(" * %v", err)
}
t.FailNow()
}
require.Empty(t, diag)
// encode
dtc := &drivers.TaskConfig{}
require.NoError(t, dtc.EncodeDriverConfig(ctyValue))
// decode
require.NoError(t, dtc.DecodeDriverConfig(out))
}
func HclConfigToInterface(t *testing.T, config string) interface{} {
t.Helper()
// Parse as we do in the jobspec parser
root, err := hcl.Parse(config)
if err != nil {
t.Fatalf("failed to hcl parse the config: %v", err)
}
// Top-level item should be a list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
t.Fatalf("root should be an object")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, list.Items[0]); err != nil {
t.Fatalf("failed to decode object: %v", err)
}
var m2 map[string]interface{}
if err := mapstructure.WeakDecode(m, &m2); err != nil {
t.Fatalf("failed to weak decode object: %v", err)
}
return m2["config"]
}
func JsonConfigToInterface(t *testing.T, config string) interface{} {
t.Helper()
// Decode from json
dec := codec.NewDecoderBytes([]byte(config), structs.JsonHandle)
var m map[string]interface{}
err := dec.Decode(&m)
if err != nil {
t.Fatalf("failed to decode: %v", err)
}
return m["Config"]
}