Skip to content

Commit

Permalink
Remove test status from readme, cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed May 25, 2019
2 parents ef121be + 300dc59 commit 0fabbe7
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 60 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -10,8 +10,6 @@ English description
===================
Home page: https://github.com/rekby/lets-proxy2

Now it is alpha and not ready for production use.

Features:
* http-01 and tls-alpn-01 validation
* HTTPS (with certificate autoissue) and HTTP reverse proxy
Expand Down
2 changes: 1 addition & 1 deletion cmd/a_main-packr.go

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

18 changes: 0 additions & 18 deletions cmd/config.go
Expand Up @@ -90,24 +90,6 @@ func defaultConfig(ctx context.Context) []byte {
return configBytes
}

func readConfig(ctx context.Context, file string) *configType {
var res configType
logger := zc.LNop(ctx).With(zap.String("config_file", file))
var fileBytes []byte
var err error
fileBytes = defaultConfig(ctx)
err = toml.Unmarshal(fileBytes, &res)
log.DebugFatal(logger, err, "Read default config.")

if file != "" {
fileBytes, err = ioutil.ReadFile(file)
log.DebugFatal(logger, err, "Read config file", zap.String("filepath", file))
err = toml.Unmarshal(fileBytes, &res)
log.InfoFatal(logger, err, "Parse config file", zap.String("filepath", file))
}
return &res
}

func mergeConfigByTemplate(ctx context.Context, c *configType, filepathTemplate string) {
logger := zc.LNop(ctx).With(zap.String("config_file", filepathTemplate))
if !hasMeta(filepathTemplate) {
Expand Down
42 changes: 42 additions & 0 deletions cmd/config_test.go
Expand Up @@ -3,8 +3,11 @@ package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"

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

"github.com/gobuffalo/packr"

"github.com/maxatome/go-testdeep"
Expand All @@ -25,3 +28,42 @@ func TestConfigEmbed(t *testing.T) {
td.CmpNoError(err)
td.CmpDeeply(string(boxBytes), string(sourceConfig))
}

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

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

_ = ioutil.WriteFile(filepath.Join(tmpDir, "config.toml"), []byte(`
[General]
IssueTimeout = 1
StorageDir = "storage1"
IncludeConfigs = ["configs/*.toml"]
`), 0600)
_ = os.MkdirAll(filepath.Join(tmpDir, "configs"), 0700)
_ = ioutil.WriteFile(filepath.Join(tmpDir, "configs/config2.toml"), []byte(`
[General]
StorageDir = "storage2"
`), 0600)

var config configType
mergeConfigBytes(ctx, &config, defaultConfig(ctx), "")
mergeConfigByFilepath(ctx, &config, filepath.Join(tmpDir, "config.toml"))
td.CmpDeeply(config.General.IssueTimeout, 1)
td.CmpDeeply(config.General.StorageDir, "storage2")
}

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

testdeep.CmpNotNil(t, getConfig(ctx))
}
36 changes: 36 additions & 0 deletions cmd/log_test.go
@@ -0,0 +1,36 @@
package main

import (
"testing"

"github.com/maxatome/go-testdeep"
"go.uber.org/zap/zapcore"
)

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

res, err := parseLogLevel("debug")
td.CmpNoError(err)
td.CmpDeeply(res, zapcore.DebugLevel)

res, err = parseLogLevel("info")
td.CmpNoError(err)
td.CmpDeeply(res, zapcore.InfoLevel)

res, err = parseLogLevel("warning")
td.CmpNoError(err)
td.CmpDeeply(res, zapcore.WarnLevel)

res, err = parseLogLevel("error")
td.CmpNoError(err)
td.CmpDeeply(res, zapcore.ErrorLevel)

res, err = parseLogLevel("fatal")
td.CmpNoError(err)
td.CmpDeeply(res, zapcore.FatalLevel)

res, err = parseLogLevel("")
td.CmpError(err)
td.CmpDeeply(res, zapcore.InfoLevel)
}
4 changes: 4 additions & 0 deletions cmd/static/default-config.toml
@@ -1,3 +1,7 @@
## ATTENTION
# it is example file only. It builtin in binary and don't read from file system.
# changes in config_default.toml have no real effect.

[General]

# Seconds for issue every certificate. Cancel issue and return error if timeout.
Expand Down
8 changes: 8 additions & 0 deletions internal/acme_client_manager/client_manager_test.go
Expand Up @@ -2,6 +2,7 @@
package acme_client_manager

