forked from telia-oss/sidecred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_test.go
65 lines (55 loc) · 1.87 KB
/
state_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
package sidecred_test
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/telia-oss/sidecred"
)
var fixedTestTime = time.Date(2020, 1, 30, 12, 0, 0, 0, time.UTC)
func TestState(t *testing.T) {
tests := []struct {
description string
stateID string
expectFound bool
expectedJSON string
expectedFinalJSON string
}{
{
description: "state works",
stateID: testStateID,
expectedJSON: strings.TrimSpace(`
{"providers":[{"type":"random","resources":[{"type":"random","id":"fake.state.id","store":"","expiration":"2020-01-30T12:00:00Z","deposed":false}]}],"stores":[{"type":"inprocess","name":"","secrets":[{"resource_id":"fake.state.id","path":"fake.store.path","expiration":"2020-01-30T12:00:00Z"}]}]}
`),
expectedFinalJSON: strings.TrimSpace(`
{"providers":[{"type":"random","resources":[]}],"stores":[{"type":"inprocess","name":"","secrets":[]}]}
`),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
state := sidecred.NewState()
state.AddResource(&sidecred.Resource{
Type: sidecred.Randomized,
ID: tc.stateID,
Expiration: fixedTestTime,
})
storeConfig := &sidecred.StoreConfig{Type: sidecred.Inprocess}
state.AddSecret(storeConfig, &sidecred.Secret{
ResourceID: tc.stateID,
Path: "fake.store.path",
Expiration: fixedTestTime,
})
outputJSON, err := json.Marshal(state)
require.NoError(t, err)
assert.Equal(t, tc.expectedJSON, string(outputJSON))
state.RemoveResource(&sidecred.Resource{Type: sidecred.Randomized, ID: tc.stateID})
state.RemoveSecret(storeConfig, &sidecred.Secret{Path: "fake.store.path"})
finalOutputJSON, err := json.Marshal(state)
require.NoError(t, err)
assert.Equal(t, tc.expectedFinalJSON, string(finalOutputJSON))
})
}
}