Skip to content

Commit

Permalink
Add draft Golang Code generation
Browse files Browse the repository at this point in the history
PoC for SDK conformance testing.
Code generator should use code templates from sdk_client use testReady
scenarios from harness folder and produce go file which would run the
scenario.
  • Loading branch information
aLekSer committed Jun 20, 2019
1 parent f9ef4f2 commit 73ee02a
Show file tree
Hide file tree
Showing 12 changed files with 588 additions and 2 deletions.
36 changes: 36 additions & 0 deletions build/build-sdk-conformance/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 Google Inc. 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.
FROM golang:latest

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}

WORKDIR /go/src/agones.dev/agones
ENV PATH /usr/local/go/bin:/go/bin:$PATH

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

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

# Copyright 2019 Google Inc. 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

cd /go/src/agones.dev/agones/test/sdk
./sdk --test="" --sdk="nodejs"
cd /go/src/agones.dev/agones/test/sdk/bin/nodejs
npm install --cache /tmp/empty-cache
npm rebuild

cd /go/src/agones.dev/agones/test/sdk
./sdk --verify=true --sdk nodejs
26 changes: 26 additions & 0 deletions build/build-sdk-conformance/sidecar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

# Copyright 2019 Google Inc. 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=off
cd /go/src/agones.dev/agones/cmd/sdk-server
go build
cd /go/src/agones.dev/agones/test/sdk
rm -rf ./bin
mkdir -p ./bin
mv /go/src/agones.dev/agones/cmd/sdk-server/sdk-server ./bin
go build
31 changes: 31 additions & 0 deletions build/build-sdk-conformance/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Copyright 2019 Google Inc. 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=off

cd /go/src/agones.dev/agones/test/sdk
./sdk --test=""
cd ./bin/golang/
dirs=($(find . -mindepth 1 -type d))
for dir in "${dirs[@]}"; do
cd "$dir"
go build
echo $PWD
cd -
done
cd /go/src/agones.dev/agones/test/sdk
./sdk --verify=true
12 changes: 12 additions & 0 deletions build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build_sdk_base_remote_tag = $(REGISTRY)/$(build_sdk_base_tag)
build_sdk_prefix = agones-build-sdk-
grpc_release_tag = v1.16.1
sdk_build_folder = build-sdk-images/
sdk_build_conformance-folder = build-sdk-conformance/
SDK_FOLDER ?= go
COMMAND ?= gen

Expand Down Expand Up @@ -108,3 +109,14 @@ ensure-build-sdk-image-base:
# create the build image sdk if it doesn't exist
ensure-build-sdk-image:
$(MAKE) build-build-sdk-image SDK_FOLDER=$(SDK_FOLDER)

build-conformance-tests:
cd $(sdk_build_conformance-folder); \
docker build --tag=conformance:$(build_version) ./ $(DOCKER_BUILD_ARGS)
docker run --rm $(common_mounts) -e "VERSION=$(VERSION)" $(DOCKER_RUN_ARGS) conformance:$(build_version) /root/sidecar.sh

run-conformance-tests:
docker run --rm $(common_mounts) -e "VERSION=$(VERSION)" $(DOCKER_RUN_ARGS) conformance:$(build_version) /root/test.sh

run-conformance-jstests:
docker run --rm $(common_mounts) -e "VERSION=$(VERSION)" $(DOCKER_RUN_ARGS) conformance:$(build_version) /root/jstest.sh
22 changes: 20 additions & 2 deletions cmd/sdk-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"agones.dev/agones/pkg"
"agones.dev/agones/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -51,6 +52,7 @@ const (
localFlag = "local"
fileFlag = "file"
addressFlag = "address"
timeout = "timeout"
)

