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