Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Merge f320f5b into ca34e81
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioIcy committed Apr 6, 2021
2 parents ca34e81 + f320f5b commit 6b21dc0
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 310 deletions.
16 changes: 10 additions & 6 deletions Makefile
Expand Up @@ -13,25 +13,29 @@ setup-protobuf-macos:
@brew install protobuf
@go get -u github.com/gogo/protobuf/protoc-gen-gogofaster

run-testing-server:
@docker-compose -f ./testing/json/docker-compose.yml up -d etcd nats && go run ./testing/json/main.go
run-testing-json-server:
@docker-compose -f ./testing/json/docker-compose.yml up -d etcd nats redis && go run ./testing/json/main.go

run-testing-bots:
@go run *.go run -d ./testing/json/specs/ --config ./testing/json/config/config.yaml
run-testing-json-bots:
@go run *.go run --duration 5s -d ./testing/json/specs/ --config ./testing/json/config/config.yaml

kill-testing-deps:
kill-testing-json-deps:
@docker-compose -f ./testing/json/docker-compose.yml down; true

run-testing-proto-server:
@(cd ./testing/protobuffer/ && make protos-compile)
@docker-compose -f ./testing/protobuffer/docker-compose.yml up -d etcd nats && go run ./testing/protobuffer/main.go

run-testing-proto-bots:
@go run *.go run --duration 10s -d ./testing/protobuffer/specs/ --config ./testing/protobuffer/config/config.yaml
@go run *.go run --duration 5s -d ./testing/protobuffer/specs/ --config ./testing/protobuffer/config/config.yaml

kill-testing-proto-deps:
@docker-compose -f ./testing/protobuffer/docker-compose.yml down; true

build-mac:
@mkdir -p out
@GOOS=darwin GOARCH=amd64 go build -o ./out/pitaya-bot-darwin ./main.go

build-linux:
@mkdir -p out
@GOOS=linux GOARCH=amd64 go build -o ./out/pitaya-bot-linux ./main.go
Expand Down
21 changes: 7 additions & 14 deletions README.md
Expand Up @@ -40,27 +40,20 @@ make setup

Here's how to run the testing example with JSON serializer:

Start etcd and nats (this command requires docker-compose and will run etcd and nats containers locally, you may run etcd and/or nats without docker if you prefer)
Start the dependencies (this command requires docker-compose, but you may run the dependencies locally if need be) and the pitaya server:
```
docker-compose -f ./testing/json/docker-compose.yml up -d etcd
```
run the server from testing example
```
make run-testing-server
$ make run-testing-json-server
```

Now a pitaya server should be running in one terminal. In another one, you can use pitaya-bot testing example:
Now a pitaya server should be running in one terminal. In another one, you can run pitaya-bot with the test specs:
```
$ pitaya-bot run -d ./testing/json/specs/ --config ./testing/json/config/config.yaml
testing/json/specs/default.json 1755
INFO[0000] Found 1 specs to be executed function=launch source=pitaya-bot
...
$ make run-testing-json-bots
```

To run the protobuf example you can run:
For the examples with protobuf, instead run:
```
make run-testing-proto-server
pitaya-bot run -d ./testing/protobuffer/specs --config ./testing/protobuffer/config/config.yaml
$ make run-testing-proto-server
$ make run-testing-proto-bots
```

## Running the tests
Expand Down
62 changes: 40 additions & 22 deletions bot/pitaya_client.go
@@ -1,6 +1,7 @@
package bot

