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

Beginning to write tests #43

Merged
merged 36 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e2d2712
Beginning to write tests
arschles Feb 4, 2021
03092cc
Improve status conditions
khaosdoctor Feb 4, 2021
ca2f813
Merge branch 'main' of github.com:kedacore/http-add-on into tests
arschles Feb 9, 2021
2475ef2
removing old httpscaledobject CRD
arschles Feb 9, 2021
c17713d
Merge branch 'main' of github.com:kedacore/http-add-on into tests
arschles Feb 11, 2021
b062a6a
Merge branch 'tests' of github.com:arschles/http-add-on into tests
arschles Feb 11, 2021
5eacf58
starting on the user test
arschles Feb 11, 2021
1d1528b
refactoring typed Kubernetes clients out
arschles Feb 11, 2021
3c0aa82
finishing delete logic
arschles Feb 11, 2021
126f41e
removing now-unused code
arschles Feb 11, 2021
b73cf5c
removing commented unused code
arschles Feb 11, 2021
27c1ad3
fixing lint error
arschles Feb 11, 2021
590b405
Merge branch 'main' into improve-conditions
arschles Feb 12, 2021
d68fbd3
Merge branch 'improve-conditions' of github.com:khaosdoctor/http-add-…
arschles Feb 12, 2021
9390b2c
first test for createUserApp
arschles Feb 12, 2021
8b3e53b
more checks
arschles Feb 12, 2021
34cce00
finishing tests for the service
arschles Feb 12, 2021
1cae350
starting with scaled object tests
arschles Feb 12, 2021
f4ed5cb
adding GH action for running tests
arschles Feb 12, 2021
93cb7f8
going back to old versions of things
arschles Feb 12, 2021
646349e
running tests on all pushes to main and PRs
arschles Feb 12, 2021
5244fd9
go mod tidy and a new Makefile target
arschles Feb 12, 2021
5988372
a few fixes
arschles Feb 23, 2021
1d2396b
fixing compile errs
arschles Feb 23, 2021
08490a4
more testing
arschles Feb 23, 2021
8ce81ef
fixing tests
arschles Feb 23, 2021
9677c78
adding initial tests for the external scaler
arschles Feb 23, 2021
c969f0f
starting interceptor tests
arschles Feb 23, 2021
eb1a921
more interceptor testing
arschles Feb 23, 2021
921e4bf
starting on proxy handler test
arschles Feb 23, 2021
d1c2c56
more tests for the interceptor, including middleware
arschles Feb 23, 2021
e3971ec
fixing scaler compile errs
arschles Feb 23, 2021
8d25b72
commenting cluster connection code
arschles Feb 24, 2021
00e417b
Merge branch 'main' of github.com:kedacore/http-add-on into tests
arschles Feb 26, 2021
231a950
removing unneeded NameNamespaceInfo
arschles Feb 26, 2021
90c99f4
fixing zap logging compile err
arschles Feb 26, 2021
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
20 changes: 20 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Run tests
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
run_tests:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Test
run: go test ./...
33 changes: 30 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ INTERCEPTOR_DOCKER_IMG?=arschles/keda-http-interceptor:${GIT_TAG}
OPERATOR_DOCKER_IMG?=arschles/keda-http-operator:${GIT_TAG}
NAMESPACE?=kedahttp

#####
# scaler targets
#####

.PHONY: gen-scaler
gen-scaler:
protoc \
Expand All @@ -17,6 +21,10 @@ gen-scaler:
build-scaler:
go build -o bin/scaler ./scaler

.PHONY: test-scaler
test-scaler:
go test ./scaler/...

.PHONY: docker-build-scaler
docker-build-scaler:
docker build -t ${SCALER_DOCKER_IMG} -f scaler/Dockerfile .
Expand All @@ -25,10 +33,18 @@ docker-build-scaler:
docker-push-scaler: docker-build-scaler
docker push ${SCALER_DOCKER_IMG}

#####
# Interceptor targets
#####

.PHONY: build-interceptor
build-interceptor:
go build -o bin/interceptor ./interceptor

