starters_test.go 2.78 KB
Newer Older
1 2 3
package tests

import (
4
	"bytes"
5 6 7 8 9
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
10 11 12
	"testing"

	"github.com/bmizerany/assert"
13 14 15 16 17 18
)

const testPort = "9090"

var currentClient = 0

19
// startPlatform creates root certificate for the platform and starts the platform.
20
func startPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssp")

	// Create temporary directory for platform
	dir, err := ioutil.TempDir(tmpDir, "p_")
	if err != nil {
		return nil, nil, err
	}

	// Init
	cmd := exec.Command(path, "-cn", "localhost", "-path", dir, "-v", "init")
	err = cmd.Run()
	if err != nil {
		return nil, nil, err
	}

	// Get root certificate
	ca, err := ioutil.ReadFile(filepath.Join(dir, "dfssp_rootCA.pem"))
	if err != nil {
		return nil, nil, err
	}

	// Start
43
	cmd = exec.Command(path, "-db", dbURI, "-path", dir, "-p", testPort, "-v", "start")
44 45 46 47 48 49 50
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Start()

	return cmd, ca, err
}

51
// createClient creates a new working directory for a client, creating ca.pem.
52 53
// It returns a ready-to-run command, but you probably want to set the last argument of the command.
func createClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")

	// Create temporary directory for client
	clientID := strconv.Itoa(currentClient)
	currentClient++
	dir, err := ioutil.TempDir(tmpDir, "c"+clientID+"_")
	if err != nil {
		return nil, err
	}

	caPath := filepath.Join(dir, "ca.pem")
	certPath := filepath.Join(dir, "cert.pem")
	keyPath := filepath.Join(dir, "key.pem")

	// Write the CA
	err = ioutil.WriteFile(caPath, ca, os.ModePerm)
	if err != nil {
		return nil, err
	}

	// Prepare the client command.
	// The last argument is up to you!
76 77
	cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "localhost:"+testPort, "-key", keyPath, "-v")

78 79
	return cmd, nil
}
80

81 82
// newClient clones the current command to another one.
// It's very useful when doing several commands on the same client.
83 84 85 86 87 88 89 90
func newClient(old *exec.Cmd) *exec.Cmd {
	cmd := exec.Command(old.Path)
	cmd.Args = old.Args
	cmd.Stdout = old.Stdout
	cmd.Stderr = old.Stderr
	return cmd
}

91
// setLastArg sets or updates the last argument of a command.
92 93 94 95 96 97
func setLastArg(cmd *exec.Cmd, str string, override bool) {
	if override {
		cmd.Args = cmd.Args[:(len(cmd.Args) - 1)]
	}
	cmd.Args = append(cmd.Args, str)
}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

// checkStderr runs the provided command and compares the stderr output with the given one.
// It returns the value of cmd.Wait()
func checkStderr(t *testing.T, cmd *exec.Cmd, value string) error {
	cmd.Stderr = nil
	stderr, _ := cmd.StderrPipe()
	err := cmd.Start()
	assert.Equal(t, nil, err)

	buf := new(bytes.Buffer)
	_, _ = buf.ReadFrom(stderr)
	s := buf.String()
	assert.Equal(t, value, s)

	return cmd.Wait()
}