Skip to content

Commit

Permalink
add test fixenv
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Sep 2, 2021
1 parent b958a92 commit a45cccd
Show file tree
Hide file tree
Showing 21 changed files with 746 additions and 77 deletions.
14 changes: 4 additions & 10 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ func TestConfigEmbed(t *testing.T) {
}

func TestReadConfig(t *testing.T) {
ctx, cancel := th.TestContext(t)
e, ctx, cancel := th.NewEnv(t)
defer cancel()

td := testdeep.NewT(t)
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
td.Fatal(err)
}
defer func() {
_ = os.RemoveAll(tmpDir)
}()
tmpDir := th.TmpDir(e)

_ = ioutil.WriteFile(filepath.Join(tmpDir, "config.toml"), []byte(`
[General]
Expand All @@ -71,8 +65,8 @@ StorageDir = "storage2"
}

func TestGetConfig(t *testing.T) {
ctx, cancel := th.TestContext(t)
e, ctx, cancel := th.NewEnv(t)
defer cancel()

testdeep.CmpNotNil(t, getConfig(ctx))
e.NotNil(getConfig(ctx))
}
20 changes: 9 additions & 11 deletions cmd/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/rekby/lets-proxy2/internal/th"

"github.com/maxatome/go-testdeep"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -40,13 +41,10 @@ func TestParseLogLever(t *testing.T) {
}

func TestInitLogger(t *testing.T) {
td := testdeep.NewT(t)
e, _, flush := th.NewEnv(t)
defer flush()

tmpDir, err := ioutil.TempDir("", "")
if err != nil {
td.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := th.TmpDir(e)

// LogToFile, logLevel
logFile := filepath.Join(tmpDir, "log.txt")
Expand All @@ -63,9 +61,9 @@ func TestInitLogger(t *testing.T) {
logger.Sync()

fileBytes, err := ioutil.ReadFile(logFile)
td.CmpNoError(err)
td.True(strings.Contains(string(fileBytes), testError))
td.False(strings.Contains(string(fileBytes), testInfo))
e.CmpNoError(err)
e.True(strings.Contains(string(fileBytes), testError))
e.False(strings.Contains(string(fileBytes), testInfo))

// DevelMode
config = logConfig{DeveloperMode: false, LogLevel: "info", EnableLogToStdErr: true}
Expand All @@ -74,7 +72,7 @@ func TestInitLogger(t *testing.T) {

config = logConfig{DeveloperMode: true, LogLevel: "info"}
logger = initLogger(config)
td.CmpPanic(func() {
e.CmpPanic(func() {
logger.DPanic(testError)
}, testError)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.4.1
github.com/prometheus/client_model v0.2.0
github.com/rekby/fixenv v0.0.0-20210828185014-c79c816ca0c9
github.com/rekby/zapcontext v0.0.4
github.com/rogpeppe/go-internal v1.6.0 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.5.1 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/zap v1.11.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rekby/fixenv v0.0.0-20210828185014-c79c816ca0c9 h1:gw5CuVblN/ZRe6C6tFZON55eEIQDDv23GVNW5SZRlOE=
github.com/rekby/fixenv v0.0.0-20210828185014-c79c816ca0c9/go.mod h1:YEzfMavb6H20GB60BqZuygxbTBoMnoo5fkQrqAE4fUo=
github.com/rekby/zapcontext v0.0.4 h1:85600nHTteGCLcuOhGp/SzXHymm9QcCA5sn+MPKCodY=
github.com/rekby/zapcontext v0.0.4/go.mod h1:lTIxvHAwWXBZBPPfEvmAEXPbVEcTwd52VaASZWZWcxI=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -169,6 +171,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -265,6 +268,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
36 changes: 15 additions & 21 deletions internal/acme_client_manager/client_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

"golang.org/x/crypto/acme"

"github.com/maxatome/go-testdeep"

"github.com/rekby/lets-proxy2/internal/cache"

"github.com/gojuno/minimock/v3"
Expand All @@ -25,12 +23,10 @@ const testACMEServer = "https://acme-server:4001/dir"

//go:generate minimock -i github.com/rekby/lets-proxy2/internal/cache.Bytes -o ./cache_bytes_mock_test.go
func TestClientManagerCreateNew(t *testing.T) {
ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()

td := testdeep.NewT(t)

mc := minimock.NewController(td)
mc := minimock.NewController(e)
defer mc.Finish()

c := NewBytesMock(mc)
Expand All @@ -45,22 +41,20 @@ func TestClientManagerCreateNew(t *testing.T) {
c.GetMock.Return(nil, cache.ErrCacheMiss)
manager.DirectoryURL = testACMEServer
client, err := manager.GetClient(ctx)
td.CmpNoError(err)
td.NotNil(client)
e.CmpNoError(err)
e.NotNil(client)

client2, err := manager.GetClient(ctx)
td.CmpNoError(err)
td.True(client == client2)
e.CmpNoError(err)
e.True(client == client2)
}

func TestClientManagerGetFromCache(t *testing.T) {
ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()
ctx = zc.WithLogger(ctx, zap.NewNop().WithOptions(zap.Development()))

td := testdeep.NewT(t)

mc := minimock.NewController(td)
mc := minimock.NewController(e)
defer mc.Finish()

c := NewBytesMock(mc)
Expand All @@ -80,18 +74,18 @@ func TestClientManagerGetFromCache(t *testing.T) {

c.GetMock.Return(stateBytes, nil)
client, err := manager.GetClient(ctx)
td.CmpNoError(err)
td.NotNil(client)
td.CmpDeeply(client.Key, state.PrivateKey)
e.CmpNoError(err)
e.NotNil(client)
e.CmpDeeply(client.Key, state.PrivateKey)

client2, err := manager.GetClient(ctx)
td.CmpNoError(err)
td.True(client == client2)
e.CmpNoError(err)
e.True(client == client2)

ctxCancelled, ctxCancelledCancel := context.WithCancel(ctx)
ctxCancelledCancel()

client3, err := manager.GetClient(ctxCancelled)
td.CmpError(err)
td.Nil(client3)
e.CmpError(err)
e.Nil(client3)
}
11 changes: 2 additions & 9 deletions internal/cache/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ package cache

import (
"bytes"
"io/ioutil"
"os"
"testing"

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

func TestDiskCache(t *testing.T) {
ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()

dirPath, err := ioutil.TempDir("", "lets-proxy2-test-")
defer os.RemoveAll(dirPath)

if err != nil {
t.Fatal(err)
}
dirPath := th.TmpDir(e)

c := &DiskCache{Dir: dirPath}
res, err := c.Get(ctx, "asd")
Expand Down
18 changes: 5 additions & 13 deletions internal/cache/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestMemoryCache(t *testing.T) {
ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()

c := NewMemoryCache("test")
Expand All @@ -22,27 +22,19 @@ func TestMemoryCache(t *testing.T) {

data := []byte("aaa")
err = c.Put(ctx, "asd", data)
if err != nil {
t.Error(err)
}
e.CmpNoError(err)

res, err = c.Get(ctx, "asd")
if !bytes.Equal(res, data) {
t.Error(res)
}
if err != nil {
t.Error(err)
}
e.CmpNoError(err)

err = c.Delete(ctx, "asd")
if err != nil {
t.Error(err)
}
e.CmpNoError(err)

err = c.Delete(ctx, "non-existed-key")
if err != nil {
t.Error(err)
}
e.CmpNoError(err)

res, err = c.Get(ctx, "asd")
if len(res) != 0 {
Expand Down
22 changes: 10 additions & 12 deletions internal/cache/value_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@ import (
)

func TestValueLRUAsCache(t *testing.T) {
td := testdeep.NewT(t)

ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()

c := NewMemoryValueLRU("test")
res, err := c.Get(ctx, "asd")
td.Nil(res)
td.CmpDeeply(err, ErrCacheMiss)
e.Nil(res)
e.CmpDeeply(err, ErrCacheMiss)

data := []byte("aaa")
err = c.Put(ctx, "asd", data)
td.CmpNoError(err)
e.CmpNoError(err)

res, err = c.Get(ctx, "asd")
td.CmpDeeply(res, data)
td.CmpNoError(err)
e.CmpDeeply(res, data)
e.CmpNoError(err)

err = c.Delete(ctx, "asd")
td.CmpNoError(err)
e.CmpNoError(err)

err = c.Delete(ctx, "non-existed-key")
td.CmpNoError(err)
e.CmpNoError(err)

res, err = c.Get(ctx, "asd")
td.Nil(res)
td.CmpDeeply(err, ErrCacheMiss)
e.Nil(res)
e.CmpDeeply(err, ErrCacheMiss)
}

func TestValueLRULimitAtPut(t *testing.T) {
Expand Down
72 changes: 72 additions & 0 deletions internal/th/fixenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package th

import (
"context"
"sync"
"testing"

"github.com/maxatome/go-testdeep"

"github.com/rekby/fixenv"
)

type Env struct {
ctx context.Context
*fixenv.EnvT
TD
}

func NewEnv(t *testing.T) (env *Env, ctx context.Context, cancel func()) {
td := testdeep.NewT(t)
ctx, ctxCancel := TestContext(td)
tWrap := &testWrapper{T: td}
env = &Env{
EnvT: fixenv.NewEnv(tWrap),
ctx: ctx,
TD: TD{T: td},
}
tWrap.Cleanup(ctxCancel)
return env, ctx, tWrap.startCleanups
}

func (e *Env) T() fixenv.T {
return e.EnvT.T()
}

// TD struct need for rename embedded field in Env
type TD struct {
*testdeep.T
}

type testWrapper struct {
*testdeep.T

m sync.Mutex
cleanups []func()
cleanupsStarted bool
}

func (t *testWrapper) Cleanup(f func()) {
t.m.Lock()
defer t.m.Unlock()

t.cleanups = append(t.cleanups, f)
}

func (t *testWrapper) startCleanups() {
t.m.Lock()
started := t.cleanupsStarted
if !started {
t.cleanupsStarted = true
}
t.m.Unlock()

if started {
return
}

for i := len(t.cleanups) - 1; i >= 0; i-- {
f := t.cleanups[i]
f()
}
}
Loading

0 comments on commit a45cccd

Please sign in to comment.