Skip to content

Commit 4bfb83c

Browse files
committed
address comments
1 parent a019760 commit 4bfb83c

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
# - atlas,deployments,local,auth,new # needs docker to run
3636
# - atlas,deployments,local,nocli # needs docker to run
3737
# - atlas,deployments,local,noauth # needs docker to run
38-
- atlas,generic
38+
# - atlas,generic # tests are failing in master
3939
# - atlas,interactive # does not work well with snapshots
4040
- atlas,ldap
4141
- atlas,livemigrations
@@ -50,7 +50,7 @@ jobs:
5050
- atlas,plugin,update
5151
- atlas,processes
5252
- atlas,search
53-
- atlas,search_nodes
53+
# - atlas,search_nodes # tests are failing in master
5454
- atlas,serverless,instance
5555
- atlas,streams
5656
- atlas,streams_with_cluster

.github/workflows/update-e2e-tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
workflow_dispatch:
55
schedule:
66
- cron: 0 5 * * 1-5 # weekdays at 5:00 AM UTC
7+
pull_request: # TODO: remove once snapshots are updated
78
jobs:
89
update-tests:
910
runs-on: ubuntu-latest
@@ -34,7 +35,7 @@ jobs:
3435
# - atlas,deployments,local,auth,new # needs docker to run
3536
# - atlas,deployments,local,nocli # needs docker to run
3637
# - atlas,deployments,local,noauth # needs docker to run
37-
- atlas,generic
38+
# - atlas,generic # tests are failing in master
3839
# - atlas,interactive # does not work well with snapshots
3940
- atlas,ldap
4041
- atlas,livemigrations
@@ -49,7 +50,7 @@ jobs:
4950
- atlas,plugin,update
5051
- atlas,processes
5152
- atlas,search
52-
- atlas,search_nodes
53+
# - atlas,search_nodes # tests are failing in master
5354
- atlas,serverless,instance
5455
- atlas,streams
5556
- atlas,streams_with_cluster

test/e2e/atlas_e2e_test_generator_test.go

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,53 @@ import (
2828
"os"
2929
"os/exec"
3030
"path"
31-
"strconv"
3231
"strings"
3332
"testing"
3433

3534
"github.com/stretchr/testify/require"
3635
atlasv2 "go.mongodb.org/atlas-sdk/v20250312001/admin"
3736
)
3837

