1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-05 13:25:19 +02:00
sap-jenkins-library/cmd/gcpPublishEvent_test.go
Googlom af05acad58
feat(events): Publish events to GCP PubSub by each step (#5122)
* test

* test

* draft

* generator

* some polishing

* go mod tidy

* fix unit

* fix unit

* fix unit

* fix unit

* fix unit

* resolve review comments

* resolve review comments

* add debug message on successful publish

* refactor to use global vault client

* cleanup

* rename

* clenup

* refactor

* remove token revocation

* handle nil vaultClient and add comments

* feat(events): Publish events (generated part) (#5131)

* add generated

* add generated

* refactor vaultClient usage

* fix unit tests

* fix unit tests

* fix
2024-10-11 14:55:39 +05:00

72 lines
1.5 KiB
Go

package cmd
import (
"github.com/SAP/jenkins-library/pkg/gcp"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type mockGcpPublishEventUtilsBundle struct {
config *gcpPublishEventOptions
}
func (g *mockGcpPublishEventUtilsBundle) GetConfig() *gcpPublishEventOptions {
return g.config
}
func (g *mockGcpPublishEventUtilsBundle) NewPubsubClient(_, _, _, _, _ string) gcp.PubsubClient {
return &mockPubsubClient{}
}
type mockPubsubClient struct {
}
func (p *mockPubsubClient) Publish(topic string, _ []byte) error {
if topic == "goodTestCase" {
return nil
} else if topic == "badTestCase" {
return errors.New("failed to send request")
}
return nil
}
func TestRunGcpPublishEvent(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
t.Parallel()
// init
mock := &mockGcpPublishEventUtilsBundle{
config: &gcpPublishEventOptions{
EventType: "PipelineRunStarted",
EventSource: "unittest",
Topic: "goodTestCase",
}}
// test
err := runGcpPublishEvent(mock)
// assert
assert.NoError(t, err)
})
t.Run("error path", func(t *testing.T) {
t.Parallel()
// init
mock := &mockGcpPublishEventUtilsBundle{
config: &gcpPublishEventOptions{
EventType: "PipelineRunStarted",
EventSource: "unittest",
Topic: "badTestCase",
}}
// test
err := runGcpPublishEvent(mock)
// assert
assert.EqualError(t, err, "failed to publish event: failed to send request")
})
}