github.com/SAP/jenkins-library@v1.362.0/cmd/ansSendEvent_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "fmt" 8 "github.com/SAP/jenkins-library/pkg/ans" 9 "github.com/SAP/jenkins-library/pkg/xsuaa" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "testing" 13 ) 14 15 const testTimestamp = 1651585103 16 17 func TestRunAnsSendEvent(t *testing.T) { 18 tests := []struct { 19 name string 20 config ansSendEventOptions 21 ansMock ansMock 22 wantErrMsg string 23 }{ 24 { 25 name: "overwriting EventType", 26 config: defaultEventOptions(), 27 }, 28 { 29 name: "bad service key", 30 config: ansSendEventOptions{AnsServiceKey: `{"forgot": "closing", "bracket": "json"`}, 31 wantErrMsg: `error unmarshalling ANS serviceKey: unexpected end of JSON input`, 32 }, 33 { 34 name: "invalid event json", 35 config: ansSendEventOptions{AnsServiceKey: goodServiceKey, Severity: "WRONG_SEVERITY"}, 36 wantErrMsg: `Severity must be one of [INFO NOTICE WARNING ERROR FATAL]: event JSON failed the validation`, 37 }, 38 { 39 name: "fail to send", 40 config: defaultEventOptions(), 41 ansMock: ansMock{failToSend: true}, 42 wantErrMsg: `failed to send`, 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 if err := runAnsSendEvent(&tt.config, &tt.ansMock); tt.wantErrMsg != "" { 48 assert.EqualError(t, err, tt.wantErrMsg) 49 } else { 50 require.NoError(t, err) 51 assert.Equal(t, "https://my.test.backend", tt.ansMock.testANS.URL) 52 assert.Equal(t, defaultXsuaa(), tt.ansMock.testANS.XSUAA) 53 assert.Equal(t, defaultEvent(), tt.ansMock.testEvent) 54 } 55 56 }) 57 } 58 } 59 60 func defaultEventOptions() ansSendEventOptions { 61 return ansSendEventOptions{ 62 AnsServiceKey: goodServiceKey, 63 EventType: "myEvent", 64 Severity: "INFO", 65 Category: "NOTIFICATION", 66 Subject: "testStep", 67 Body: "Call from Piper step: testStep", 68 Priority: 123, 69 Tags: map[string]interface{}{"myNumber": 456}, 70 ResourceName: "myResourceName", 71 ResourceType: "myResourceType", 72 ResourceInstance: "myResourceInstance", 73 ResourceTags: map[string]interface{}{"myBoolean": true}, 74 } 75 } 76 77 func defaultEvent() ans.Event { 78 return ans.Event{ 79 EventType: "myEvent", 80 EventTimestamp: testTimestamp, 81 Severity: "INFO", 82 Category: "NOTIFICATION", 83 Subject: "testStep", 84 Body: "Call from Piper step: testStep", 85 Priority: 123, 86 Tags: map[string]interface{}{"myNumber": 456}, 87 Resource: &ans.Resource{ 88 ResourceName: "myResourceName", 89 ResourceType: "myResourceType", 90 ResourceInstance: "myResourceInstance", 91 Tags: map[string]interface{}{"myBoolean": true}, 92 }, 93 } 94 } 95 96 func defaultXsuaa() xsuaa.XSUAA { 97 return xsuaa.XSUAA{ 98 OAuthURL: "https://my.test.oauth.provider", 99 ClientID: "myTestClientID", 100 ClientSecret: "super secret", 101 } 102 } 103 104 const goodServiceKey = `{ 105 "url": "https://my.test.backend", 106 "client_id": "myTestClientID", 107 "client_secret": "super secret", 108 "oauth_url": "https://my.test.oauth.provider" 109 }` 110 111 type ansMock struct { 112 testANS ans.ANS 113 testEvent ans.Event 114 failToSend bool 115 } 116 117 func (am *ansMock) Send(event ans.Event) error { 118 if am.failToSend { 119 return fmt.Errorf("failed to send") 120 } 121 event.EventTimestamp = testTimestamp 122 am.testEvent = event 123 return nil 124 } 125 126 func (am ansMock) CheckCorrectSetup() error { 127 return fmt.Errorf("not implemented") 128 } 129 130 func (am *ansMock) SetServiceKey(serviceKey ans.ServiceKey) { 131 am.testANS.SetServiceKey(serviceKey) 132 }