github.com/argoproj/argo-cd@v1.8.7/util/argo/audit_logger_test.go (about) 1 package argo 2 3 import ( 4 "bytes" 5 "os" 6 "testing" 7 8 log "github.com/sirupsen/logrus" 9 "github.com/stretchr/testify/assert" 10 11 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "k8s.io/client-go/kubernetes/fake" 13 14 argoappv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 15 ) 16 17 // Helper to capture log entries generated by the logger and return it as string 18 func captureLogEntries(run func()) string { 19 f := log.StandardLogger().Formatter 20 log.SetFormatter(&log.TextFormatter{DisableColors: true}) 21 defer log.SetFormatter(f) 22 output := bytes.NewBuffer(nil) 23 log.SetOutput(output) 24 defer log.SetOutput(os.Stdout) 25 26 run() 27 28 return output.String() 29 } 30 31 func TestNewAuditLogger(t *testing.T) { 32 logger := NewAuditLogger("default", fake.NewSimpleClientset(), "somecomponent") 33 assert.NotNil(t, logger) 34 } 35 36 func TestLogAppProjEvent(t *testing.T) { 37 38 logger := NewAuditLogger("default", fake.NewSimpleClientset(), "somecomponent") 39 assert.NotNil(t, logger) 40 41 proj := argoappv1.AppProject{ 42 ObjectMeta: v1.ObjectMeta{ 43 Name: "default", 44 Namespace: "argocd", 45 ResourceVersion: "1", 46 UID: "a-b-c-d-e", 47 }, 48 Spec: argoappv1.AppProjectSpec{ 49 Description: "Test project", 50 }, 51 } 52 53 ei := EventInfo{ 54 Reason: "test", 55 Type: "info", 56 } 57 58 output := captureLogEntries(func() { 59 logger.LogAppProjEvent(&proj, ei, "This is a test message") 60 }) 61 62 assert.Contains(t, output, "level=info") 63 assert.Contains(t, output, "project=default") 64 assert.Contains(t, output, "reason=test") 65 assert.Contains(t, output, "type=info") 66 assert.Contains(t, output, "msg=\"This is a test message\"") 67 } 68 69 func TestLogAppEvent(t *testing.T) { 70 logger := NewAuditLogger("default", fake.NewSimpleClientset(), "somecomponent") 71 assert.NotNil(t, logger) 72 73 app := argoappv1.Application{ 74 ObjectMeta: v1.ObjectMeta{ 75 Name: "testapp", 76 Namespace: "argocd", 77 ResourceVersion: "1", 78 UID: "a-b-c-d-e", 79 }, 80 Spec: argoappv1.ApplicationSpec{ 81 Destination: argoappv1.ApplicationDestination{ 82 Server: "https://127.0.0.1:6443", 83 Namespace: "testns", 84 }, 85 }, 86 } 87 88 ei := EventInfo{ 89 Reason: "test", 90 Type: "info", 91 } 92 93 output := captureLogEntries(func() { 94 logger.LogAppEvent(&app, ei, "This is a test message") 95 }) 96 97 assert.Contains(t, output, "level=info") 98 assert.Contains(t, output, "application=testapp") 99 assert.Contains(t, output, "dest-namespace=testns") 100 assert.Contains(t, output, "dest-server=\"https://127.0.0.1:6443\"") 101 assert.Contains(t, output, "reason=test") 102 assert.Contains(t, output, "type=info") 103 assert.Contains(t, output, "msg=\"This is a test message\"") 104 105 } 106 107 func TestLogResourceEvent(t *testing.T) { 108 logger := NewAuditLogger("default", fake.NewSimpleClientset(), "somecomponent") 109 assert.NotNil(t, logger) 110 111 res := argoappv1.ResourceNode{ 112 ResourceRef: argoappv1.ResourceRef{ 113 Group: "argocd.argoproj.io", 114 Version: "v1alpha1", 115 Kind: "SignatureKey", 116 Name: "testapp", 117 Namespace: "argocd", 118 UID: "a-b-c-d-e", 119 }, 120 } 121 122 ei := EventInfo{ 123 Reason: "test", 124 Type: "info", 125 } 126 127 output := captureLogEntries(func() { 128 logger.LogResourceEvent(&res, ei, "This is a test message") 129 }) 130 131 assert.Contains(t, output, "level=info") 132 assert.Contains(t, output, "name=testapp") 133 assert.Contains(t, output, "reason=test") 134 assert.Contains(t, output, "type=info") 135 assert.Contains(t, output, "msg=\"This is a test message\"") 136 }