forked from ignite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
166 lines (141 loc) · 3.88 KB
/
env.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package envtest
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/cenkalti/backoff"
"github.com/stretchr/testify/require"
"github.com/ignite/cli/ignite/pkg/cosmosfaucet"
"github.com/ignite/cli/ignite/pkg/gocmd"
"github.com/ignite/cli/ignite/pkg/gomodulepath"
"github.com/ignite/cli/ignite/pkg/httpstatuschecker"
"github.com/ignite/cli/ignite/pkg/xurl"
)
const (
ConfigYML = "config.yml"
)
var (
// IgniteApp hold the location of the ignite binary used in the integration
// tests. The binary is compiled the first time the env.New() function is
// invoked.
IgniteApp = path.Join(os.TempDir(), "ignite-tests", "ignite")
isCI, _ = strconv.ParseBool(os.Getenv("CI"))
compileBinaryOnce sync.Once
)
// Env provides an isolated testing environment and what's needed to
// make it possible.
type Env struct {
t *testing.T
ctx context.Context
}
// New creates a new testing environment.
func New(t *testing.T) Env {
ctx, cancel := context.WithCancel(context.Background())
e := Env{
t: t,
ctx: ctx,
}
t.Cleanup(cancel)
compileBinaryOnce.Do(func() {
compileBinary(ctx)
})
return e
}
func compileBinary(ctx context.Context) {
wd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("unable to get working dir: %v", err))
}
_, appPath, err := gomodulepath.Find(wd)
if err != nil {
panic(fmt.Sprintf("unable to read go module path: %v", err))
}
var (
output, binary = filepath.Split(IgniteApp)
path = path.Join(appPath, "ignite", "cmd", "ignite")
)
err = gocmd.BuildPath(ctx, output, binary, path, nil)
if err != nil {
panic(fmt.Sprintf("error while building binary: %v", err))
}
}
func (e Env) T() *testing.T {
return e.t
}
// SetCleanup registers a function to be called when the test (or subtest) and all its
// subtests complete.
func (e Env) SetCleanup(f func()) {
e.t.Cleanup(f)
}
// Ctx returns parent context for the test suite to use for cancelations.
func (e Env) Ctx() context.Context {
return e.ctx
}
// IsAppServed checks that app is served properly and servers are started to listening before ctx canceled.
func (e Env) IsAppServed(ctx context.Context, apiAddr string) error {
checkAlive := func() error {
addr, err := xurl.HTTP(apiAddr)
if err != nil {
return err
}
ok, err := httpstatuschecker.Check(ctx, fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/node_info", addr))
if err == nil && !ok {
err = errors.New("app is not online")
}
if HasTestVerboseFlag() {
fmt.Printf("IsAppServed at %s: %v\n", addr, err)
}
return err
}
return backoff.Retry(checkAlive, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx))
}
// IsFaucetServed checks that faucet of the app is served properly
func (e Env) IsFaucetServed(ctx context.Context, faucetClient cosmosfaucet.HTTPClient) error {
checkAlive := func() error {
_, err := faucetClient.FaucetInfo(ctx)
return err
}
return backoff.Retry(checkAlive, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx))
}
// TmpDir creates a new temporary directory.
func (e Env) TmpDir() (path string) {
return e.t.TempDir()
}
// Home returns user's home dir.
func (e Env) Home() string {
home, err := os.UserHomeDir()
require.NoError(e.t, err)
return home
}
// AppHome returns app's root home/data dir path.
func (e Env) AppHome(name string) string {
return filepath.Join(e.Home(), fmt.Sprintf(".%s", name))
}
// Must fails the immediately if not ok.
// t.Fail() needs to be called for the failing tests before running Must().
func (e Env) Must(ok bool) {
if !ok {
e.t.FailNow()
}
}
func (e Env) HasFailed() bool {
return e.t.Failed()
}
func (e Env) RequireExpectations() {
e.Must(e.HasFailed())
}
func Contains(s, partial string) bool {
return strings.Contains(s, strings.TrimSpace(partial))
}
func HasTestVerboseFlag() bool {
return flag.Lookup("test.v").Value.String() == "true"
}