Skip to content

Commit 7aee7f9

Browse files
committed
fix test
2 parents 21f4f4d + 57c99ce commit 7aee7f9

File tree

7 files changed

+93
-71
lines changed

7 files changed

+93
-71
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ This is the number of seconds you want Gonsul to wait between checks on the repo
308308
This is the file extensions that Gonsul should consider as inputs to populate our Consul. Please set each extension
309309
without the dot, and separate each extension with a comma.
310310

311+
312+
### `--timeout`
313+
> `require:` **no**
314+
> `default:` **5**
315+
> `example:` **`--timeout=20`**
316+
317+
The number of seconds for the client to wait for a response from Consul
318+
319+
311320
## Gonsul Exit Codes
312321
Whenever an error occurs, and Gonsul exits with a code other than 0, we try to return a meaningful code, such as:
313322

configuration/config.go

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"errors"
88
"flag"
99
"fmt"
10+
"github.com/miniclip/gonsul/interfaces"
1011
"io/ioutil"
1112
"os"
1213
"strings"
13-
"github.com/miniclip/gonsul/interfaces"
1414
)
1515

1616
const StrategyDry = "DRYRUN"
@@ -21,26 +21,27 @@ const StrategyHook = "HOOK"
2121
var config *Config
2222

2323
type Config struct {
24-
shouldClone bool
25-
logLevel int
26-
strategy string
27-
repoUrl string
28-
repoSSHKey string
29-
repoSSHUser string
30-
repoBranch string
31-
repoRemoteName string
32-
repoBasePath string
33-
repoRootDir string
34-
consulURL string
35-
consulACL string
36-
consulBasePath string
37-
expandJSON bool
38-
doSecrets bool
39-
secretsMap map[string]string
40-
allowDeletes bool
41-
pollInterval int
42-
Working chan bool
43-
validExtensions []string
24+
shouldClone bool
25+
logLevel int
26+
strategy string
27+
repoUrl string
28+
repoSSHKey string
29+
repoSSHUser string
30+
repoBranch string
31+
repoRemoteName string
32+
repoBasePath string
33+
repoRootDir string
34+
consulURL string
35+
consulACL string
36+
consulBasePath string
37+
expandJSON bool
38+
doSecrets bool
39+
secretsMap map[string]string
40+
allowDeletes bool
41+
pollInterval int
42+
Working chan bool
43+
validExtensions []string
44+
timeout int
4445
}
4546

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

113114
return &Config{
114-
shouldClone: clone,
115-
logLevel: errorLevel,
116-
strategy: strategy,
117-
repoUrl: *flags.RepoURL,
118-
repoSSHKey: *flags.RepoSSHKey,
119-
repoSSHUser: *flags.RepoSSHUser,
120-
repoBranch: *flags.RepoBranch,
121-
repoRemoteName: *flags.RepoRemoteName,
122-
repoBasePath: *flags.RepoBasePath,
123-
repoRootDir: *flags.RepoRootDir,
124-
consulURL: *flags.ConsulURL,
125-
consulACL: *flags.ConsulACL,
126-
consulBasePath: *flags.ConsulBasePath,
127-
expandJSON: *flags.ExpandJSON,
128-
doSecrets: doSecrets,
129-
secretsMap: secrets,
130-
allowDeletes: *flags.AllowDeletes,
131-
pollInterval: *flags.PollInterval,
132-
Working: make(chan bool, 1),
133-
validExtensions: extensions,
115+
shouldClone: clone,
116+
logLevel: errorLevel,
117+
strategy: strategy,
118+
repoUrl: *flags.RepoURL,
119+
repoSSHKey: *flags.RepoSSHKey,
120+
repoSSHUser: *flags.RepoSSHUser,
121+
repoBranch: *flags.RepoBranch,
122+
repoRemoteName: *flags.RepoRemoteName,
123+
repoBasePath: *flags.RepoBasePath,
124+
repoRootDir: *flags.RepoRootDir,
125+
consulURL: *flags.ConsulURL,
126+
consulACL: *flags.ConsulACL,
127+
consulBasePath: *flags.ConsulBasePath,
128+
expandJSON: *flags.ExpandJSON,
129+
doSecrets: doSecrets,
130+
secretsMap: secrets,
131+
allowDeletes: *flags.AllowDeletes,
132+
pollInterval: *flags.PollInterval,
133+
Working: make(chan bool, 1),
134+
validExtensions: extensions,
135+
timeout: *flags.Timeout,
134136
}, nil
135137
}
136138

@@ -210,6 +212,10 @@ func (config *Config) GetValidExtensions() []string {
210212
return config.validExtensions
211213
}
212214

