|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package cfgwarn |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/joeshaw/multierror" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + |
| 27 | + "github.com/elastic/elastic-agent-libs/config" |
| 28 | +) |
| 29 | + |
| 30 | +func TestRemovedSetting(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + cfg *config.C |
| 34 | + lookup string |
| 35 | + expected error |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "no obsolete setting", |
| 39 | + lookup: "notfound", |
| 40 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 41 | + "hello.world": "ok", |
| 42 | + }), |
| 43 | + expected: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "obsolete setting found", |
| 47 | + lookup: "hello", |
| 48 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 49 | + "hello.world": "ok", |
| 50 | + }), |
| 51 | + expected: errors.New("setting 'hello' has been removed"), |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + functions := []struct { |
| 56 | + name string |
| 57 | + fn func(*config.C, string) error |
| 58 | + }{ |
| 59 | + {name: "checkRemovedSetting", fn: checkRemovedSetting}, |
| 60 | + {name: "checkRemoved6xSetting", fn: CheckRemoved6xSetting}, |
| 61 | + } |
| 62 | + |
| 63 | + for _, function := range functions { |
| 64 | + t.Run(function.name, func(t *testing.T) { |
| 65 | + for _, test := range tests { |
| 66 | + t.Run(test.name, func(t *testing.T) { |
| 67 | + err := function.fn(test.cfg, test.lookup) |
| 68 | + assert.Equal(t, test.expected, err) |
| 69 | + }) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestRemovedSettings(t *testing.T) { |
| 76 | + tests := []struct { |
| 77 | + name string |
| 78 | + cfg *config.C |
| 79 | + lookup []string |
| 80 | + expected error |
| 81 | + }{ |
| 82 | + { |
| 83 | + name: "no obsolete setting", |
| 84 | + lookup: []string{"notfound"}, |
| 85 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 86 | + "hello.world": "ok", |
| 87 | + }), |
| 88 | + expected: nil, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "obsolete setting found", |
| 92 | + lookup: []string{"hello"}, |
| 93 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 94 | + "hello.world": "ok", |
| 95 | + }), |
| 96 | + expected: multierror.Errors{errors.New("setting 'hello' has been removed")}.Err(), |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "multiple obsolete settings", |
| 100 | + lookup: []string{"hello", "bad"}, |
| 101 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 102 | + "hello.world": "ok", |
| 103 | + "bad": "true", |
| 104 | + }), |
| 105 | + expected: multierror.Errors{ |
| 106 | + errors.New("setting 'hello' has been removed"), |
| 107 | + errors.New("setting 'bad' has been removed"), |
| 108 | + }.Err(), |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "multiple obsolete settings not on first level", |
| 112 | + lookup: []string{"filebeat.config.prospectors", "filebeat.prospectors"}, |
| 113 | + cfg: config.MustNewConfigFrom(map[string]interface{}{ |
| 114 | + "filebeat.prospectors": "ok", |
| 115 | + "filebeat.config.prospectors": map[string]interface{}{"ok": "ok1"}, |
| 116 | + }), |
| 117 | + expected: multierror.Errors{ |
| 118 | + errors.New("setting 'filebeat.config.prospectors' has been removed"), |
| 119 | + errors.New("setting 'filebeat.prospectors' has been removed"), |
| 120 | + }.Err(), |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + functions := []struct { |
| 125 | + name string |
| 126 | + fn func(*config.C, ...string) error |
| 127 | + }{ |
| 128 | + {name: "checkRemovedSetting", fn: checkRemovedSettings}, |
| 129 | + {name: "checkRemoved6xSetting", fn: CheckRemoved6xSettings}, |
| 130 | + } |
| 131 | + |
| 132 | + for _, function := range functions { |
| 133 | + t.Run(function.name, func(t *testing.T) { |
| 134 | + for _, test := range tests { |
| 135 | + t.Run(test.name, func(t *testing.T) { |
| 136 | + err := checkRemovedSettings(test.cfg, test.lookup...) |
| 137 | + assert.Equal(t, test.expected, err) |
| 138 | + }) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments