github.com/hernad/nomad@v1.6.112/helper/pluginutils/hclutils/types_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hclutils_test 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/helper/pluginutils/hclutils" 10 "github.com/hernad/nomad/plugins/shared/hclspec" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestMapStrInt_JsonArrays(t *testing.T) { 15 spec := hclspec.NewObject(map[string]*hclspec.Spec{ 16 "port_map": hclspec.NewAttr("port_map", "list(map(number))", false), 17 }) 18 19 type PidMapTaskConfig struct { 20 PortMap hclutils.MapStrInt `codec:"port_map"` 21 } 22 23 parser := hclutils.NewConfigParser(spec) 24 25 expected := PidMapTaskConfig{ 26 PortMap: map[string]int{ 27 "http": 80, 28 "https": 443, 29 "ssh": 25, 30 }, 31 } 32 33 t.Run("hcl case", func(t *testing.T) { 34 config := ` 35 config { 36 port_map { 37 http = 80 38 https = 443 39 ssh = 25 40 } 41 }` 42 // Test decoding 43 var tc PidMapTaskConfig 44 parser.ParseHCL(t, config, &tc) 45 46 require.EqualValues(t, expected, tc) 47 48 }) 49 jsonCases := []struct { 50 name string 51 json string 52 }{ 53 { 54 "array of map entries", 55 `{"Config": {"port_map": [{"http": 80}, {"https": 443}, {"ssh": 25}]}}`, 56 }, 57 { 58 "array with one map", 59 `{"Config": {"port_map": [{"http": 80, "https": 443, "ssh": 25}]}}`, 60 }, 61 { 62 "array of maps", 63 `{"Config": {"port_map": [{"http": 80, "https": 443}, {"ssh": 25}]}}`, 64 }, 65 } 66 67 for _, c := range jsonCases { 68 t.Run("json:"+c.name, func(t *testing.T) { 69 // Test decoding 70 var tc PidMapTaskConfig 71 parser.ParseJson(t, c.json, &tc) 72 73 require.EqualValues(t, expected, tc) 74 75 }) 76 } 77 } 78 79 func TestMapStrStr_JsonArrays(t *testing.T) { 80 spec := hclspec.NewObject(map[string]*hclspec.Spec{ 81 "port_map": hclspec.NewAttr("port_map", "list(map(string))", false), 82 }) 83 84 type PidMapTaskConfig struct { 85 PortMap hclutils.MapStrStr `codec:"port_map"` 86 } 87 88 parser := hclutils.NewConfigParser(spec) 89 90 expected := PidMapTaskConfig{ 91 PortMap: map[string]string{ 92 "http": "80", 93 "https": "443", 94 "ssh": "25", 95 }, 96 } 97 98 t.Run("hcl case", func(t *testing.T) { 99 config := ` 100 config { 101 port_map { 102 http = "80" 103 https = "443" 104 ssh = "25" 105 } 106 }` 107 // Test decoding 108 var tc PidMapTaskConfig 109 parser.ParseHCL(t, config, &tc) 110 111 require.EqualValues(t, expected, tc) 112 113 }) 114 jsonCases := []struct { 115 name string 116 json string 117 }{ 118 { 119 "array of map entries", 120 `{"Config": {"port_map": [{"http": "80"}, {"https": "443"}, {"ssh": "25"}]}}`, 121 }, 122 { 123 "array with one map", 124 `{"Config": {"port_map": [{"http": "80", "https": "443", "ssh": "25"}]}}`, 125 }, 126 { 127 "array of maps", 128 `{"Config": {"port_map": [{"http": "80", "https": "443"}, {"ssh": "25"}]}}`, 129 }, 130 } 131 132 for _, c := range jsonCases { 133 t.Run("json:"+c.name, func(t *testing.T) { 134 // Test decoding 135 var tc PidMapTaskConfig 136 parser.ParseJson(t, c.json, &tc) 137 138 require.EqualValues(t, expected, tc) 139 140 }) 141 } 142 }