File: rpc_client_test.go

package info (click to toggle)
golang-github-hashicorp-go-plugin 0.0~git20160212.0.cccb4a1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 188 kB
  • ctags: 125
  • sloc: makefile: 3
file content (64 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package plugin

import (
	"bytes"
	"io"
	"testing"
	"time"
)

func TestClient_App(t *testing.T) {
	client, _ := TestPluginRPCConn(t, map[string]Plugin{
		"test": new(testInterfacePlugin),
	})
	defer client.Close()

	raw, err := client.Dispense("test")
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	impl, ok := raw.(testInterface)
	if !ok {
		t.Fatalf("bad: %#v", raw)
	}

	result := impl.Double(21)
	if result != 42 {
		t.Fatalf("bad: %#v", result)
	}
}

func TestClient_syncStreams(t *testing.T) {
	client, server := TestPluginRPCConn(t, map[string]Plugin{})

	// Create streams for the server that we can talk to
	stdout_r, stdout_w := io.Pipe()
	stderr_r, stderr_w := io.Pipe()
	server.Stdout = stdout_r
	server.Stderr = stderr_r

	// Start the data copying
	var stdout_out, stderr_out bytes.Buffer
	stdout := bytes.NewBufferString("stdouttest")
	stderr := bytes.NewBufferString("stderrtest")
	go client.SyncStreams(&stdout_out, &stderr_out)
	go io.Copy(stdout_w, stdout)
	go io.Copy(stderr_w, stderr)

	// Unfortunately I can't think of a better way to make sure all the
	// copies above go through so let's just exit.
	time.Sleep(100 * time.Millisecond)

	// Close everything, and lets test the result
	client.Close()
	stdout_w.Close()
	stderr_w.Close()

	if v := stdout_out.String(); v != "stdouttest" {
		t.Fatalf("bad: %s", v)
	}
	if v := stderr_out.String(); v != "stderrtest" {
		t.Fatalf("bad: %s", v)
	}
}