-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import dockertest for integration test (#27)
- Loading branch information
Showing
10 changed files
with
318 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.