From 74a9c0a66852d66346079bdb1e718e256e44eb6f Mon Sep 17 00:00:00 2001 From: Dan Jaglowski Date: Tue, 27 Sep 2022 11:26:04 -0400 Subject: [PATCH] [pkg/stanza] Remove deprecated test functions from operatortest This PR completes the migration to operatortest's new confmap-based unmarshaling strategy. All preexisting usages of operatortest have been migrated to the new format. The changes here are just to remove the old code and remaining usages of it. --- pkg/stanza/fileconsumer/config_test.go | 74 ---------------- pkg/stanza/go.mod | 2 +- pkg/stanza/operator/input/file/config_test.go | 80 ----------------- .../{operatortest_confmap.go => confmap.go} | 0 .../operator/operatortest/mapstructure.go | 52 ----------- .../operator/operatortest/operatortest.go | 86 ------------------- 6 files changed, 1 insertion(+), 293 deletions(-) rename pkg/stanza/operator/operatortest/{operatortest_confmap.go => confmap.go} (100%) delete mode 100644 pkg/stanza/operator/operatortest/mapstructure.go delete mode 100644 pkg/stanza/operator/operatortest/operatortest.go diff --git a/pkg/stanza/fileconsumer/config_test.go b/pkg/stanza/fileconsumer/config_test.go index f22b232b2153e..9409f74202fb1 100644 --- a/pkg/stanza/fileconsumer/config_test.go +++ b/pkg/stanza/fileconsumer/config_test.go @@ -20,7 +20,6 @@ import ( "testing" "time" - "github.com/mitchellh/mapstructure" "github.com/stretchr/testify/require" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" @@ -505,76 +504,3 @@ func TestBuild(t *testing.T) { }) } } - -func NewTestConfig() *Config { - cfg := NewConfig() - cfg.Include = []string{"i1", "i2"} - cfg.Exclude = []string{"e1", "e2"} - cfg.Splitter = helper.NewSplitterConfig() - cfg.Splitter.Multiline = helper.MultilineConfig{ - LineStartPattern: "start", - LineEndPattern: "end", - } - cfg.FingerprintSize = 1024 - cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "utf16"} - return cfg -} - -func TestMapStructureDecodeConfigWithHook(t *testing.T) { - expect := NewTestConfig() - cfgMap := map[string]interface{}{ - "attributes": map[string]interface{}{}, - "resource": map[string]interface{}{}, - "include": expect.Include, - "exclude": expect.Exclude, - "poll_interval": 200 * time.Millisecond, - "multiline": map[string]interface{}{ - "line_start_pattern": expect.Splitter.Multiline.LineStartPattern, - "line_end_pattern": expect.Splitter.Multiline.LineEndPattern, - }, - "force_flush_period": 500 * time.Millisecond, - "include_file_name": true, - "include_file_path": false, - "start_at": "end", - "fingerprint_size": "1024", - "max_log_size": "1mib", - "max_concurrent_files": 1024, - "encoding": "utf16", - } - - var actual Config - dc := &mapstructure.DecoderConfig{Result: &actual, DecodeHook: operatortest.JSONUnmarshalerHook()} - ms, err := mapstructure.NewDecoder(dc) - require.NoError(t, err) - err = ms.Decode(cfgMap) - require.NoError(t, err) - require.Equal(t, expect, &actual) -} - -func TestMapStructureDecodeConfig(t *testing.T) { - expect := NewTestConfig() - cfgMap := map[string]interface{}{ - "attributes": map[string]interface{}{}, - "resource": map[string]interface{}{}, - "include": expect.Include, - "exclude": expect.Exclude, - "poll_interval": 200 * time.Millisecond, - "multiline": map[string]interface{}{ - "line_start_pattern": expect.Splitter.Multiline.LineStartPattern, - "line_end_pattern": expect.Splitter.Multiline.LineEndPattern, - }, - "include_file_name": true, - "include_file_path": false, - "start_at": "end", - "fingerprint_size": 1024, - "max_log_size": 1024 * 1024, - "max_concurrent_files": 1024, - "encoding": "utf16", - "force_flush_period": 500 * time.Millisecond, - } - - var actual Config - err := mapstructure.Decode(cfgMap, &actual) - require.NoError(t, err) - require.Equal(t, expect, &actual) -} diff --git a/pkg/stanza/go.mod b/pkg/stanza/go.mod index 45ee4cb6f04ad..7eeb9560f1b36 100644 --- a/pkg/stanza/go.mod +++ b/pkg/stanza/go.mod @@ -7,7 +7,7 @@ require ( github.com/bmatcuk/doublestar/v3 v3.0.0 github.com/jpillora/backoff v1.0.0 github.com/json-iterator/go v1.1.12 - github.com/mitchellh/mapstructure v1.5.0 + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/observiq/ctimefmt v1.0.0 github.com/observiq/nanojack v0.0.0-20201106172433-343928847ebc github.com/stretchr/testify v1.8.0 diff --git a/pkg/stanza/operator/input/file/config_test.go b/pkg/stanza/operator/input/file/config_test.go index d2cccb2b62fe5..5976bcd27efd0 100644 --- a/pkg/stanza/operator/input/file/config_test.go +++ b/pkg/stanza/operator/input/file/config_test.go @@ -21,7 +21,6 @@ import ( "testing" "time" - "github.com/mitchellh/mapstructure" "github.com/stretchr/testify/require" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" @@ -673,82 +672,3 @@ func requireSamePreEmitOptions(t *testing.T, expect, actual []preEmitOption) { require.Equal(t, expectFuncName, actualFuncName) } } - -func NewTestConfig() *Config { - cfg := NewConfigWithID("config_test") - cfg.Include = []string{"i1", "i2"} - cfg.Exclude = []string{"e1", "e2"} - cfg.Splitter = helper.NewSplitterConfig() - cfg.Splitter.Multiline = helper.MultilineConfig{ - LineStartPattern: "start", - LineEndPattern: "end", - } - cfg.FingerprintSize = 1024 - cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "utf16"} - return cfg -} - -func TestMapStructureDecodeConfigWithHook(t *testing.T) { - expect := NewTestConfig() - input := map[string]interface{}{ - // Config - "id": "config_test", - "type": "file_input", - "attributes": map[string]interface{}{}, - "resource": map[string]interface{}{}, - "include": expect.Include, - "exclude": expect.Exclude, - "poll_interval": 200 * time.Millisecond, - "multiline": map[string]interface{}{ - "line_start_pattern": expect.Splitter.Multiline.LineStartPattern, - "line_end_pattern": expect.Splitter.Multiline.LineEndPattern, - }, - "force_flush_period": 500 * time.Millisecond, - "include_file_name": true, - "include_file_path": false, - "start_at": "end", - "fingerprint_size": "1024", - "max_log_size": "1mib", - "max_concurrent_files": 1024, - "encoding": "utf16", - } - - var actual Config - dc := &mapstructure.DecoderConfig{Result: &actual, DecodeHook: operatortest.JSONUnmarshalerHook()} - ms, err := mapstructure.NewDecoder(dc) - require.NoError(t, err) - err = ms.Decode(input) - require.NoError(t, err) - require.Equal(t, expect, &actual) -} - -func TestMapStructureDecodeConfig(t *testing.T) { - expect := NewTestConfig() - input := map[string]interface{}{ - // Config - "id": "config_test", - "type": "file_input", - "attributes": map[string]interface{}{}, - "resource": map[string]interface{}{}, - "include": expect.Include, - "exclude": expect.Exclude, - "poll_interval": 200 * time.Millisecond, - "multiline": map[string]interface{}{ - "line_start_pattern": expect.Splitter.Multiline.LineStartPattern, - "line_end_pattern": expect.Splitter.Multiline.LineEndPattern, - }, - "include_file_name": true, - "include_file_path": false, - "start_at": "end", - "fingerprint_size": 1024, - "max_log_size": 1024 * 1024, - "max_concurrent_files": 1024, - "encoding": "utf16", - "force_flush_period": 500 * time.Millisecond, - } - - var actual Config - err := mapstructure.Decode(input, &actual) - require.NoError(t, err) - require.Equal(t, expect, &actual) -} diff --git a/pkg/stanza/operator/operatortest/operatortest_confmap.go b/pkg/stanza/operator/operatortest/confmap.go similarity index 100% rename from pkg/stanza/operator/operatortest/operatortest_confmap.go rename to pkg/stanza/operator/operatortest/confmap.go diff --git a/pkg/stanza/operator/operatortest/mapstructure.go b/pkg/stanza/operator/operatortest/mapstructure.go deleted file mode 100644 index 66afc1d25fc96..0000000000000 --- a/pkg/stanza/operator/operatortest/mapstructure.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package operatortest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest" - -import ( - "encoding/json" - "reflect" - - "github.com/mitchellh/mapstructure" -) - -// make mapstructure use struct UnmarshalJSON to decode -func JSONUnmarshalerHook() mapstructure.DecodeHookFunc { - return func(from reflect.Value, to reflect.Value) (interface{}, error) { - if to.CanAddr() { - to = to.Addr() - } - - // If the destination implements the unmarshaling interface - u, ok := to.Interface().(json.Unmarshaler) - if !ok { - return from.Interface(), nil - } - - // If it is nil and a pointer, create and assign the target value first - if to.IsNil() && to.Type().Kind() == reflect.Ptr { - to.Set(reflect.New(to.Type().Elem())) - u = to.Interface().(json.Unmarshaler) - } - v := from.Interface() - bytes, err := json.Marshal(v) - if err != nil { - return nil, err - } - if err := u.UnmarshalJSON(bytes); err != nil { - return to.Interface(), err - } - return to.Interface(), nil - } -} diff --git a/pkg/stanza/operator/operatortest/operatortest.go b/pkg/stanza/operator/operatortest/operatortest.go deleted file mode 100644 index b7f268d1ca5a2..0000000000000 --- a/pkg/stanza/operator/operatortest/operatortest.go +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package operatortest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest" - -import ( - "fmt" - "os" - "path" - "testing" - - "github.com/mitchellh/mapstructure" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v2" -) - -func configFromFileViaYaml(file string, config interface{}) error { - bytes, err := os.ReadFile(file) // #nosec - configs load based on user specified directory - if err != nil { - return fmt.Errorf("could not find config file: %w", err) - } - if err := yaml.Unmarshal(bytes, config); err != nil { - return fmt.Errorf("failed to read config file as yaml: %w", err) - } - - return nil -} - -func configFromFileViaMapstructure(file string, config interface{}) error { - bytes, err := os.ReadFile(file) // #nosec - configs load based on user specified directory - if err != nil { - return fmt.Errorf("could not find config file: %w", err) - } - - raw := map[string]interface{}{} - - if err = yaml.Unmarshal(bytes, raw); err != nil { - return fmt.Errorf("failed to read data from yaml: %w", err) - } - - dc := &mapstructure.DecoderConfig{ - Result: config, - DecodeHook: mapstructure.ComposeDecodeHookFunc( - mapstructure.StringToTimeDurationHookFunc(), - JSONUnmarshalerHook(), - ), - } - ms, err := mapstructure.NewDecoder(dc) - if err != nil { - return err - } - err = ms.Decode(raw) - if err != nil { - return err - } - return nil -} - -// Run Unmarshalls yaml files and compares them against the expected. -func (c ConfigUnmarshalTest) RunDeprecated(t *testing.T, config interface{}) { - mapConfig := config - yamlConfig := config - yamlErr := configFromFileViaYaml(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", c.Name)), yamlConfig) - mapErr := configFromFileViaMapstructure(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", c.Name)), mapConfig) - - if c.ExpectErr { - require.Error(t, mapErr) - require.Error(t, yamlErr) - } else { - require.NoError(t, yamlErr) - require.Equal(t, c.Expect, yamlConfig) - require.NoError(t, mapErr) - require.Equal(t, c.Expect, mapConfig) - } -}