Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Revert "bootstrap e2e runner on first run instead of startup (#1957)" (
Browse files Browse the repository at this point in the history
…#1959)

This reverts commit 202ccb7.
  • Loading branch information
mikehelmick authored Mar 28, 2021
1 parent 7a52533 commit 1e05c89
Showing 1 changed file with 24 additions and 69 deletions.
93 changes: 24 additions & 69 deletions cmd/e2e-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

Expand All @@ -36,19 +35,12 @@ import (
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/controller/middleware"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/render"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)

var (
mu sync.Mutex
setupComplete bool = false
closeFunc func() error
)

func main() {
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

Expand Down Expand Up @@ -110,6 +102,28 @@ func realMain(ctx context.Context) error {
return fmt.Errorf("failed to create renderer: %w", err)
}

// Bootstrap the environment
resp, err := envstest.Bootstrap(ctx, db)
if err != nil {
return fmt.Errorf("failed to bootstrap testsuite: %w", err)
}
defer func() {
if err := resp.Cleanup(); err != nil {
logger.Errorw("failed to cleanup", "error", err)
}
}()

// Verify that SMS is configured on the realm
if !project.SkipE2ESMS {
has, err := resp.Realm.HasSMSConfig(db)
if err != nil {
return fmt.Errorf("failed to check if realm has sms config: %w", err)
}
if !has {
return fmt.Errorf("realm does not have sms config, configure it or set E2E_SKIP_SMS to continue")
}
}

// Create the enx-redirect client if the URL was specified
var enxRedirectClient *clients.ENXRedirectClient
if u := cfg.ENXRedirectURL; u != "" {
Expand All @@ -121,13 +135,8 @@ func realMain(ctx context.Context) error {
}
}

defer func() {
if closeFunc != nil {
if err := closeFunc(); err != nil {
logger.Errorw("failed to cleanup", "error", err)
}
}
}()
cfg.VerificationAdminAPIKey = resp.AdminAPIKey
cfg.VerificationAPIServerKey = resp.DeviceAPIKey

// Create the router
r := mux.NewRouter()
Expand All @@ -147,10 +156,6 @@ func realMain(ctx context.Context) error {
recovery := middleware.Recovery(h)
r.Use(recovery)

// E2E setup / boostrap function.
bootstrapE2E := bootstrap(ctx, db, cfg)
r.Use(bootstrapE2E)

r.Handle("/default", handleDefault(cfg, h))
r.Handle("/revise", handleRevise(cfg, h))
r.Handle("/enx-redirect", handleENXRedirect(enxRedirectClient, h))
Expand All @@ -169,56 +174,6 @@ func realMain(ctx context.Context) error {
return srv.ServeHTTPHandler(ctx, mux)
}

func bootstrap(ctx context.Context, db *database.Database, cfg *config.E2ERunnerConfig) mux.MiddlewareFunc {
logger := logging.FromContext(ctx).Named("e2erunner.bootstrap")
bootstrapFn := func() error {
mu.Lock()
defer mu.Unlock()

if !setupComplete {
// Bootstrap the environment
resp, err := envstest.Bootstrap(ctx, db)
if err != nil {
return fmt.Errorf("failed to bootstrap testsuite: %w", err)
}
// set the outer close function for later
closeFunc = func() error { return resp.Cleanup() }

// Verify that SMS is configured on the realm
if !project.SkipE2ESMS {
has, err := resp.Realm.HasSMSConfig(db)
if err != nil {
return fmt.Errorf("failed to check if realm has sms config: %w", err)
}
if !has {
return fmt.Errorf("realm does not have sms config, configure it or set E2E_SKIP_SMS to continue")
}
}

cfg.VerificationAdminAPIKey = resp.AdminAPIKey
cfg.VerificationAPIServerKey = resp.DeviceAPIKey
}
// only mark setup complete if it successfully runs, otherwise try again on next execution.
setupComplete = true
return nil
}

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := bootstrapFn(); err != nil {
logger.Errorw("unable to bootsrap e2e-runner", "error", err)
w.WriteHeader(http.StatusInternalServerError)
if _, e := w.Write([]byte(fmt.Sprintf("unable to boostrap runner: %v", err))); e != nil {
logger.Errorw("unable to write http response", "error", e)
}
return
}

next.ServeHTTP(w, r)
})
}
}

// handleDefault handles the default end-to-end scenario.
func handleDefault(cfg *config.E2ERunnerConfig, h *render.Renderer) http.Handler {
c := *cfg
Expand Down

0 comments on commit 1e05c89

Please sign in to comment.