forked from shwuandwing/spiffe-helper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspiffe-helper_test.go
160 lines (130 loc) · 3.89 KB
/
spiffe-helper_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"crypto/x509"
"github.com/spiffe/spire/proto/api/workload"
"github.com/spiffe/spire/test/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path"
"sync"
"testing"
"time"
)
//Creates a Sidecar with a Mocked WorkloadAPIClient and tests that
//running the Sidecar Daemon, when a SVID Response is sent to the
//UpdateChan on the WorkloadAPI client, the PEM files are stored on disk
func TestSidecar_RunDaemon(t *testing.T) {
var wg sync.WaitGroup
tmpdir, err := ioutil.TempDir("", "sidecar-run-daemon")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
config := &SidecarConfig{
Cmd: "echo",
CertDir: tmpdir,
SvidFileName: "svid.pem",
SvidKeyFileName: "svid_key.pem",
SvidBundleFileName: "svid_bundle.pem",
}
updateMockChan := make(chan *workload.X509SVIDResponse)
workloadClient := MockWorkloadClient{
mockChan: updateMockChan,
}
sidecar := sidecar{
config: config,
workloadAPIClient: workloadClient,
}
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
err = sidecar.RunDaemon(ctx)
require.NoError(t, err)
}()
x509SvidTestResponse := x509SvidResponse(t)
//send a X509SVIDResponse to Updates channel
updateMockChan <- x509SvidTestResponse
//send signal to stop the Daemon
cancel()
wg.Wait()
//The expected files
svidFile := path.Join(tmpdir, config.SvidFileName)
svidKeyFile := path.Join(tmpdir, config.SvidKeyFileName)
svidBundleFile := path.Join(tmpdir, config.SvidBundleFileName)
if _, err := os.Stat(svidFile); err != nil {
t.Errorf("error %v with file: %v", err, svidFile)
}
if _, err := os.Stat(svidKeyFile); err != nil {
t.Errorf("error %v with file: %v", err, svidKeyFile)
}
if _, err := os.Stat(svidBundleFile); err != nil {
t.Errorf("error %v with file: %v", err, svidBundleFile)
}
}
//Tests that when there is no defaultTimeout in the config, it uses
//the default defaultTimeout set in a constant in the spiffe_helper
func Test_getTimeout_default(t *testing.T) {
config := &SidecarConfig{}
expectedTimeout := defaultTimeout
actualTimeout, err := getTimeout(config)
assert.NoError(t, err)
if actualTimeout != expectedTimeout {
t.Errorf("Expected defaultTimeout : %v, got %v", expectedTimeout, actualTimeout)
}
}
//Tests that when there is a timeout set in the config, it's used that one
func Test_getTimeout_custom(t *testing.T) {
config := &SidecarConfig{
Timeout: "10s",
}
expectedTimeout := time.Second * 10
actualTimeout, err := getTimeout(config)
assert.NoError(t, err)
if actualTimeout != expectedTimeout {
t.Errorf("Expected defaultTimeout : %v, got %v", expectedTimeout, actualTimeout)
}
}
func Test_getTimeout_return_error_when_parsing_fails(t *testing.T) {
config := &SidecarConfig{
Timeout: "invalid",
}
actualTimeout, err := getTimeout(config)
assert.Empty(t, actualTimeout)
assert.NotEmpty(t, err)
}
type MockWorkloadClient struct {
mockChan chan *workload.X509SVIDResponse
}
func (m MockWorkloadClient) Start() error {
return nil
}
func (m MockWorkloadClient) Stop() {}
func (m MockWorkloadClient) UpdateChan() <-chan *workload.X509SVIDResponse {
return m.mockChan
}
// creates a X509SVIDResponse reading test certs from files
func x509SvidResponse(t *testing.T) *workload.X509SVIDResponse {
svid, key, err := util.LoadSVIDFixture()
if err != nil {
t.Errorf("could not load svid fixture: %v", err)
}
ca, _, err := util.LoadCAFixture()
if err != nil {
t.Errorf("could not load ca fixture: %v", err)
}
keyData, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
t.Errorf("could not marshal private key: %v", err)
}
svidMsg := &workload.X509SVID{
SpiffeId: "spiffe://example.org/foo",
X509Svid: svid.Raw,
X509SvidKey: keyData,
Bundle: ca.Raw,
}
return &workload.X509SVIDResponse{
Svids: []*workload.X509SVID{svidMsg},
}
}