starters_test.go 3.88 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
	"testing"
11
	"time"
12

13
	"github.com/stretchr/testify/assert"
14
15
16
17
18
19
)

const testPort = "9090"

var currentClient = 0

20
// startPlatform creates root certificate for the platform and the TTP, and starts both modules
21
func startPlatform(tmpDir string) (platform, ttp, demo *exec.Cmd, stop func(), ca []byte, err error) {
22
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssp")
23
	ttpPath := filepath.Join(os.Getenv("GOPATH"), "bin", "dfsst")
24
	demoPath := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssd")
25
26
27
28

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

	// Init
ElyKar's avatar
ElyKar committed
33
	cmd := exec.Command(path, "--path", dir, "-v", "init")
34
	ttpsPath := filepath.Join(dir, "ttps")
35
36
	err = cmd.Run()
	if err != nil {
37
38
39
40
		return
	}

	// Create TTP working directory
41
	cmd = exec.Command(path, "--path", dir, "--ttps", ttpsPath, "--addr", "localhost:9098", "ttp")
42
43
44
	err = cmd.Run()
	if err != nil {
		return
45
46
47
	}

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

Axel's avatar
Axel committed
53
54
55
56
57
58
59
	// Start demonstrator
	demo = exec.Command(demoPath, "--port", "9099", "nogui")
	demo.Stdout = os.Stdout
	demo.Stderr = os.Stderr
	err = demo.Start()
	time.Sleep(time.Second)

60
	// Start platform
61
	platform = exec.Command(path, "--db", dbURI, "--path", dir, "-p", testPort, "--ttps", ttpsPath, "-d", "localhost:9099", "-v", "start")
62
63
64
65
66
	platform.Stdout = os.Stdout
	platform.Stderr = os.Stderr
	err = platform.Start()

	// Start TTP
67
	ttp = exec.Command(ttpPath, "--db", dbURI, "--port", "9098", "-d", "localhost:9099", "-v", "start")
68
69
70
71
72
	ttp.Dir = filepath.Join(dir, "ttp")
	ttp.Stdout = os.Stdout
	ttp.Stderr = os.Stderr
	_ = ioutil.WriteFile(filepath.Join(ttp.Dir, "ca.pem"), ca, 0600)
	err = ttp.Start()
73

74
75
76
77
78
79
80
81
	stop = func() {
		_ = platform.Process.Kill()
		_ = ttp.Process.Kill()
		_ = os.RemoveAll(tmpDir)
		time.Sleep(2 * time.Second)
		_ = demo.Process.Kill()
	}

82
	return
83
84
}

85
// createClient creates a new working directory for a client, creating ca.pem.
86
// It returns a ready-to-run command, but you probably want to set the last argument of the command.
Loïck Bonniot's avatar
Loïck Bonniot committed
87
func createClient(tmpDir string, ca []byte, port int) (*exec.Cmd, error) {
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	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!
110
	cmd := exec.Command(path, "--ca", caPath, "--cert", certPath, "--host", "127.0.0.1:"+testPort, "--key", keyPath, "--port", strconv.Itoa(port), "--timeout", "3s" ,"-v", "-d", "localhost:9099")
111

112
113
	return cmd, nil
}
114

115
116
// newClient clones the current command to another one.
// It's very useful when doing several commands on the same client.
117
118
119
120
121
122
123
124
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
}

125
// setLastArg sets or updates the last argument of a command.
126
127
128
129
130
131
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)
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

// 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()
}