Skip to content

Commit

Permalink
import dockertest for integration test (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sillygod authored Jul 19, 2020
1 parent ab7fd2e commit e2ff3de
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 9 deletions.
15 changes: 15 additions & 0 deletions backends/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ func (suite *FileBackendTestSuite) TestAutoCreateDirIfNonExist() {
backend.Close()
}

func (suite *FileBackendTestSuite) TestMultiClose() {
backend, err := NewFileBackend("/tmp/hello")
suite.Nil(err)
backend.Close()
backend.Close()

}

func (suite *FileBackendTestSuite) TestLengthShouldBeZero() {
backend, err := NewFileBackend("/tmp/hello")
suite.Nil(err)
n := backend.Length()
suite.Equal(0, n)
}

func (suite *FileBackendTestSuite) TestDeleteFileAfterCleaned() {
backend, err := NewFileBackend("/tmp/hello")
suite.Nil(err)
Expand Down
76 changes: 76 additions & 0 deletions backends/memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package backends

import (
"context"
"io/ioutil"
"testing"
"time"

"github.com/stretchr/testify/suite"
)

type MemoryBackendTestSuite struct {
suite.Suite
}

func (suite *MemoryBackendTestSuite) SetupSuite() {
err := InitGroupCacheRes(50 * 1024 * 1024)
suite.Nil(err)
}

func (suite *MemoryBackendTestSuite) TearDownSuite() {
err := ReleaseGroupCacheRes()
suite.Nil(err)
}

func (suite *MemoryBackendTestSuite) TestWriteInCache() {
ctx := context.Background()
backend, err := NewInMemoryBackend(ctx, "hello", time.Now())
suite.Nil(err)

content := []byte("hello world")
length, err := backend.Write(content)
suite.Nil(err)
suite.Equal(len(content), length)

// now start to write in the groupcache
backend.Close()
suite.Equal(len(content), backend.Length())

// test the content get from the reader will be consistent with the original one
reader, err := backend.GetReader()
suite.Nil(err)
result, err := ioutil.ReadAll(reader)
suite.Nil(err)
suite.Equal(result, content)
}

func (suite *MemoryBackendTestSuite) TestReadExistingCacheInGroupCache() {

ctx := context.Background()
backend, err := NewInMemoryBackend(ctx, "hello", time.Now())
suite.Nil(err)

content := []byte("hello world")
length, err := backend.Write(content)
suite.Nil(err)
suite.Equal(len(content), length)
backend.Close()

// new a InMemoryBackend with the same key and test getting cache content
// this case will happen when caddy restart or other scenario.
// The cache is in groupcache but the client doesn't has the correspond backend mapping
anotherBackend, err := NewInMemoryBackend(context.Background(), "hello", time.Now())
suite.Nil(err)
reader, err := anotherBackend.GetReader()
suite.Nil(err)

result, err := ioutil.ReadAll(reader)
suite.Nil(err)
suite.Equal(result, content)

}

func TestMemoryBackendTestSuite(t *testing.T) {
suite.Run(t, new(MemoryBackendTestSuite))
}
2 changes: 1 addition & 1 deletion backends/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r *RedisBackend) Length() int {
return r.content.Len()
}

// Close writeh the temp buffer's content to the groupcache
// Close write the temp buffer's content to the groupcache
func (r *RedisBackend) Close() error {
_, err := client.Set(r.Key, r.content.Bytes(), r.expiration.Sub(time.Now())).Result()
return err
Expand Down
72 changes: 72 additions & 0 deletions backends/redis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package backends

import (
"context"
"fmt"
"io/ioutil"
"log"
"testing"
"time"

"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/suite"
)

type RedisBackendTestSuite struct {
suite.Suite
pool *dockertest.Pool
resource *dockertest.Resource
}

func (suite *RedisBackendTestSuite) SetupSuite() {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatal(err)
}

resource, err := pool.Run("redis", "5.0.9", []string{})
if err != nil {
log.Fatal(err)
}

suite.pool = pool
suite.resource = resource

port := resource.GetPort("6379/tcp")
InitRedisClient(fmt.Sprintf("localhost:%s", port), "", 0)
}

func (suite *RedisBackendTestSuite) TestParseRedisConfig() {
opts, err := ParseRedisConfig("localhost:6379 1 songa")
suite.Nil(err)
opts.Addr = "localhost:6379"
opts.DB = 1
opts.Password = "songa"
}

func (suite *RedisBackendTestSuite) TestWriteCacheInRedis() {
backend, err := NewRedisBackend(context.Background(), "hello", time.Now().Add(5*time.Minute))
suite.Nil(err)
content := []byte("hello world")
backend.Write(content)

suite.Equal(len(content), backend.Length())

backend.Close()

reader, err := backend.GetReader()
suite.Nil(err)
result, err := ioutil.ReadAll(reader)
suite.Nil(err)
suite.Equal(content, result)
}

func (suite *RedisBackendTestSuite) TearDownSuite() {
if err := suite.pool.Purge(suite.resource); err != nil {
log.Fatal(err)
}
}

func TestRedisBackendTestSuite(t *testing.T) {
suite.Run(t, new(RedisBackendTestSuite))
}
3 changes: 2 additions & 1 deletion build/test_coverage.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

PROJECT_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)
go test -coverprofile=/tmp/c.out ./...
rm -f /tmp/c.out
go test -v -cover -coverprofile=/tmp/c.out ./...
go tool cover -html=/tmp/c.out
6 changes: 3 additions & 3 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
keyMatchPath = "match_path"
keyCacheKey = "cache_key"
keyCacheBucketsNum = "cache_bucket_num"
keyCacheMaxMemorySzie = "cache_max_memory_size"
keyCacheMaxMemorySize = "cache_max_memory_size"
keyCacheType = "cache_type"
keyRedisConnctionSetting = "redis_connection_setting"
// format: addr db password or addr db or addr
Expand Down Expand Up @@ -161,7 +161,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if len(args) != 1 {
return d.Err("Invalid usage of lock_timeout in cache config")
}
duration, err := time.ParseDuration(parameter)
duration, err := time.ParseDuration(args[0])
if err != nil {
return d.Err(fmt.Sprintf("%s:%s , %s", keyLockTimeout, "invalid duration", parameter))
}
Expand All @@ -172,7 +172,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.Err("Invalid usage of default_max_age in cache config.")
}