38+
const updateSnapshotsEnvVarKey = "UPDATE_SNAPSHOTS"
39+
40+
type snapshotData struct {
41+
Body []byte
42+
Status int
43+
Method string
44+
Path string
45+
46+
Headers map[string][]string
47+
}
48+
49+
func (d snapshotData) Compare(v snapshotData) int {
50+
methodCmp := strings.Compare(d.Method, v.Method)
51+
if methodCmp != 0 {
52+
return methodCmp
53+
}
54+
55+
pathCmp := strings.Compare(d.Path, v.Path)
56+
if pathCmp != 0 {
57+
return pathCmp
58+
}
59+
60+
statusCmp := d.Status - v.Status
61+
if statusCmp != 0 {
62+
return statusCmp
63+
}
64+
65+
return bytes.Compare(d.Body, v.Body)
66+
}
67+
68+
func (d snapshotData) Write(w http.ResponseWriter) (int, error) {
69+
for k, v := range d.Headers {
70+
w.Header()[k] = v
71+
}
72+
73+
w.WriteHeader(d.Status)
74+
75+
return w.Write(d.Body)
76+
}
77+
3978
// atlasE2ETestGenerator is about providing capabilities to provide projects and clusters for our e2e tests.
4079
type atlasE2ETestGenerator struct {
4180
projectID string
@@ -49,7 +88,7 @@ type atlasE2ETestGenerator struct {
4988
t *testing.T
5089
fileIDs map[string]int
5190
memoryMap map[string]any
52-
lastData map[string][]string
91+
lastData *snapshotData
5392
}
5493

5594
// Log formats its arguments using default formatting, analogous to Println,
@@ -394,11 +433,11 @@ func (g *atlasE2ETestGenerator) snapshotNameStepBack(r *http.Request) {
394433
}
395434

396435
func updateSnapshots() bool {
397-
return isTrue(os.Getenv("UPDATE_SNAPSHOTS"))
436+
return isTrue(os.Getenv(updateSnapshotsEnvVarKey))
398437
}
399438

400439
func skipSnapshots() bool {
401-
return os.Getenv("UPDATE_SNAPSHOTS") == "skip"
440+
return os.Getenv(updateSnapshotsEnvVarKey) == "skip"
402441
}
403442

404443
func (g *atlasE2ETestGenerator) loadMemory() {
@@ -439,7 +478,7 @@ func (g *atlasE2ETestGenerator) storeMemory() {
439478
}
440479
}
441480

442-
func (g *atlasE2ETestGenerator) readSnapshot(r *http.Request) []byte {
481+
func (g *atlasE2ETestGenerator) readSnapshot(r *http.Request) snapshotData {
443482
g.t.Helper()
444483

445484
filename := g.snapshotName(r)
@@ -454,7 +493,12 @@ func (g *atlasE2ETestGenerator) readSnapshot(r *http.Request) []byte {
454493
g.t.Fatal(err)
455494
}
456495

457-
return buf
496+
var data snapshotData
497+
if err := json.Unmarshal(buf, &data); err != nil {
498+
g.t.Fatal(err)
499+
}
500+
501+
return data
458502
}
459503

460504
func (g *atlasE2ETestGenerator) snapshotServer() {
@@ -485,31 +529,27 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
485529
return nil // skip 401
486530
}
487531

488-
data := map[string][]string{}
489-
for k, v := range resp.Header {
490-
data[k] = v
532+
data := snapshotData{
533+
Path: resp.Request.URL.Path,
534+
Method: resp.Request.Method,
535+
Status: resp.StatusCode,
536+
Headers: resp.Header,
491537
}
492538

493-
data["__path__"] = []string{resp.Request.URL.Path}
494-
495-
data["__method__"] = []string{resp.Request.Method}
496-
497-
data["__status__"] = []string{strconv.Itoa(resp.StatusCode)}
498-
499539
var buf bytes.Buffer
500540
if _, err := io.Copy(&buf, resp.Body); err != nil {
501541
return err
502542
}
503543
resp.Body.Close()
504544
resp.Body = io.NopCloser(&buf)
505545

506-
data["__body__"] = []string{base64.StdEncoding.EncodeToString(buf.Bytes())}
546+
data.Body = buf.Bytes()
507547

508-
if g.lastData != nil && data["__method__"][0] == g.lastData["__method__"][0] && data["__path__"][0] == g.lastData["__path__"][0] && data["__status__"][0] == g.lastData["__status__"][0] && data["__body__"][0] == g.lastData["__body__"][0] {
548+
if g.lastData != nil && data.Compare(*g.lastData) == 0 {
509549
return nil // skip same content
510550
}
511551

512-
g.lastData = data
552+
g.lastData = &data
513553

514554
out, err := json.MarshalIndent(data, "", " ")
515555
if err != nil {
@@ -529,35 +569,10 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
529569
return
530570
}
531571

532-
buf := g.readSnapshot(r)
533-
var data map[string][]string
534-
if err := json.Unmarshal(buf, &data); err != nil {
572+
data := g.readSnapshot(r)
573+
if _, err := data.Write(w); err != nil {
535574
g.t.Fatal(err)
536575
}
537-
538-
for k, v := range data {
539-
if k == "__body__" || k == "__status__" || k == "__method__" || k == "__path__" {
540-
continue
541-
}
542-
w.Header()[k] = v
543-
}
544-
545-
status, err := strconv.Atoi(data["__status__"][0])
546-
if err != nil {
547-
g.t.Fatal(err)
548-
}
549-
w.WriteHeader(status)
550-
551-
if data["__body__"] != nil {
552-
body, err := base64.StdEncoding.DecodeString(data["__body__"][0])
553-
if err != nil {
554-
g.t.Fatal(err)
555-
}
556-
_, err = w.Write(body)
557-
if err != nil {
558-
g.t.Fatal(err)
559-
}
560-
}
561576
}))
562577

563578
g.t.Cleanup(func() {

0 commit comments

Comments
 (0)