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

Refactor of workerqueue health testing #164

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pkg/gameservers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewController(
c.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "gameserver-controller"})

c.workerqueue = workerqueue.NewWorkerQueue(c.syncGameServer, c.logger, stable.GroupName+".GameServerController")
health.AddLivenessCheck("game-server-worker-queue", healthcheck.Check(c.workerqueue.Healthy))
health.AddLivenessCheck("gameserver-workerqueue", healthcheck.Check(c.workerqueue.Healthy))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know of a convention for naming these liveness checks from Kubernetes?

Copy link
Member Author

Choose a reason for hiding this comment

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

No idea. I just figured keep it closer to the names of the CRDs and objects that back them.


wh.AddHandler("/mutate", stablev1alpha1.Kind("GameServer"), admv1beta1.Create, c.creationMutationHandler)
wh.AddHandler("/validate", stablev1alpha1.Kind("GameServer"), admv1beta1.Create, c.creationValidationHandler)
Expand Down
30 changes: 0 additions & 30 deletions pkg/gameservers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package gameservers
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -185,35 +184,6 @@ func TestControllerWatchGameServers(t *testing.T) {
assert.Equal(t, "default/test", <-received)
}

func TestControllerHealthCheck(t *testing.T) {
m := newMocks()
m.extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, newEstablishedCRD(), nil
})
health := healthcheck.NewHandler()
c := NewController(webhooks.NewWebHook("", ""), health, 10, 20, "sidecar:dev", false,
m.kubeClient, m.kubeInformationFactory, m.extClient, m.agonesClient, m.agonesInformerFactory)

c.workerqueue.SyncHandler = func(name string) error {
return nil
}

stop, cancel := startInformers(m, c.gameServerSynced)

go http.ListenAndServe("localhost:9090", health)

go func() {
err := c.Run(1, stop)
assert.Nil(t, err, "Run should not error")
}()

testHTTPHealth(t, "http://localhost:9090/live", "{}\n", http.StatusOK)

cancel()

testHTTPHealth(t, "http://localhost:9090/live", "{}\n", http.StatusServiceUnavailable)
}

func TestControllerCreationMutationHandler(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 1 addition & 2 deletions pkg/gameservers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ package gameservers

import (
"context"
"time"

"io"
"io/ioutil"
"net/http"
"testing"
"time"

"agones.dev/agones/pkg/apis/stable/v1alpha1"
agonesfake "agones.dev/agones/pkg/client/clientset/versioned/fake"
Expand Down
55 changes: 55 additions & 0 deletions pkg/util/workerqueue/workerqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ import (
"testing"
"time"

"io/ioutil"
"net/http"
"net/http/httptest"

"github.com/heptiolabs/healthcheck"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)

func TestWorkerQueueRun(t *testing.T) {
t.Parallel()

received := make(chan string)
defer close(received)

Expand Down Expand Up @@ -56,6 +64,8 @@ func TestWorkerQueueRun(t *testing.T) {
}

func TestWorkerQueueHealthy(t *testing.T) {
t.Parallel()

done := make(chan struct{})
handler := func(string) error {
<-done
Expand All @@ -81,3 +91,48 @@ func TestWorkerQueueHealthy(t *testing.T) {
assert.Equal(t, 0, wq.RunCount())
assert.EqualError(t, wq.Healthy(), "want 1 worker goroutine(s), got 0")
}

func TestWorkQueueHealthCheck(t *testing.T) {
t.Parallel()

health := healthcheck.NewHandler()
handler := func(string) error {
return nil
}
wq := NewWorkerQueue(handler, logrus.WithField("source", "test"), "test")
health.AddLivenessCheck("test", wq.Healthy)

server := httptest.NewServer(health)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great choice, as all the port handling between different tests isn't our responsibility.

defer server.Close()

stop := make(chan struct{})
go wq.Run(1, stop)

url := server.URL + "/live"

f := func(t *testing.T, url string, status int) {
resp, err := http.Get(url)
assert.Nil(t, err)
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, status, resp.StatusCode)
assert.Equal(t, []byte("{}\n"), body)
}

f(t, url, http.StatusOK)

close(stop)
// closing can take a short while
err := wait.PollImmediate(time.Second, 5*time.Second, func() (bool, error) {
rc := wq.RunCount()
logrus.WithField("runcount", rc).Info("Checking run count")
return rc == 0, nil
})
assert.Nil(t, err)

// gate
assert.Error(t, wq.Healthy())
f(t, url, http.StatusServiceUnavailable)
}