Skip to content

Commit

Permalink
Merge pull request #155 from rekby/devel
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
rekby committed Mar 28, 2021
2 parents 30a9bd6 + 7d1fe81 commit 3794685
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 6 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ env:
jobs:
- GO_VERSION=1.10
- GO_VERSION=1.16
global:
- COVERALLS_PARALLEL=true

services:
- docker
Expand All @@ -25,7 +23,7 @@ after_script:
# Install and upload test coverage must not failure for test build
- test -n "$TRAVIS_TAG" || go get golang.org/x/tools/cmd/cover
- test -n "$TRAVIS_TAG" || go get github.com/mattn/goveralls
- test -n "$TRAVIS_TAG" || goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
- test -n "$TRAVIS_TAG" || COVERALLS_PARALLEL=true goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

before_deploy:
- git config --local user.name "$GIT_NAME"
Expand Down
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
test:
image: golang:${GO_VERSION:-1.16}
working_dir: /go/src/github.com/rekby/lets-proxy2
command: go test -covermode=count -coverprofile=coverage.out ${LETS_PROXY_TEST_OPTIONS:-} ./internal/cert_manager/...
command: go test -covermode=count -coverprofile=coverage.out ${LETS_PROXY_TEST_OPTIONS:-} ./...
environment:
GOCACHE: "/go/src/github.com/rekby/lets-proxy2/.cache"
GOFLAGS: "-mod=vendor"
Expand All @@ -37,6 +37,10 @@ services:
networks:
acmenet:
ipv4_address: 10.40.50.4
depends_on:
- acme-server
- fake-dns
- nginx
networks:
acmenet:
driver: bridge
Expand Down
60 changes: 60 additions & 0 deletions internal/domain_checker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package domain_checker

import (
"errors"
"testing"

"github.com/gojuno/minimock/v3"
"github.com/maxatome/go-testdeep"
"github.com/rekby/lets-proxy2/internal/domain"
"github.com/rekby/lets-proxy2/internal/th"

"github.com/rekby/lets-proxy2/internal/docker"
)

//go:generate minimock -i internalDocker -o internal_docker_mock_test.go -g

type internalDocker interface {
docker.Interface
}

func TestDocker(t *testing.T) {
const domainName = "domain"

td := testdeep.NewT(t)

ctx, flush := th.TestContext(td)
defer flush()

mc := minimock.NewController(td)

table := []struct {
Name string
Domain string
DockerTargetResult *docker.DomainInfo
DockerTargetError error
Result bool
ErrorString string
}{
{"ok", "domain", &docker.DomainInfo{TargetAddress: "asd"}, nil, true, ""},
{"not found", "domain", nil, docker.ErrNotFound, false, ""},
{"bad domain", "domain:adsfasdf:dfasdf", nil, nil, false, "normalize domain"},
{"docker-error", "domain", nil, errors.New("test-err"), false, "test-err"},
}

for _, test := range table {
dockerMock := NewInternalDockerMock(mc)
if test.DockerTargetResult != nil || test.DockerTargetError != nil {
testDomain, err := domain.NormalizeDomain(test.Domain)
td.CmpNoError(err)
dockerMock.GetTargetMock.Expect(ctx, testDomain).Return(test.DockerTargetResult, test.DockerTargetError)
}

checker := NewDockerChecker(dockerMock)
res, err := checker.IsDomainAllowed(ctx, test.Domain)
td.Cmp(res, test.Result)
if diff := th.ErrorSubstringCmp(err, test.ErrorString); diff != "" {
td.Errorf("Test name: '%v', diff error: %v", test.Name, diff)
}
}
}
286 changes: 286 additions & 0 deletions internal/domain_checker/internal_docker_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions internal/proxy/http_round_tripper_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3794685

Please sign in to comment.