github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/TykTechnologies/tyk/apidef"
    13  )
    14  
    15  func TestDefaultValueAndWriteDefaultConf(t *testing.T) {
    16  	cases := []struct {
    17  		FieldName   string
    18  		EnvVarName  string
    19  		FieldGetter func(*Config) interface{}
    20  
    21  		defaultValue  interface{}
    22  		expectedValue interface{}
    23  	}{
    24  		{
    25  			"ListenPort", "TYK_GW_LISTENPORT",
    26  			func(c *Config) interface{} { return c.ListenPort },
    27  			8080, 9090,
    28  		},
    29  		{
    30  			"DnsCacheEnabled", "TYK_GW_DNSCACHE_ENABLED",
    31  			func(c *Config) interface{} { return c.DnsCache.Enabled },
    32  			false, true,
    33  		},
    34  		{
    35  			"DnsCacheTTL", "TYK_GW_DNSCACHE_TTL",
    36  			func(c *Config) interface{} { return c.DnsCache.TTL },
    37  			int64(3600), int64(300),
    38  		},
    39  		{
    40  			"CheckInterval", "TYK_GW_DNSCACHE_CHECKINTERVAL",
    41  			func(c *Config) interface{} { return c.DnsCache.CheckInterval },
    42  			int64(60),
    43  			int64(60), //CheckInterval shouldn't be configured from *.conf and env var
    44  		},
    45  		{
    46  			"CheckMultipleIPsHandleStrategy", "TYK_GW_DNSCACHE_MULTIPLEIPSHANDLESTRATEGY",
    47  			func(c *Config) interface{} { return c.DnsCache.MultipleIPsHandleStrategy },
    48  			NoCacheStrategy,
    49  			RandomStrategy,
    50  		},
    51  	}
    52  
    53  	for _, tc := range cases {
    54  		t.Run(tc.FieldName, func(t *testing.T) {
    55  			conf := &Config{}
    56  			os.Unsetenv(tc.EnvVarName)
    57  			defer os.Unsetenv(tc.EnvVarName)
    58  			if err := WriteDefault("", conf); err != nil {
    59  				t.Fatal(err)
    60  			}
    61  			if !reflect.DeepEqual(tc.FieldGetter(conf), tc.defaultValue) {
    62  				t.Fatalf("Expected %v to be set to its default %v, but got %v", tc.FieldName, tc.defaultValue, tc.FieldGetter(conf))
    63  			}
    64  			expectedValue := fmt.Sprint(tc.expectedValue)
    65  			os.Setenv(tc.EnvVarName, expectedValue)
    66  			if err := WriteDefault("", conf); err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			if !reflect.DeepEqual(tc.FieldGetter(conf), tc.expectedValue) {
    70  				t.Fatalf("Expected %s to be set to %v, but got %v", tc.FieldName, tc.expectedValue, tc.FieldGetter(conf))
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestConfigFiles(t *testing.T) {
    77  	dir, err := ioutil.TempDir("", "tyk")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	defer os.RemoveAll(dir)
    82  	conf := &Config{}
    83  	path1 := filepath.Join(dir, "tyk1.conf")
    84  	path2 := filepath.Join(dir, "tyk2.conf")
    85  
    86  	if err := WriteDefault(path1, conf); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	if conf.ListenPort != 8080 {
    90  		t.Fatalf("Expected ListenPort to be set to its default")
    91  	}
    92  	bs, _ := ioutil.ReadFile(path1)
    93  	if !strings.Contains(string(bs), "8080") {
    94  		t.Fatalf("Expected 8080 to be in the written conf file")
    95  	}
    96  	os.Remove(path1)
    97  
    98  	paths := []string{path1, path2}
    99  	// should write default config to path1 and return nil
   100  	if err := Load(paths, conf); err != nil {
   101  		t.Fatalf("Load with no existing configs errored")
   102  	}
   103  	if _, err := os.Stat(path1); err != nil {
   104  		t.Fatalf("Load with no configs did not write a default config file")
   105  	}
   106  	if _, err := os.Stat(path2); err == nil {
   107  		t.Fatalf("Load with no configs wrote too many default config files")
   108  	}
   109  	if conf.OriginalPath != path1 {
   110  		t.Fatalf("OriginalPath was not set properly")
   111  	}
   112  
   113  	// both exist, we use path1
   114  	os.Link(path1, path2)
   115  	if err := Load(paths, conf); err != nil {
   116  		t.Fatalf("Load with an existing config errored")
   117  	}
   118  	if conf.OriginalPath != path1 {
   119  		t.Fatalf("OriginalPath was not set properly")
   120  	}
   121  
   122  	// path2 exists but path1 doesn't
   123  	os.Remove(path1)
   124  	if err := Load(paths, conf); err != nil {
   125  		t.Fatalf("Load with an existing config errored")
   126  	}
   127  	if _, err := os.Stat(path1); err == nil {
   128  		t.Fatalf("Load with a config wrote a default config file")
   129  	}
   130  	if conf.OriginalPath != path2 {
   131  		t.Fatalf("OriginalPath was not set properly")
   132  	}
   133  
   134  	// path1 exists but is invalid
   135  	os.Remove(path2)
   136  	ioutil.WriteFile(path1, []byte("{"), 0644)
   137  	if err := Load(paths, conf); err == nil {
   138  		t.Fatalf("Load with an invalid config did not error")
   139  	}
   140  }
   141  
   142  func TestConfig_GetEventTriggers(t *testing.T) {
   143  
   144  	assert := func(t *testing.T, config string, expected string) {
   145  		conf := &Config{}
   146  
   147  		f, err := ioutil.TempFile("", "tyk.conf")
   148  		if err != nil {
   149  			t.Fatal(err)
   150  		}
   151  		defer f.Close()
   152  
   153  		_, err = f.Write([]byte(config))
   154  		if err != nil {
   155  			t.Fatal(err)
   156  		}
   157  
   158  		paths := []string{f.Name()}
   159  
   160  		if err := Load(paths, conf); err != nil {
   161  			t.Fatal(err)
   162  		}
   163  
   164  		triggers := conf.GetEventTriggers()
   165  
   166  		if _, ok := triggers[apidef.TykEvent(expected)]; !ok || len(triggers) != 1 {
   167  			t.Fatal("Config is not loaded correctly")
   168  		}
   169  	}
   170  
   171  	t.Run("Deprecated configuration", func(t *testing.T) {
   172  		deprecated := `{"event_trigers_defunct": {"deprecated": []}}`
   173  		assert(t, deprecated, "deprecated")
   174  	})
   175  
   176  	t.Run("Current configuration", func(t *testing.T) {
   177  		current := `{"event_triggers_defunct": {"current": []}}`
   178  		assert(t, current, "current")
   179  	})
   180  
   181  	t.Run("Both configured", func(t *testing.T) {
   182  		both := `{"event_trigers_defunct": {"deprecated": []}, "event_triggers_defunct": {"current": []}}`
   183  		assert(t, both, "current")
   184  	})
   185  
   186  }