Skip to content

Commit

Permalink
Make tests faster by fast polling
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed May 11, 2020
1 parent d65e8e2 commit d82a093
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 22 deletions.
32 changes: 22 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ matrix:
allow_failures:
- go: tip

env:
- GOLANGCI_RELEASE="v1.26.0"

before_install:
- GO111MODULE=off go get github.com/mattn/goveralls
- GO111MODULE=off go get github.com/lawrencewoodman/roveralls
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_RELEASE}
cache:
directories:
- $GOPATH/pkg/mod

script:
- make test
- make lint
- travis_wait 20 roveralls
- goveralls -coverprofile=roveralls.coverprofile -service=travis-ci

jobs:
include:
- name: "Linting"
go: "1.14.x"
env:
- GOLANGCI_RELEASE="v1.26.0"
before_install:
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_RELEASE}
script:
- make lint
- name: "Coverage"
go: "1.14.x"
before_install:
- GO111MODULE=off go get github.com/mattn/goveralls
- GO111MODULE=off go get github.com/lawrencewoodman/roveralls
script:
- travis_wait 20 roveralls
- goveralls -coverprofile=roveralls.coverprofile -service=travis-ci
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ lint:
.PHONY: verify test

test:
go test -v -race $(shell go list ./... | grep -v /vendor/)
go test -v -race ./...

# The build targets allow to build the binary and docker image
.PHONY: build build.docker build.mini
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package config

// FAST_POLL used for fast testing
var FAST_POLL = false
23 changes: 23 additions & 0 deletions internal/testutils/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testutils

import (
"io/ioutil"
"os"

"log"

"github.com/sirupsen/logrus"
"sigs.k8s.io/external-dns/internal/config"
)

func init() {
config.FAST_POLL = true
if os.Getenv("DEBUG") == "" {
logrus.SetOutput(ioutil.Discard)
log.SetOutput(ioutil.Discard)
} else {
if level, err := logrus.ParseLevel(os.Getenv("DEBUG")); err == nil {
logrus.SetLevel(level)
}
}
}
2 changes: 1 addition & 1 deletion source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewIstioGatewaySource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return serviceInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewIngressSource(kubeClient kubernetes.Interface, namespace, annotationFilt
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return ingressInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions source/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"

"sigs.k8s.io/external-dns/endpoint"
Expand Down Expand Up @@ -1018,7 +1017,7 @@ func testIngressEndpoints(t *testing.T) {
var err error

// wait up to a few seconds for new resources to appear in informer cache.
err = wait.Poll(time.Second, 3*time.Second, func() (bool, error) {
err = poll(time.Second, 3*time.Second, func() (bool, error) {
res, err = ingressSource.Endpoints()
if err != nil {
// stop waiting if we get an error
Expand Down
2 changes: 1 addition & 1 deletion source/ingressroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewContourIngressRouteSource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return ingressRouteInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewNodeSource(kubeClient kubernetes.Interface, annotationFilter, fqdnTempla
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return nodeInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/ocproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewOcpRouteSource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return routeInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion source/routegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ func TestRouteGroupsEndpoints(t *testing.T) {
}} {
t.Run(tt.name, func(t *testing.T) {
if tt.fqdnTemplate != "" {
println("fqdnTemplate is set")
tmpl, err := parseTemplate(tt.fqdnTemplate)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilt
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return serviceInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"

"sigs.k8s.io/external-dns/endpoint"
Expand Down Expand Up @@ -1118,7 +1117,7 @@ func testServiceSourceEndpoints(t *testing.T) {
var res []*endpoint.Endpoint

// wait up to a few seconds for new resources to appear in informer cache.
err = wait.Poll(time.Second, 3*time.Second, func() (bool, error) {
err = poll(time.Second, 3*time.Second, func() (bool, error) {
res, err = client.Endpoints()
if err != nil {
// stop waiting if we get an error
Expand Down
11 changes: 11 additions & 0 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/internal/config"
)

const (
Expand Down Expand Up @@ -224,3 +226,12 @@ func matchLabelSelector(selector labels.Selector, srcAnnotations map[string]stri
annotations := labels.Set(srcAnnotations)
return selector.Matches(annotations)
}

func poll(interval time.Duration, timeout time.Duration, condition wait.ConditionFunc) error {
if config.FAST_POLL {
interval = 8 * time.Millisecond
timeout = 5 * time.Second
}

return wait.Poll(interval, timeout, condition)
}

0 comments on commit d82a093

Please sign in to comment.