forked from spiffe/spiffe-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidecar_test.go
93 lines (79 loc) · 2.21 KB
/
sidecar_test.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
workload "github.com/spiffe/sidecar/wlapi"
//workload "github.com/spiffe/spire/pkg/api/workload"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
testTimeSeconds = 20
testTTL = 10
)
// TestSidecar_Integration will run the sidecar with an 'echo' command simulating ghostunnel
// and a simple webserver to mock the Workload API to the sidecar.
// The objetive is to make sure sidecar is requesting certs and invoking command successfully.
// TODO: 'echo' command exits immediately so we cannot test SIGUSR1 signalling. Improve this.
func TestSidecar_Integration(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-certs")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpdir)
config := &SidecarConfig{
GhostunnelCmd: "echo",
CertDir: tmpdir,
}
fmt.Printf("Will test for %d seconds.\n", testTimeSeconds)
go sendInterrupt(testTimeSeconds)
workloadClient := MockWorkloadClient{}
sidecar := NewSidecar(nil, config, workloadClient)
err = sidecar.RunDaemon()
if err != nil {
panic(err)
}
}
func sendInterrupt(seconds int) {
time.Sleep(time.Second * time.Duration(seconds))
fmt.Printf("Tested for %d seconds. Will interrupt!\n", testTimeSeconds)
p, err := os.FindProcess(os.Getpid())
if err != nil {
panic(err)
}
err = p.Signal(os.Interrupt)
if err != nil {
panic(err)
}
}
func readFile(file string) (bytes []byte) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return
}
type MockWorkloadClient struct {
}
func (m MockWorkloadClient) FetchAllBundles(ctx context.Context, in *workload.Empty, opts ...grpc.CallOption) (bundles *workload.Bundles, err error) {
bundles = &workload.Bundles{
Ttl: testTTL,
Bundles: []*workload.WorkloadEntry{
&workload.WorkloadEntry{
SpiffeId: "example.org/id",
Svid: readFile("keys/svid.pem"),
SvidPrivateKey: readFile("keys/svid_pk.pem"),
SvidBundle: readFile("keys/bundle.pem"),
FederatedBundles: nil,
},
},
}
err = nil
return
}
func (m MockWorkloadClient) FetchBundles(ctx context.Context, in *workload.SpiffeID, opts ...grpc.CallOption) (*workload.Bundles, error) {
panic("Not implemented")
}