2019-02-02 20:20:26 +00:00
|
|
|
package hclutils_test
|
2018-08-09 20:20:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl2/hcldec"
|
2019-02-02 20:20:26 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/docker"
|
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
|
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2019-03-06 02:47:06 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-08-09 20:20:10 +00:00
|
|
|
"github.com/kr/pretty"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseHclInterface_Hcl(t *testing.T) {
|
2019-02-02 20:20:26 +00:00
|
|
|
dockerDriver := new(docker.Driver)
|
|
|
|
dockerSpec, err := dockerDriver.TaskConfigSchema()
|
|
|
|
require.NoError(t, err)
|
|
|
|
dockerDecSpec, diags := hclspecutils.Convert(dockerSpec)
|
|
|
|
require.False(t, diags.HasErrors())
|
2018-08-09 20:20:10 +00:00
|
|
|
|
2019-02-02 20:20:26 +00:00
|
|
|
vars := map[string]cty.Value{
|
|
|
|
"NOMAD_ALLOC_INDEX": cty.NumberIntVal(2),
|
|
|
|
"NOMAD_META_hello": cty.StringVal("world"),
|
|
|
|
}
|
2018-08-09 20:20:10 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
config interface{}
|
|
|
|
spec hcldec.Spec
|
2019-02-02 20:20:26 +00:00
|
|
|
vars map[string]cty.Value
|
2018-08-09 20:20:10 +00:00
|
|
|
expected interface{}
|
|
|
|
expectedType interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single string attr",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-09 20:20:10 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single string attr json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2"
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "number attr",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
pids_limit = 2
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PidsLimit: 2,
|
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "number attr json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"pids_limit": "2"
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PidsLimit: 2,
|
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "number attr interpolated",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
pids_limit = "${2 + 2}"
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PidsLimit: 4,
|
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "number attr interploated json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"pids_limit": "${2 + 2}"
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PidsLimit: 4,
|
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi attr",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
args = ["foo", "bar"]
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
|
|
|
Args: []string{"foo", "bar"},
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi attr json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"args": ["foo", "bar"]
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
|
|
|
Args: []string{"foo", "bar"},
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi attr variables",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
args = ["${NOMAD_META_hello}", "${NOMAD_ALLOC_INDEX}"]
|
|
|
|
pids_limit = "${NOMAD_ALLOC_INDEX + 2}"
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
vars: vars,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
|
|
|
Args: []string{"world", "2"},
|
2019-02-02 20:20:26 +00:00
|
|
|
PidsLimit: 4,
|
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi attr variables json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"args": ["foo", "bar"]
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
|
|
|
Args: []string{"foo", "bar"},
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "port_map",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
port_map {
|
2019-02-02 20:20:26 +00:00
|
|
|
foo = 1234
|
|
|
|
bar = 5678
|
2018-08-10 18:14:12 +00:00
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PortMap: map[string]int{
|
|
|
|
"foo": 1234,
|
|
|
|
"bar": 5678,
|
2018-08-10 18:14:12 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "port_map json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"port_map": [{
|
2019-02-02 20:20:26 +00:00
|
|
|
"foo": 1234,
|
|
|
|
"bar": 5678
|
2018-08-10 18:14:12 +00:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
PortMap: map[string]int{
|
|
|
|
"foo": 1234,
|
|
|
|
"bar": 5678,
|
2018-08-10 18:14:12 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "devices",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2018-08-10 18:14:12 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
devices = [
|
|
|
|
{
|
|
|
|
host_path = "/dev/sda1"
|
|
|
|
container_path = "/dev/xvdc"
|
|
|
|
cgroup_permissions = "r"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host_path = "/dev/sda2"
|
|
|
|
container_path = "/dev/xvdd"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`),
|
2019-02-02 20:20:26 +00:00
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{
|
2018-08-09 20:20:10 +00:00
|
|
|
{
|
|
|
|
HostPath: "/dev/sda1",
|
|
|
|
ContainerPath: "/dev/xvdc",
|
|
|
|
CgroupPermissions: "r",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HostPath: "/dev/sda2",
|
|
|
|
ContainerPath: "/dev/xvdd",
|
|
|
|
},
|
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
Mounts: []docker.DockerMount{},
|
|
|
|
},
|
|
|
|
expectedType: &docker.TaskConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "docker_logging",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.HclConfigToInterface(t, `
|
2019-02-02 20:20:26 +00:00
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
network_mode = "host"
|
|
|
|
dns_servers = ["169.254.1.1"]
|
|
|
|
logging {
|
|
|
|
type = "syslog"
|
|
|
|
config {
|
|
|
|
tag = "driver-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
|
|
|
Image: "redis:3.2",
|
|
|
|
NetworkMode: "host",
|
|
|
|
DNSServers: []string{"169.254.1.1"},
|
|
|
|
Logging: docker.DockerLogging{
|
|
|
|
Type: "syslog",
|
|
|
|
Config: map[string]string{
|
|
|
|
"tag": "driver-test",
|
|
|
|
},
|
|
|
|
},
|
2019-02-04 18:56:05 +00:00
|
|
|
Devices: []docker.DockerDevice{},
|
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-02 20:20:26 +00:00
|
|
|
name: "docker_json",
|
2019-02-12 19:46:37 +00:00
|
|
|
config: hclutils.JsonConfigToInterface(t, `
|
2019-02-02 20:20:26 +00:00
|
|
|
{
|
|
|
|
"Config": {
|
|
|
|
"image": "redis:3.2",
|
|
|
|
"devices": [
|
|
|
|
{
|
|
|
|
"host_path": "/dev/sda1",
|
|
|
|
"container_path": "/dev/xvdc",
|
|
|
|
"cgroup_permissions": "r"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"host_path": "/dev/sda2",
|
|
|
|
"container_path": "/dev/xvdd"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
spec: dockerDecSpec,
|
|
|
|
expected: &docker.TaskConfig{
|
2018-08-09 20:20:10 +00:00
|
|
|
Image: "redis:3.2",
|
2019-02-02 20:20:26 +00:00
|
|
|
Devices: []docker.DockerDevice{
|
2018-08-09 20:20:10 +00:00
|
|
|
{
|
|
|
|
HostPath: "/dev/sda1",
|
|
|
|
ContainerPath: "/dev/xvdc",
|
|
|
|
CgroupPermissions: "r",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HostPath: "/dev/sda2",
|
|
|
|
ContainerPath: "/dev/xvdd",
|
|
|
|
},
|
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
Mounts: []docker.DockerMount{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
2019-02-02 20:20:26 +00:00
|
|
|
expectedType: &docker.TaskConfig{},
|
2018-08-09 20:20:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-02-02 20:20:26 +00:00
|
|
|
c := c
|
2018-08-09 20:20:10 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
t.Logf("Val: % #v", pretty.Formatter(c.config))
|
|
|
|
// Parse the interface
|
2019-02-02 20:20:26 +00:00
|
|
|
ctyValue, diag := hclutils.ParseHclInterface(c.config, c.spec, c.vars)
|
2018-08-09 20:20:10 +00:00
|
|
|
if diag.HasErrors() {
|
|
|
|
for _, err := range diag.Errs() {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-02-02 20:20:26 +00:00
|
|
|
// Test encoding
|
|
|
|
taskConfig := &drivers.TaskConfig{}
|
|
|
|
require.NoError(t, taskConfig.EncodeDriverConfig(ctyValue))
|
|
|
|
|
|
|
|
// Test decoding
|
|
|
|
require.NoError(t, taskConfig.DecodeDriverConfig(c.expectedType))
|
2018-08-09 20:20:10 +00:00
|
|
|
|
|
|
|
require.EqualValues(t, c.expected, c.expectedType)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 02:47:06 +00:00
|
|
|
|
|
|
|
func TestParseNullFields(t *testing.T) {
|
|
|
|
spec := hclspec.NewObject(map[string]*hclspec.Spec{
|
|
|
|
"array_field": hclspec.NewAttr("array_field", "list(string)", false),
|
|
|
|
"string_field": hclspec.NewAttr("string_field", "string", false),
|
|
|
|
"boolean_field": hclspec.NewAttr("boolean_field", "bool", false),
|
|
|
|
"number_field": hclspec.NewAttr("number_field", "number", false),
|
|
|
|
"block_field": hclspec.NewBlock("block_field", false, hclspec.NewObject((map[string]*hclspec.Spec{
|
|
|
|
"f": hclspec.NewAttr("f", "string", true),
|
|
|
|
}))),
|
|
|
|
"block_list_field": hclspec.NewBlockList("block_list_field", hclspec.NewObject((map[string]*hclspec.Spec{
|
|
|
|
"f": hclspec.NewAttr("f", "string", true),
|
|
|
|
}))),
|
|
|
|
})
|
|
|
|
|
|
|
|
type Sub struct {
|
|
|
|
F string `codec:"f"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskConfig struct {
|
|
|
|
Array []string `codec:"array_field"`
|
|
|
|
String string `codec:"string_field"`
|
|
|
|
Boolean bool `codec:"boolean_field"`
|
|
|
|
Number int64 `codec:"number_field"`
|
|
|
|
Block Sub `codec:"block_field"`
|
|
|
|
BlockList []Sub `codec:"block_list_field"`
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
json string
|
|
|
|
expected TaskConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"omitted fields",
|
|
|
|
`{"Config": {}}`,
|
|
|
|
TaskConfig{BlockList: []Sub{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"explicitly nil",
|
|
|
|
`{"Config": {
|
|
|
|
"array_field": null,
|
|
|
|
"string_field": null,
|
|
|
|
"boolean_field": null,
|
|
|
|
"number_field": null,
|
|
|
|
"block_field": null,
|
|
|
|
"block_list_field": null}}`,
|
|
|
|
TaskConfig{BlockList: []Sub{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// for sanity checking that the fields are actually set
|
|
|
|
"explicitly set to not null",
|
|
|
|
`{"Config": {
|
|
|
|
"array_field": ["a"],
|
|
|
|
"string_field": "a",
|
|
|
|
"boolean_field": true,
|
|
|
|
"number_field": 5,
|
|
|
|
"block_field": [{"f": "a"}],
|
|
|
|
"block_list_field": [{"f": "a"}, {"f": "b"}]}}`,
|
|
|
|
TaskConfig{
|
|
|
|
Array: []string{"a"},
|
|
|
|
String: "a",
|
|
|
|
Boolean: true,
|
|
|
|
Number: 5,
|
|
|
|
Block: Sub{"a"},
|
|
|
|
BlockList: []Sub{{"a"}, {"b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := hclutils.NewConfigParser(spec)
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
var tc TaskConfig
|
|
|
|
parser.ParseJson(t, c.json, &tc)
|
|
|
|
|
|
|
|
require.EqualValues(t, c.expected, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|