var (
Expand All @@ -69,6 +71,7 @@ func main() {
logger.WithField("grpcPort", grpcPort).WithField("Address", ctlConf.Address).Fatalf("Could not listen on grpcPort")
}
stop := signals.NewStopChannel()
timedStop := make(chan struct{})
grpcServer := grpc.NewServer()
// don't graceful stop, because if we get a kill signal
// then the gameserver is being shut down, and we no longer
Expand All @@ -89,6 +92,12 @@ func main() {
if err != nil {
logger.WithError(err).Fatal("Could not start local sdk server")
}
if ctlConf.Timeout != 0 {
go func() {
time.Sleep(time.Duration(ctlConf.Timeout) * time.Second)
close(timedStop)
}()
}
} else {
var config *rest.Config
config, err = rest.InClusterConfig()
Expand Down Expand Up @@ -127,7 +136,11 @@ func main() {
go runGrpc(grpcServer, lis)
go runGateway(ctx, grpcEndpoint, mux, httpServer)

<-stop
select {
case <-stop:
case <-timedStop:
}

logger.Info("shutting down sdk server")
}

Expand Down Expand Up @@ -190,22 +203,26 @@ func parseEnvFlags() config {
viper.SetDefault(localFlag, false)
viper.SetDefault(fileFlag, "")
viper.SetDefault(addressFlag, "localhost")
viper.SetDefault(timeout, 0)
pflag.Bool(localFlag, viper.GetBool(localFlag),
"Set this, or LOCAL env, to 'true' to run this binary in local development mode. Defaults to 'false'")
pflag.StringP(fileFlag, "f", viper.GetString(fileFlag), "Set this, or FILE env var to the path of a local yaml or json file that contains your GameServer resoure configuration")
pflag.String(addressFlag, viper.GetString(addressFlag), "The Address to bind the server grpcPort to. Defaults to 'localhost")
pflag.String(addressFlag, viper.GetString(addressFlag), "The Address to bind the server grpcPort to. Defaults to 'localhost'")
pflag.Int(timeout, viper.GetInt(timeout), "Time of execution before close. Useful for tests")
pflag.Parse()

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
runtime.Must(viper.BindEnv(localFlag))
runtime.Must(viper.BindEnv(gameServerNameEnv))
runtime.Must(viper.BindEnv(podNamespaceEnv))
runtime.Must(viper.BindEnv(timeout))
runtime.Must(viper.BindPFlags(pflag.CommandLine))

return config{
IsLocal: viper.GetBool(localFlag),
Address: viper.GetString(addressFlag),
LocalFile: viper.GetString(fileFlag),
Timeout: viper.GetInt(timeout),
}
}

Expand All @@ -214,4 +231,5 @@ type config struct {
Address string
IsLocal bool
LocalFile string
Timeout int
}
26 changes: 26 additions & 0 deletions test/sdk/harness/testGetGameserver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

steps:
- init
- ready
- getGameServer
- setlabel
- setannotation
- cleanup
expected:
- Ready
- getting GameServer details
- Setting label
- Setting annotation
24 changes: 24 additions & 0 deletions test/sdk/harness/testHealth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.

steps:
- init
- ready
- health
- shutdown
- cleanup
expected:
- Ready
- Health
- Shutdown
26 changes: 26 additions & 0 deletions test/sdk/harness/testReady.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

steps:
- init
- ready
- allocate
- setlabel
- setannotation
- cleanup
expected:
- Ready
- Allocate
- Setting label
- Setting annotation
78 changes: 78 additions & 0 deletions test/sdk/sdk_client/golang.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 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.

functions:
health: |
time.Sleep(1*time.Second)
for i := 0 ; i < 5; i ++ {
err = s.Health()
if err != nil {
log.Fatalf("Could not send Health check")
} else {
log.Println("Health message sent")
}
}
ready: |
err = s.Ready()
if err != nil {
log.Fatalf("Could not send ready message")
}
shutdown: |
err = s.Shutdown()
if err != nil {
log.Fatalf("Could not send shutdown request")
}
getGameServer: |
gs, err := s.GameServer()
if err != nil {
log.Fatalf("Could not get gameserver parameters")
}
log.Println(gs)
allocate: |
err = s.Allocate()
if err != nil {
log.Fatalf("Could not send allocate request")
}
setlabel: |
err = s.SetLabel("new", "label")
if err != nil {
log.Fatalf("Could not set label")
}
setannotation: |
err = s.SetAnnotation("new", "annotation")
if err != nil {
log.Fatalf("Could not set annotation")
}
init: |
package main
import (
"time"
"log"
sdk "agones.dev/agones/sdks/go"
)
func main() {
log.Println("starting")
time.Sleep(100 * time.Millisecond)
s, err := sdk.NewSDK()
if err != nil {
log.Fatalf("Could not connect to sdk: %v", err)
} else {
log.Println("SDK initialised")
}
cleanup: |
time.Sleep(3*time.Second)
}
Loading

0 comments on commit 73ee02a

Please sign in to comment.