Kafka tests (#944)

A simple test for Kafka integration which runs locally provided cluster has Kafka and Fission install has Kafka integratiom enabled (mqtrigger-kafka deployment present).
This commit is contained in:
Vishal
2018-10-22 20:07:51 +05:30
committed by GitHub
parent 619b390af3
commit e7f1d4564a
12 changed files with 159 additions and 121 deletions
+6
View File
@@ -0,0 +1,6 @@
module.exports = async function(context) {
return {
status: 200,
body: "Hello, World!"
};
}
+6
View File
@@ -0,0 +1,6 @@
module.exports = async function(context) {
return {
status: 400,
body: "Hello, World!"
};
}
+112
View File
@@ -0,0 +1,112 @@
// This file originally came from official Nats.io GitHub repository.
// You can reach original file with the following link:
// https://github.com/nats-io/go-nats-streaming/tree/master/examples
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore
package main
import (
"flag"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/nats-io/go-nats-streaming"
)
var usageStr = `
Usage: stan-pub [options] <subject> <message>
Options:
-s, --server <url> NATS Streaming server URL(s)
-c, --cluster <cluster name> NATS Streaming cluster name
-id,--clientid <client ID> NATS Streaming client ID
-a, --async Asynchronous publish mode
`
// NOTE: Use tls scheme for TLS, e.g. stan-pub -s tls://demo.nats.io:4443 foo hello
func usage() {
fmt.Printf("%s\n", usageStr)
os.Exit(0)
}
func main() {
var clusterID string
var clientID string
var async bool
var URL string
flag.StringVar(&URL, "s", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "server", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&clusterID, "c", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clusterID, "cluster", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clientID, "id", "stan-pub", "The NATS Streaming client ID to connect with")
flag.StringVar(&clientID, "clientid", "stan-pub", "The NATS Streaming client ID to connect with")
flag.BoolVar(&async, "a", false, "Publish asynchronously")
flag.BoolVar(&async, "async", false, "Publish asynchronously")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
usage()
}
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(URL))
if err != nil {
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, URL)
}
defer sc.Close()
subj, msg := args[0], []byte(args[1])
ch := make(chan bool)
var glock sync.Mutex
var guid string
acb := func(lguid string, err error) {
glock.Lock()
log.Printf("Received ACK for guid %s\n", lguid)
defer glock.Unlock()
if err != nil {
log.Fatalf("Error in server ack for guid %s: %v\n", lguid, err)
}
if lguid != guid {
log.Fatalf("Expected a matching guid in ack callback, got %s vs %s\n", lguid, guid)
}
ch <- true
}
if async != true {
err = sc.Publish(subj, msg)
if err != nil {
log.Fatalf("Error during publish: %v\n", err)
}
log.Printf("Published [%s] : '%s'\n", subj, msg)
} else {
glock.Lock()
guid, err = sc.PublishAsync(subj, msg, acb)
if err != nil {
log.Fatalf("Error during async publish: %v\n", err)
}
glock.Unlock()
if guid == "" {
log.Fatal("Expected non-empty guid to be returned.")
}
log.Printf("Published [%s] : '%s' [guid: %s]\n", subj, msg, guid)
select {
case <-ch:
break
case <-time.After(5 * time.Second):
log.Fatal("timeout")
}
}
}
+133
View File
@@ -0,0 +1,133 @@
// This file originally came from official Nats.io GitHub repository.
// You can reach original file with the following link:
// https://github.com/nats-io/go-nats-streaming/tree/master/examples
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore
package main
import (
"flag"
"log"
"time"
"github.com/nats-io/go-nats-streaming"
"github.com/nats-io/go-nats-streaming/pb"
)
var usageStr = `
Usage: stan-sub [options] <subject>
Options:
-s, --server <url> NATS Streaming server URL(s)
-c, --cluster <cluster name> NATS Streaming cluster name
-id,--clientid <client ID> NATS Streaming client ID
Subscription Options:
--qgroup <name> Queue group
--seq <seqno> Start at seqno
--all Deliver all available messages
--last Deliver starting with last published message
--since <duration> Deliver messages in last interval (e.g. 1s, 1hr)
(for more information: https://golang.org/pkg/time/#ParseDuration)
--durable <name> Durable subscriber name
--unsubscribe Unsubscribe the durable on exit
`
// NOTE: Use tls scheme for TLS, e.g. stan-sub -s tls://demo.nats.io:4443 foo
func usage() {
log.Fatalf(usageStr)
}
func printMsg(m *stan.Msg) {
log.Printf("[%s]: '%s'", m.Subject, m.Data)
}
func main() {
var clusterID string
var clientID string
var showTime bool
var startSeq uint64
var startDelta string
var deliverAll bool
var deliverLast bool
var durable string
var qgroup string
var unsubscribe bool
var URL string
// defaultID := fmt.Sprintf("client.%s", nuid.Next())
flag.StringVar(&URL, "s", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "server", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&clusterID, "c", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clusterID, "cluster", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clientID, "id", "", "The NATS Streaming client ID to connect with")
flag.StringVar(&clientID, "clientid", "", "The NATS Streaming client ID to connect with")
flag.BoolVar(&showTime, "t", false, "Display timestamps")
// Subscription options
flag.Uint64Var(&startSeq, "seq", 0, "Start at sequence no.")
flag.BoolVar(&deliverAll, "all", false, "Deliver all")
flag.BoolVar(&deliverLast, "last", false, "Start with last value")
flag.StringVar(&startDelta, "since", "", "Deliver messages since specified time offset")
flag.StringVar(&durable, "durable", "", "Durable subscriber name")
flag.StringVar(&qgroup, "qgroup", "", "Queue group name")
flag.BoolVar(&unsubscribe, "unsubscribe", false, "Unsubscribe the durable on exit")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if clientID == "" {
log.Printf("Error: A unique client ID must be specified.")
usage()
}
if len(args) < 1 {
log.Printf("Error: A subject must be specified.")
usage()
}
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(URL))
if err != nil {
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, URL)
}
// log.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", URL, clusterID, clientID)
subj := args[0]
exit := make(chan struct{})
mcb := func(msg *stan.Msg) {
printMsg(msg)
exit <- struct{}{}
}
startOpt := stan.StartAt(pb.StartPosition_NewOnly)
if startSeq != 0 {
startOpt = stan.StartAtSequence(startSeq)
} else if deliverLast == true {
startOpt = stan.StartWithLastReceived()
} else if deliverAll == true {
log.Print("subscribing with DeliverAllAvailable")
startOpt = stan.DeliverAllAvailable()
} else if startDelta != "" {
ago, err := time.ParseDuration(startDelta)
if err != nil {
sc.Close()
log.Fatal(err)
}
startOpt = stan.StartAtTimeDelta(ago)
}
sub, err := sc.QueueSubscribe(subj, qgroup, mcb, startOpt, stan.DurableName(durable))
if err != nil {
sc.Close()
log.Fatal(err)
}
<-exit
sub.Unsubscribe()
}
+71
View File
@@ -0,0 +1,71 @@
#!/bin/bash
#
# Create a function and trigger it using NATS
#
set -euo pipefail
set +x
ROOT=$(dirname $0)/../../..
DIR=$(dirname $0)
clusterID="fissionMQTrigger"
topic="foo.bar"
resptopic="foo.foo"
expectedRespOutput="[foo.foo]: 'Hello, World!'"
cleanup() {
log "Cleaning up..."
fission env delete --name nodejs || true
fission fn delete --name $fn || true
fission mqtrigger delete --name $mqt || true
}
if [ -z "${TEST_NOCLEANUP:-}" ]; then
trap cleanup EXIT
else
log "TEST_NOCLEANUP is set; not cleaning up test artifacts afterwards."
fi
log "Pre-test cleanup"
fission env delete --name nodejs || true
log "Creating nodejs env"
fission env create --name nodejs --image fission/node-env
log "Creating function"
fn=hello-$(date +%s)
fission fn create --name $fn --env nodejs --code $DIR/main.js --method GET
log "Creating message queue trigger"
mqt=mqt-$(date +%s)
fission mqtrigger create --name $mqt --function $fn --mqtype "nats-streaming" --topic $topic --resptopic $resptopic
# wait until nats trigger is created
sleep 5
#
# Send a message
#
log "Sending message"
go run $DIR/stan-pub.go -s $FISSION_NATS_STREAMING_URL -c $clusterID -id clientPub $topic ""
#
# Wait for message on response topic
#
log "Waiting for response"
TIMEOUT=timeout
if [ $(uname -s) == 'Darwin' ]
then
# If this fails on mac os, do "brew install coreutils".
TIMEOUT=gtimeout
fi
response=$($TIMEOUT 120s go run $DIR/stan-sub.go --last -s $FISSION_NATS_STREAMING_URL -c $clusterID -id clientSub $resptopic 2>&1)
if [[ "$response" != "$expectedRespOutput" ]]; then
log "$response is not equal to $expectedRespOutput"
exit 1
fi
log "Subscriber received expected response: $response"
+81
View File
@@ -0,0 +1,81 @@
#!/bin/bash
#
# Create a function and trigger it using NATS
# To run this on Minikube, uncomment line 18
set -euo pipefail
set +x
ROOT=$(dirname $0)/../../..
DIR=$(dirname $0)
clusterID="fissionMQTrigger"
topic="foo.bar"
resptopic="foo.foo"
errortopic="foo.error"
maxretries=1
# FISSION_NATS_STREAMING_URL="http://defaultFissionAuthToken@$(minikube ip):4222"
expectedRespOutput="[foo.error]: 'Hello, World!'"
cleanup() {
log "Cleaning up..."
fission env delete --name nodejs || true
fission fn delete --name $fn || true
fission mqtrigger delete --name $mqt || true
}
if [ -z "${TEST_NOCLEANUP:-}" ]; then
trap cleanup EXIT
else
log "TEST_NOCLEANUP is set; not cleaning up test artifacts afterwards."
fi
log "Pre-test cleanup"
fission env delete --name nodejs || true
log "Creating nodejs env"
fission env create --name nodejs --image fission/node-env
log "Creating function"
fn=hello-$(date +%s)
fission fn create --name $fn --env nodejs --code $DIR/main_error.js --method GET
log "Creating message queue trigger"
mqt=mqt-$(date +%s)
fission mqtrigger create --name $mqt --function $fn --mqtype "nats-streaming" --topic $topic --resptopic $resptopic --errortopic $errortopic --maxretries $maxretries
log "Updated mqtrigger list"
fission mqtrigger list
# wait until nats trigger is created
sleep 5
#
# Send a message
#
log "Sending message"
go run $DIR/stan-pub.go -s $FISSION_NATS_STREAMING_URL -c $clusterID -id clientPub $topic ""
#
# Wait for message on error topic
#
log "Waiting for response"
TIMEOUT=timeout
if [ $(uname -s) == 'Darwin' ]
then
# If this fails on mac os, do "brew install coreutils".
TIMEOUT=gtimeout
fi
response=$(go run $DIR/stan-sub.go --last -s $FISSION_NATS_STREAMING_URL -c $clusterID -id clientSub $errortopic 2>&1)
log "Subscriber received response: $response"
fission mqtrigger delete --name $mqt
# kubectl delete functions --all
if [[ "$response" != "$expectedRespOutput" ]]; then
log "$response is not equal to $expectedRespOutput"
exit 1
else
log "Responses match."
fi