duration, err := time.ParseDuration(parameter)
duration, err := time.ParseDuration(args[0])
if err != nil {
return d.Err(fmt.Sprintf("%s:%s, %s", keyDefaultMaxAge, "Invalid duration ", parameter))
}
Expand Down
83 changes: 83 additions & 0 deletions caddyfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package httpcache

import (
"testing"

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/stretchr/testify/suite"
)

type CaddyfileTestSuite struct {
suite.Suite
}

func (suite *CaddyfileTestSuite) TestSettingFileCacheType() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
cache_type file
match_path /assets
lock_timeout 10m
path /tmp/cache
default_max_age 5m
status_header "X-Cache-Status"
cache_key "{http.request.method} {http.request.host}{http.request.uri.path} {http.request.uri.query}"
cache_bucket_num 1024
match_header Content-Type image/png
match_header X-Forwarded-For 144.30.20
}
`),
}
handler, err := parseCaddyfile(h)
suite.Nil(err)

mh, ok := handler.(*Handler)
suite.True(ok, "the caddyhttp middlewareHandler should be castable to Handler")
suite.Equal(file, mh.Config.Type)

suite.Equal(3, len(mh.Config.RuleMatchersRaws))
}

func (suite *CaddyfileTestSuite) TestErrorSetMultipleCacheType() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
cache_type file in_memory
}
`),
}
_, err := parseCaddyfile(h)
suite.Error(err, "it should raise the invalid usage of cache_type")
}

func (suite *CaddyfileTestSuite) TestInvalidParameter() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
whocares haha
}
`),
}
_, err := parseCaddyfile(h)
suite.Error(err, "invalid parameter")
}

func (suite *CaddyfileTestSuite) TestDistributedCacheConfig() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
distributed consul {
service_name "cache_server"
}
}
`),
}

_, err := parseCaddyfile(h)
suite.Nil(err)
}

func TestCaddyfileTestSuite(t *testing.T) {
suite.Run(t, new(CaddyfileTestSuite))
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.14
require (
github.com/armon/go-metrics v0.3.3 // indirect
github.com/caddyserver/caddy/v2 v2.1.1
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/containerd/continuity v0.0.0-20200710164510-efbc4488d8fe // indirect
github.com/go-redis/redis v6.15.8+incompatible
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
github.com/hashicorp/consul/api v1.5.0
Expand All @@ -17,8 +19,12 @@ require (
github.com/mitchellh/go-testing-interface v1.0.3 // indirect
github.com/mitchellh/mapstructure v1.3.1 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/ory/dockertest/v3 v3.6.0
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.15.0
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
)
Loading

0 comments on commit e2ff3de

Please sign in to comment.