import (
"context"
"crypto/rsa"
"encoding/json"
"math/big"
Expand Down Expand Up @@ -78,4 +79,11 @@ func TestClientManagerGetFromCache(t *testing.T) {
client2, err := manager.GetClient(ctx)
td.CmpNoError(err)
td.True(client == client2)

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

client3, err := manager.GetClient(ctxCancelled)
td.CmpError(err)
td.Nil(client3)
}
14 changes: 4 additions & 10 deletions internal/cert_manager/cert-state.go
Expand Up @@ -19,7 +19,7 @@ type certState struct {
issueContext context.Context // nil if no issue process now
issueContextCancel func()
cert *tls.Certificate
locked bool
useAsIs bool // certificate locked by flag. It deny renew and some internal checks.
lastError error
}

Expand Down Expand Up @@ -108,20 +108,14 @@ func (s *certState) CertSet(ctx context.Context, locked bool, cert *tls.Certific

s.mu.Lock()
s.cert = cert
s.locked = locked
s.useAsIs = locked
s.lastError = nil
s.mu.Unlock()
}

func (s *certState) SetLocked() {
s.mu.Lock()
s.locked = true
s.mu.Unlock()
}

func (s *certState) GetLocked() bool {
func (s *certState) GetUseAsIs() bool {
s.mu.RLock()
defer s.mu.RUnlock()

return s.locked
return s.useAsIs
}
76 changes: 52 additions & 24 deletions internal/cert_manager/cert-state_test.go
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

td "github.com/maxatome/go-testdeep"
"github.com/maxatome/go-testdeep"
"github.com/rekby/lets-proxy2/internal/th"
)

Expand All @@ -21,24 +21,24 @@ func TestCertState(t *testing.T) {
defer flush()

s := &certState{}
td.CmpTrue(t, s.StartIssue(ctx))
td.CmpFalse(t, s.StartIssue(ctx))
testdeep.CmpTrue(t, s.StartIssue(ctx))
testdeep.CmpFalse(t, s.StartIssue(ctx))

cert := &tls.Certificate{Leaf: &x509.Certificate{Subject: pkix.Name{CommonName: "asd"}}}

s.FinishIssue(ctx, cert, nil)

rCert, rErr := s.Cert()
td.CmpDeeply(t, rCert, cert)
td.CmpNil(t, rErr)
testdeep.CmpDeeply(t, rCert, cert)
testdeep.CmpNil(t, rErr)

s = &certState{}
err1 := errors.New("1")
td.CmpTrue(t, s.StartIssue(ctx))
testdeep.CmpTrue(t, s.StartIssue(ctx))
s.FinishIssue(ctx, nil, err1)
rCert, rErr = s.Cert()
td.CmpNil(t, rCert)
td.CmpDeeply(t, rErr, err1)
testdeep.CmpNil(t, rCert)
testdeep.CmpDeeply(t, rErr, err1)

}

Expand Down Expand Up @@ -150,15 +150,15 @@ func TestCertState_WaitFinishIssue(t *testing.T) {
//nolint:govet
ctxTimeout, _ := context.WithTimeout(ctx, timeout)
rCert, rErr := s.WaitFinishIssue(ctxTimeout)
td.CmpNil(t, rCert)
td.CmpNil(t, rErr)
testdeep.CmpNil(t, rCert)
testdeep.CmpNil(t, rErr)

s.StartIssue(ctx)
//nolint:govet
ctxTimeout, _ = context.WithTimeout(ctx, timeout)
rCert, rErr = s.WaitFinishIssue(ctxTimeout)
td.CmpNil(t, rCert)
td.CmpError(t, rErr)
testdeep.CmpNil(t, rCert)
testdeep.CmpError(t, rErr)

cert1 := &tls.Certificate{Leaf: &x509.Certificate{Subject: pkix.Name{CommonName: "asdasd"}}}
go func() {
Expand All @@ -168,8 +168,8 @@ func TestCertState_WaitFinishIssue(t *testing.T) {
//nolint:govet
ctxTimeout, _ = context.WithTimeout(ctx, timeout)
rCert, rErr = s.WaitFinishIssue(ctxTimeout)
td.CmpNoError(t, rErr)
td.CmpDeeply(t, rCert, cert1)
testdeep.CmpNoError(t, rErr)
testdeep.CmpDeeply(t, rCert, cert1)

s.StartIssue(ctx)
err2 := errors.New("2")
Expand All @@ -180,8 +180,8 @@ func TestCertState_WaitFinishIssue(t *testing.T) {
//nolint:govet
ctxTimeout, _ = context.WithTimeout(ctx, timeout)
rCert, rErr = s.WaitFinishIssue(ctxTimeout)
td.CmpNil(t, rCert)
td.CmpDeeply(t, rErr, err2)
testdeep.CmpNil(t, rCert)
testdeep.CmpDeeply(t, rErr, err2)
}

func TestCertState_FinishIssuePanic(t *testing.T) {
Expand All @@ -194,24 +194,52 @@ func TestCertState_FinishIssuePanic(t *testing.T) {
cert1 := &tls.Certificate{Leaf: &x509.Certificate{Subject: pkix.Name{CommonName: "asdf"}}}
err1 := errors.New("2")

td.CmpPanic(t, func() {
testdeep.CmpPanic(t, func() {
s.FinishIssue(th.NoLog(ctx), cert1, nil)
}, td.NotEmpty())
}, testdeep.NotEmpty())

rCert, rErr := s.Cert()
td.CmpDeeply(t, rCert, cert1)
td.CmpNil(t, rErr)
testdeep.CmpDeeply(t, rCert, cert1)
testdeep.CmpNil(t, rErr)

s = certState{}
s.StartIssue(ctx)
td.CmpPanic(t, func() {
testdeep.CmpPanic(t, func() {
s.FinishIssue(th.NoLog(ctx), nil, nil)
}, td.NotEmpty())
}, testdeep.NotEmpty())

s = certState{}
s.StartIssue(ctx)
td.CmpPanic(t, func() {
testdeep.CmpPanic(t, func() {
s.FinishIssue(th.NoLog(ctx), cert1, err1)
}, td.NotEmpty())
}, testdeep.NotEmpty())

}

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

td := testdeep.NewT(t)
s := certState{}
cert := &tls.Certificate{
OCSPStaple: []byte{1, 2, 3},
}
s.CertSet(ctx, true, cert)
td.CmpDeeply(s.cert, cert)
td.True(s.useAsIs)

s.CertSet(ctx, false, nil)
td.Nil(s.cert)
td.False(s.useAsIs)
}

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

s := certState{}
td.False(s.GetUseAsIs())

s.useAsIs = true
td.True(s.GetUseAsIs())
}

0 comments on commit 0fabbe7

Please sign in to comment.