Skip to content

Commit

Permalink
Add Integration Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kemurayama committed Nov 2, 2022
1 parent 48eeac2 commit c23cab5
Show file tree
Hide file tree
Showing 314 changed files with 33,919 additions and 1,186 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ help:
@echo "Setup a Skaffold file for debugging !!RUN AFTER CREATING YOUR CLUSTER!!"
@echo " make skaffold-setup"
@echo ""
@echo "Run integration test. "
@echo " make integration-test"
@echo ""

# build space-agon docker images
.PHONY: build
Expand Down Expand Up @@ -118,3 +121,9 @@ install:
.PHONY: uninstall
uninstall:
kubectl delete -f deploy.yaml

# integration test
.PHONY: integration-test
integration-test:
go test -count=1 -v -timeout 60s test/integration_test.go

20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ In case testing your original match making logics, [`skaffold`](https://skaffold

### Setup

1. Create a space-agon k8s cluster.
1. [Create a space-agon k8s cluster.](#create-the-resources-and-install-gaming-oss)
1. [Install `skaffold`](https://skaffold.dev/docs/install/) if you haven't.
1. Run `make skaffold-setup` on the project root to make a `skaffold.yaml`

Expand All @@ -158,6 +158,24 @@ skaffold dev

Modifying applications during `skaffold dev` triggers Build and Deploy automatically. For more commands and details, visit [`skaffold`](https://skaffold.dev/).

### Test the Cluster

When you would like to test the application, follow the steps below.

1. [Install `skaffold`](https://skaffold.dev/docs/install/) if you haven't.
1. [Create a space-agon k8s cluster.](#create-the-resources-and-install-gaming-oss)
1. Run `make skaffold-setup` on the project root to make a `skaffold.yaml`
1. Run below commands for E2E test.

```bash
# Run you space-agon applications
skaffold dev

# Open another terminal and
# Run Test command
make integration-test
```

## LICENSE

This is [Apache 2.0 License](./LICENSE).
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.14.6 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/joonix/log v0.0.0-20180502111528-d2d3f2f4a806 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
Expand All @@ -54,13 +57,15 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20210224155714-063164c882e6 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
105 changes: 101 additions & 4 deletions go.sum

Large diffs are not rendered by default.

170 changes: 170 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
Copyright 2022 Google LLC
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
https://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 test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"

"golang.org/x/net/websocket"

"github.com/googleforgames/space-agon/game/pb"
"github.com/googleforgames/space-agon/game/protostream"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
ompb "open-match.dev/open-match/pkg/pb"
)

const (
NAMESPACE = "default"
SVC = "frontend"
)

var (
pip string
gsconn string
)

func TestMain(m *testing.M) {
// Confirm LB PIP
confirmLBPIP()
// Execute Test Cases
m.Run()

}

func TestAssignment(t *testing.T) {
url := "ws://" + pip + "/matchmake/"
origin := "http://" + pip + "/"

achan := getAssignment(url, origin)
achan2 := getAssignment(url, origin)

a1, a2 := <-achan, <-achan2

if a1.Connection != a2.Connection {
t.Errorf("Different Assignments !!")
}
gsconn = a1.Connection
}

func TestConnectGameServer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
url := "ws://" + gsconn + "/connect/"
origin := "http://" + gsconn
cid1 := connectGameServer(url, origin, ctx)
cid2 := connectGameServer(url, origin, ctx)

if cid1 != 1 {
t.Errorf("First ClientID should be 1 but %d", cid1)
}

if cid2 != 2 {
t.Errorf("Second ClientID should be 2 but %d", cid2)
}
}

func connectGameServer(url string, origin string, ctx context.Context) int64 {
ws, err := websocket.Dial(url, "", origin)
if err != nil {
panic(err)
}
stream := protostream.NewProtoStream(ws)
clientInitialize := &pb.ClientInitialize{}
err = stream.Recv(clientInitialize)
if err != nil {
panic(err)
}
cid := clientInitialize.Cid

go func() {
LOOP:
for {
select {
case <-ctx.Done():
ws.Close()
break LOOP
}
}
}()
return cid
}

func getAssignment(url string, origin string) <-chan ompb.Assignment {
ch := make(chan ompb.Assignment)
ws, err := websocket.Dial(url, "", origin)
if err != nil {
panic(err)
}
stream := protostream.NewProtoStream(ws)
as := &ompb.Assignment{}
go func() {
defer ws.Close()
defer close(ch)
for range time.Tick(time.Second * 1) {
err := stream.Recv(as)
if err != nil {
fmt.Printf("Failed to receive an assignment with error: %s. Wait for 1 second.\n", err.Error())
continue
}
fmt.Printf("Connection: %v.\n", as.Connection)
ch <- *as
break
}
}()
return ch
}

func confirmLBPIP() {
var kubecfg string
ctx := context.Background()
if home := os.Getenv("HOME"); home != "" {
kubecfg = filepath.Join(home, ".kube", "config")
} else {
panic("Check if your config exists in ~/.kube")
}
cfg, err := clientcmd.BuildConfigFromFlags("", kubecfg)
if err != nil {
panic(err)
}
cli, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}
svc := cli.CoreV1().Services(NAMESPACE)
for range time.Tick(time.Second * 1) {
result, err := svc.Get(ctx, SVC, metav1.GetOptions{})
if err != nil {
fmt.Printf("Unable to get svc: %s, error: %s\n", SVC, err.Error())
continue
}
if len(result.Status.LoadBalancer.Ingress) == 0 {
fmt.Printf("Waiting for assignment of a PIP\n")
continue
} else {
pip = result.Status.LoadBalancer.Ingress[0].IP
fmt.Printf("PIP is %v\n", pip)
break
}
}
}
12 changes: 12 additions & 0 deletions vendor/github.com/imdario/mergo/.deepsource.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/imdario/mergo/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/imdario/mergo/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions vendor/github.com/imdario/mergo/CODE_OF_CONDUCT.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/imdario/mergo/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c23cab5

Please # to comment.