Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SDK server HTTP API test #1079

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions build/build-sdk-images/restapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# 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.
ARG BASE_IMAGE=agones-build-sdk-base:latest
FROM $BASE_IMAGE

RUN apt-get update && \
apt-get install -y wget jq && \
apt-get clean

# install go
WORKDIR /usr/local
ENV GO_VERSION=1.12
ENV GO111MODULE=on
ENV GOPATH /go
RUN wget -q https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
tar -xzf go${GO_VERSION}.linux-amd64.tar.gz && rm go${GO_VERSION}.linux-amd64.tar.gz && mkdir -p ${GOPATH}

RUN curl -sL https://deb.nodesource.com/setup_11.x | bash - && \
apt-get install -y nodejs

RUN apt-get install -y openjdk-8-jre

ENV PATH /usr/local/go/bin:/go/bin:$PATH


# code generation scripts
COPY *.sh /root/
RUN chmod +x /root/*.sh
20 changes: 20 additions & 0 deletions build/build-sdk-images/restapi/build-sdk-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

# Copyright 2019 Google LLC All Rights Reserved.
#
# 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.


mkdir /go/src/agones.dev/agones/swagger
wget -q http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.8/swagger-codegen-cli-2.4.8.jar -O /tmp/swagger-codegen-cli.jar
java -jar /tmp/swagger-codegen-cli.jar generate -i /go/src/agones.dev/agones/sdk.swagger.json -l go -o /go/src/agones.dev/agones/test/sdk/restapi/swagger
19 changes: 19 additions & 0 deletions build/build-sdk-images/restapi/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Copyright 2019 Google LLC All Rights Reserved.
#
# 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.

set -ex
rm -rf /go/src/agones.dev/agones/test/sdk/restapi/swagger
rm /go/src/agones.dev/agones/test/sdk/restapi/http-api-test.go
21 changes: 21 additions & 0 deletions build/build-sdk-images/restapi/sdktest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

# Copyright 2019 Google LLC All Rights Reserved.
#
# 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.

set -ex
GO111MODULE=on
cd /go/src/agones.dev/agones/test/sdk/restapi
cp ./http-api-test.go.nolint ./http-api-test.go
go run http-api-test.go
20 changes: 14 additions & 6 deletions build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ gen-sdk-grpc: COMMAND := gen
gen-sdk-grpc: run-sdk-command

# Runs a command on all supported languages, use COMMAND variable to select which command.
run-all-sdk-command: run-sdk-command-go run-sdk-command-rust run-sdk-command-cpp run-sdk-command-node
run-all-sdk-command: run-sdk-command-go run-sdk-command-rust run-sdk-command-cpp run-sdk-command-node run-sdk-command-restapi

run-sdk-command-node:
$(MAKE) run-sdk-command COMMAND=$(COMMAND) SDK_FOLDER=node
Expand All @@ -75,6 +75,10 @@ run-sdk-command-rust:
run-sdk-command-go:
$(MAKE) run-sdk-command COMMAND=$(COMMAND) SDK_FOLDER=go

run-sdk-command-restapi:
$(MAKE) run-sdk-command COMMAND=$(COMMAND) SDK_FOLDER=restapi


# Runs a command for a specific SDK if it exists.
run-sdk-command:
cd $(sdk_build_folder); \
Expand Down Expand Up @@ -117,9 +121,9 @@ ensure-build-sdk-image:
# Run SDK conformance Sidecar server in docker in order to run
# SDK client test against it. Useful for test development
run-sdk-conformance-local: TIMEOUT ?= 30
run-sdk-conformance-local: TESTS ?= ready,allocate,setlabel,setannotation,gameserver,health,shutdown,watch
run-sdk-conformance-local: TESTS ?= ready,allocate,setlabel,setannotation,gameserver,health,shutdown,watch,reserve
run-sdk-conformance-local: ensure-agones-sdk-image
docker run -e "ADDRESS=" -p 59357:59357 \
docker run -e "ADDRESS=" -p 59357:59357 -p 59358:59358 \
-e "TEST=$(TESTS)" -e "TIMEOUT=$(TIMEOUT)" $(sidecar_tag)

# Run SDK conformance test, previously built, for a specific SDK_FOLDER
Expand All @@ -132,8 +136,8 @@ run-sdk-conformance-no-build: GRPC_PORT ?= 59357
run-sdk-conformance-no-build: HTTP_PORT ?= 59358
run-sdk-conformance-no-build: ensure-agones-sdk-image
run-sdk-conformance-no-build: ensure-build-sdk-image
DOCKER_RUN_ARGS="--net host -e AGONES_SDK_GRPC_PORT=$(GRPC_PORT) $(DOCKER_RUN_ARGS)" COMMAND=sdktest $(MAKE) run-sdk-command & \
docker run -p $(GRPC_PORT):$(GRPC_PORT) -e "ADDRESS=" -e "TEST=$(TESTS)" -e "TIMEOUT=$(TIMEOUT)" -e "DELAY=$(DELAY)" \
DOCKER_RUN_ARGS="--net host -e AGONES_SDK_GRPC_PORT=$(GRPC_PORT) -e AGONES_SDK_HTTP_PORT=$(HTTP_PORT) $(DOCKER_RUN_ARGS)" COMMAND=sdktest $(MAKE) run-sdk-command & \
docker run -p $(GRPC_PORT):$(GRPC_PORT) -p $(HTTP_PORT):$(HTTP_PORT) -e "ADDRESS=" -e "TEST=$(TESTS)" -e "TIMEOUT=$(TIMEOUT)" -e "DELAY=$(DELAY)" \
--net=host $(sidecar_tag) --grpc-port $(GRPC_PORT) --http-port $(HTTP_PORT)

# Run SDK conformance test for a specific SDK_FOLDER
Expand All @@ -151,8 +155,12 @@ run-sdk-conformance-test-go:
run-sdk-conformance-test-rust:
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust

run-sdk-conformance-test-rest:
$(MAKE) run-sdk-conformance-test SDK_FOLDER=restapi HTTP_PORT=9050
$(MAKE) run-sdk-command COMMAND=clean SDK_FOLDER=restapi
roberthbailey marked this conversation as resolved.
Show resolved Hide resolved

# Run a conformance test for all SDKs supported
run-sdk-conformance-tests: run-sdk-conformance-test-node run-sdk-conformance-test-go run-sdk-conformance-test-rust
run-sdk-conformance-tests: run-sdk-conformance-test-node run-sdk-conformance-test-go run-sdk-conformance-test-rust run-sdk-conformance-test-rest

# Clean package directories and binary files left
# after building conformance tests for all SDKs supported
Expand Down
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ steps:
- name: "make-docker"
id: sdk-conformance
dir: "build"
args: [ "-j", "3", "run-sdk-conformance-tests"]
args: [ "-j", "4", "run-sdk-conformance-tests"]
waitFor:
- build-images

Expand Down
113 changes: 113 additions & 0 deletions test/sdk/restapi/http-api-test.go.nolint
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019 Google LLC All Rights Reserved.
//
// 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 main

import (
"log"
"os"
"time"

"agones.dev/agones/test/sdk/restapi/swagger"
roberthbailey marked this conversation as resolved.
Show resolved Hide resolved
"golang.org/x/net/context"
)

func main() {
log.Println("Client is starting")
conf := swagger.NewConfiguration()
portStr := os.Getenv("AGONES_SDK_HTTP_PORT")
conf.BasePath = "http://localhost:" + portStr
cli := swagger.NewAPIClient(conf)

ctx := context.Background()

// Wait for SDK server to start the test (30 seconds)
for c := 0; c < 5; c++ {
_, _, err := cli.SDKApi.Ready(ctx, swagger.SdkEmpty{})
if err == nil {
break
} else {
log.Printf("Could not send Ready: %v\n", err)
}
time.Sleep(1 * time.Second)
}

c := make(chan string)

once := true
go func() {
for {
gs, _, err := cli.SDKApi.WatchGameServer(ctx)
log.Printf("Watch response: %+v", gs)
if err != nil {
log.Printf("Error in WatchGameServer: %v\n", err)
return
} else {
if gs.ObjectMeta != nil {
uid := gs.ObjectMeta.Uid
if once {
c <- uid
once = false
}
} else {
log.Printf("Could not read GS Uid \n")
}
}
}
}()

_, _, err := cli.SDKApi.Health(ctx, swagger.SdkEmpty{})
if err != nil {
log.Fatalf("Could not send health check: %v\n", err)
}

_, _, err = cli.SDKApi.Reserve(ctx, swagger.SdkDuration{"5"})
if err != nil {
log.Fatalf("Could not send Reserve: %v\n", err)
}

_, _, err = cli.SDKApi.Allocate(ctx, swagger.SdkEmpty{})
if err != nil {
log.Fatalf("Could not send Allocate: %v\n", err)
}

gs, _, err := cli.SDKApi.GetGameServer(ctx)
if err != nil {
log.Fatalf("Could not GetGameserver: %v\n", err)
}

creationTS := gs.ObjectMeta.CreationTimestamp

_, _, err = cli.SDKApi.SetLabel(ctx, swagger.SdkKeyValue{"creationTimestamp", creationTS})
if err != nil {
log.Fatalf("Could not SetLabel: %v\n", err)
}

// TODO: fix WatchGameServer() HTTP API Swagger definition and remove the following lines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The go routine above writes to c as does this one. Can we delete this TODO and the below go routine? Or does the watch never fire?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watch function does not return, mentioned this in above comment, it is waiting for EOF, and "result". Opened a ticket for that.

go func() {
c <- gs.ObjectMeta.Uid
}()

uid := <-c
_, _, err = cli.SDKApi.SetAnnotation(ctx, swagger.SdkKeyValue{"UID", uid})
if err != nil {
log.Fatalf("Could not SetAnnotation: %v\n", err)
}

_, _, err = cli.SDKApi.Shutdown(ctx, swagger.SdkEmpty{})
if err != nil {
log.Fatalf("Could not GetGameserver: %v\n", err)
}
log.Println("REST API test finished, all queries were performed")
}