|
1 |
| -//go:build e2e |
2 |
| - |
| 1 | +// IN_THIS_PR: Figure out how to make this work: //go:build e2e |
3 | 2 | package e2e
|
4 | 3 |
|
5 | 4 | import (
|
| 5 | + "encoding/json" |
6 | 6 | "fmt"
|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
@@ -34,6 +34,8 @@ const (
|
34 | 34 | chainId = "0001"
|
35 | 35 | )
|
36 | 36 |
|
| 37 | +// TODO_IN_THIS_COMMIT: Rename `validator` to something more appropriate |
| 38 | + |
37 | 39 | type rootSuite struct {
|
38 | 40 | gocuke.TestingT
|
39 | 41 |
|
@@ -87,6 +89,38 @@ func (s *rootSuite) TheUserRunsTheCommand(cmd string) {
|
87 | 89 | s.validator.result = res
|
88 | 90 | }
|
89 | 91 |
|
| 92 | +// TheDeveloperRunsTheCommand is similar to TheUserRunsTheCommand but exclusive to `debug` commands |
| 93 | +func (s *rootSuite) TheDeveloperRunsTheCommand(cmd string) { |
| 94 | + cmds := strings.Split(cmd, " ") |
| 95 | + cmds = append([]string{"debug"}, cmds...) |
| 96 | + fmt.Println("OLSH", cmds) |
| 97 | + res, err := s.validator.RunCommand(cmds...) |
| 98 | + require.NoError(s, err) |
| 99 | + s.validator.result = res |
| 100 | + // IN_THIS_PR: Should we validate anything here? |
| 101 | +} |
| 102 | + |
| 103 | +func (s *rootSuite) WaitForTheNetworkToReachHeight(height int64) { |
| 104 | + args := []string{ |
| 105 | + "Query", |
| 106 | + "Height", |
| 107 | + } |
| 108 | + type expectedResponse struct { |
| 109 | + Height *int64 `json:"Height"` |
| 110 | + } |
| 111 | + validate := func(res *expectedResponse) bool { |
| 112 | + return res != nil && res.Height != nil |
| 113 | + } |
| 114 | + |
| 115 | + resRaw, err := s.validator.RunCommand(args...) |
| 116 | + require.NoError(s, err) |
| 117 | + |
| 118 | + res := getResponseFromStdout[expectedResponse](s, resRaw.Stdout, validate) |
| 119 | + require.NotNil(s, res) |
| 120 | + |
| 121 | + require.Equal(s, height, *(*res).Height) |
| 122 | +} |
| 123 | + |
90 | 124 | func (s *rootSuite) TheUserShouldBeAbleToSeeStandardOutputContaining(arg1 string) {
|
91 | 125 | require.Contains(s, s.validator.result.Stdout, arg1)
|
92 | 126 | }
|
@@ -190,3 +224,44 @@ func inClusterConfig(t gocuke.TestingT) *rest.Config {
|
190 | 224 |
|
191 | 225 | return config
|
192 | 226 | }
|
| 227 | + |
| 228 | +// |
| 229 | + |
| 230 | +// getResponseFromStdout returns the first non-log output from stdout when running a command |
| 231 | +// For example, when running `p1 Query Height`, the output is: |
| 232 | +// |
| 233 | +// {"level":"info","module":"e2e","time":"2023-07-11T15:46:07-07:00","message":"..."} |
| 234 | +// {"height":3} |
| 235 | +// |
| 236 | +// And will return the following map so it can be used by the caller: |
| 237 | +// |
| 238 | +// map[height:3] |
| 239 | +func getResponseFromStdout[T any](t gocuke.TestingT, stdout string, validate func(res *T) bool) *T { |
| 240 | + t.Helper() |
| 241 | + |
| 242 | + for _, s := range strings.Split(stdout, "\n") { |
| 243 | + fmt.Println(s) |
| 244 | + var m T |
| 245 | + if err := json.Unmarshal([]byte(s), &m); err != nil { |
| 246 | + fmt.Println("ERR", s) |
| 247 | + continue |
| 248 | + } |
| 249 | + if !validate(&m) { |
| 250 | + continue |
| 251 | + } |
| 252 | + fmt.Println("NO ERR", m) |
| 253 | + return &m |
| 254 | + } |
| 255 | + return nil |
| 256 | +} |
| 257 | + |
| 258 | +// func getLogs |
| 259 | +// isLog := func(m map[string]any) bool { |
| 260 | +// keys := []string{"level", "time", "message"} //"module", |
| 261 | +// for _, key := range keys { |
| 262 | +// if _, ok := m[key]; !ok { |
| 263 | +// return false |
| 264 | +// } |
| 265 | +// } |
| 266 | +// return true |
| 267 | +// } |
0 commit comments