215+
func (config *Config) GetTimeout() int {
216+
return config.timeout
217+
}
218+
213219
func buildSecretsMap(secretsFile string, repoRootPath string) (map[string]string, error) {
214220
var file = secretsFile
215221
if _, err := os.Stat(file); os.IsNotExist(err) {

configuration/config_test.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package configuration
22

33
import (
4-
"testing"
54
. "github.com/onsi/gomega"
5+
"testing"
66

7-
"github.com/miniclip/gonsul/tests/mocks"
7+
"fmt"
88
"github.com/miniclip/gonsul/errorutil"
99
"github.com/miniclip/gonsul/interfaces"
10-
"fmt"
10+
"github.com/miniclip/gonsul/tests/mocks"
1111
)
1212

1313
func TestGetConfigSuccess(t *testing.T) {
@@ -34,6 +34,7 @@ func TestGetConfigSuccess(t *testing.T) {
3434
false,
3535
60,
3636
"json,txt,ini",
37+
10,
3738
)
3839

3940
// Setup expectations
@@ -71,47 +72,47 @@ func TestGetConfigMultipleFail(t *testing.T) {
7172
}
7273
}
7374

74-
7575
func getMultipleWrongConfigs() []interfaces.ConfigFlags {
7676
return []interfaces.ConfigFlags{
77-
getConfigFlagsFor("WRONG_LOG_LEVEL", StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
78-
getConfigFlagsFor(errorutil.LogDebug, "WRONG_STRATEGY", "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
79-
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
80-
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini"),
81-
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, ""),
82-
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-fail.json", false, 60, "json,txt,ini"),
83-
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-non-existent.json", false, 60, "json,txt,ini"),
77+
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),
78+
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),
79+
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
80+
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "", "", false, "tests/test-secrets-file-success.json", false, 60, "json,txt,ini", 10),
81+
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-success.json", false, 60, "", 10),
82+
getConfigFlagsFor(errorutil.LogDebug, StrategyOnce, "", "", "", "", "", "/", "./..", "http://consul.com", "some-acl-1234567890-qwerty", "", false, "tests/test-secrets-file-fail.json", false, 60, "json,txt,ini", 10),
83+
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),
8484
}
8585
}
8686

87-
8887
func getConfigFlagsFor(
8988
ll, s, ru, rsk, rsu, rb, rrn, rbp, rr, cu, ca, cbp string,
9089
ej bool,
9190
sf string,
9291
ad bool,
9392
pi int,
9493
ie string,
94+
ti int,
9595
) interfaces.ConfigFlags {
9696
configFlags := interfaces.ConfigFlags{
97-
LogLevel: &ll,
98-
Strategy: &s,
99-
RepoURL: &ru,
100-
RepoSSHKey: &rsk,
101-
RepoSSHUser: &rsu,
102-
RepoBranch: &rb,
103-
RepoRemoteName: &rrn,
104-
RepoBasePath: &rbp,
105-
RepoRootDir: &rr,
106-
ConsulURL: &cu,
107-
ConsulACL: &ca,
108-
ConsulBasePath: &cbp,
109-
ExpandJSON: &ej,
110-
SecretsFile: &sf,
111-
AllowDeletes: &ad,
112-
PollInterval: &pi,
97+
LogLevel: &ll,
98+
Strategy: &s,
99+
RepoURL: &ru,
100+
RepoSSHKey: &rsk,
101+
RepoSSHUser: &rsu,
102+
RepoBranch: &rb,
103+
RepoRemoteName: &rrn,
104+
RepoBasePath: &rbp,
105+
RepoRootDir: &rr,
106+
ConsulURL: &cu,
107+
ConsulACL: &ca,
108+
ConsulBasePath: &cbp,
109+
ExpandJSON: &ej,
110+
SecretsFile: &sf,
111+
AllowDeletes: &ad,
112+
PollInterval: &pi,
113113
ValidExtensions: &ie,
114+
Timeout: &ti,
114115
}
115116

116117
return configFlags
117-
}
118+
}

configuration/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (flags *ConfigFlagsParser) Parse() interfaces.ConfigFlags {
3131
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)")
3232
flags.Flags.PollInterval = flag.Int("poll-interval", 60, "The number of seconds for the repository polling interval")
3333
flags.Flags.ValidExtensions = flag.String("input-ext", "json,txt,ini", "A comma separated list of file extensions valid as input")
34+
flags.Flags.Timeout = flag.Int("timeout", 5, "The number of seconds for the client to wait for a response from Consul")
3435

3536
// Parse our command line flags
3637
flag.Parse()

importer/helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func createLiveData(client *http.Client) map[string]string {
9797
return nil
9898
}
9999

100+
if resp.StatusCode >= 400 {
101+
errorutil.ExitError(errors.New("Invalid response from consul: "+resp.Status), errorutil.ErrorFailedConsulConnection, &logger)
102+
}
103+
100104
// Read response from HTTP Response
101105
bodyBytes, err := ioutil.ReadAll(resp.Body)
102106
if err != nil {

importer/importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Start(localData map[string]string, conf *configuration.Config, log *errorut
2929
// create a Client
3030
// A Client is an HTTP client
3131
client := &http.Client{
32-
Timeout: time.Second * 5,
32+
Timeout: time.Second * time.Duration(conf.GetTimeout()),
3333
}
3434

3535
// Populate our Consul live data

interfaces/configurations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ConfigFlags struct {
1818
AllowDeletes *bool
1919
PollInterval *int
2020
ValidExtensions *string
21+
Timeout *int
2122
}
2223

2324
type IConfigFlags interface {

0 commit comments

Comments
 (0)