Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardev committed Jun 25, 2019
2 parents 21f4f4d + 57c99ce commit 7aee7f9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 71 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ This is the number of seconds you want Gonsul to wait between checks on the repo
This is the file extensions that Gonsul should consider as inputs to populate our Consul. Please set each extension
without the dot, and separate each extension with a comma.


### `--timeout`
> `require:` **no**
> `default:` **5**
> `example:` **`--timeout=20`**
The number of seconds for the client to wait for a response from Consul


## Gonsul Exit Codes
Whenever an error occurs, and Gonsul exits with a code other than 0, we try to return a meaningful code, such as:

Expand Down
88 changes: 47 additions & 41 deletions configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"errors"
"flag"
"fmt"
"github.com/miniclip/gonsul/interfaces"
"io/ioutil"
"os"
"strings"
"github.com/miniclip/gonsul/interfaces"
)

const StrategyDry = "DRYRUN"
Expand All @@ -21,26 +21,27 @@ const StrategyHook = "HOOK"
var config *Config

type Config struct {
shouldClone bool
logLevel int
strategy string
repoUrl string
repoSSHKey string
repoSSHUser string
repoBranch string
repoRemoteName string
repoBasePath string
repoRootDir string
consulURL string
consulACL string
consulBasePath string
expandJSON bool
doSecrets bool
secretsMap map[string]string
allowDeletes bool
pollInterval int
Working chan bool
validExtensions []string
shouldClone bool
logLevel int
strategy string
repoUrl string
repoSSHKey string
repoSSHUser string
repoBranch string
repoRemoteName string
repoBasePath string
repoRootDir string
consulURL string
consulACL string
consulBasePath string
expandJSON bool
doSecrets bool
secretsMap map[string]string
allowDeletes bool
pollInterval int
Working chan bool
validExtensions []string
timeout int
}

func GetConfig(flagParser interfaces.IConfigFlags) (*Config, error) {
Expand Down Expand Up @@ -111,26 +112,27 @@ func buildConfig(flags interfaces.ConfigFlags) (*Config, error) {
}

return &Config{
shouldClone: clone,
logLevel: errorLevel,
strategy: strategy,
repoUrl: *flags.RepoURL,
repoSSHKey: *flags.RepoSSHKey,
repoSSHUser: *flags.RepoSSHUser,
repoBranch: *flags.RepoBranch,
repoRemoteName: *flags.RepoRemoteName,
repoBasePath: *flags.RepoBasePath,
repoRootDir: *flags.RepoRootDir,
consulURL: *flags.ConsulURL,
consulACL: *flags.ConsulACL,
consulBasePath: *flags.ConsulBasePath,
expandJSON: *flags.ExpandJSON,
doSecrets: doSecrets,
secretsMap: secrets,
allowDeletes: *flags.AllowDeletes,
pollInterval: *flags.PollInterval,
Working: make(chan bool, 1),
validExtensions: extensions,
shouldClone: clone,
logLevel: errorLevel,
strategy: strategy,
repoUrl: *flags.RepoURL,
repoSSHKey: *flags.RepoSSHKey,
repoSSHUser: *flags.RepoSSHUser,
repoBranch: *flags.RepoBranch,
repoRemoteName: *flags.RepoRemoteName,
repoBasePath: *flags.RepoBasePath,
repoRootDir: *flags.RepoRootDir,
consulURL: *flags.ConsulURL,
consulACL: *flags.ConsulACL,
consulBasePath: *flags.ConsulBasePath,
expandJSON: *flags.ExpandJSON,
doSecrets: doSecrets,
secretsMap: secrets,
allowDeletes: *flags.AllowDeletes,
pollInterval: *flags.PollInterval,
Working: make(chan bool, 1),
validExtensions: extensions,
timeout: *flags.Timeout,
}, nil
}

Expand Down Expand Up @@ -210,6 +212,10 @@ func (config *Config) GetValidExtensions() []string {
return config.validExtensions
}

func (config *Config) GetTimeout() int {
return config.timeout
}

