Skip to content

Commit

Permalink
Test exclude functionality in cli/config/integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kyhavlov committed Aug 2, 2016
1 parent 37e2df0 commit eaa0a19
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Usage
| `syslog-facility` | The facility to use when sending to syslog. This requires the use of `-syslog`. The default value is `LOCAL0`.
| `token` | The [Consul API token][Consul ACLs]. There is no default value.
| `prefix`* | The source prefix including the , with optional destination prefix, separated by a colon (`:`). This option is additive and may be specified multiple times for multiple prefixes to replicate.
| `exclude`* | A prefix to exclude replication of. This option is additive and may be specified multiple times for multiple prefixes to replicate.
| `exclude` | A prefix to exclude during replication. This option is additive and may be specified multiple times for multiple prefixes to exclude.
| `wait` | The `minimum(:maximum)` to wait for stability before replicating, separated by a colon (`:`). If the optional maximum value is omitted, it is assumed to be 4x the required minimum value. There is no default value.
| `retry` | The amount of time to wait if Consul returns an error when communicating with the API. The default value is 5 seconds.
| `config` | The path to a configuration file or directory of configuration files on disk, relative to the current working directory. Values specified on the CLI take precedence over values specified in the configuration file. There is no default value.
Expand Down Expand Up @@ -81,6 +81,15 @@ $ consul-replicate \
-once
```

Replicate all keys under "global" from the nyc1 , but exclude the global/private prefix:

```shell
$ consul-replicate \
-prefix "global@nyc1" \
-exclude "global/private" \
-once
```

### Configuration File(s)
The Consul Replicate configuration files are written in [HashiCorp Configuration Language (HCL)][HCL]. By proxy, this means the Consul Replicate configuration file is JSON-compatible. For more information, please see the [HCL specification][HCL].

Expand Down
19 changes: 19 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,25 @@ func TestParseFlags_prefixes(t *testing.T) {
}
}

func TestParseFlags_excludes(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, err := cli.parseFlags([]string{
"-exclude", "excluded/",
})
if err != nil {
t.Fatal(err)
}

if len(config.Excludes) != 1 {
t.Fatal("expected 1 exclude")
}

exclude := config.Excludes[0]
if exclude.Source != "excluded/" {
t.Errorf("expected %q to be %q", exclude.Source, "excluded/")
}
}

func TestParseFlags_syslog(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, err := cli.parseFlags([]string{
Expand Down
26 changes: 26 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ func TestMerge_Prefixes(t *testing.T) {
}
}

func TestMerge_Excludes(t *testing.T) {
config1 := testConfig(`
exclude {
source = "foo"
}
`, t)
config2 := testConfig(`
exclude {
source = "foo-2"
}
`, t)
config1.Merge(config2)

if len(config1.Excludes) != 2 {
t.Fatalf("bad excludes %d", len(config1.Excludes))
}

if config1.Excludes[0].Source != "foo" {
t.Errorf("bad source: %#v", config1.Excludes[0].Source)
}

if config1.Excludes[1].Source != "foo-2" {
t.Errorf("bad source: %#v", config1.Excludes[1].Source)
}
}

func TestMerge_wait(t *testing.T) {
config1 := testConfig(`
wait = "1s:1s"
Expand Down
6 changes: 3 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ func (r *Runner) replicate(prefix *Prefix, excludes []*Exclude, doneCh chan stru
if len(excludes) > 0 {
excluded := false
for _, exclude := range excludes {
if strings.HasPrefix(key, exclude.Source) {
log.Printf("[DEBUG] (runner) key %q has prefix %q, excluding"+
key, exclude.Source)
if strings.HasPrefix(pair.Path, exclude.Source) {
log.Printf("[DEBUG] (runner) key %q has prefix %q, excluding",
pair.Path, exclude.Source)
excluded = true
}
}
Expand Down
3 changes: 3 additions & 0 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestNewRunner_initialize(t *testing.T) {
&Prefix{SourceRaw: "3", Destination: "6"},
&Prefix{SourceRaw: "4", Destination: "7"},
},
Excludes: []*Exclude{
&Exclude{Source: "3"},
},
}

runner, err := NewRunner(config, once)
Expand Down
7 changes: 6 additions & 1 deletion scripts/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ echo $CONSUL_REPLICATE_BIN
$CONSUL_REPLICATE_BIN \
-consul $ADDRESS_DC2 \
-prefix "global@dc1:backup" \
-exclude "global/555" \
-log-level $LOG_LEVEL &
CONSUL_REPLICATE_PID=$!
sleep 5
Expand All @@ -80,7 +81,11 @@ echo
echo "CHECKING DC2 FOR REPLICATION"
for i in `seq 1 1000`;
do
curl -s $ADDRESS_DC2/v1/kv/backup/$i | grep "dGVzdCBkYXRh"
if [ $i -ne "555" ]; then
curl -s $ADDRESS_DC2/v1/kv/backup/$i | grep "dGVzdCBkYXRh"
else
curl -sw '%{http_code}' $ADDRESS_DC2/v1/kv/backup/$i | grep "404"
fi
done

echo
Expand Down

0 comments on commit eaa0a19

Please sign in to comment.