This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from KohlsTechnology/e2e
Add automated end to end test
- Loading branch information
Showing
11 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"repos": [ | ||
{ | ||
"name": "e2e", | ||
"url": "http://github.com/KohlsTechnology/git2consul-go.git", | ||
"branches": [ | ||
"master" | ||
], | ||
"source_root": "/pkg/e2e/data/create/" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mozart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
classical |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"repos": [ | ||
{ | ||
"name": "e2e", | ||
"url": "http://github.com/KohlsTechnology/git2consul-go.git", | ||
"branches": [ | ||
"master" | ||
], | ||
"source_root": "/pkg/e2e/data/delete/" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mozart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"repos": [ | ||
{ | ||
"name": "e2e", | ||
"url": "http://github.com/KohlsTechnology/git2consul-go.git", | ||
"branches": [ | ||
"master" | ||
], | ||
"source_root": "/pkg/e2e/data/update/" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
beethoven |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
classical |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// +build e2e | ||
|
||
package e2e | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/consul/api" | ||
) | ||
|
||
func TestE2E(t *testing.T) { | ||
// Setup local Consul test server | ||
consulCmd := exec.Command("consul", "agent", "-dev") | ||
consulPipeReader, consulPipeWriter := io.Pipe() | ||
consulScanner := bufio.NewScanner(consulPipeReader) | ||
consulTee := io.MultiWriter(consulPipeWriter, os.Stderr) // dumping the output on Stderr can be useful when debugging this test | ||
|
||
consulCmd.Stdout = consulTee | ||
consulCmd.Stderr = consulTee | ||
if err := consulCmd.Start(); err != nil { | ||
t.Fatalf("failed to start local consul server: %s", err) | ||
} | ||
defer consulCmd.Process.Signal(syscall.SIGTERM) | ||
|
||
consulDone := make(chan error, 0) | ||
go func() { | ||
consulDone <- consulCmd.Wait() | ||
}() | ||
|
||
err := waitForString(consulScanner, 20*time.Second, "==> Consul agent running!") | ||
if err != nil { | ||
t.Fatal("initialization of consul server failed", err) | ||
} | ||
t.Log("initialization of consul server finished") | ||
|
||
client, err := api.NewClient(api.DefaultConfig()) | ||
if err != nil { | ||
t.Fatal("failed to initialize consul api client", err) | ||
} | ||
t.Log("initilization of consul api finished") | ||
kv := client.KV() | ||
|
||
projectDir, err := GetRootProjectDir() | ||
if err != nil { | ||
t.Fatal("failed to get working project directory", err) | ||
} | ||
|
||
g2cCmd := exec.Command(projectDir+"/git2consul", | ||
"-config", | ||
projectDir+"/pkg/e2e/data/create-config.json", | ||
"-debug", | ||
"-once") | ||
err = executeCommand(g2cCmd, "Terminating git2consul") | ||
if err != nil { | ||
t.Fatal("git2consul run failed", err) | ||
} else { | ||
t.Log("git2consul ran successfully") | ||
} | ||
|
||
pair, _, err := kv.Get("e2e/master/genre", nil) | ||
if err != nil { | ||
t.Fatal("failed to get pair e2e/master/genre", err) | ||
} | ||
if pair == nil { | ||
t.Fatal("did not find expected pair at e2e/master/genre") | ||
} | ||
if strings.TrimSpace(string(pair.Value)) != "classical" { | ||
t.Errorf("got %s want %s", string(pair.Value), "classical") | ||
} else { | ||
t.Log("confirmed e2e/master/genre was properly set") | ||
} | ||
|
||
// TODO: remove delete when issue #31 is resolved this is a workaround because if | ||
// the ref matches the current commit, the kv pairs will not be updated | ||
_, err = kv.Delete("e2e/master.ref", nil) | ||
if err != nil { | ||
t.Fatal("failed to delete git reference") | ||
} | ||
|
||
g2cCmd = exec.Command(projectDir+"/git2consul", | ||
"-config", | ||
projectDir+"/pkg/e2e/data/update-config.json", | ||
"-debug", | ||
"-once") | ||
err = executeCommand(g2cCmd, "Terminating git2consul") | ||
if err != nil { | ||
t.Fatal("git2consul run failed", err) | ||
} else { | ||
t.Log("git2consul ran successfully") | ||
} | ||
|
||
pair, _, err = kv.Get("e2e/master/artist", nil) | ||
if err != nil { | ||
t.Fatal("failed to get pair e2e/master/artist", err) | ||
} | ||
if pair == nil { | ||
t.Fatal("did not find expected pair at e2e/master/artist") | ||
} | ||
if strings.TrimSpace(string(pair.Value)) != "beethoven" { | ||
t.Errorf("got %s want %s", string(pair.Value), "beethoven") | ||
} else { | ||
t.Log("confirmed e2e/master/genre was properly updated") | ||
} | ||
|
||
// TODO: add stage to simulate delete of kv pair | ||
} | ||
|
||
func executeCommand(command *exec.Cmd, expectedLog string) (err error) { | ||
commandPipeReader, commandPipeWriter := io.Pipe() | ||
|
||
commandScanner := bufio.NewScanner(commandPipeReader) | ||
commandTee := io.MultiWriter(commandPipeWriter, os.Stderr) // dumping the output on Stderr can be useful when debugging this test | ||
|
||
command.Stdout = commandTee | ||
command.Stderr = commandTee | ||
if err := command.Start(); err != nil { | ||
return err | ||
} | ||
defer command.Process.Signal(syscall.SIGTERM) | ||
|
||
commandDone := make(chan error, 0) | ||
go func() { | ||
commandDone <- command.Wait() | ||
}() | ||
|
||
err = waitForString(commandScanner, 20*time.Second, expectedLog) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func waitForString(s *bufio.Scanner, timeout time.Duration, want string) error { | ||
done := make(chan error) | ||
go func() { | ||
for s.Scan() { | ||
if strings.Contains(s.Text(), want) { | ||
done <- nil | ||
return | ||
} | ||
} | ||
if s.Err() != nil { | ||
done <- s.Err() | ||
} | ||
done <- fmt.Errorf("process finished without printing expected string %q", want) | ||
}() | ||
select { | ||
case err := <-done: | ||
return err | ||
case <-time.After(timeout): | ||
return fmt.Errorf("wait for string %q timed out", want) | ||
} | ||
} | ||
|
||
// GetRootProjectDir returns path to root directory of project | ||
func GetRootProjectDir() (string, error) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
for !strings.HasSuffix(wd, "git2consul-go") { | ||
if wd == "/" { | ||
return "", errors.New(`cannot find project directory, "/" reached`) | ||
} | ||
wd = filepath.Dir(wd) | ||
} | ||
return wd, nil | ||
} |