github.com/jeffalder/nr.yml/v3@v3.0.1-0.20200721182236-9572b92f5925/nr_yml/base_config_test.go (about)

     1  //   Copyright 2020, Jeff Alder
     2  //
     3  //   Licensed under the Apache License, Version 2.0 (the "License");
     4  //   you may not use this file except in compliance with the License.
     5  //   You may obtain a copy of the License at
     6  //
     7  //       http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  package nr_yml
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/newrelic/go-agent/v3/newrelic"
    19  	"github.com/stretchr/testify/assert"
    20  	"testing"
    21  )
    22  
    23  func TestIndividualItems(t *testing.T) {
    24  	withContents(`
    25  production:
    26    agent_enabled: false
    27    high_security: true
    28    security_policies_token: ffff-0000-ffff-0000
    29    host: staging-collector.example.com
    30    labels:
    31      label1: value1
    32      label2: value2
    33  `, t, func(filename string, t *testing.T) {
    34  		cfg := new(newrelic.Config)
    35  		ConfigFromYamlFile(filename)(cfg)
    36  		assert.NoError(t, cfg.Error)
    37  		assert.False(t, cfg.Enabled)
    38  		assert.True(t, cfg.HighSecurity)
    39  		assert.Equal(t, "ffff-0000-ffff-0000", cfg.SecurityPoliciesToken)
    40  		assert.Equal(t, "staging-collector.example.com", cfg.Host)
    41  		assert.Equal(t, "value1", cfg.Labels["label1"])
    42  		assert.Equal(t, "value2", cfg.Labels["label2"])
    43  	})
    44  }
    45  
    46  type MockLogger struct{}
    47  
    48  func (MockLogger) Error(msg string, context map[string]interface{}) {}
    49  func (MockLogger) Warn(msg string, context map[string]interface{})  {}
    50  func (MockLogger) Info(msg string, context map[string]interface{})  {}
    51  func (MockLogger) Debug(msg string, context map[string]interface{}) {}
    52  func (MockLogger) DebugEnabled() bool {
    53  	return false
    54  }
    55  
    56  func TestLogStream(t *testing.T) {
    57  	for _, logStream := range [...]string{"STDOUT", "Stdout", "stdout", "STDERR", "Stderr", "stderr"} {
    58  		withContents(fmt.Sprintf(`
    59  production:
    60    log_stream_name: %s
    61  `, logStream), t, func(filename string, t *testing.T) {
    62  			cfg := new(newrelic.Config)
    63  			mockLogger := new(MockLogger)
    64  			cfg.Logger = mockLogger
    65  			ConfigFromYamlFile(filename)(cfg)
    66  			assert.NoError(t, cfg.Error)
    67  			assert.NotEqual(t, mockLogger, cfg.Logger)
    68  			assert.False(t, cfg.Logger.DebugEnabled())
    69  		})
    70  	}
    71  }
    72  
    73  func TestDebugLogging(t *testing.T) {
    74  	for _, logLevel := range [...]string{"DEBUG", "Debug", "debug"} {
    75  		withContents(fmt.Sprintf(`
    76  production:
    77    log_stream_name: STDOUT
    78    log_level: %s
    79  `, logLevel), t, func(filename string, t *testing.T) {
    80  			cfg := new(newrelic.Config)
    81  			ConfigFromYamlFile(filename)(cfg)
    82  			assert.NoError(t, cfg.Error)
    83  			assert.True(t, cfg.Logger.DebugEnabled())
    84  		})
    85  	}
    86  }
    87  
    88  func TestOtherLogLevelSettingIsNonDebug(t *testing.T) {
    89  	withContents(`
    90  production:
    91    log_stream_name: STDOUT
    92    log_level: info
    93  		`, t, func(filename string, t *testing.T) {
    94  		cfg := new(newrelic.Config)
    95  		ConfigFromYamlFile(filename)(cfg)
    96  		assert.NoError(t, cfg.Error)
    97  		assert.False(t, cfg.Logger.DebugEnabled())
    98  	})
    99  }