2024-05-06 09:28:28 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-10-11 14:55:39 +05:00
|
|
|
"github.com/SAP/jenkins-library/pkg/gcp"
|
2024-05-06 09:28:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockGcpPublishEventUtilsBundle struct {
|
|
|
|
config *gcpPublishEventOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *mockGcpPublishEventUtilsBundle) GetConfig() *gcpPublishEventOptions {
|
|
|
|
return g.config
|
|
|
|
}
|
|
|
|
|
2024-10-11 14:55:39 +05:00
|
|
|
func (g *mockGcpPublishEventUtilsBundle) NewPubsubClient(_, _, _, _, _ string) gcp.PubsubClient {
|
|
|
|
return &mockPubsubClient{}
|
2024-05-06 09:28:28 +02:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:55:39 +05:00
|
|
|
type mockPubsubClient struct {
|
2024-05-06 09:28:28 +02:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:55:39 +05:00
|
|
|
func (p *mockPubsubClient) Publish(topic string, _ []byte) error {
|
2024-05-06 09:28:28 +02:00
|
|
|
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")
|
|
|
|
})
|
|
|
|
}
|