github.com/hernad/nomad@v1.6.112/command/node_meta_apply_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/ci" 10 "github.com/hernad/nomad/helper/pointer" 11 "github.com/shoenig/test/must" 12 ) 13 14 func TestNodeMeta_parseMapFromArgs(t *testing.T) { 15 ci.Parallel(t) 16 17 cases := []struct { 18 name string 19 input []string 20 exp map[string]*string 21 }{ 22 { 23 name: "EmptyEquals", 24 input: []string{"key1=val1", "key2=val2", "key3="}, 25 exp: map[string]*string{ 26 "key1": pointer.Of("val1"), 27 "key2": pointer.Of("val2"), 28 "key3": pointer.Of(""), 29 }, 30 }, 31 { 32 name: "EmptyNoEquals", 33 input: []string{"key1=val1", "key2=val2", "key4"}, 34 exp: map[string]*string{ 35 "key1": pointer.Of("val1"), 36 "key2": pointer.Of("val2"), 37 "key4": pointer.Of(""), 38 }, 39 }, 40 { 41 name: "Nil", 42 input: nil, 43 exp: map[string]*string{}, 44 }, 45 { 46 name: "Empty", 47 input: []string{}, 48 exp: map[string]*string{}, 49 }, 50 { 51 name: "EmptyArg", 52 input: []string{""}, 53 exp: map[string]*string{ 54 "": pointer.Of(""), 55 }, 56 }, 57 { 58 name: "WeirdArgs", 59 input: []string{"=", "foo==bar"}, 60 exp: map[string]*string{ 61 "": pointer.Of(""), 62 "foo": pointer.Of("=bar"), 63 }, 64 }, 65 { 66 name: "WeirderArgs", 67 input: []string{"=foo=bar", "\x00=\x01"}, 68 exp: map[string]*string{ 69 "": pointer.Of("foo=bar"), 70 "\x00": pointer.Of("\x01"), 71 }, 72 }, 73 } 74 75 for i := range cases { 76 tc := cases[i] 77 t.Run(tc.name, func(t *testing.T) { 78 must.MapEq(t, tc.exp, parseMapFromArgs(tc.input)) 79 }) 80 } 81 } 82 83 func TestNodeMeta_applyNodeMetaUnset(t *testing.T) { 84 ci.Parallel(t) 85 86 cases := []struct { 87 name string 88 unset string 89 meta map[string]*string 90 exp map[string]*string 91 }{ 92 { 93 name: "CommaParty", 94 unset: ",,,", 95 meta: map[string]*string{ 96 "foo": pointer.Of("bar"), 97 }, 98 exp: map[string]*string{ 99 "foo": pointer.Of("bar"), 100 }, 101 }, 102 { 103 name: "Empty", 104 unset: "", 105 meta: map[string]*string{ 106 "foo": pointer.Of("bar"), 107 }, 108 exp: map[string]*string{ 109 "foo": pointer.Of("bar"), 110 }, 111 }, 112 { 113 name: "UnsetNew", 114 unset: "unset", 115 meta: map[string]*string{ 116 "foo": pointer.Of("bar"), 117 }, 118 exp: map[string]*string{ 119 "foo": pointer.Of("bar"), 120 "unset": nil, 121 }, 122 }, 123 { 124 name: "UnsetExisting", 125 unset: "foo", 126 meta: map[string]*string{ 127 "foo": pointer.Of("bar"), 128 }, 129 exp: map[string]*string{ 130 "foo": nil, 131 }, 132 }, 133 { 134 name: "UnsetBoth", 135 unset: ",foo,unset,", 136 meta: map[string]*string{ 137 "foo": pointer.Of("bar"), 138 }, 139 exp: map[string]*string{ 140 "foo": nil, 141 "unset": nil, 142 }, 143 }, 144 } 145 146 for i := range cases { 147 tc := cases[i] 148 t.Run(tc.name, func(t *testing.T) { 149 applyNodeMetaUnset(tc.meta, tc.unset) 150 must.MapEq(t, tc.exp, tc.meta) 151 }) 152 } 153 }