github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/helper/pluginutils/hclutils/types_test.go (about)

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