forked from Templum/rabbitmq-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
114 lines (89 loc) · 2.94 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// +build integration
/*
* Copyright (c) Simon Pelczer 2021. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
package main
import (
"context"
"fmt"
"os"
"path"
"testing"
"time"
"github.com/Templum/rabbitmq-connector/pkg/openfaas"
"github.com/Templum/rabbitmq-connector/pkg/types"
t "github.com/openfaas/faas-provider/types"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
)
func getPathToExampleTopology() string {
dir, _ := os.Getwd()
return path.Join(dir, "artifacts", "example_topology.yaml")
}
const TOPIC = "Foo"
func TestMain(m *testing.M) {
fmt.Print("Integration Test")
_ = os.Setenv("basic_auth", "false")
_ = os.Setenv("OPEN_FAAS_GW_URL", "http://localhost:8080")
_ = os.Setenv("RMQ_USER", "user")
_ = os.Setenv("RMQ_PASS", "pass")
_ = os.Setenv("RMQ_HOST", "localhost")
_ = os.Setenv("PATH_TO_TOPOLOGY", getPathToExampleTopology())
defer os.Unsetenv("basic_auth")
defer os.Unsetenv("OPEN_FAAS_GW_URL")
defer os.Unsetenv("RMQ_USER")
defer os.Unsetenv("RMQ_PASS")
defer os.Unsetenv("RMQ_HOST")
defer os.Unsetenv("PATH_TO_TOPOLOGY")
os.Exit(m.Run())
}
func getOpenFaaSClient() openfaas.FunctionFetcher {
httpClient := types.MakeHTTPClient(false, 256, 60*time.Second)
ofClient := openfaas.NewClient(httpClient, nil, os.Getenv("OPEN_FAAS_GW_URL"))
return ofClient
}
func getIntegrationFaaSFunction(client openfaas.FunctionFetcher) t.FunctionStatus {
functions, _ := client.GetFunctions(context.Background(), "")
return functions[0]
}
func establishChannel(connectionURL string) (*amqp.Channel, error) {
conn, err := amqp.Dial(connectionURL)
if err != nil {
return nil, err
}
return conn.Channel()
}
func publishMessage(channel *amqp.Channel, topic string, message string) error {
return channel.Publish(
"AEx", // exchange
topic, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(message),
})
}
func Test_main(t *testing.T) {
go main()
time.Sleep(1 * time.Second)
client := getOpenFaaSClient()
channel, err := establishChannel("amqp://user:pass@localhost:5672")
assert.NoError(t, err, "failed to establish connection with RabbitMQ. Will abort integration test here")
before := getIntegrationFaaSFunction(client)
assert.GreaterOrEqual(t, before.InvocationCount, float64(0), "should be 0 or more")
assert.Contains(t, (*before.Annotations)["topic"], TOPIC, "should listen for TOPIC Foo")
publishedMessages := 0
for i := 0; i < 1000; i++ {
err := publishMessage(channel, TOPIC, "Hello World!")
if err == nil {
publishedMessages += 1
}
time.Sleep(50 * time.Millisecond)
}
time.Sleep(5 * time.Second)
after := getIntegrationFaaSFunction(client)
assert.Greater(t, after.InvocationCount, before.InvocationCount)
assert.GreaterOrEqual(t, after.InvocationCount, float64(publishedMessages), "should invoked at least published amount times")
}