Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/295: Use testing.Testing() where appropriate #351

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Most recent version is listed first.
# v0.0.72
- ong/log: Use slog from stdlib: https://github.com/komuw/ong/pull/349
- ong/xcontext: Remove the package since Go v1.21 has similar functionality: https://github.com/komuw/ong/pull/350
- Use testing.Testing() where appropriate: https://github.com/komuw/ong/pull/351

# v0.0.71
- Only use test dependencies in test files: https://github.com/komuw/ong/pull/348
Expand Down
10 changes: 10 additions & 0 deletions automax/automax.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package automax automatically sets GOMEMLIMIT & GOMAXPROCS to match the linux container memory & cpu quotas, if any.
package automax

import "testing"

// config is used for tests.
type config struct {
memCgroupV1 string
Expand All @@ -14,6 +16,10 @@ type config struct {
//
// The optional argument c is only used for internal test purposes.
func SetMem(c ...config) func() {
if len(c) > 0 && !testing.Testing() {
panic("optional argument c should only be used for internal test purposes")
}

return setMem(c...)
}

Expand All @@ -23,5 +29,9 @@ func SetMem(c ...config) func() {
//
// The optional argument c is only used for internal test purposes.
func SetCpu(c ...config) func() {
if len(c) > 0 && !testing.Testing() {
panic("optional argument c should only be used for internal test purposes")
}

return setCpu(c...)
}
9 changes: 6 additions & 3 deletions internal/acme/acme.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"strings"
"sync"
"testing"
"time"

"golang.org/x/net/idna"
Expand Down Expand Up @@ -267,9 +268,11 @@ type manager struct {
func initManager(domain, email, acmeDirectoryUrl string, l *slog.Logger, testDiskCache ...string) *manager {
diskCacheDir := ""

if len(testDiskCache) > 0 {
// allow for tests.
// todo: check if `testing.Testing()` and panic if `testDiskCache` is there and it is not testing.
if len(testDiskCache) > 0 && !testing.Testing() {
panic("optional argument testDiskCache should only be used for internal test purposes")
}

if len(testDiskCache) > 0 && testing.Testing() {
diskCacheDir = testDiskCache[0]
} else {
d, errA := diskCachedir()
Expand Down
8 changes: 5 additions & 3 deletions internal/acme/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"strconv"
"strings"
"testing"
"time"

"golang.org/x/net/idna"
Expand All @@ -39,16 +40,17 @@ func diskCachedir() (string, error) {
4. https://github.com/gopasspw/gopass/blob/v1.15.3-rc1/pkg/tempfile/mount_linux.go#L13-L27
*/

// todo: Check testing.Testing() and set dir==/tmp/ for tests.
// This is so that we do not fill the disk.

dir, _ := os.UserConfigDir()
if dir == "" {
dir = "/dev/shm"
}
if dir == "" {
dir = "/tmp/"
}
if testing.Testing() {
// Set dir==/tmp/ for tests, so that we do not fill the disk.
dir = "/tmp/"
}

dir = filepath.Join(dir, "ong_acme")
if err := os.MkdirAll(dir, 0o755); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions internal/acme/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/http/httputil"
"sync"
"testing"
"time"
)

Expand Down Expand Up @@ -77,8 +78,9 @@ type logRT struct {
func (lt *logRT) RoundTrip(req *http.Request) (res *http.Response, err error) {
ctx := req.Context()
requestType := getRequestType(ctx)
// todo: Gate this under testing.Testing()
req.Header.Set("ONG-TEST-REQ-TYPE", requestType)
if testing.Testing() {
req.Header.Set("ONG-TEST-REQ-TYPE", requestType)
}
start := time.Now()
url := req.URL.Redacted()

Expand Down
17 changes: 17 additions & 0 deletions internal/tst/tst.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
)

// TlsServer starts a test TLS server at a predetermined port and returns it.
// It's upto callers to close the server.
func TlsServer(h http.Handler, domain string, httpsPort uint16) (*httptest.Server, error) {
if !testing.Testing() {
panic("this func should only be called from tests")
}

ts := httptest.NewUnstartedServer(h)
if err := ts.Listener.Close(); err != nil {
return nil, err
Expand All @@ -33,18 +38,30 @@ func TlsServer(h http.Handler, domain string, httpsPort uint16) (*httptest.Serve
// GetPort returns a random port.
// The idea is that different tests should run on different independent ports to avoid collisions.
func GetPort() uint16 {
if !testing.Testing() {
panic("this func should only be called from tests")
}

r := rand.Intn(10_000) + 1
p := math.MaxUint16 - uint16(r)
return p
}

// SecretKey returns a secret key that is valid and can be used in tests.
func SecretKey() string {
if !testing.Testing() {
panic("this func should only be called from tests")
}

return "super-h@rd-Pa$1word"
}

// Ping waits for port to be open, it fails after a number of given seconds.
func Ping(port uint16) error {
if !testing.Testing() {
panic("this func should only be called from tests")
}

var err error
count := 0
maxCount := 12
Expand Down