Skip to content

Commit

Permalink
upgrading to ginkgo 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkanatsios committed Feb 9, 2022
1 parent e4ef11d commit f1acb5f
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 255 deletions.
300 changes: 149 additions & 151 deletions cmd/e2e/gameserverapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
mpsv1alpha1 "github.com/playfab/thundernetes/pkg/operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -29,176 +29,174 @@ const (
var imgName string

var _ = Describe("GameServerAPI tests", func() {
Context("GameServerAPI tests", func() {
imgName = os.Getenv("IMG")
Expect(imgName).NotTo(BeEmpty())
imgName = os.Getenv("IMG")
Expect(imgName).NotTo(BeEmpty())

client := http.Client{
Timeout: 5 * time.Second,
}

testNamespace := "gameserverapi"
url := fmt.Sprintf("http://%s:%d/api/v1", host, port)
client := http.Client{
Timeout: 5 * time.Second,
}

buildName := "test-build-gameserverapi"
buildNameNoImage := "test-build-gameserverapi-no-image"
testNamespace := "gameserverapi"
url := fmt.Sprintf("http://%s:%d/api/v1", host, port)

It("should return an error when creating a GameServerBuild that does not have a name", func() {
build := createGameServerBuild(buildNameNoImage, testNamespace)
build.Name = ""
b, err := json.Marshal(build)
Expect(err).ToNot(HaveOccurred())
req, err := client.Post(url+"/gameserverbuilds", contentType, bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(req.StatusCode).To(Equal(http.StatusInternalServerError))
})
buildName := "test-build-gameserverapi"
buildNameNoImage := "test-build-gameserverapi-no-image"

It("should return an error when deleting a non-existent GameServerBuild", func() {
req, err := http.NewRequest("DELETE", url+"/gameserverbuilds/nonexistentbuild", nil)
Expect(err).ToNot(HaveOccurred())
res, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusNotFound))
})
It("should return an error when creating a GameServerBuild that does not have a name", func() {
build := createGameServerBuild(buildNameNoImage, testNamespace)
build.Name = ""
b, err := json.Marshal(build)
Expect(err).ToNot(HaveOccurred())
req, err := client.Post(url+"/gameserverbuilds", contentType, bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(req.StatusCode).To(Equal(http.StatusInternalServerError))
})

It("should return an error when getting an non-existing GameServerBuild", func() {
r, err := client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, "nonexistentbuild"))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})
It("should return an error when deleting a non-existent GameServerBuild", func() {
req, err := http.NewRequest("DELETE", url+"/gameserverbuilds/nonexistentbuild", nil)
Expect(err).ToNot(HaveOccurred())
res, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusNotFound))
})

It("should return an error when getting an non-existing GameServer", func() {
r, err := client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, "nonexistentgameserver"))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})
It("should return an error when getting an non-existing GameServerBuild", func() {
r, err := client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, "nonexistentbuild"))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})

