forked from coreos/etcd-ca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add workflow and basic tests
- Loading branch information
Showing
2 changed files
with
112 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,34 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRunnable(t *testing.T) { | ||
cmd := exec.Command("../bin/etcd-ca") | ||
_, err := cmd.CombinedOutput() | ||
const ( | ||
binPath = "../bin/etcd-ca" | ||
depotDir = ".etcd-ca-test" | ||
hostname = "host1" | ||
) | ||
|
||
func run(command string, args ...string) (string, string, error) { | ||
var stdoutBytes, stderrBytes bytes.Buffer | ||
args = append([]string{"--depot-path", depotDir}, args...) | ||
cmd := exec.Command(command, args...) | ||
cmd.Stdout = &stdoutBytes | ||
cmd.Stderr = &stderrBytes | ||
err := cmd.Run() | ||
return stdoutBytes.String(), stderrBytes.String(), err | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("Received unexpected error: %v", err) | ||
func TestVersion(t *testing.T) { | ||
stdout, stderr, err := run(binPath, "--version") | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if !strings.Contains(stdout, "version") { | ||
t.Fatalf("Received unexpected stdout: %v", stdout) | ||
} | ||
} |
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,88 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// TestWorkflow runs etcd-ca in the normal workflow | ||
// and traverses all commands | ||
func TestWorkflow(t *testing.T) { | ||
os.RemoveAll(depotDir) | ||
defer os.RemoveAll(depotDir) | ||
|
||
stdout, stderr, err := run(binPath, "init") | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "Created") != 2 { | ||
t.Fatalf("Received insufficient create: %v", stdout) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "new-cert", hostname) | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "Created") != 2 { | ||
t.Fatalf("Received insufficient create: %v", stdout) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "sign", hostname) | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "Created") != 1 { | ||
t.Fatalf("Received insufficient create: %v", stdout) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "chain") | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "CERTIFICATE") != 2 { | ||
t.Fatalf("Received insufficient CERTIFICATE: %v", stdout) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "chain", hostname) | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "CERTIFICATE") != 4 { | ||
t.Fatalf("Received insufficient CERTIFICATE: %v", stdout) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "export", hostname) | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
ioutil.WriteFile("1.tar", []byte(stdout), 0644) | ||
defer os.Remove("1.tar") | ||
|
||
err = exec.Command("tar", "xvf", "1.tar").Run() | ||
if err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
keyFile := fmt.Sprintf("%v.host.priv.key", hostname) | ||
crtFile := fmt.Sprintf("%v.host.crt", hostname) | ||
defer os.Remove(keyFile) | ||
defer os.Remove(crtFile) | ||
if _, err = os.Stat(keyFile); err != nil { | ||
t.Fatal("Failed stating", keyFile) | ||
} | ||
if _, err = os.Stat(crtFile); err != nil { | ||
t.Fatal("Failed stating", keyFile) | ||
} | ||
|
||
stdout, stderr, err = run(binPath, "status") | ||
if stderr != "" || err != nil { | ||
t.Fatalf("Received unexpected error: %v, %v", stderr, err) | ||
} | ||
if strings.Count(stdout, "expiration") != 2 { | ||
t.Fatalf("Received insufficient expiration: %v", stdout) | ||
} | ||
|
||
} |