File: context_test.go

package info (click to toggle)
golang-github-cretz-bine 0.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 652 kB
  • sloc: makefile: 3
file content (99 lines) | stat: -rw-r--r-- 2,569 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package tests

import (
	"context"
	"flag"
	"os"
	"testing"
	"time"

	"github.com/cretz/bine/tor"
	"github.com/stretchr/testify/require"
)

var torEnabled bool
var torExePath string
var torVerbose bool
var torIncludeNetworkTests bool
var globalEnabledNetworkContext *TestContext

func TestMain(m *testing.M) {
	flag.BoolVar(&torEnabled, "tor", false, "Whether any of the integration tests are enabled")
	flag.StringVar(&torExePath, "tor.path", "tor", "The Tor exe path")
	flag.BoolVar(&torVerbose, "tor.verbose", false, "Show verbose test info")
	flag.BoolVar(&torIncludeNetworkTests, "tor.network", false, "Include network tests")
	flag.Parse()
	exitCode := m.Run()
	if globalEnabledNetworkContext != nil {
		globalEnabledNetworkContext.CloseTorOnClose = true
		globalEnabledNetworkContext.Close()
	}
	os.Exit(exitCode)
}

func GlobalEnabledNetworkContext(t *testing.T) *TestContext {
	if !torEnabled || !torIncludeNetworkTests {
		t.Skip("Only runs if -tor and -tor.network are set")
	}
	if globalEnabledNetworkContext == nil {
		ctx := NewTestContext(t, nil)
		ctx.CloseTorOnClose = false
		// 45 second wait for enable network
		enableCtx, enableCancel := context.WithTimeout(ctx, 45*time.Second)
		defer enableCancel()
		ctx.Require.NoError(ctx.EnableNetwork(enableCtx, true))
		globalEnabledNetworkContext = ctx
	} else {
		globalEnabledNetworkContext.T = t
		globalEnabledNetworkContext.Require = require.New(t)
	}
	return globalEnabledNetworkContext
}

type TestContext struct {
	context.Context
	*testing.T
	*tor.Tor
	Require         *require.Assertions
	CloseTorOnClose bool
}

func NewTestContext(t *testing.T, conf *tor.StartConf) *TestContext {
	if !torEnabled {
		t.Skip("Only runs if -tor is set")
	}
	// Build start conf
	if conf == nil {
		conf = &tor.StartConf{}
	}
	conf.ExePath = torExePath
	if torVerbose {
		conf.DebugWriter = os.Stdout
		conf.NoHush = true
	} else {
		conf.ExtraArgs = append(conf.ExtraArgs, "--quiet")
	}
	ret := &TestContext{Context: context.Background(), T: t, Require: require.New(t), CloseTorOnClose: true}
	// Start tor
	var err error
	if ret.Tor, err = tor.Start(ret.Context, conf); err != nil {
		defer ret.Close()
		t.Fatal(err)
	}
	return ret
}

// Deadline to disambiguate context and test deadline and use test one.
func (t *TestContext) Deadline() (time.Time, bool) { return t.T.Deadline() }

func (t *TestContext) Close() {
	if t.CloseTorOnClose {
		if err := t.Tor.Close(); err != nil {
			if t.Failed() {
				t.Logf("Failure on close: %v", err)
			} else {
				t.Errorf("Failure on close: %v", err)
			}
		}
	}
}