starters_test.go 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package tests

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
)

const testPort = "9090"

var currentClient = 0

// StartPlatform creates root certificate for the platform and starts the platform.
16
func startPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	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
39
	cmd = exec.Command(path, "-db", dbURI, "-path", dir, "-p", testPort, "-v", "start")
40 41 42 43 44 45 46 47
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Start()

	return cmd, ca, err
}

// CreateClient creates a new working directory for a client, creating ca.pem.
48 49
// 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) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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!
72 73 74 75
	cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "localhost:"+testPort, "-key", keyPath, "-v")

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
76 77 78

	return cmd, nil
}
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

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
}

// SetLastArg sets or updates the last argument of a command.
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)
}