@@ -17,8 +17,11 @@ limitations under the License.
17
17
package audit
18
18
19
19
import (
20
+ "io"
20
21
"os"
22
+ "os/exec"
21
23
"os/user"
24
+ "strings"
22
25
"testing"
23
26
24
27
"github.com/spf13/pflag"
@@ -27,6 +30,33 @@ import (
27
30
)
28
31
29
32
func TestAudit (t * testing.T ) {
33
+ var auditFilename string
34
+
35
+ t .Run ("setup" , func (t * testing.T ) {
36
+ f , err := os .CreateTemp ("" , "audit.json" )
37
+ if err != nil {
38
+ t .Fatalf ("failed creating temporary file: %v" , err )
39
+ }
40
+ auditFilename = f .Name ()
41
+
42
+ s := `{"data":{"args":"-p mini1","command":"start","endTime":"Wed, 03 Feb 2021 15:33:05 MST","profile":"mini1","startTime":"Wed, 03 Feb 2021 15:30:33 MST","user":"user1"},"datacontenttype":"application/json","id":"9b7593cb-fbec-49e5-a3ce-bdc2d0bfb208","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.si gs.minikube.audit"}
43
+ {"data":{"args":"-p mini1","command":"start","endTime":"Wed, 03 Feb 2021 15:33:05 MST","profile":"mini1","startTime":"Wed, 03 Feb 2021 15:30:33 MST","user":"user1"},"datacontenttype":"application/json","id":"9b7593cb-fbec-49e5-a3ce-bdc2d0bfb208","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.si gs.minikube.audit"}
44
+ {"data":{"args":"--user user2","command":"logs","endTime":"Tue, 02 Feb 2021 16:46:20 MST","profile":"minikube","startTime":"Tue, 02 Feb 2021 16:46:00 MST","user":"user2"},"datacontenttype":"application/json","id":"fec03227-2484-48b6-880a-88fd010b5efd","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}
45
+ {"data":{"args":"-p mini1","command":"start","endTime":"Wed, 03 Feb 2021 15:33:05 MST","profile":"mini1","startTime":"Wed, 03 Feb 2021 15:30:33 MST","user":"user1"},"datacontenttype":"application/json","id":"9b7593cb-fbec-49e5-a3ce-bdc2d0bfb208","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.si gs.minikube.audit"}
46
+ {"data":{"args":"--user user2","command":"logs","endTime":"Tue, 02 Feb 2021 16:46:20 MST","profile":"minikube","startTime":"Tue, 02 Feb 2021 16:46:00 MST","user":"user2"},"datacontenttype":"application/json","id":"fec03227-2484-48b6-880a-88fd010b5efd","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}
47
+ `
48
+
49
+ if _ , err := f .WriteString (s ); err != nil {
50
+ t .Fatalf ("failed writing to file: %v" , err )
51
+ }
52
+ if _ , err := f .Seek (0 , io .SeekStart ); err != nil {
53
+ t .Fatalf ("failed seeking to start of file: %v" , err )
54
+ }
55
+
56
+ currentLogFile = f
57
+ viper .Set (config .MaxAuditEntries , 3 )
58
+ })
59
+
30
60
t .Run ("username" , func (t * testing.T ) {
31
61
u , err := user .Current ()
32
62
if err != nil {
@@ -168,7 +198,6 @@ func TestAudit(t *testing.T) {
168
198
mockArgs (t , test .args )
169
199
170
200
got := isDeletePurge ()
171
-
172
201
if got != test .want {
173
202
t .Errorf ("test.args = %q; isDeletePurge() = %t; want %t" , test .args , got , test .want )
174
203
}
@@ -211,11 +240,18 @@ func TestAudit(t *testing.T) {
211
240
if err != nil {
212
241
t .Fatal ("start failed" )
213
242
}
214
- err = LogCommandEnd (auditID )
243
+ if err := LogCommandEnd (auditID ); err != nil {
244
+ t .Fatal (err )
245
+ }
215
246
247
+ b , err := exec .Command ("wc" , "-l" , auditFilename ).Output ()
216
248
if err != nil {
217
249
t .Fatal (err )
218
250
}
251
+ if ! strings .Contains (string (b ), "3" ) {
252
+ t .Errorf ("MaxAuditEntries did not work, expected 3 lines in the audit log found %s" , string (b ))
253
+ }
254
+
219
255
})
220
256
221
257
t .Run ("LogCommandEndNonExistingID" , func (t * testing.T ) {
0 commit comments