It("should create a new GameServerBuild, list it and then delete it", func() {
build := createGameServerBuild(buildName, testNamespace)
b, err := json.Marshal(build)
Expect(err).ToNot(HaveOccurred())
req, err := client.Post(url+"/gameserverbuilds", contentType, bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(req.StatusCode).To(Equal(http.StatusCreated))
It("should return an error when getting an non-existing GameServer", func() {
r, err := client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, "nonexistentgameserver"))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})

// list all the GameServerBuilds and see if the one we created is there
r, err := client.Get(url + "/gameserverbuilds")
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
defer r.Body.Close()
var l mpsv1alpha1.GameServerBuildList
body, err := ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &l)
Expect(err).ToNot(HaveOccurred())
var found bool
for _, b := range l.Items {
if b.Name == buildName {
found = true
break
}
It("should create a new GameServerBuild, list it and then delete it", func() {
build := createGameServerBuild(buildName, testNamespace)
b, err := json.Marshal(build)
Expect(err).ToNot(HaveOccurred())
req, err := client.Post(url+"/gameserverbuilds", contentType, bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(req.StatusCode).To(Equal(http.StatusCreated))

// list all the GameServerBuilds and see if the one we created is there
r, err := client.Get(url + "/gameserverbuilds")
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
defer r.Body.Close()
var l mpsv1alpha1.GameServerBuildList
body, err := ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &l)
Expect(err).ToNot(HaveOccurred())
var found bool
for _, b := range l.Items {
if b.Name == buildName {
found = true
break
}
Expect(found).To(BeTrue())

// get the specific GameServerBuild
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
var bu mpsv1alpha1.GameServerBuild
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &bu)
}
Expect(found).To(BeTrue())

// get the specific GameServerBuild
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
var bu mpsv1alpha1.GameServerBuild
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &bu)
Expect(err).ToNot(HaveOccurred())
Expect(bu.Name).To(Equal(buildName))

// list GameServers for GameServerBuild
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s/gameservers", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
var gsList mpsv1alpha1.GameServerList
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &gsList)
Expect(err).ToNot(HaveOccurred())
Expect(len(gsList.Items)).To(Equal(2))

gsName := gsList.Items[0].Name
// get a GameServer
r, err = client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName))
Expect(err).ToNot(HaveOccurred())
var gs mpsv1alpha1.GameServer
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &gs)
Expect(err).ToNot(HaveOccurred())
Expect(gs.Name).To(Equal(gsName))

// delete this GameServer
req1, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName), nil)
Expect(err).ToNot(HaveOccurred())
res, err := client.Do(req1)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// make sure this GameServer is not returned any more
// a finalizer runs so it will not disappear at once
Eventually(func() int {
r, err = client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName))
Expect(err).ToNot(HaveOccurred())
Expect(bu.Name).To(Equal(buildName))
return r.StatusCode
}, timeout, interval).Should(Equal(http.StatusNotFound))

// list GameServers for GameServerBuild
// make sure controller creates an extra GameServer
Eventually(func() int {
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s/gameservers", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
var gsList mpsv1alpha1.GameServerList
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &gsList)
Expect(err).ToNot(HaveOccurred())
Expect(len(gsList.Items)).To(Equal(2))
return len(gsList.Items)
}, time.Second*5, time.Millisecond*500).Should(Equal(2))

gsName := gsList.Items[0].Name
// get a GameServer
r, err = client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName))
Expect(err).ToNot(HaveOccurred())
var gs mpsv1alpha1.GameServer
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &gs)
Expect(err).ToNot(HaveOccurred())
Expect(gs.Name).To(Equal(gsName))
// TODO: allocate so GameServerDetails can be created
// TODO: list GameServerDetails for GameServerBuild
// TODO: get a GameServerDetail

// delete this GameServer
req1, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName), nil)
Expect(err).ToNot(HaveOccurred())
res, err := client.Do(req1)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// make sure this GameServer is not returned any more
// a finalizer runs so it will not disappear at once
Eventually(func() int {
r, err = client.Get(fmt.Sprintf("%s/gameservers/%s/%s", url, testNamespace, gsName))
Expect(err).ToNot(HaveOccurred())
return r.StatusCode
}, timeout, interval).Should(Equal(http.StatusNotFound))

// make sure controller creates an extra GameServer
Eventually(func() int {
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s/gameservers", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
var gsList mpsv1alpha1.GameServerList
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &gsList)
Expect(err).ToNot(HaveOccurred())
return len(gsList.Items)
}, time.Second*5, time.Millisecond*500).Should(Equal(2))

// TODO: allocate so GameServerDetails can be created
// TODO: list GameServerDetails for GameServerBuild
// TODO: get a GameServerDetail

