Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ignite
GitHub Repository: ignite/cli
Path: blob/main/integration/env.go
1007 views
1
package envtest
2
3
import (
4
"context"
5
"flag"
6
"fmt"
7
"os"
8
"path"
9
"path/filepath"
10
"strconv"
11
"sync"
12
"testing"
13
"time"
14
15
"github.com/cenkalti/backoff"
16
"github.com/stretchr/testify/require"
17
18
"github.com/ignite/cli/v29/ignite/pkg/cosmosfaucet"
19
"github.com/ignite/cli/v29/ignite/pkg/env"
20
"github.com/ignite/cli/v29/ignite/pkg/errors"
21
"github.com/ignite/cli/v29/ignite/pkg/gocmd"
22
"github.com/ignite/cli/v29/ignite/pkg/httpstatuschecker"
23
"github.com/ignite/cli/v29/ignite/pkg/xurl"
24
)
25
26
const (
27
envDoNotTrack = "DO_NOT_TRACK"
28
)
29
30
var (
31
// IgniteApp hold the location of the ignite binary used in the integration
32
// tests. The binary is compiled the first time the env.New() function is
33
// invoked.
34
IgniteApp = path.Join(os.TempDir(), "ignite-tests", "ignite")
35
36
IsCI, _ = strconv.ParseBool(os.Getenv("CI"))
37
compileBinaryOnce sync.Once
38
)
39
40
// Env provides an isolated testing environment and what's needed to
41
// make it possible.
42
type Env struct {
43
t *testing.T
44
ctx context.Context
45
}
46
47
// New creates a new testing environment.
48
func New(t *testing.T) Env {
49
t.Helper()
50
ctx, cancel := context.WithCancel(t.Context())
51
e := Env{
52
t: t,
53
ctx: ctx,
54
}
55
// To avoid conflicts with the default config folder located in $HOME, we
56
// set an other one thanks to env var.
57
cfgDir := path.Join(t.TempDir(), ".ignite")
58
env.SetConfigDir(cfgDir)
59
enableDoNotTrackEnv(t)
60
61
t.Cleanup(cancel)
62
compileBinaryOnce.Do(func() {
63
compileBinary(ctx)
64
})
65
return e
66
}
67
68
func compileBinary(ctx context.Context) {
69
wd, err := os.Getwd()
70
if err != nil {
71
panic(fmt.Sprintf("unable to get working dir: %v", err))
72
}
73
pkgs, err := gocmd.List(ctx, wd, []string{"-m", "-f={{.Dir}}", "github.com/ignite/cli/v29"})
74
if err != nil {
75
panic(fmt.Sprintf("unable to list ignite cli package: %v", err))
76
}
77
if len(pkgs) != 1 {
78
panic(fmt.Sprintf("expected only one package, got %d", len(pkgs)))
79
}
80
appPath := pkgs[0]
81
var (
82
output, binary = filepath.Split(IgniteApp)
83
path = path.Join(appPath, "ignite", "cmd", "ignite")
84
)
85
err = gocmd.BuildPath(ctx, output, binary, path, nil)
86
if err != nil {
87
panic(fmt.Sprintf("error while building binary: %v", err))
88
}
89
}
90
91
func (e Env) T() *testing.T {
92
return e.t
93
}
94
95
// SetCleanup registers a function to be called when the test (or subtest) and all its
96
// subtests complete.
97
func (e Env) SetCleanup(f func()) {
98
e.t.Cleanup(f)
99
}
100
101
// Ctx returns parent context for the test suite to use for cancelations.
102
func (e Env) Ctx() context.Context {
103
return e.ctx
104
}
105
106
// IsAppServed checks that app is served properly and servers are started to listening before ctx canceled.
107
func (e Env) IsAppServed(ctx context.Context, apiAddr string) error {
108
checkAlive := func() error {
109
addr, err := xurl.HTTP(apiAddr)
110
if err != nil {
111
return err
112
}
113
114
ok, err := httpstatuschecker.Check(ctx, fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/node_info", addr))
115
if err == nil && !ok {
116
err = errors.New("waiting for app")
117
}
118
if HasTestVerboseFlag() {
119
fmt.Printf("IsAppServed at %s: %v\n", addr, err)
120
}
121
return err
122
}
123
124
return backoff.Retry(checkAlive, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx))
125
}
126
127
// IsFaucetServed checks that faucet of the app is served properly.
128
func (e Env) IsFaucetServed(ctx context.Context, faucetClient cosmosfaucet.HTTPClient) error {
129
checkAlive := func() error {
130
_, err := faucetClient.FaucetInfo(ctx)
131
return err
132
}
133
134
return backoff.Retry(checkAlive, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx))
135
}
136
137
// TmpDir creates a new temporary directory.
138
func (e Env) TmpDir() (path string) {
139
return e.t.TempDir()
140
}
141
142
// Home returns user's home dir.
143
func (e Env) Home() string {
144
home, err := os.UserHomeDir()
145
require.NoError(e.t, err)
146
return home
147
}
148
149
// AppHome returns app's root home/data dir path.
150
func (e Env) AppHome(name string) string {
151
return filepath.Join(e.Home(), fmt.Sprintf(".%s", name))
152
}
153
154
// Must fails the immediately if not ok.
155
// t.Fail() needs to be called for the failing tests before running Must().
156
func (e Env) Must(ok bool) {
157
if !ok {
158
e.t.FailNow()
159
}
160
}
161
162
func (e Env) HasFailed() bool {
163
return e.t.Failed()
164
}
165
166
func (e Env) RequireExpectations() {
167
e.Must(e.HasFailed())
168
}
169
170
// enableDoNotTrackEnv set true the DO_NOT_TRACK env var.
171
func enableDoNotTrackEnv(t *testing.T) {
172
t.Helper()
173
t.Setenv(envDoNotTrack, "true")
174
}
175
176
func HasTestVerboseFlag() bool {
177
return flag.Lookup("test.v").Value.String() == "true"
178
}
179
180