.PHONY: test-interceptor
test-interceptor:
go test ./interceptor/...

.PHONY: docker-build-interceptor
docker-build-interceptor:
docker build -t ${INTERCEPTOR_DOCKER_IMG} -f interceptor/Dockerfile .
Expand All @@ -37,14 +53,18 @@ docker-build-interceptor:
docker-push-interceptor: docker-build-interceptor
docker push ${INTERCEPTOR_DOCKER_IMG}

# .PHONY: build-operator
# build-operator-cli:
# cargo build --bin operator
#####
# operator targets
#####

.PHONY: build-operator
build-operator:
go build -o bin/operator ./operator

.PHONY: test-operator
test-operator:
go test ./operator/...

.PHONY: docker-build-operator
docker-build-operator:
docker build -t ${OPERATOR_DOCKER_IMG} -f operator/Dockerfile .
Expand All @@ -67,9 +87,16 @@ helm-upgrade-operator:
helm-delete-operator:
helm delete -n ${NAMESPACE} kedahttp

#####
# universal targets
#####

.PHONY: build-all
build-all: build-scaler build-interceptor build-operator

.PHONY: test-all
test-all: test-scaler test-interceptor test-operator

.PHONY: docker-build-all
docker-build-all: docker-build-scaler docker-build-interceptor docker-build-operator

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0
k8s.io/api v0.20.2
Expand Down
36 changes: 0 additions & 36 deletions go.sum

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions interceptor/admin_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"encoding/json"
"errors"
)

func (i *InterceptorSuite) TestQueueSizeHandlerSuccess() {
reader := &fakeQueueCountReader{
current: 123,
err: nil,
}

handler := newQueueSizeHandler(reader)
_, echoCtx, rec := newTestCtx("GET", "/queue")
err := handler(echoCtx)
i.NoError(err)
i.Equal(200, rec.Code, "response code")
respMap := map[string]int{}
decodeErr := json.NewDecoder(rec.Body).Decode(&respMap)
i.NoError(decodeErr)
i.Equalf(1, len(respMap), "response JSON length was not 1")
sizeVal, ok := respMap["current_size"]
i.Truef(ok, "'current_size' entry not available in return JSON")
i.Equalf(reader.current, sizeVal, "returned JSON queue size was wrong")
reader.err = errors.New("test error")
i.Error(handler(echoCtx))
}

func (i *InterceptorSuite) TestQueueSizeHandlerFail() {
reader := &fakeQueueCountReader{
current: 0,
err: errors.New("test error"),
}

handler := newQueueSizeHandler(reader)
_, echoCtx, rec := newTestCtx("GET", "/queue")
err := handler(echoCtx)
i.Error(err)
i.Equal(500, rec.Code, "response code")
}
46 changes: 46 additions & 0 deletions interceptor/interceptor_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"net/http/httptest"
"testing"

echo "github.com/labstack/echo/v4"
"github.com/stretchr/testify/suite"
)

type InterceptorSuite struct {
suite.Suite
}

func TestInterceptor(t *testing.T) {
suite.Run(t, new(InterceptorSuite))
}

func newTestCtx(method, path string) (*echo.Echo, echo.Context, *httptest.ResponseRecorder) {
req := httptest.NewRequest(method, path, nil)
rec := httptest.NewRecorder()
e := echo.New()
return e, e.NewContext(req, rec), rec
}

type fakeQueueCounter struct {
resizedCh chan int
}

func (f *fakeQueueCounter) Resize(i int) error {
f.resizedCh <- i
return nil
}

func (f *fakeQueueCounter) Current() (int, error) {
return 0, nil
}

type fakeQueueCountReader struct {
current int
err error
}

func (f *fakeQueueCountReader) Current() (int, error) {
return f.current, f.err
}
53 changes: 53 additions & 0 deletions interceptor/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"math"
"sync"
"time"

echo "github.com/labstack/echo/v4"
)

