Implement support for Azure storage message queue triggers (#371)

These commits implement support for consuming messages from an Azure storage queue to trigger Fission functions.

* Add stubbed Azure message queue implementation and modify Helm charts.

This commit stubs an implementation for an Azure storage message queue trigger
that will be completed by future commits.

It also modifies the Helm chart to add support for deploying Fission with an
mqtrigger configured for Azure storage queue triggers.

* Add Azure Go SDK to glide.

This commit adds the Azure Go SDK to glide for the upcoming work to support
Azure storage queue triggers.

* Implement Azure message queue trigger.

This commit implements a message queue trigger based on Azure storage queues.

Required message queue trigger manager environment variables:

* AZURE_STORAGE_ACCOUNT_NAME - the Azure storage account to use.
* AZURE_STORAGE_ACCOUNT_KEY - the Azure storage account key.

When creating a message queue trigger, the topic will be the Azure storage
queue to receive messages from.

* Add CA certificates to fission-bundle.

This commit adds the root CA certificates to the fission-bundle image. This
allows Fission to contact third-party APIs that use HTTPS with root CA
signed certificates.

* Add Makefile to build and test.

This commit adds a simple Makefile for building the client and bundle, running
tests, creating the Docker image, and pushing the Docker image.
This commit is contained in:
Peter Huene
2018-02-09 10:38:06 -08:00
committed by Soam Vasani
parent 46895b03c8
commit 4d0e6af5de
15 changed files with 982 additions and 32 deletions
+376
View File
@@ -0,0 +1,376 @@
/*
Copyright 2017 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package messageQueue
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/fission/fission"
"github.com/fission/fission/crd"
log "github.com/sirupsen/logrus"
"github.com/Azure/azure-sdk-for-go/storage"
)
// TODO: some of these constants should probably be environment variables
const (
// AzureQueuePollingInterval is the polling interval (default is 1 minute).
AzureQueuePollingInterval = time.Minute
// AzureQueueRetryLimit is the limit for attempts to retry invoking a function.
AzureQueueRetryLimit = 3
// AzureMessageFetchCount is the number of messages to fetch at a time.
AzureMessageFetchCount = 10
// AzureMessageVisibilityTimeout is the visibility timeout for dequeued messages.
AzureMessageVisibilityTimeout = time.Minute
// AzurePoisonQueueSuffix is the suffix used for posion queues.
AzurePoisonQueueSuffix = "-poison"
// AzureFunctionInvocationTimeout is the amount of time to wait for a triggered function to execute.
AzureFunctionInvocationTimeout = 10 * time.Minute
)
// AzureStorageConnection represents an Azure storage connection.
type AzureStorageConnection struct {
routerURL string
service AzureQueueService
httpClient AzureHTTPClient
}
// AzureQueueSubscription represents an Azure storage message queue subscription.
type AzureQueueSubscription struct {
queue AzureQueue
queueName string
outputQueueName string
functionURL string
contentType string
unsubscribe chan bool
done chan bool
}
// AzureQueueService is the interface that abstracts the Azure storage service.
// This exists to enable unit testing.
type AzureQueueService interface {
GetQueue(name string) AzureQueue
}
// AzureQueue is the interface that abstracts Azure storage queues.
// This exists to enable unit testing.
type AzureQueue interface {
Create(options *storage.QueueServiceOptions) error
NewMessage(text string) AzureMessage
GetMessages(options *storage.GetMessagesOptions) ([]AzureMessage, error)
}
// AzureMessage is the interface that abstracts Azure storage messages.
// This exists to enable unit testing.
type AzureMessage interface {
Bytes() []byte
Put(options *storage.PutMessageOptions) error
Delete(options *storage.QueueServiceOptions) error
}
// AzureHTTPClient is the interface that abstract HTTP requests made by the trigger.
// This exists to enable unit testing.
type AzureHTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type azureQueueService struct {
service storage.QueueServiceClient
}
func (qs azureQueueService) GetQueue(name string) AzureQueue {
return azureQueue{
ref: qs.service.GetQueueReference(name),
}
}
type azureQueue struct {
ref *storage.Queue
}
func (qr azureQueue) Create(options *storage.QueueServiceOptions) error {
exists, err := qr.ref.Exists()
if err != nil {
return err
}
if exists {
return nil
}
return qr.ref.Create(options)
}
func (qr azureQueue) NewMessage(text string) AzureMessage {
return azureMessage{
ref: qr.ref.GetMessageReference(text),
bytes: []byte(text),
}
}
func (qr azureQueue) GetMessages(options *storage.GetMessagesOptions) ([]AzureMessage, error) {
msgs, err := qr.ref.GetMessages(options)
if err != nil {
return nil, err
}
messages := make([]AzureMessage, len(msgs))
for i := range msgs {
bytes, err := base64.StdEncoding.DecodeString(msgs[i].Text)
if err != nil {
return nil, err
}
messages[i] = azureMessage{
ref: &msgs[i],
bytes: bytes,
}
}
return messages, nil
}
type azureMessage struct {
ref *storage.Message
bytes []byte
}
func (m azureMessage) Bytes() []byte {
return m.bytes
}
func (m azureMessage) Put(options *storage.PutMessageOptions) error {
return m.ref.Put(options)
}
func (m azureMessage) Delete(options *storage.QueueServiceOptions) error {
return m.ref.Delete(options)
}
func newAzureQueueService(client storage.Client) AzureQueueService {
return azureQueueService{
service: client.GetQueueService(),
}
}
func newAzureStorageConnection(routerURL string, config MessageQueueConfig) (MessageQueue, error) {
account := os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")
if len(account) == 0 {
return nil, errors.New("Required environment variable 'AZURE_STORAGE_ACCOUNT_NAME' is not set")
}
key := os.Getenv("AZURE_STORAGE_ACCOUNT_KEY")
if len(key) == 0 {
return nil, errors.New("Required environment variable 'AZURE_STORAGE_ACCOUNT_KEY' is not set")
}
log.Infof("Creating Azure storage connection to storage account '%s'.", account)
client, err := storage.NewBasicClient(account, key)
if err != nil {
return nil, fmt.Errorf("Failed to Azure create storage client: %v", err)
}
return &AzureStorageConnection{
routerURL: routerURL,
service: newAzureQueueService(client),
httpClient: &http.Client{
Timeout: AzureFunctionInvocationTimeout,
},
}, nil
}
func (asc AzureStorageConnection) subscribe(trigger *crd.MessageQueueTrigger) (messageQueueSubscription, error) {
log.Infof("Subscribing to Azure storage queue '%s'.", trigger.Spec.Topic)
if trigger.Spec.FunctionReference.Type != fission.FunctionReferenceTypeFunctionName {
return nil, fmt.Errorf("Unsupported function reference type (%v) for trigger %v", trigger.Spec.FunctionReference.Type, trigger.Metadata.Name)
}
subscription := &AzureQueueSubscription{
queue: asc.service.GetQueue(trigger.Spec.Topic),
queueName: trigger.Spec.Topic,
outputQueueName: trigger.Spec.ResponseTopic,
functionURL: asc.routerURL + "/" + strings.TrimPrefix(fission.UrlForFunction(trigger.Spec.FunctionReference.Name), "/"),
contentType: trigger.Spec.ContentType,
unsubscribe: make(chan bool),
done: make(chan bool),
}
go runAzureQueueSubscription(asc, subscription)
return subscription, nil
}
func (asc AzureStorageConnection) unsubscribe(subscription messageQueueSubscription) error {
sub := subscription.(*AzureQueueSubscription)
log.Infof("Unsubscribing from Azure storage queue '%s'.", sub.queueName)
// Let the worker know we've unsubscribed
sub.unsubscribe <- true
// Wait until the subscription is done
<-sub.done
return nil
}
func runAzureQueueSubscription(conn AzureStorageConnection, sub *AzureQueueSubscription) {
var wg sync.WaitGroup
// Process the queue before waiting
pollAzureQueueSubscription(conn, sub, &wg)
timer := time.NewTimer(AzureQueuePollingInterval)
for {
log.Infof("Waiting for %v before polling Azure storage queue '%s'.", AzureQueuePollingInterval, sub.queueName)
select {
case <-sub.unsubscribe:
timer.Stop()
wg.Wait()
sub.done <- true
return
case <-timer.C:
pollAzureQueueSubscription(conn, sub, &wg)
timer.Reset(AzureQueuePollingInterval)
continue
}
}
}
func pollAzureQueueSubscription(conn AzureStorageConnection, sub *AzureQueueSubscription, wg *sync.WaitGroup) {
log.Infof("Polling messages for Azure storage queue '%s'.", sub.queueName)
err := sub.queue.Create(nil)
if err != nil {
log.Errorf("Failed to create message queue '%s': %v", sub.queueName, err)
return
}
for {
err := sub.queue.Create(nil)
if err != nil {
log.Errorf("Failed to create message queue '%s': %v", sub.queueName, err)
return
}
messages, err := sub.queue.GetMessages(&storage.GetMessagesOptions{
NumOfMessages: AzureMessageFetchCount,
VisibilityTimeout: int(AzureMessageVisibilityTimeout / time.Second),
})
if err != nil {
log.Errorf("Failed to retrieve messages from Azure storage queue '%s': %v", sub.queueName, err)
break
}
if len(messages) == 0 {
break
}
wg.Add(len(messages))
for _, msg := range messages {
go func(conn AzureStorageConnection, sub *AzureQueueSubscription, msg AzureMessage) {
defer wg.Done()
invokeTriggeredFunction(conn, sub, msg)
}(conn, sub, msg)
}
}
}
func invokeTriggeredFunction(conn AzureStorageConnection, sub *AzureQueueSubscription, message AzureMessage) {
defer message.Delete(nil)
log.Printf("Making HTTP request to %s.", sub.functionURL)
for i := 0; i <= AzureQueueRetryLimit; i++ {
if i > 0 {
log.Infof("Retry #%d for request to %s.", i, sub.functionURL)
}
request, err := http.NewRequest("POST", sub.functionURL, bytes.NewReader(message.Bytes()))
if err != nil {
log.Errorf("Failed to create HTTP request to %s: %v", sub.functionURL, err)
continue
}
request.Header.Add("X-Fission-MQTrigger-Topic", sub.queueName)
if len(sub.outputQueueName) > 0 {
request.Header.Add("X-Fission-MQTrigger-RespTopic", sub.outputQueueName)
}
if i > 0 {
request.Header.Add("X-Fission-MQTrigger-RetryCount", strconv.Itoa(i))
}
request.Header.Add("Content-Type", sub.contentType)
response, err := conn.httpClient.Do(request)
if err != nil {
log.Errorf("Request to %s failed: %v", sub.functionURL, err)
continue
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Errorf("Failed to read response body from %s: %v.", sub.functionURL, err)
continue
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
log.Printf("Request to %s returned failure: %s (%d).", sub.functionURL, string(body), response.StatusCode)
continue
}
if len(sub.outputQueueName) > 0 {
outputQueue := conn.service.GetQueue(sub.outputQueueName)
err = outputQueue.Create(nil)
if err != nil {
log.Errorf("Failed to create output queue '%s': %v.", sub.outputQueueName, err)
return
}
outputMessage := outputQueue.NewMessage(string(body))
err = outputMessage.Put(nil)
if err != nil {
log.Errorf("Failed to post response body from %s to output queue '%s': %v.", sub.functionURL, sub.outputQueueName, err)
return
}
}
// Function invocation was successful
return
}
log.Errorf("Request to %s failed after %d retries; moving message to poison queue.", sub.functionURL, AzureQueueRetryLimit)
poisonQueueName := sub.queueName + AzurePoisonQueueSuffix
poisonQueue := conn.service.GetQueue(poisonQueueName)
err := poisonQueue.Create(nil)
if err != nil {
log.Errorf("Failed to create poison queue '%s': %v", poisonQueueName, err)
return
}
poisonMessage := poisonQueue.NewMessage(string(message.Bytes()))
err = poisonMessage.Put(nil)
if err != nil {
log.Errorf("Failed to post response body from %s to output queue '%s': %v", sub.functionURL, poisonQueueName, err)
return
}
}
+449
View File
@@ -0,0 +1,449 @@
/*
Copyright 2017 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package messageQueue
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/fission/fission"
"github.com/fission/fission/crd"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/Azure/azure-sdk-for-go/storage"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
const (
DummyRouterURL = "http://localhost"
)
type azureQueueServiceMock struct {
mock.Mock
}
func (m *azureQueueServiceMock) GetQueue(name string) AzureQueue {
args := m.Called(name)
return args.Get(0).(AzureQueue)
}
type azureQueueMock struct {
mock.Mock
}
func (m *azureQueueMock) Create(options *storage.QueueServiceOptions) error {
args := m.Called(options)
return args.Error(0)
}
func (m *azureQueueMock) NewMessage(text string) AzureMessage {
args := m.Called(text)
return args.Get(0).(AzureMessage)
}
func (m *azureQueueMock) GetMessages(options *storage.GetMessagesOptions) ([]AzureMessage, error) {
args := m.Called(options)
return args.Get(0).([]AzureMessage), args.Error(1)
}
type azureMessageMock struct {
mock.Mock
}
func (m *azureMessageMock) Bytes() []byte {
args := m.Called()
return args.Get(0).([]byte)
}
func (m *azureMessageMock) Put(options *storage.PutMessageOptions) error {
args := m.Called(options)
return args.Error(0)
}
func (m *azureMessageMock) Delete(options *storage.QueueServiceOptions) error {
args := m.Called(options)
return args.Error(0)
}
type azureHTTPClientMock struct {
mock.Mock
bodyHandler func(res *http.Response)
}
func (m *azureHTTPClientMock) Do(req *http.Request) (*http.Response, error) {
args := m.Called(req)
res := args.Get(0).(*http.Response)
err := args.Error(1)
if res != nil && m.bodyHandler != nil {
m.bodyHandler(res)
}
return res, err
}
func TestNewStorageConnectionMissingAccountName(t *testing.T) {
connection, err := newAzureStorageConnection(DummyRouterURL, MessageQueueConfig{
MQType: ASQ,
Url: "",
})
require.Nil(t, connection)
require.Error(t, err, "Required environment variable 'AZURE_STORAGE_ACCOUNT_NAME' is not set")
}
func TestNewStorageConnectionMissingAccessKey(t *testing.T) {
_ = os.Setenv("AZURE_STORAGE_ACCOUNT_NAME", "accountname")
connection, err := newAzureStorageConnection(DummyRouterURL, MessageQueueConfig{
MQType: ASQ,
Url: "",
})
_ = os.Unsetenv("AZURE_STORAGE_ACCOUNT_NAME")
require.Nil(t, connection)
require.Error(t, err, "Required environment variable 'AZURE_STORAGE_ACCOUNT_KEY' is not set")
}
func TestNewStorageConnection(t *testing.T) {
_ = os.Setenv("AZURE_STORAGE_ACCOUNT_NAME", "accountname")
_ = os.Setenv("AZURE_STORAGE_ACCOUNT_KEY", "bm90IGEga2V5")
connection, err := newAzureStorageConnection(DummyRouterURL, MessageQueueConfig{
MQType: "azure-storage-queue",
Url: "",
})
_ = os.Unsetenv("AZURE_STORAGE_ACCOUNT_NAME")
_ = os.Unsetenv("AZURE_STORAGE_ACCOUNT_KEY")
require.NoError(t, err)
require.IsType(t, &AzureStorageConnection{}, connection)
p := connection.(*AzureStorageConnection)
require.Equal(t, DummyRouterURL, p.routerURL)
require.NotNil(t, p.service)
}
func TestAzureStorageQueueSingleMessage(t *testing.T) {
runAzureStorageQueueTest(t, 1, false)
}
func TestAzureStorageQueueMultipleMessages(t *testing.T) {
runAzureStorageQueueTest(t, 10, false)
}
func TestAzureStorageQueueSingleOutputMessage(t *testing.T) {
runAzureStorageQueueTest(t, 1, true)
}
func TestAzureStorageQueueMultipleOutputMessages(t *testing.T) {
runAzureStorageQueueTest(t, 10, true)
}
func TestAzureStorageQueuePoisonMessage(t *testing.T) {
const (
TriggerName = "queuetrigger"
QueueName = "inputqueue"
MessageBody = "input"
FunctionName = "badfunc"
ContentType = "text/plain"
)
// Mock a HTTP client that returns different failures
httpClient := new(azureHTTPClientMock)
httpClient.On(
"Do",
mock.MatchedBy(httpRequestMatcher(t, QueueName, "", "", ContentType, FunctionName, MessageBody)),
).Return(
&http.Response{
StatusCode: 500,
Body: ioutil.NopCloser(strings.NewReader("server error")),
},
nil,
).Once()
httpClient.On(
"Do",
mock.MatchedBy(httpRequestMatcher(t, QueueName, "", "1", ContentType, FunctionName, MessageBody)),
).Return(
&http.Response{
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader("not found")),
},
nil,
).Once()
httpClient.On(
"Do",
mock.MatchedBy(httpRequestMatcher(t, QueueName, "", "2", ContentType, FunctionName, MessageBody)),
).Return(
&http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(strings.NewReader("bad request")),
},
nil,
).Once()
httpClient.On(
"Do",
mock.MatchedBy(httpRequestMatcher(t, QueueName, "", "3", ContentType, FunctionName, MessageBody)),
).Return(
&http.Response{
StatusCode: 403,
Body: ioutil.NopCloser(strings.NewReader("not authorized")),
},
nil,
).Once()
// Mock a queue message with "input" as the message body
message := new(azureMessageMock)
message.On("Bytes").Return([]byte(MessageBody))
message.On(
"Delete",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil)
// Mock a queue that performs a no-op create, returns a "poison" message, and then returns no more messages
queue := new(azureQueueMock)
queue.On(
"Create",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil)
queue.On(
"GetMessages",
mock.MatchedBy(
func(options *storage.GetMessagesOptions) bool {
return options.NumOfMessages == AzureMessageFetchCount &&
options.VisibilityTimeout == int(AzureMessageVisibilityTimeout/time.Second)
},
),
).Return([]AzureMessage{message}, nil).Once()
queue.On(
"GetMessages",
mock.MatchedBy(
func(options *storage.GetMessagesOptions) bool {
return options.NumOfMessages == AzureMessageFetchCount &&
options.VisibilityTimeout == int(AzureMessageVisibilityTimeout/time.Second)
},
),
).Return([]AzureMessage{}, nil)
// Mock a poison queue message that performs a no-op Put
poisonMessage := new(azureMessageMock)
poisonMessage.On(
"Put",
mock.MatchedBy(
func(options *storage.PutMessageOptions) bool {
return options == nil
},
),
).Return(nil)
// Mock a poison queue that performs a no-op create and creates a new message
poisonQueue := new(azureQueueMock)
poisonQueue.On(
"Create",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil)
poisonQueue.On("NewMessage", MessageBody).Return(poisonMessage).Once()
// Mock the queue service to return the input queue
service := new(azureQueueServiceMock)
service.On("GetQueue", QueueName).Return(queue).Once()
service.On("GetQueue", QueueName+AzurePoisonQueueSuffix).Return(poisonQueue).Once()
// Create the storage connection and subscribe to the trigger
connection := AzureStorageConnection{
routerURL: DummyRouterURL,
service: service,
httpClient: httpClient,
}
subscription, err := connection.subscribe(&crd.MessageQueueTrigger{
Metadata: metav1.ObjectMeta{
Name: TriggerName,
},
Spec: fission.MessageQueueTriggerSpec{
FunctionReference: fission.FunctionReference{
Type: fission.FunctionReferenceTypeFunctionName,
Name: FunctionName,
},
MessageQueueType: ASQ,
Topic: QueueName,
ContentType: ContentType,
},
})
require.NoError(t, err)
require.NotNil(t, subscription)
connection.unsubscribe(subscription)
mock.AssertExpectationsForObjects(t, httpClient, message, poisonMessage, queue, poisonQueue, service)
}
func httpRequestMatcher(t *testing.T, queue string, responseQueue string, retry string, contentType string, functionName string, body string) func(*http.Request) bool {
expectedURL := fmt.Sprintf("%s/fission-function/%s", DummyRouterURL, functionName)
return func(req *http.Request) bool {
requestBody, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
req.Body = ioutil.NopCloser(strings.NewReader(string(requestBody)))
return queue == req.Header.Get("X-Fission-MQTrigger-Topic") &&
responseQueue == req.Header.Get("X-Fission-MQTrigger-RespTopic") &&
retry == req.Header.Get("X-Fission-MQTrigger-RetryCount") &&
contentType == req.Header.Get("Content-Type") &&
req.URL.String() == expectedURL &&
string(requestBody) == body
}
}
func runAzureStorageQueueTest(t *testing.T, count int, output bool) {
const (
TriggerName = "queuetrigger"
QueueName = "inputqueue"
OutputQueueName = "outputqueue"
MessageBody = "input"
FunctionName = "testfunc"
FunctionResponse = "output"
ContentType = "text/plain"
)
responseTopic := ""
if output {
responseTopic = OutputQueueName
}
// Mock a HTTP client that returns 200 with "output" for the body
httpClient := new(azureHTTPClientMock)
httpClient.bodyHandler = func(res *http.Response) {
res.Body = ioutil.NopCloser(strings.NewReader(FunctionResponse))
}
httpClient.On(
"Do",
mock.MatchedBy(httpRequestMatcher(t, QueueName, responseTopic, "", ContentType, FunctionName, MessageBody)),
).Return(&http.Response{StatusCode: 200}, nil).Times(count)
// Mock a queue message with "input" as the message body
message := new(azureMessageMock)
message.On("Bytes").Return([]byte(MessageBody)).Times(count)
message.On(
"Delete",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil).Times(count)
// Mock a queue that performs a no-op create, returns a message the specified number of times, and then returns no more messages
queue := new(azureQueueMock)
queue.On(
"Create",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil)
queue.On(
"GetMessages",
mock.MatchedBy(
func(options *storage.GetMessagesOptions) bool {
return options.NumOfMessages == AzureMessageFetchCount &&
options.VisibilityTimeout == int(AzureMessageVisibilityTimeout/time.Second)
},
),
).Return([]AzureMessage{message}, nil).Times(count)
queue.On(
"GetMessages",
mock.MatchedBy(
func(options *storage.GetMessagesOptions) bool {
return options.NumOfMessages == AzureMessageFetchCount &&
options.VisibilityTimeout == int(AzureMessageVisibilityTimeout/time.Second)
},
),
).Return([]AzureMessage{}, nil)
// Mock the output queue if needed
outputMessage := new(azureMessageMock)
outputQueue := new(azureQueueMock)
if output {
outputMessage.On(
"Put",
mock.MatchedBy(
func(options *storage.PutMessageOptions) bool {
return options == nil
},
),
).Return(nil).Times(count)
outputQueue.On(
"Create",
mock.MatchedBy(
func(options *storage.QueueServiceOptions) bool {
return options == nil
},
),
).Return(nil).Times(count)
outputQueue.On("NewMessage", FunctionResponse).Return(outputMessage).Times(count)
}
// Mock the queue service to return the input queue
service := new(azureQueueServiceMock)
service.On("GetQueue", QueueName).Return(queue).Once()
if output {
service.On("GetQueue", OutputQueueName).Return(outputQueue).Times(count)
}
// Create the storage connection and subscribe to the trigger
connection := AzureStorageConnection{
routerURL: DummyRouterURL,
service: service,
httpClient: httpClient,
}
subscription, err := connection.subscribe(&crd.MessageQueueTrigger{
Metadata: metav1.ObjectMeta{
Name: TriggerName,
},
Spec: fission.MessageQueueTriggerSpec{
FunctionReference: fission.FunctionReference{
Type: fission.FunctionReferenceTypeFunctionName,
Name: FunctionName,
},
MessageQueueType: ASQ,
Topic: QueueName,
ResponseTopic: responseTopic,
ContentType: ContentType,
},
})
require.NoError(t, err)
require.NotNil(t, subscription)
connection.unsubscribe(subscription)
mock.AssertExpectationsForObjects(t, httpClient, message, outputMessage, queue, outputQueue, service)
}
+10
View File
@@ -18,6 +18,7 @@ package messageQueue
import (
"errors"
"regexp"
"time"
log "github.com/sirupsen/logrus"
@@ -28,6 +29,7 @@ import (
const (
NATS string = "nats-streaming"
ASQ string = "azure-storage-queue"
)
const (
@@ -36,6 +38,10 @@ const (
GET_ALL_TRIGGERS
)
var (
validAzureQueueName = regexp.MustCompile("^[a-z0-9][a-z0-9\\-]*[a-z0-9]$")
)
type (
messageQueueSubscription interface{}
@@ -87,6 +93,8 @@ func MakeMessageQueueTriggerManager(fissionClient *crd.FissionClient, routerUrl
switch mqConfig.MQType {
case NATS:
messageQueue, err = makeNatsMessageQueue(routerUrl, mqConfig)
case ASQ:
messageQueue, err = newAzureStorageConnection(routerUrl, mqConfig)
default:
err = errors.New("No matched message queue type found")
}
@@ -222,6 +230,8 @@ func IsTopicValid(mqType string, topic string) bool {
switch mqType {
case NATS:
return isTopicValidForNats(topic)
case ASQ:
return len(topic) >= 3 && len(topic) <= 63 && validAzureQueueName.MatchString(topic)
}
return false
}