Skip to content

Commit 0882e1c

Browse files
authored
E2E: Move e2e mage targets to new package (#1404)
1 parent 7bb0803 commit 0882e1c

File tree

3 files changed

+92
-72
lines changed

3 files changed

+92
-72
lines changed

build/common.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ import (
2121

2222
"github.com/grafana/grafana-plugin-sdk-go/build/info"
2323
"github.com/grafana/grafana-plugin-sdk-go/build/utils"
24-
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e"
25-
ca "github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/certificate_authority"
26-
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/config"
27-
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/fixture"
28-
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/storage"
2924
"github.com/grafana/grafana-plugin-sdk-go/internal"
3025
)
3126

@@ -446,60 +441,6 @@ func Clean() error {
446441
return nil
447442
}
448443

449-
// E2E is a namespace.
450-
type E2E mg.Namespace
451-
452-
// Append starts the E2E proxy in append mode.
453-
func (E2E) Append() error {
454-
return e2eProxy(e2e.ProxyModeAppend)
455-
}
456-
457-
// Overwrite starts the E2E proxy in overwrite mode.
458-
func (E2E) Overwrite() error {
459-
return e2eProxy(e2e.ProxyModeOverwrite)
460-
}
461-
462-
// Replay starts the E2E proxy in replay mode.
463-
func (E2E) Replay() error {
464-
return e2eProxy(e2e.ProxyModeReplay)
465-
}
466-
467-
// Certificate prints the CA certificate to stdout.
468-
func (E2E) Certificate() error {
469-
cfg, err := config.LoadConfig("proxy.json")
470-
if err != nil {
471-
return err
472-
}
473-
474-
if cert, _, err := ca.LoadKeyPair(cfg.CAConfig.Cert, cfg.CAConfig.PrivateKey); err == nil {
475-
fmt.Print(string(cert))
476-
return nil
477-
}
478-
479-
fmt.Print(string(ca.CACertificate))
480-
return nil
481-
}
482-
483-
func e2eProxy(mode e2e.ProxyMode) error {
484-
cfg, err := config.LoadConfig("proxy.json")
485-
if err != nil {
486-
return err
487-
}
488-
fixtures := make([]*fixture.Fixture, 0)
489-
for _, s := range cfg.Storage {
490-
switch s.Type {
491-
case config.StorageTypeHAR:
492-
store := storage.NewHARStorage(s.Path)
493-
fixtures = append(fixtures, fixture.NewFixture(store))
494-
case config.StorageTypeOpenAPI:
495-
store := storage.NewOpenAPIStorage(s.Path)
496-
fixtures = append(fixtures, fixture.NewFixture(store))
497-
}
498-
}
499-
proxy := e2e.NewProxy(mode, fixtures, cfg)
500-
return proxy.Start()
501-
}
502-
503444
// checkLinuxPtraceScope verifies that ptrace is configured as required.
504445
func checkLinuxPtraceScope() error {
505446
ptracePath := "/proc/sys/kernel/yama/ptrace_scope"

experimental/e2e/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,33 @@ The goal of the proxy is to provide a way to record and replay HTTP interactions
3838

3939
2. Add the proxy's CA certificate to your environment ([instructions](#ca-certificate-setup)).
4040

41-
3. Start proxy using one of the [commands listed below](#mage-commands). For example:
41+
3. Add the E2E mage targets to your `Magefile.go` imports.
4242

43+
```diff
44+
package main
45+
46+
import (
47+
// mage:import
48+
build "github.com/grafana/grafana-plugin-sdk-go/build"
49+
+ // mage:import
50+
+ _ "github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/mage"
51+
)
4352
```
53+
54+
4. Start proxy using one of the [commands listed below](#mage-commands). For example:
55+
56+
```bash
4457
mage e2e:append
4558
```
4659

47-
4. Point Grafana at the proxy by exporting the `HTTP_PROXY` and `HTTPS_PROXY` environment variables:
60+
5. Point Grafana at the proxy by exporting the `HTTP_PROXY` and `HTTPS_PROXY` environment variables:
4861

49-
```
62+
```bash
5063
export HTTP_PROXY=127.0.0.1:9999
5164
export HTTPS_PROXY=127.0.0.1:9999
5265
```
5366

54-
5. Start Grafana
67+
6. Start Grafana
5568

5669
**Note:** Only queries with **absolute time ranges** should be used with the proxy. Relative time ranges are not supported in the default matcher.
5770

@@ -63,27 +76,27 @@ This step is needed so that the proxy can intercept HTTPS traffic. By default, t
6376

6477
1. Add CA certificate:
6578

66-
```
79+
```bash
6780
mage e2e:certificate && sudo tee /usr/share/ca-certificates/extra/ca.crt
6881
```
6982

7083
2. Update the CA store:
7184

72-
```
85+
```bash
7386
sudo update-ca-certificates --fresh
7487
```
7588

7689
### MacOS
7790

7891
1. Create a temporary copy of the CA certificate:
7992

80-
```
93+
```bash
8194
mage e2e:certificate > /tmp/grafana-e2e.crt
8295
```
8396

8497
2. Add CA certificate:
8598

86-
```
99+
```bash
87100
sudo security add-trusted-cert -d -p ssl -p basic -k /Library/Keychains/System.keychain /tmp/grafana-e2e.crt
88101
```
89102

@@ -186,31 +199,31 @@ Example configuration:
186199

187200
Append mode should be used to record interactions for any new tests. It will record requests and responses for any requests that haven't been seen before, and return recorded responses for any requests that match previously recorded interactions.
188201

189-
```
202+
```bash
190203
mage e2e:append
191204
```
192205

193206
### Overwrite mode
194207

195208
Overwrite mode should be used if previously recorded interactions need to be replaced with new data.
196209

197-
```
210+
```bash
198211
mage e2e:overwrite
199212
```
200213

201214
### Replay mode
202215

203216
Replay mode should be used in CI or locally if only playback of recorded data is needed. Replay mode will return recorded responses for any matching requests, and pass any requests that don't match recorded interactions to the target API.
204217

205-
```
218+
```bash
206219
mage e2e:replay
207220
```
208221

209222
### CA Certificate
210223

211224
This command prints the CA certificate to stdout so that it can be added to the local test environment. For more information, see the [CA Setup](#ca-certificate-setup) section above.
212225

213-
```
226+
```bash
214227
mage e2e:certificate
215228
```
216229

@@ -300,7 +313,7 @@ func CustomE2E() error {
300313

301314
Start the proxy using the new `CustomE2E` mage target:
302315

303-
```
316+
```bash
304317
mage CustomE2E
305318
```
306319

experimental/e2e/mage/e2e.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package mage
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e"
7+
ca "github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/certificate_authority"
8+
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/config"
9+
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/fixture"
10+
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/storage"
11+
"github.com/magefile/mage/mg"
12+
)
13+
14+
// E2e is a namespace.
15+
type E2e mg.Namespace
16+
17+
// Append starts the E2E proxy in append mode.
18+
func (E2e) Append() error {
19+
return e2eProxy(e2e.ProxyModeAppend)
20+
}
21+
22+
// Overwrite starts the E2E proxy in overwrite mode.
23+
func (E2e) Overwrite() error {
24+
return e2eProxy(e2e.ProxyModeOverwrite)
25+
}
26+
27+
// Replay starts the E2E proxy in replay mode.
28+
func (E2e) Replay() error {
29+
return e2eProxy(e2e.ProxyModeReplay)
30+
}
31+
32+
// Certificate prints the CA certificate to stdout.
33+
func (E2e) Certificate() error {
34+
cfg, err := config.LoadConfig("proxy.json")
35+
if err != nil {
36+
return err
37+
}
38+
39+
if cert, _, err := ca.LoadKeyPair(cfg.CAConfig.Cert, cfg.CAConfig.PrivateKey); err == nil {
40+
fmt.Print(string(cert))
41+
return nil
42+
}
43+
44+
fmt.Print(string(ca.CACertificate))
45+
return nil
46+
}
47+
48+
func e2eProxy(mode e2e.ProxyMode) error {
49+
cfg, err := config.LoadConfig("proxy.json")
50+
if err != nil {
51+
return err
52+
}
53+
fixtures := make([]*fixture.Fixture, 0)
54+
for _, s := range cfg.Storage {
55+
switch s.Type {
56+
case config.StorageTypeHAR:
57+
store := storage.NewHARStorage(s.Path)
58+
fixtures = append(fixtures, fixture.NewFixture(store))
59+
case config.StorageTypeOpenAPI:
60+
store := storage.NewOpenAPIStorage(s.Path)
61+
fixtures = append(fixtures, fixture.NewFixture(store))
62+
}
63+
}
64+
proxy := e2e.NewProxy(mode, fixtures, cfg)
65+
return proxy.Start()
66+
}

0 commit comments

Comments
 (0)