github.com/jeffalder/nr.yml/v3@v3.0.1-0.20200721182236-9572b92f5925/nr_yml/config_from_yaml_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  	"github.com/newrelic/go-agent/v3/newrelic"
    18  	"github.com/stretchr/testify/assert"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"testing"
    23  )
    24  
    25  func TestFileDoesNotExist(t *testing.T) {
    26  	cfg := new(newrelic.Config)
    27  	ConfigFromYamlFile("/tmp/file/does/not/exist/please/do/not/create/me")(cfg)
    28  	assert.Error(t, cfg.Error)
    29  }
    30  
    31  func TestEmptyConfigFileProducesError(t *testing.T) {
    32  	withContents("", t, func(filename string, t *testing.T) {
    33  		cfg := new(newrelic.Config)
    34  		ConfigFromYamlFile(filename)(cfg)
    35  		assert.Error(t, cfg.Error)
    36  	})
    37  }
    38  
    39  func TestGarbageConfigFileProducesError(t *testing.T) {
    40  	withContents("asdf", t, func(filename string, t *testing.T) {
    41  		cfg := new(newrelic.Config)
    42  		ConfigFromYamlFile(filename)(cfg)
    43  		assert.Error(t, cfg.Error)
    44  	})
    45  }
    46  
    47  func TestFileWithOnlyEnvNoError(t *testing.T) {
    48  	withContents("production:", t, func(filename string, t *testing.T) {
    49  		cfg := new(newrelic.Config)
    50  		ConfigFromYamlFile(filename)(cfg)
    51  		assert.NoError(t, cfg.Error)
    52  	})
    53  }
    54  
    55  func TestFileWithWrongEnvError(t *testing.T) {
    56  	withContents("other_env:", t, func(filename string, t *testing.T) {
    57  		cfg := new(newrelic.Config)
    58  		ConfigFromYamlFile(filename)(cfg)
    59  		assert.Error(t, cfg.Error)
    60  	})
    61  }
    62  
    63  func TestFileWithNonDefaultEnvNoError(t *testing.T) {
    64  	withContents("other_env:", t, func(filename string, t *testing.T) {
    65  		cfg := new(newrelic.Config)
    66  		ConfigFromYamlFileEnvironment(filename, "other_env")(cfg)
    67  		assert.NoError(t, cfg.Error)
    68  	})
    69  }
    70  
    71  func TestFileWithAnchorAliasIncludes(t *testing.T) {
    72  	withContents(`
    73  common: &default
    74    app_name: app-default
    75    license_key: license-default
    76  production:
    77    <<: *default
    78    license_key: license-production
    79  `, t, func(filename string, t *testing.T) {
    80  		cfg := new(newrelic.Config)
    81  		ConfigFromYamlFile(filename)(cfg)
    82  		assert.NoError(t, cfg.Error)
    83  		assert.Equal(t, "app-default", cfg.AppName)
    84  		assert.Equal(t, "license-production", cfg.License)
    85  	})
    86  }
    87  
    88  func TestDefaultFile(t *testing.T) {
    89  	// create temp directory, queue for removal
    90  	tempDir, err := ioutil.TempDir(os.TempDir(), "somedir*")
    91  	assert.NoError(t, err)
    92  	defer os.RemoveAll(tempDir)
    93  
    94  	tempFile := path.Join(tempDir, "newrelic.yml")
    95  	err = ioutil.WriteFile(tempFile, []byte(`
    96  common: &default
    97    app_name: app-default
    98    license_key: license-default
    99  production:
   100    <<: *default
   101    license_key: license-production
   102  `), 0644)
   103  	assert.NoError(t, err)
   104  
   105  	wd, _ := os.Getwd()
   106  	assert.NoError(t, os.Chdir(tempDir))
   107  	defer os.Chdir(wd)
   108  	
   109  	cfg := new(newrelic.Config)
   110  	ConfigFromDefaultYaml()(cfg)
   111  	assert.NoError(t, cfg.Error)
   112  	assert.Equal(t, "app-default", cfg.AppName)
   113  	assert.Equal(t, "license-production", cfg.License)
   114  
   115  }