Skip to content

Commit

Permalink
Remove the test dependency on redis
Browse files Browse the repository at this point in the history
Use miniredis instead.

main.go reads the config and sets up the storage, so there is no easy
way to replace the port when running the tests. The smallest change that
I could think of is not resetting it when running tests.

Fixes TykTechnologies#361.
  • Loading branch information
mvdan authored and lonelycode committed Jan 25, 2017
1 parent ce53a85 commit e24c521
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ matrix:
env: SKIP_LINT=true
- go: 1.7.x

services:
- redis-server

install:
- go get -t ./... # test deps
- go install -v ./...
- go get -u golang.org/x/tools/cmd/goimports

Expand Down
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ You need to have working Go enironment: see [golang.org](https://golang.org/doc/

To build and test Tyk use built-in `go` commands: `go build` and `go test -v`. If you want to just test a subset of the project, you can pass the `-run` argument with name of the test.

Currently in order for tests to pass, a **Redis host is required**. We know, this is terrible and should be handled with an interface, and it is, however in the current version there is a hard requirement for the application to have its default memory setup to use redis as part of a deployment, this is to make it easier to install the application for the end-user. Future versions will work around this, or we may drop the memory requirement. Simplest way to run Redis is to use official Docker image [https://hub.docker.com/_/redis/](https://hub.docker.com/_/redis/)

### Adding dependencies

If your patch depends on new packages, ensure that they will be put in `/vendor` folder. `git` is very handy when it comes to vendoring, because it automatically creates needed directories, example: `git clone https://github.com/Shopify/sarama.git vendor/github.com/Shopify/sarama`.

### Geo IP features
This product utilises GeoLite2 data created by MaxMind, available from [http://www.maxmind.com](http://www.maxmind.com).
This product utilises GeoLite2 data created by MaxMind, available from [http://www.maxmind.com](http://www.maxmind.com).
74 changes: 38 additions & 36 deletions config_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,32 @@ import (
const ENV_PREVIX = "TYK_GW"

// WriteDefaultConf will create a default configuration file and set the storage type to "memory"
func WriteDefaultConf(configStruct *Config) {
configStruct.ListenAddress = ""
configStruct.ListenPort = 8080
configStruct.Secret = "352d20ee67be67f6340b4c0605b044b7"
configStruct.TemplatePath = "./templates"
configStruct.TykJSPath = "./js/tyk.js"
configStruct.MiddlewarePath = "./middleware"
configStruct.Storage.Type = "redis"
configStruct.AppPath = "./apps/"
configStruct.Storage.Host = "localhost"
configStruct.Storage.Username = ""
configStruct.Storage.Password = ""
configStruct.Storage.Database = 0
configStruct.Storage.MaxIdle = 100
configStruct.Storage.Port = 6379
configStruct.EnableAnalytics = false
configStruct.HealthCheck.EnableHealthChecks = true
configStruct.HealthCheck.HealthCheckValueTimeout = 60
configStruct.AnalyticsConfig.IgnoredIPs = make([]string, 0)
configStruct.UseAsyncSessionWrite = false
configStruct.HideGeneratorHeader = false
configStruct.OauthRedirectUriSeparator = ""
newConfig, err := json.MarshalIndent(configStruct, "", " ")
overrideErr := envconfig.Process(ENV_PREVIX, &configStruct)
func WriteDefaultConf(conf *Config) {
conf.ListenAddress = ""
conf.ListenPort = 8080
conf.Secret = "352d20ee67be67f6340b4c0605b044b7"
conf.TemplatePath = "./templates"
conf.TykJSPath = "./js/tyk.js"
conf.MiddlewarePath = "./middleware"
conf.Storage.Type = "redis"
conf.AppPath = "./apps/"
conf.Storage.Host = "localhost"
conf.Storage.Username = ""
conf.Storage.Password = ""
conf.Storage.Database = 0
conf.Storage.MaxIdle = 100
if !runningTests {
conf.Storage.Port = 6379
}
conf.EnableAnalytics = false
conf.HealthCheck.EnableHealthChecks = true
conf.HealthCheck.HealthCheckValueTimeout = 60
conf.AnalyticsConfig.IgnoredIPs = make([]string, 0)
conf.UseAsyncSessionWrite = false
conf.HideGeneratorHeader = false
conf.OauthRedirectUriSeparator = ""
newConfig, err := json.MarshalIndent(conf, "", " ")
overrideErr := envconfig.Process(ENV_PREVIX, &conf)
if overrideErr != nil {
log.Error("Failed to process environment variables: ", overrideErr)
}
Expand All @@ -53,38 +55,38 @@ func WriteDefaultConf(configStruct *Config) {
// LoadConfig will load the configuration file from filePath, if it can't open
// the file for reading, it assumes there is no configuration file and will try to create
// one on the default path (tyk.conf in the local directory)
func loadConfig(filePath string, configStruct *Config) {
func loadConfig(filePath string, conf *Config) {
configuration, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load configuration file")
log.Error(err)
log.Info("Writing a default file to ./tyk.conf")

WriteDefaultConf(configStruct)
WriteDefaultConf(conf)

log.Info("Loading default configuration...")
loadConfig("tyk.conf", configStruct)
loadConfig("tyk.conf", conf)
} else {
if err := json.Unmarshal(configuration, &configStruct); err != nil {
if err := json.Unmarshal(configuration, &conf); err != nil {
log.Error("Couldn't unmarshal configuration")
log.Error(err)
}

overrideErr := envconfig.Process(ENV_PREVIX, configStruct)
overrideErr := envconfig.Process(ENV_PREVIX, conf)
if overrideErr != nil {
log.Error("Failed to process environment variables after file load: ", overrideErr)
}
}

if configStruct.SlaveOptions.CallTimeout == 0 {
configStruct.SlaveOptions.CallTimeout = 30
if conf.SlaveOptions.CallTimeout == 0 {
conf.SlaveOptions.CallTimeout = 30
}
if configStruct.SlaveOptions.PingTimeout == 0 {
configStruct.SlaveOptions.PingTimeout = 60
if conf.SlaveOptions.PingTimeout == 0 {
conf.SlaveOptions.PingTimeout = 60
}
GlobalRPCPingTimeout = time.Second * time.Duration(configStruct.SlaveOptions.PingTimeout)
GlobalRPCCallTimeout = time.Second * time.Duration(configStruct.SlaveOptions.CallTimeout)
configStruct.EventTriggers = InitGenericEventHandlers(configStruct.EventHandlers)
GlobalRPCPingTimeout = time.Second * time.Duration(conf.SlaveOptions.PingTimeout)
GlobalRPCCallTimeout = time.Second * time.Duration(conf.SlaveOptions.CallTimeout)
conf.EventTriggers = InitGenericEventHandlers(conf.EventHandlers)
}

func (c *Config) loadIgnoredIPs() {
Expand Down
15 changes: 13 additions & 2 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/alicebob/miniredis"
"github.com/justinas/alice"
)

func init() {
fmt.Println("THIS IS THE TEST SETUP INIT")
runningTests = true
}

func TestMain(m *testing.M) {
s, err := miniredis.Run()
if err != nil {
panic(err)
}
defer s.Close()
config.Storage.Port, _ = strconv.Atoi(s.Port())
initialiseSystem(map[string]interface{}{})
os.Exit(m.Run())
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand Down

0 comments on commit e24c521

Please sign in to comment.