-
Notifications
You must be signed in to change notification settings - Fork 7
/
sops_test.go
163 lines (124 loc) · 3.87 KB
/
sops_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
161
162
163
package filecrypt
import (
"errors"
"testing"
mock_oswrap "github.com/Ibotta/sopstool/oswrap/mock"
"github.com/golang/mock/gomock"
)
func TestEncryptFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run enc", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandStdoutToFile("myfile.sops.yaml", gomock.Eq([]string{"sops", "-e", "myfile.yaml"})).Return(nil)
sops.execWrap = mock
err := sops.EncryptFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.execWrap = origEw
})
}
func TestDecryptFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run dec", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandStdoutToFile("myfile.yaml", gomock.Eq([]string{"sops", "-d", "myfile.sops.yaml"})).Return(nil)
sops.execWrap = mock
err := sops.DecryptFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.execWrap = origEw
})
t.Run("run dec returns error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandStdoutToFile("myfile.yaml", gomock.Eq([]string{"sops", "-d", "myfile.sops.yaml"})).Return(errors.New("did someting bad"))
sops.execWrap = mock
err := sops.DecryptFile("myfile.yaml")
if err == nil {
t.Errorf("TestEncryptFile() expected an error, got %v", err)
}
sops.execWrap = origEw
})
}
func TestDecryptFilePrint(t *testing.T) {
origEw := sops.execWrap
t.Run("run dec print", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandDirect(gomock.Eq([]string{"sops", "-d", "myfile.sops.yaml"})).Return(nil)
sops.execWrap = mock
err := sops.DecryptFilePrint("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.execWrap = origEw
})
}
func TestRemoveFile(t *testing.T) {
origOw := sops.osWrap
t.Run("removes", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockOsWrap(ctrl)
mock.EXPECT().Remove(gomock.Eq("myfile.yaml")).Return(nil)
sops.osWrap = mock
err := sops.RemoveFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.osWrap = origOw
})
}
func TestRemoveCryptFile(t *testing.T) {
origOw := sops.osWrap
t.Run("run rm", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockOsWrap(ctrl)
mock.EXPECT().Remove(gomock.Eq("myfile.sops.yaml")).Return(nil)
sops.osWrap = mock
err := sops.RemoveCryptFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.osWrap = origOw
})
}
func TestRotateFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run rotate", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandDirect(gomock.Eq([]string{"sops", "-i", "-r", "myfile.sops.yaml"})).Return(nil)
sops.execWrap = mock
err := sops.RotateFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.execWrap = origEw
})
}
func TestEditFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run edit", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)
mock.EXPECT().RunCommandDirect(gomock.Eq([]string{"sops", "myfile.sops.yaml"})).Return(nil)
sops.execWrap = mock
err := sops.EditFile("myfile.yaml")
if err != nil {
t.Errorf("TestEncryptFile() unexpected error %v", err)
}
sops.execWrap = origEw
})
}