@@ -17,8 +17,11 @@ limitations under the License.
1717package audit
1818
1919import (
20+ "io"
2021 "os"
22+ "os/exec"
2123 "os/user"
24+ "strings"
2225 "testing"
2326
2427 "github.com/spf13/pflag"
@@ -27,6 +30,33 @@ import (
2730)
2831
2932func 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+
3060 t .Run ("username" , func (t * testing.T ) {
3161 u , err := user .Current ()
3262 if err != nil {
@@ -168,7 +198,6 @@ func TestAudit(t *testing.T) {
168198 mockArgs (t , test .args )
169199
170200 got := isDeletePurge ()
171-
172201 if got != test .want {
173202 t .Errorf ("test.args = %q; isDeletePurge() = %t; want %t" , test .args , got , test .want )
174203 }
@@ -211,11 +240,18 @@ func TestAudit(t *testing.T) {
211240 if err != nil {
212241 t .Fatal ("start failed" )
213242 }
214- err = LogCommandEnd (auditID )
243+ if err := LogCommandEnd (auditID ); err != nil {
244+ t .Fatal (err )
245+ }
215246
247+ b , err := exec .Command ("wc" , "-l" , auditFilename ).Output ()
216248 if err != nil {
217249 t .Fatal (err )
218250 }
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+
219255 })
220256
221257 t .Run ("LogCommandEndNonExistingID" , func (t * testing.T ) {
0 commit comments