@@ -37,6 +37,14 @@ import (
3737
3838const updateSnapshotsEnvVarKey = "UPDATE_SNAPSHOTS"
3939
40+ type snapshotMode int
41+
42+ const (
43+ snapshotModeReplay snapshotMode = iota
44+ snapshotModeUpdate
45+ snapshotModeSkip
46+ )
47+
4048type snapshotData struct {
4149 Body []byte
4250 Status int
@@ -77,18 +85,19 @@ func (d snapshotData) Write(w http.ResponseWriter) (int, error) {
7785
7886// atlasE2ETestGenerator is about providing capabilities to provide projects and clusters for our e2e tests.
7987type atlasE2ETestGenerator struct {
80- projectID string
81- projectName string
82- clusterName string
83- clusterRegion string
84- tier string
85- mDBVer string
86- enableBackup bool
87- firstProcess * atlasv2.ApiHostViewAtlas
88- t * testing.T
89- fileIDs map [string ]int
90- memoryMap map [string ]any
91- lastData * snapshotData
88+ projectID string
89+ projectName string
90+ clusterName string
91+ clusterRegion string
92+ tier string
93+ mDBVer string
94+ enableBackup bool
95+ firstProcess * atlasv2.ApiHostViewAtlas
96+ t * testing.T
97+ fileIDs map [string ]int
98+ memoryMap map [string ]any
99+ lastData * snapshotData
100+ currentSnapshotMode snapshotMode
92101}
93102
94103// Log formats its arguments using default formatting, analogous to Println,
@@ -111,7 +120,7 @@ func (g *atlasE2ETestGenerator) Logf(format string, args ...any) {
111120// newAtlasE2ETestGenerator creates a new instance of atlasE2ETestGenerator struct.
112121func newAtlasE2ETestGenerator (t * testing.T , opts ... func (g * atlasE2ETestGenerator )) * atlasE2ETestGenerator {
113122 t .Helper ()
114- g := & atlasE2ETestGenerator {t : t , fileIDs : map [string ]int {}, memoryMap : map [string ]any {}}
123+ g := & atlasE2ETestGenerator {t : t , currentSnapshotMode : snapshotModeSkip , fileIDs : map [string ]int {}, memoryMap : map [string ]any {}}
115124 for _ , opt := range opts {
116125 opt (g )
117126 }
@@ -516,9 +525,13 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
516525 }
517526
518527 if updateSnapshots () {
528+ g .currentSnapshotMode = snapshotModeUpdate
529+
519530 dir := g .snapshotDir ()
520531 _ = os .RemoveAll (dir )
521532 } else {
533+ g .currentSnapshotMode = snapshotModeReplay
534+
522535 g .loadMemory ()
523536 }
524537
@@ -563,7 +576,7 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
563576 }
564577
565578 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
566- if updateSnapshots () {
579+ if g . currentSnapshotMode == snapshotModeUpdate {
567580 r .Host = targetURL .Host
568581 proxy .ServeHTTP (w , r )
569582 return
@@ -576,7 +589,7 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
576589 }))
577590
578591 g .t .Cleanup (func () {
579- if updateSnapshots () {
592+ if g . currentSnapshotMode == snapshotModeUpdate {
580593 g .storeMemory ()
581594 }
582595 server .Close ()
@@ -588,56 +601,62 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
588601func (g * atlasE2ETestGenerator ) memory (key string , value any ) any {
589602 g .t .Helper ()
590603
591- if skipSnapshots () {
604+ switch g .currentSnapshotMode {
605+ case snapshotModeSkip :
592606 return value
593- }
594-
595- if updateSnapshots () {
607+ case snapshotModeUpdate :
596608 _ , ok := g .memoryMap [key ]
597609 if ok {
598610 g .t .Fatalf ("memory key %q already exists" , key )
599611 }
600612 g .memoryMap [key ] = value
601613 return value
614+ case snapshotModeReplay :
615+ data , ok := g .memoryMap [key ]
616+ if ! ok {
617+ g .t .Fatalf ("memory key %q not found" , key )
618+ }
619+ return data
620+ default :
621+ g .t .Fatalf ("unexpected snapshot mode: %v" , g .currentSnapshotMode )
622+ return nil
602623 }
603- data , ok := g .memoryMap [key ]
604- if ! ok {
605- g .t .Fatalf ("memory key %q not found" , key )
606- }
607- return data
608624}
609625
610626func (g * atlasE2ETestGenerator ) memoryFunc (key string , value any , marshal func (value any ) ([]byte , error ), unmarshal func ([]byte ) (any , error )) any {
611627 g .t .Helper ()
612628
613- if skipSnapshots () {
629+ switch g .currentSnapshotMode {
630+ case snapshotModeSkip :
614631 return value
615- }
616-
617- if updateSnapshots () {
632+ case snapshotModeUpdate :
618633 data , err := marshal (value )
619634 if err != nil {
620635 g .t .Fatalf ("marshal: %v" , err )
621636 }
622637 g .memoryMap [key ] = base64 .StdEncoding .EncodeToString (data )
623638 return value
639+ case snapshotModeReplay :
640+ data , ok := g .memoryMap [key ]
641+ if ! ok {
642+ g .t .Fatalf ("memory key %q not found" , key )
643+ }
644+ buf , err := base64 .StdEncoding .DecodeString (data .(string ))
645+ if err != nil {
646+ g .t .Fatalf ("decode: %v" , err )
647+ }
648+ r , err := unmarshal (buf )
649+ if err != nil {
650+ g .t .Fatalf ("unmarshal: %v" , err )
651+ }
652+ return r
653+ default :
654+ g .t .Fatalf ("unexpected snapshot mode: %v" , g .currentSnapshotMode )
655+ return nil
624656 }
625- data , ok := g .memoryMap [key ]
626- if ! ok {
627- g .t .Fatalf ("memory key %q not found" , key )
628- }
629- buf , err := base64 .StdEncoding .DecodeString (data .(string ))
630- if err != nil {
631- g .t .Fatalf ("decode: %v" , err )
632- }
633- r , err := unmarshal (buf )
634- if err != nil {
635- g .t .Fatalf ("unmarshal: %v" , err )
636- }
637- return r
638657}
639658
640- func (g * atlasE2ETestGenerator ) memoryRand (key string , n int64 ) * big.Int { //nolint:unparam // in case there are more than one random values in the same test
659+ func (g * atlasE2ETestGenerator ) memoryRand (key string , n int64 ) * big.Int {
641660 g .t .Helper ()
642661
643662 r , ok := g .memoryFunc (key , must (RandInt (n )), func (value any ) ([]byte , error ) {
0 commit comments