2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2020-01-17 19:46:51 +00:00
|
|
|
"os"
|
2019-11-02 14:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var kubernetesSuiteName = "Kubernetes"
|
|
|
|
|
|
|
|
func init() {
|
2022-12-12 23:15:32 +00:00
|
|
|
dockerEnvironment := NewDockerEnvironment([]string{
|
|
|
|
"internal/suites/docker-compose.yml",
|
|
|
|
"internal/suites/example/compose/k3d/docker-compose.yml",
|
|
|
|
})
|
|
|
|
|
|
|
|
k3d := K3D{}
|
2019-11-02 14:32:58 +00:00
|
|
|
kubectl := Kubectl{}
|
|
|
|
|
|
|
|
setup := func(suitePath string) error {
|
2022-12-12 23:15:32 +00:00
|
|
|
if err := dockerEnvironment.Up(); err != nil {
|
2019-11-30 16:49:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
err := waitUntilK3DIsReady(dockerEnvironment)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
exists, err := k3d.ClusterExists()
|
2019-11-02 14:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Kubernetes cluster already exists")
|
2019-11-02 14:32:58 +00:00
|
|
|
} else {
|
2022-12-12 23:15:32 +00:00
|
|
|
err = k3d.CreateCluster()
|
2019-11-02 14:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Building authelia:dist image or use cache if already built...")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
if os.Getenv("CI") != t {
|
2020-01-17 19:46:51 +00:00
|
|
|
if err := utils.Shell("authelia-scripts docker build").Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-12 23:15:32 +00:00
|
|
|
|
|
|
|
if err := utils.Shell("docker save authelia:dist -o internal/suites/example/kube/authelia-image-dev.tar").Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Loading images into Kubernetes container...")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-01-17 19:46:51 +00:00
|
|
|
if err := loadDockerImages(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Waiting for cluster to be ready...")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
if err := waitAllPodsAreReady(namespaceKube, 5*time.Minute); err != nil {
|
2019-11-16 15:22:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Waiting for dashboard to be ready...")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
err = waitAllPodsAreReady(namespaceDashboard, 2*time.Minute)
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Bearer token for UI user:")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
if err := kubectl.GetDashboardToken(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("Waiting for services to be ready...")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
if err := waitAllPodsAreReady(namespaceAuthelia, 5*time.Minute); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:30:53 +00:00
|
|
|
return err
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown := func(suitePath string) error {
|
2022-12-12 23:15:32 +00:00
|
|
|
if err := k3d.DeleteCluster(); err != nil {
|
|
|
|
return err
|
2020-12-16 01:47:31 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
return dockerEnvironment.Down()
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalRegistry.Register(kubernetesSuiteName, Suite{
|
|
|
|
SetUp: setup,
|
2019-12-27 11:07:53 +00:00
|
|
|
SetUpTimeout: 12 * time.Minute,
|
2019-11-30 16:49:52 +00:00
|
|
|
TestTimeout: 2 * time.Minute,
|
2019-11-02 14:32:58 +00:00
|
|
|
TearDown: teardown,
|
2019-11-30 16:49:52 +00:00
|
|
|
TearDownTimeout: 2 * time.Minute,
|
2022-12-12 23:15:32 +00:00
|
|
|
Description: "This suite has been created to test Authelia in a Kubernetes context and using Traefik as the ingress controller.",
|
2019-11-02 14:32:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadDockerImages() error {
|
2022-12-12 23:15:32 +00:00
|
|
|
k3d := K3D{}
|
|
|
|
images := []string{"/authelia/authelia-image-coverage.tar"}
|
|
|
|
|
|
|
|
if os.Getenv("CI") != t {
|
|
|
|
images = []string{"/authelia/authelia-image-dev.tar"}
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
for _, image := range images {
|
2022-12-12 23:15:32 +00:00
|
|
|
err := k3d.LoadImage(image)
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
func waitAllPodsAreReady(namespace string, timeout time.Duration) error {
|
2019-11-02 14:32:58 +00:00
|
|
|
kubectl := Kubectl{}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Infof("Checking services in %s namespace are running...", namespace)
|
|
|
|
|
|
|
|
if err := kubectl.WaitPodsReady(namespace, timeout); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-12-12 23:15:32 +00:00
|
|
|
log.Info("All pods are ready")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return nil
|
|
|
|
}
|