func (i *InterceptorSuite) TestCountMiddleware() {
queueCounter := &fakeQueueCounter{}
middleware := countMiddleware(queueCounter)
var wg sync.WaitGroup
wg.Add(1)
handler := middleware(func(c echo.Context) error {
wg.Done()
return c.String(200, "OK")
})
_, echoCtx, _ := newTestCtx("GET", "/something")
go func() {
i.NoError(handler(echoCtx))
}()

// after the handler was called, first wait for it to complete.
// then check to make sure the pending queue size was increased and decreased.
//
// the increase and decrease operations happen in goroutines, so the ordering
// isn't guaranteed
wg.Wait()
timer := time.NewTimer(200 * time.Millisecond)
defer timer.Stop()
resizes := []int{}
done := false
for i := 0; i < 2; i++ {
if len(resizes) == 2 || done {
break
}
select {
case i := <-queueCounter.resizedCh:
resizes = append(resizes, i)
case <-timer.C:
done = true
break
}
}
agg := 0
for _, delta := range resizes {
i.Equal(1, math.Abs(float64(delta)))
agg += delta
}
i.Equal(0, agg, "sum of all the resize operations")
}
28 changes: 28 additions & 0 deletions interceptor/proxy_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"net/http"
"net/http/httptest"
"net/url"
)

func (i *InterceptorSuite) TestForwardingHandler() {
const originReturn = "hello test"
forwardedRequests := []*http.Request{}
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
forwardedRequests = append(forwardedRequests, r)
w.Write([]byte(originReturn))
}))
defer testServer.Close()

forwardURL, err := url.Parse(testServer.URL)
i.NoError(err)
handler := newForwardingHandler(forwardURL)
const path = "/testfwd"
_, echoCtx, rec := newTestCtx("GET", path)
i.NoError(handler(echoCtx))
i.Equal(1, len(forwardedRequests), "number of requests that were forwarded")
forwardedRequest := forwardedRequests[0]
i.Equal(path, forwardedRequest.URL.Path)
i.Equal(originReturn, string(rec.Body.Bytes()))
}
76 changes: 76 additions & 0 deletions operator/controllers/external_scaler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package controllers

import (
"context"
"time"

logrtest "github.com/go-logr/logr/testing"
"github.com/kedacore/http-add-on/operator/api/v1alpha1"
"github.com/kedacore/http-add-on/operator/controllers/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var _ = Describe("ExternalScaler", func() {
Context("Creating the external scaler", func() {
It("Should properly create the Deployment and Service", func() {
const name = "testapp"
const namespace = "testns"
ctx := context.Background()
cl := fake.NewFakeClient()
cfg := config.AppInfo{
Name: name,
Port: 8081,
Image: "arschles/testimg",
Namespace: namespace,
}
logger := logrtest.NullLogger{}
httpso := &v1alpha1.HTTPScaledObject{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Spec: v1alpha1.HTTPScaledObjectSpec{
AppName: name,
Image: "arschles/testapp",
Port: 8081,
},
}
err := createExternalScaler(ctx, cfg, cl, logger, httpso)
Expect(err).To(BeNil())

// // make sure that httpso has the right conditions on it
Expect(len(httpso.Status.Conditions)).To(Equal(1))
cond1 := httpso.Status.Conditions[0]
cond1ts, err := time.Parse(time.RFC3339, cond1.Timestamp)
Expect(err).To(BeNil())
Expect(time.Now().Sub(cond1ts) >= 0).To(BeTrue())
Expect(cond1.Type).To(Equal(v1alpha1.Created))
Expect(cond1.Status).To(Equal(metav1.ConditionTrue))
Expect(cond1.Reason).To(Equal(v1alpha1.CreatedExternalScaler))

// check that the external scaler deployment was created
deployment := new(appsv1.Deployment)
err = cl.Get(ctx, client.ObjectKey{
Name: cfg.ExternalScalerDeploymentName(),
Namespace: cfg.Namespace,
}, deployment)
Expect(err).To(BeNil())

// check that the external scaler service was created
service := new(corev1.Service)
err = cl.Get(ctx, client.ObjectKey{
Name: cfg.ExternalScalerServiceName(),
Namespace: cfg.Namespace,
}, service)
Expect(err).To(BeNil())

})
})

})
Loading