Skip to content

Commit

Permalink
feat(tests): add workflow and basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengq committed Mar 12, 2014
1 parent b6d0c15 commit cc8452b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
29 changes: 24 additions & 5 deletions tests/basic_test.go
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)
}
}
88 changes: 88 additions & 0 deletions tests/workflow_test.go
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)
}

}

0 comments on commit cc8452b

Please sign in to comment.