Files
sablier/e2e/e2e_test.go
Alexis Couvreur dfb9bacf59 feat(providers): add provider.auto-stop-on-startup argument (#346)
This feature adds the capability to stop unregistered running instances upon startup.

Previously, you had to stop running instances manually or issue an initial request that will shut down instances afterwards.

With this change, all discovered instances will be shutdown. They need to be registered using labels. E.g.: sablier.enable=true

Fixes #153
2024-10-01 17:30:14 -07:00

172 lines
4.0 KiB
Go

//go:build e2e
// +build e2e
package e2e
import (
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/gavv/httpexpect/v2"
)
func Test_Dynamic(t *testing.T) {
e := httpexpect.Default(t, "http://localhost:8080/dynamic/")
e.GET("/whoami").
Expect().
Status(http.StatusOK).
Body().
Contains(`Dynamic Whoami`).
Contains(`Your instance(s) will stop after 1 minute of inactivity`)
e.GET("/whoami").
WithMaxRetries(10).
WithRetryDelay(time.Second, time.Second*2).
WithRetryPolicy(httpexpect.RetryCustomHandler).
WithCustomHandler(func(resp *http.Response, _ error) bool {
if resp.Body != nil {
// Check body if available, etc.
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return true
}
return !strings.Contains(string(body), "Host: localhost:8080")
}
return false
}).
Expect().
Status(http.StatusOK).
Body().Contains(`Host: localhost:8080`)
}
func Test_Blocking(t *testing.T) {
e := httpexpect.Default(t, "http://localhost:8080/blocking/")
e.GET("/whoami").
Expect().
Status(http.StatusOK).
Body().Contains(`Host: localhost:8080`)
}
func Test_Multiple(t *testing.T) {
e := httpexpect.Default(t, "http://localhost:8080/multiple/")
e.GET("/whoami").
Expect().
Status(http.StatusOK).
Body().
Contains(`Multiple Whoami`).
Contains(`Your instance(s) will stop after 1 minute of inactivity`)
e.GET("/whoami").
WithMaxRetries(10).
WithRetryDelay(time.Second, time.Second*2).
WithRetryPolicy(httpexpect.RetryCustomHandler).
WithCustomHandler(func(resp *http.Response, _ error) bool {
if resp.Body != nil {
// Check body if available, etc.
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return true
}
return !strings.Contains(string(body), "Host: localhost:8080")
}
return false
}).
Expect().
Status(http.StatusOK).
Body().Contains(`Host: localhost:8080`)
e.GET("/nginx").
WithMaxRetries(10).
WithRetryDelay(time.Second, time.Second*2).
WithRetryPolicy(httpexpect.RetryCustomHandler).
WithCustomHandler(func(resp *http.Response, _ error) bool {
if resp.Body != nil {
// Check body if available, etc.
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return true
}
return !strings.Contains(string(body), "nginx/")
}
return false
}).
Expect().
Status(http.StatusNotFound).
Body().Contains(`nginx/`)
}
func Test_Healthy(t *testing.T) {
e := httpexpect.Default(t, "http://localhost:8080/healthy/")
e.GET("/nginx").
Expect().
Status(http.StatusOK).
Body().
Contains(`Healthy Nginx`).
Contains(`Your instance(s) will stop after 1 minute of inactivity`)
e.GET("/nginx").
WithMaxRetries(10).
WithRetryDelay(time.Second, time.Second*2).
WithRetryPolicy(httpexpect.RetryCustomHandler).
WithCustomHandler(func(resp *http.Response, _ error) bool {
if resp.Body != nil {
// Check body if available, etc.
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return true
}
return !strings.Contains(string(body), "nginx/")
}
return false
}).
Expect().
Status(http.StatusNotFound).
Body().Contains(`nginx/`)
}
func Test_Group(t *testing.T) {
e := httpexpect.Default(t, "http://localhost:8080/")
e.GET("/group").
Expect().
Status(http.StatusOK).
Body().
Contains(`Group E2E`).
Contains(`Your instance(s) will stop after 1 minute of inactivity`)
e.GET("/group").
WithMaxRetries(10).
WithRetryDelay(time.Second, time.Second*2).
WithRetryPolicy(httpexpect.RetryCustomHandler).
WithCustomHandler(func(resp *http.Response, _ error) bool {
if resp.Body != nil {
// Check body if available, etc.
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return true
}
return !strings.Contains(string(body), "Host: localhost:8080")
}
return false
}).
Expect().
Status(http.StatusOK).
Body().Contains(`Host: localhost:8080`)
}