func buildSecretsMap(secretsFile string, repoRootPath string) (map[string]string, error) {
var file = secretsFile
if _, err := os.Stat(file); os.IsNotExist(err) {
Expand Down
59 changes: 30 additions & 29 deletions configuration/config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package configuration

import (
"testing"
. "github.com/onsi/gomega"
"testing"

"github.com/miniclip/gonsul/tests/mocks"
"fmt"
"github.com/miniclip/gonsul/errorutil"
"github.com/miniclip/gonsul/interfaces"
"fmt"
"github.com/miniclip/gonsul/tests/mocks"
)

func TestGetConfigSuccess(t *testing.T) {
Expand All @@ -34,6 +34,7 @@ func TestGetConfigSuccess(t *testing.T) {
false,
60,
"json,txt,ini",
10,
)

// Setup expectations
Expand Down Expand Up @@ -71,47 +72,47 @@ func TestGetConfigMultipleFail(t *testing.T) {
}
}


func getMultipleWrongConfigs() []interfaces.ConfigFlags {
return []interfaces.ConfigFlags{
getConfigFlagsFor("WRONG_LOG_LEVEL", StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
getConfigFlagsFor(errorutil.LogDebug, "WRONG_STRATEGY", "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, ""),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-fail.json", false, 60, "json,txt,ini"),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-non-existent.json", false, 60, "json,txt,ini"),
getConfigFlagsFor("WRONG_LOG_LEVEL", StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
getConfigFlagsFor(errorutil.LogDebug, "WRONG_STRATEGY", "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "", 10),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-fail.json", false, 60, "json,txt,ini", 10),
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-non-existent.json", false, 60, "json,txt,ini", 10),
}
}


func getConfigFlagsFor(
ll, s, ru, rsk, rsu, rb, rrn, rbp, rr, cu, ca, cbp string,
ej bool,
sf string,
ad bool,
pi int,
ie string,
ti int,
) interfaces.ConfigFlags {
configFlags := interfaces.ConfigFlags{
LogLevel: &ll,
Strategy: &s,
RepoURL: &ru,
RepoSSHKey: &rsk,
RepoSSHUser: &rsu,
RepoBranch: &rb,
RepoRemoteName: &rrn,
RepoBasePath: &rbp,
RepoRootDir: &rr,
ConsulURL: &cu,
ConsulACL: &ca,
ConsulBasePath: &cbp,
ExpandJSON: &ej,
SecretsFile: &sf,
AllowDeletes: &ad,
PollInterval: &pi,
LogLevel: &ll,
Strategy: &s,
RepoURL: &ru,
RepoSSHKey: &rsk,
RepoSSHUser: &rsu,
RepoBranch: &rb,
RepoRemoteName: &rrn,
RepoBasePath: &rbp,
RepoRootDir: &rr,
ConsulURL: &cu,
ConsulACL: &ca,
ConsulBasePath: &cbp,
ExpandJSON: &ej,
SecretsFile: &sf,
AllowDeletes: &ad,
PollInterval: &pi,
ValidExtensions: &ie,
Timeout: &ti,
}

return configFlags
}
}
1 change: 1 addition & 0 deletions configuration/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (flags *ConfigFlagsParser) Parse() interfaces.ConfigFlags {
flags.Flags.AllowDeletes = flag.Bool("allow-deletes", false, "Show Gonsul issue deletes? (If not, nothing will be done and a report on conflicting deletes will be shown) (Default false)")
flags.Flags.PollInterval = flag.Int("poll-interval", 60, "The number of seconds for the repository polling interval")
flags.Flags.ValidExtensions = flag.String("input-ext", "json,txt,ini", "A comma separated list of file extensions valid as input")
flags.Flags.Timeout = flag.Int("timeout", 5, "The number of seconds for the client to wait for a response from Consul")

// Parse our command line flags
flag.Parse()
Expand Down
4 changes: 4 additions & 0 deletions importer/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func createLiveData(client *http.Client) map[string]string {
return nil
}

if resp.StatusCode >= 400 {
errorutil.ExitError(errors.New("Invalid response from consul: "+resp.Status), errorutil.ErrorFailedConsulConnection, &logger)
}

// Read response from HTTP Response
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Start(localData map[string]string, conf *configuration.Config, log *errorut
// create a Client
// A Client is an HTTP client
client := &http.Client{
Timeout: time.Second * 5,
Timeout: time.Second * time.Duration(conf.GetTimeout()),
}

// Populate our Consul live data
Expand Down
1 change: 1 addition & 0 deletions interfaces/configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ConfigFlags struct {
AllowDeletes *bool
PollInterval *int
ValidExtensions *string
Timeout *int
}

type IConfigFlags interface {
Expand Down

0 comments on commit 7aee7f9

Please sign in to comment.