import (
"crypto/tls"
"encoding/json"
"fmt"
"sync"
Expand All @@ -25,56 +26,73 @@ type PClient struct {

pushesMutex sync.Mutex
pushes map[string]chan []byte

timeout time.Duration
logger logrus.FieldLogger
}

func getProtoInfo(host string, docs string, pushinfo map[string]string) *client.ProtoBufferInfo {
func getProtoInfo(host string, docs string, pushinfo map[string]string, logger logrus.FieldLogger) *client.ProtoBufferInfo {
once.Do(func() {
cli := client.NewProto(docs, logrus.InfoLevel)
for k, v := range pushinfo {
cli.AddPushResponse(k, v)
}
err := cli.LoadServerInfo(host)
if err != nil {
fmt.Println("Unable to load server documentation.")
fmt.Println(err)
logger.WithError(err).Error("Unable to load server documentation.")
} else {
instance = cli.ExportInformation()
}
})
return instance
}

func tryConnect(pClient client.PitayaClient, addr string, useTLS bool, logger logrus.FieldLogger) error {
logger.Debugf("Connecting (tls=[%v])...\n", useTLS)
if useTLS {
if err := pClient.ConnectToWS(addr, "", &tls.Config{
InsecureSkipVerify: true,
}); err != nil {
if err := pClient.ConnectTo(addr, &tls.Config{
InsecureSkipVerify: true,
}); err != nil {
return err
}
}
} else {
if err := pClient.ConnectToWS(addr, ""); err != nil {
if err := pClient.ConnectTo(addr); err != nil {
return err
}
}
}
return nil
}

// NewPClient is the PCLient constructor
func NewPClient(host string, useTLS bool, docs string, pushinfo map[string]string) (*PClient, error) {
func NewPClient(host string, useTLS bool, timeout time.Duration, logger logrus.FieldLogger, docs string, pushinfo map[string]string) (*PClient, error) {
var pclient client.PitayaClient
if docs != "" {
protoclient := client.NewProto(docs, logrus.InfoLevel)
pclient = protoclient
if err := protoclient.LoadInfo(getProtoInfo(host, docs, pushinfo)); err != nil {
if err := protoclient.LoadInfo(getProtoInfo(host, docs, pushinfo, logger)); err != nil {
return nil, err
}
} else {
pclient = client.New(logrus.InfoLevel)
}

if useTLS {
if err := pclient.ConnectToTLS(host, true); err != nil {
fmt.Println("Error connecting to server")
fmt.Println(err)
return nil, err
}
} else {
if err := pclient.ConnectTo(host); err != nil {
fmt.Println("Error connecting to server")
fmt.Println(err)
return nil, err
}
if err := tryConnect(pclient, host, useTLS, logger); err != nil {
logger.WithError(err).Error("Error connecting to server")
return nil, err
}

return &PClient{
client: pclient,
responses: make(map[uint]chan []byte),
pushes: make(map[string]chan []byte),
timeout: timeout,
logger: logger,
}, nil
}

Expand Down Expand Up @@ -134,7 +152,7 @@ func (c *PClient) Request(route string, data []byte) (Response, []byte, error) {
}

return ret, responseData, nil
case <-time.After(5 * time.Second): // TODO - pass timeout as config
case <-time.After(c.timeout):
return nil, nil, fmt.Errorf("Timeout waiting for response on route %s", route)
}
}
Expand All @@ -146,20 +164,20 @@ func (c *PClient) Notify(route string, data []byte) error {
}

// ReceivePush ...
func (c *PClient) ReceivePush(route string, timeout int) (Response, error) {
func (c *PClient) ReceivePush(route string, timeout int) (Response, []byte, error) {
ch := c.getPushChannelForRoute(route)

select {
case data := <-ch:
var ret Response
if err := json.Unmarshal(data, &ret); err != nil {
err = fmt.Errorf("Error unmarshaling response: %s", err)
return nil, err
return nil, nil, err
}

return ret, nil
return ret, data, nil
case <-time.After(time.Duration(timeout) * time.Millisecond):
return nil, fmt.Errorf("Timeout waiting for push on route %s", route)
return nil, nil, fmt.Errorf("Timeout waiting for push on route %s", route)
}
}

Expand Down
12 changes: 7 additions & 5 deletions bot/sequential.go
Expand Up @@ -78,9 +78,10 @@ func (b *SequentialBot) Run() (err error) {
}()

steps := b.spec.SequentialOperations
for _, step := range steps {
for idx, step := range steps {
err = b.runOperation(step)
if err != nil {
b.logger.WithError(err).Warnf("failed sequential step %d (%s/%s)", idx, step.Type, step.URI)
return
}
}
Expand Down Expand Up @@ -170,15 +171,15 @@ func (b *SequentialBot) runFunction(op *models.Operation) error {

func (b *SequentialBot) listenToPush(op *models.Operation) error {
b.logger.Debug("Waiting for push on route: " + op.URI)
resp, err := b.client.ReceivePush(op.URI, op.Timeout)
resp, rawResp, err := b.client.ReceivePush(op.URI, op.Timeout)
if err != nil {
return err
}

b.logger.Debug("validating expectations")
err = validateExpectations(op.Expect, resp, b.storage)
if err != nil {
return err
return NewExpectError(err, rawResp, op.Expect)
}
b.logger.Debug("received valid response")

Expand Down Expand Up @@ -254,8 +255,9 @@ func (b *SequentialBot) Connect(hosts ...string) error {
docs = b.config.GetString("server.protobuffer.docs")
}

tls := b.config.GetBool("server.tls")
client, err := NewPClient(b.host, tls, docs, pushinfo)
useTLS := b.config.GetBool("server.tls")
timeout := b.config.GetDuration("server.requestTimeout")
client, err := NewPClient(b.host, useTLS, timeout, b.logger, docs, pushinfo)
if err != nil {
b.logger.WithError(err).Error("Unable to create client...")
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Expand Up @@ -63,7 +63,7 @@ func init() {
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "./config/config.yaml", "config file")
rootCmd.PersistentFlags().IntVarP(
&verbose, "verbose", "v", 3,
&verbose, "verbose", "v", 2,
"Verbosity level => v0: Error, v1=Warning, v2=Info, v3=Debug",
)
rootCmd.PersistentFlags().BoolVarP(&logJSON, "logJSON", "j", false, "logJSON output mode")
Expand Down Expand Up @@ -100,9 +100,10 @@ func fillDefaultValues(config *viper.Viper) {
"game": "",
"prometheus.port": 9191,
"server.host": "localhost",
"server.tls": false,
"server.tls": "false",
"server.serializer": "json",
"server.protobuffer.docs": "connector.docsHandler.docs",
"server.requestTimeout": "5s",
"storage.type": "memory",
"kubernetes.config": filepath.Join(homedir.HomeDir(), ".kube", "config"),
"kubernetes.context": "",
Expand Down
7 changes: 7 additions & 0 deletions constants/errors.go
Expand Up @@ -8,3 +8,10 @@ var (
ErrStorageTypeNotFound = errors.New("storage type not found")
ErrMalformedObject = errors.New("malformed object type argument")
)

// Errors that are related to a spec
var (
ErrSpecInvalidNil = errors.New("invalid spec: nil")
ErrSpecInvalidType = errors.New("invalid spec: Type")
ErrSpecInvalidURI = errors.New("invalid spec: URI")
)
4 changes: 4 additions & 0 deletions docs/configuration.rst
Expand Up @@ -64,6 +64,10 @@ The configurations needed to access the Pitaya server being tested
- false
- bool
- Boolean to enable/disable TLS to connect with Pitaya server
* - server.requestTimeout
- 5s
- time.Duration
- Request timeout for the Pitaya client
* - server.serializer
- json
- string
Expand Down
2 changes: 1 addition & 1 deletion docs/test_writing.md
Expand Up @@ -60,7 +60,7 @@ storage:
type: "memory"
server:
host: "localhost",
host: "localhost"
tls: true
prometheus:
Expand Down
16 changes: 3 additions & 13 deletions go.mod
Expand Up @@ -6,38 +6,28 @@ require (
github.com/bsm/redis-lock v6.0.0+incompatible // indirect
github.com/evanphx/json-patch v4.1.0+incompatible // indirect
github.com/go-redis/redis v6.13.2+incompatible
github.com/gogo/protobuf v1.2.1
github.com/gogo/protobuf v1.3.0
github.com/google/btree v1.0.0 // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/google/uuid v1.0.0
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/magiconair/properties v1.8.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/nats-io/go-nats v1.5.0 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/client_golang v0.8.0
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 // indirect
github.com/sirupsen/logrus v1.0.6
github.com/spf13/afero v1.1.1 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/spf13/viper v1.1.0
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.3.0
github.com/topfreegames/extensions v8.2.2+incompatible
github.com/topfreegames/pitaya v0.14.3
github.com/topfreegames/pitaya v1.1.5
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 // indirect
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
google.golang.org/appengine v1.3.0 // indirect
Expand Down

0 comments on commit 6b21dc0

Please sign in to comment.