chore: extract getEnv in integration test #181
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 49s
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 49s
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
"me-fit/types"
|
"me-fit/types"
|
||||||
"me-fit/utils"
|
"me-fit/utils"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"me-fit/utils"
|
"me-fit/utils"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -4,13 +4,13 @@ import (
|
|||||||
"me-fit/handler"
|
"me-fit/handler"
|
||||||
"me-fit/types"
|
"me-fit/types"
|
||||||
"me-fit/utils"
|
"me-fit/utils"
|
||||||
"os"
|
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"me-fit/service"
|
"me-fit/service"
|
||||||
|
"me-fit/types"
|
||||||
"me-fit/utils"
|
"me-fit/utils"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -43,21 +44,7 @@ func TestHandleSignIn(t *testing.T) {
|
|||||||
t.Fatalf("Error inserting user: %v", err)
|
t.Fatalf("Error inserting user: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go run(ctx, db, func(key string) string {
|
go run(ctx, db, getEnv("8080"))
|
||||||
if key == "PORT" {
|
|
||||||
return "8080"
|
|
||||||
} else if key == "SMTP_ENABLED" {
|
|
||||||
return "false"
|
|
||||||
} else if key == "PROMETHEUS_ENABLED" {
|
|
||||||
return "false"
|
|
||||||
} else if key == "BASE_URL" {
|
|
||||||
return "https://localhost:8080"
|
|
||||||
} else if key == "ENVIRONMENT" {
|
|
||||||
return "test"
|
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
err = waitForReady(ctx, 5*time.Second, "http://localhost:8080")
|
err = waitForReady(ctx, 5*time.Second, "http://localhost:8080")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -89,6 +76,24 @@ func TestHandleSignIn(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEnv(port string) func(string) string {
|
||||||
|
return func(key string) string {
|
||||||
|
if key == "PORT" {
|
||||||
|
return port
|
||||||
|
} else if key == "SMTP_ENABLED" {
|
||||||
|
return "false"
|
||||||
|
} else if key == "PROMETHEUS_ENABLED" {
|
||||||
|
return "false"
|
||||||
|
} else if key == "BASE_URL" {
|
||||||
|
return "https://localhost:8080"
|
||||||
|
} else if key == "ENVIRONMENT" {
|
||||||
|
return "test"
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// waitForReady calls the specified endpoint until it gets a 200
|
// waitForReady calls the specified endpoint until it gets a 200
|
||||||
// response or until the context is cancelled or the timeout is
|
// response or until the context is cancelled or the timeout is
|
||||||
// reached.
|
// reached.
|
||||||
@@ -107,16 +112,17 @@ func waitForReady(
|
|||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
slog.Error("failed to create request: " + err.Error())
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error making request: %s\n", err.Error())
|
slog.Info("Error making request: " + err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode == http.StatusOK {
|
||||||
fmt.Println("Endpoint is ready!")
|
slog.Info("Endpoint is ready!")
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -127,7 +133,8 @@ func waitForReady(
|
|||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
default:
|
default:
|
||||||
if time.Since(startTime) >= timeout {
|
if time.Since(startTime) >= timeout {
|
||||||
return fmt.Errorf("timeout reached while waiting for endpoint")
|
slog.Error("timeout reached while waiting for endpoint")
|
||||||
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
// wait a little while between checks
|
// wait a little while between checks
|
||||||
time.Sleep(250 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
Reference in New Issue
Block a user