// update the GameServerBuild to 3 standingBy and 6 max
patchBody := map[string]interface{}{
"standingBy": 3,
"max": 6,
}
pb, err := json.Marshal(patchBody)
Expect(err).ToNot(HaveOccurred())
req2, err := http.NewRequest("PATCH", fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName), bytes.NewReader(pb))
Expect(err).ToNot(HaveOccurred())
res, err = client.Do(req2)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// get the specific GameServerBuild again and make sure the values were updated
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &bu)
Expect(err).ToNot(HaveOccurred())
Expect(bu.Spec.StandingBy).To(Equal(3))
Expect(bu.Spec.Max).To(Equal(6))

// delete the GameServerBuild
req3, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName), nil)
Expect(err).ToNot(HaveOccurred())
res, err = client.Do(req3)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// make sure the GameServerBuild is gone
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})
// update the GameServerBuild to 3 standingBy and 6 max
patchBody := map[string]interface{}{
"standingBy": 3,
"max": 6,
}
pb, err := json.Marshal(patchBody)
Expect(err).ToNot(HaveOccurred())
req2, err := http.NewRequest("PATCH", fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName), bytes.NewReader(pb))
Expect(err).ToNot(HaveOccurred())
res, err = client.Do(req2)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// get the specific GameServerBuild again and make sure the values were updated
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusOK))
body, err = ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(body, &bu)
Expect(err).ToNot(HaveOccurred())
Expect(bu.Spec.StandingBy).To(Equal(3))
Expect(bu.Spec.Max).To(Equal(6))

// delete the GameServerBuild
req3, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName), nil)
Expect(err).ToNot(HaveOccurred())
res, err = client.Do(req3)
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))

// make sure the GameServerBuild is gone
r, err = client.Get(fmt.Sprintf("%s/gameserverbuilds/%s/%s", url, testNamespace, buildName))
Expect(err).ToNot(HaveOccurred())
Expect(r.StatusCode).To(Equal(http.StatusNotFound))
})
})

Expand Down
2 changes: 1 addition & 1 deletion cmd/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down
10 changes: 10 additions & 0 deletions cmd/e2e/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build tools
// +build tools

package main

import (
_ "github.com/onsi/ginkgo/v2/ginkgo"
)

// https://onsi.github.io/ginkgo/#recommended-continuous-integration-configuration
4 changes: 2 additions & 2 deletions cmd/gameserverapi/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http/httptest"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
mpsv1alpha1 "github.com/playfab/thundernetes/pkg/operator/api/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -267,7 +267,7 @@ var _ = BeforeSuite(func() {
kubeClient = clientBuilder.Build()
Expect(kubeClient).NotTo(BeNil())

}, 60)
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
Expand Down
2 changes: 1 addition & 1 deletion cmd/nodeagent/nodeagentmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
6 changes: 4 additions & 2 deletions e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ kubectl wait --for=condition=ready --timeout=300s pod -n thundernetes-system -l

echo "-----Running end to end tests-----"
cd cmd/e2e
go mod tidy && go run $(ls -1 *.go | grep -v _test.go) ${IMAGE_NAME_NETCORE_SAMPLE}:${IMAGE_TAG}
go build && ./e2e ${IMAGE_NAME_NETCORE_SAMPLE}:${IMAGE_TAG} && rm e2e

echo "-----Running GameServer API tests-----"
# create the gameserverapi namespace for the GameServer API tests
kubectl create namespace gameserverapi
IMG=${IMAGE_NAME_NETCORE_SAMPLE}:${IMAGE_TAG} go test -count=1 ./...
go get github.com/onsi/ginkgo/v2
# https://onsi.github.io/ginkgo/#recommended-continuous-integration-configuration
IMG=${IMAGE_NAME_NETCORE_SAMPLE}:${IMAGE_TAG} go run github.com/onsi/ginkgo/v2/ginkgo -r --procs=2 --compilers=2 --randomize-all --randomize-suites --fail-on-pending --keep-going --cover --race --trace
Loading

0 comments on commit f1acb5f

Please sign in to comment.