1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-09 13:47:31 +02:00
sap-jenkins-library/cmd/gcpPublishEvent_test.go
Valentin 93c4ea61aa
Revert "Fix deployment failure with CF if password contains special char (#5197)" (#5215)
This reverts commit 8205624a22920f2ebe1e7999bf1b384aeae04e5d.

Co-authored-by: Valentin Uchkunev <valentin.uchkunev@sap.com>
2024-12-23 12:33:12 +01: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")
})
}