-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvault_test.go
89 lines (79 loc) · 2.02 KB
/
vault_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
package vault_test
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
ph "github.com/isometry/platform-health/pkg/platform_health"
vaultProvider "github.com/isometry/platform-health/pkg/provider/vault"
)
func init() {
slog.SetLogLoggerLevel(slog.LevelError)
}
func TestVaultGetHealth(t *testing.T) {
tests := []struct {
name string
delay time.Duration
response string
timeout time.Duration
expected ph.Status
}{
{
name: "Vault healthy",
response: `{"initialized":true,"sealed":false,"standby":false}`,
timeout: time.Second,
expected: ph.Status_HEALTHY,
},
{
name: "Vault uninitialized",
response: `{"initialized":false,"sealed":true,"standby":false}`,
timeout: time.Second,
expected: ph.Status_UNHEALTHY,
},
{
name: "Vault sealed",
response: `{"initialized":true,"sealed":true,"standby":false}`,
timeout: time.Second,
expected: ph.Status_UNHEALTHY,
},
{
name: "Vault timeout",
delay: 10 * time.Millisecond,
response: `{"initialized":true,"sealed":false,"standby":false}`,
timeout: time.Millisecond,
expected: ph.Status_UNHEALTHY,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), tt.timeout+time.Second)
defer cancel()
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
select {
case <-time.After(tt.delay):
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(tt.response))
case <-ctx.Done():
return
}
}))
defer server.CloseClientConnections()
defer server.Close()
instance := &vaultProvider.Vault{
Name: "TestService",
Address: server.URL,
Timeout: tt.timeout,
}
instance.SetDefaults()
result := instance.GetHealth(ctx)
assert.NotNil(t, result)
assert.Equal(t, tt.expected, result.GetStatus())
})
}
}