|
| 1 | +package util // nolint:revive |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + . "github.com/onsi/gomega" // nolint:revive |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + defaultTestIniOption = IniOption{ |
| 11 | + Section: "foo", |
| 12 | + Key: "s3_store_cacert", |
| 13 | + Value: "/etc/pki/tls/certs/ca-bundle.crt", |
| 14 | + Unique: true, |
| 15 | + } |
| 16 | + |
| 17 | + aliasKeyIniOption = IniOption{ |
| 18 | + Section: "pci", |
| 19 | + Key: "alias", |
| 20 | + Value: "{ \"device_type\": \"type-VF\", \"resource_class\": \"CUSTOM_A16_16A\", \"name\": \"A16_16A\" }", |
| 21 | + Unique: false, |
| 22 | + } |
| 23 | + |
| 24 | + deviceSpecIniOption = IniOption{ |
| 25 | + Section: "pci", |
| 26 | + Key: "device_spec", |
| 27 | + Value: "{ \"vendor_id\": \"10de\", \"product_id\": \"25b6\", \"address\": \"0000:25:00.6\", \"resource_class\": \"CUSTOM_A16_8A\", \"managed\": \"yes\" }", |
| 28 | + Unique: false, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +var tests = []struct { |
| 33 | + name string |
| 34 | + input string |
| 35 | + option IniOption |
| 36 | + expected string |
| 37 | + err string |
| 38 | +}{ |
| 39 | + { |
| 40 | + name: "empty customServiceConfig", |
| 41 | + input: "", |
| 42 | + option: defaultTestIniOption, |
| 43 | + expected: "", |
| 44 | + err: "", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "field to non-existing section", |
| 48 | + input: `[DEFAULT] |
| 49 | +debug=true |
| 50 | +enabled_backends = backend:s3 |
| 51 | +[foo] |
| 52 | +bar = bar |
| 53 | +foo = foo |
| 54 | +[backend] |
| 55 | +option1 = value1`, |
| 56 | + option: IniOption{ |
| 57 | + Section: "bar", |
| 58 | + Key: "foo", |
| 59 | + Value: "foo", |
| 60 | + }, |
| 61 | + expected: `[DEFAULT] |
| 62 | +debug=true |
| 63 | +enabled_backends = backend:s3 |
| 64 | +[foo] |
| 65 | +bar = bar |
| 66 | +foo = foo |
| 67 | +[backend] |
| 68 | +option1 = value1`, |
| 69 | + err: "could not patch target section: bar", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "add new ini line to a section in the middle", |
| 73 | + input: `[DEFAULT] |
| 74 | +debug=true |
| 75 | +enabled_backends = backend:s3 |
| 76 | +[foo] |
| 77 | +bar = bar |
| 78 | +foo = foo |
| 79 | +[backend] |
| 80 | +option1 = value1`, |
| 81 | + option: defaultTestIniOption, |
| 82 | + expected: `[DEFAULT] |
| 83 | +debug=true |
| 84 | +enabled_backends = backend:s3 |
| 85 | +[foo] |
| 86 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt |
| 87 | +bar = bar |
| 88 | +foo = foo |
| 89 | +[backend] |
| 90 | +option1 = value1`, |
| 91 | + err: "", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "section is not found, return it unchanged", |
| 95 | + input: `[DEFAULT] |
| 96 | +debug=true |
| 97 | +enabled_backends = backend:s3 |
| 98 | +[backend] |
| 99 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt `, |
| 100 | + option: defaultTestIniOption, |
| 101 | + expected: `[DEFAULT] |
| 102 | +debug=true |
| 103 | +enabled_backends = backend:s3 |
| 104 | +[backend] |
| 105 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt `, |
| 106 | + err: "could not patch target section: foo", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Add option to a section at the very beginning of customServiceConfig", |
| 110 | + input: `[foo] |
| 111 | +bar = bar |
| 112 | +foo = foo |
| 113 | +[backend] |
| 114 | +option1 = value1 `, |
| 115 | + option: defaultTestIniOption, |
| 116 | + expected: `[foo] |
| 117 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt |
| 118 | +bar = bar |
| 119 | +foo = foo |
| 120 | +[backend] |
| 121 | +option1 = value1 `, |
| 122 | + err: "", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "Add option to a section at the very bottom of customServiceConfig", |
| 126 | + input: `[DEFAULT] |
| 127 | +debug=true |
| 128 | +enabled_backends = backend:s3 |
| 129 | +[backend] |
| 130 | +# this is a comment |
| 131 | +option1 = value1 |
| 132 | +[foo] |
| 133 | +# this is a comment |
| 134 | +bar = bar |
| 135 | +foo = foo`, |
| 136 | + option: defaultTestIniOption, |
| 137 | + expected: `[DEFAULT] |
| 138 | +debug=true |
| 139 | +enabled_backends = backend:s3 |
| 140 | +[backend] |
| 141 | +# this is a comment |
| 142 | +option1 = value1 |
| 143 | +[foo] |
| 144 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt |
| 145 | +# this is a comment |
| 146 | +bar = bar |
| 147 | +foo = foo`, |
| 148 | + err: "", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "Add option to an empty target section", |
| 152 | + input: `[DEFAULT] |
| 153 | +debug=true |
| 154 | +[foo]`, |
| 155 | + option: defaultTestIniOption, |
| 156 | + expected: `[DEFAULT] |
| 157 | +debug=true |
| 158 | +[foo] |
| 159 | +s3_store_cacert = /etc/pki/tls/certs/ca-bundle.crt`, |
| 160 | + err: "", |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "key/value already present in the target section", |
| 164 | + input: `[DEFAULT] |
| 165 | +debug=true |
| 166 | +[foo] |
| 167 | +s3_store_cacert = /my/custom/path/ca-bundle.crt`, |
| 168 | + option: defaultTestIniOption, |
| 169 | + expected: `[DEFAULT] |
| 170 | +debug=true |
| 171 | +[foo] |
| 172 | +s3_store_cacert = /my/custom/path/ca-bundle.crt`, |
| 173 | + err: "key already exists in section: key s3_store_cacert in section foo", |
| 174 | + }, |
| 175 | + { |
| 176 | + name: "add new ini line anyway even though a section contains the same key ", |
| 177 | + input: `[pci] |
| 178 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.4", "resource_class": "CUSTOM_A16_16A", "managed": "no" } |
| 179 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.5", "resource_class": "CUSTOM_A16_8A", "managed": "no" } |
| 180 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_8A", "name": "A16_8A" }`, |
| 181 | + option: aliasKeyIniOption, |
| 182 | + expected: `[pci] |
| 183 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_16A", "name": "A16_16A" } |
| 184 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.4", "resource_class": "CUSTOM_A16_16A", "managed": "no" } |
| 185 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.5", "resource_class": "CUSTOM_A16_8A", "managed": "no" } |
| 186 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_8A", "name": "A16_8A" }`, |
| 187 | + err: "", |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "add new ini line anyway even though a section contains the same key ", |
| 191 | + input: `[pci] |
| 192 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.4", "resource_class": "CUSTOM_A16_16A", "managed": "no" } |
| 193 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.5", "resource_class": "CUSTOM_A16_8A", "managed": "no" } |
| 194 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_16A", "name": "A16_16A" } |
| 195 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_8A", "name": "A16_8A" }`, |
| 196 | + option: deviceSpecIniOption, |
| 197 | + expected: `[pci] |
| 198 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.6", "resource_class": "CUSTOM_A16_8A", "managed": "yes" } |
| 199 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.4", "resource_class": "CUSTOM_A16_16A", "managed": "no" } |
| 200 | +device_spec = { "vendor_id": "10de", "product_id": "25b6", "address": "0000:25:00.5", "resource_class": "CUSTOM_A16_8A", "managed": "no" } |
| 201 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_16A", "name": "A16_16A" } |
| 202 | +alias = { "device_type": "type-VF", "resource_class": "CUSTOM_A16_8A", "name": "A16_8A" }`, |
| 203 | + err: "", |
| 204 | + }, |
| 205 | +} |
| 206 | + |
| 207 | +func TestExtendCustomServiceConfig(t *testing.T) { |
| 208 | + for _, tt := range tests { |
| 209 | + t.Run(tt.name, func(t *testing.T) { |
| 210 | + g := NewWithT(t) |
| 211 | + output, err := ExtendCustomServiceConfig(tt.input, tt.option) |
| 212 | + g.Expect(output).To(Equal(tt.expected)) |
| 213 | + if err != nil { |
| 214 | + // check the string matches the expected error message |
| 215 | + g.Expect(err.Error()).To(Equal(tt.err)) |
| 216 | + } else { |
| 217 | + g.Expect(err).ToNot(HaveOccurred()) |
| 218 | + } |
| 219 | + }) |
| 220 | + } |
| 221 | +} |
0 commit comments