Path: blob/main/test/tests/components/ws-manager/maintenence_test.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsmanager56import (7"context"8"encoding/json"9"errors"10"testing"11"time"1213"google.golang.org/grpc/codes"14"google.golang.org/grpc/status"15corev1 "k8s.io/api/core/v1"16apierrors "k8s.io/apimachinery/pkg/api/errors"17metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"18"sigs.k8s.io/e2e-framework/klient"19"sigs.k8s.io/e2e-framework/pkg/envconf"20"sigs.k8s.io/e2e-framework/pkg/features"2122csapi "github.com/gitpod-io/gitpod/content-service/api"23"github.com/gitpod-io/gitpod/test/pkg/integration"24wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"25"github.com/gitpod-io/gitpod/ws-manager/api/config"26)2728func TestMaintenance(t *testing.T) {29testRepo := "https://github.com/gitpod-io/empty"30testRepoName := "empty"3132f1 := features.New("maintenance").33WithLabel("component", "ws-manager").34WithLabel("type", "maintenance").35Assess("should display maintenance message", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {36kubeClient, err := cfg.NewClient()37if err != nil {38t.Fatal(err)39}4041untilTime := time.Now().Add(1 * time.Hour)42err = configureMaintenanceMode(testCtx, &untilTime, kubeClient)43if err != nil {44t.Fatal(err)45}4647t.Cleanup(func() {48err = configureMaintenanceMode(testCtx, nil, kubeClient)49if err != nil {50t.Error(err)51}52})5354ctx, cancel := context.WithTimeout(testCtx, time.Duration(5*time.Minute))55defer cancel()5657api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())58t.Cleanup(func() {59api.Done(t)60})6162customizeWorkspace := func(swr *wsmanapi.StartWorkspaceRequest) error {63swr.Spec.Initializer = &csapi.WorkspaceInitializer{64Spec: &csapi.WorkspaceInitializer_Git{65Git: &csapi.GitInitializer{66RemoteUri: testRepo,67CheckoutLocation: testRepoName,68Config: &csapi.GitConfig{},69},70},71}72swr.Spec.WorkspaceLocation = testRepoName73return nil74}7576_, _, err = integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(customizeWorkspace))77if err == nil {78t.Fatalf("expected under maintenance error")79} else {80if !errors.Is(err, status.Error(codes.FailedPrecondition, "under maintenance")) {81t.Fatal(err)82}83}8485return testCtx86}).87Feature()8889f2 := features.New("maintenance-configuration").90WithLabel("component", "ws-manager").91WithLabel("type", "maintenance").92Assess("should display a maintenance message when configured and not when disabled", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {93kubeClient, err := cfg.NewClient()94if err != nil {95t.Fatal(err)96}9798defer func() {99err := configureMaintenanceMode(testCtx, nil, kubeClient)100if err != nil {101t.Error(err)102}103}()104105untilTime := time.Now().Add(1 * time.Hour)106err = configureMaintenanceMode(testCtx, &untilTime, kubeClient)107if err != nil {108t.Fatal(err)109}110111ctx, cancel := context.WithTimeout(testCtx, time.Duration(5*time.Minute))112defer cancel()113114api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())115t.Cleanup(func() {116api.Done(t)117})118119_, _, err = integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(swr *wsmanapi.StartWorkspaceRequest) error {120swr.Spec.Initializer = &csapi.WorkspaceInitializer{121Spec: &csapi.WorkspaceInitializer_Git{122Git: &csapi.GitInitializer{123RemoteUri: testRepo,124CheckoutLocation: testRepoName,125Config: &csapi.GitConfig{},126},127},128}129swr.Spec.WorkspaceLocation = testRepoName130return nil131}))132if err == nil {133t.Fatalf("expected under maintenance error")134} else {135if !errors.Is(err, status.Error(codes.FailedPrecondition, "under maintenance")) {136t.Fatal(err)137}138}139140err = configureMaintenanceMode(testCtx, nil, kubeClient)141if err != nil {142t.Fatal(err)143}144145time.Sleep(1 * time.Second)146147_, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(swr *wsmanapi.StartWorkspaceRequest) error {148swr.Spec.Initializer = &csapi.WorkspaceInitializer{149Spec: &csapi.WorkspaceInitializer_Git{150Git: &csapi.GitInitializer{151RemoteUri: testRepo,152CheckoutLocation: testRepoName,153Config: &csapi.GitConfig{},154},155},156}157swr.Spec.WorkspaceLocation = testRepoName158return nil159}))160if err != nil {161t.Fatal(err)162}163164if err := stopWorkspace(t, cfg, stopWs); err != nil {165t.Errorf("cannot stop workspace: %q", err)166}167168return testCtx169}).170Feature()171172testEnv.Test(t, f1, f2)173}174175func configureMaintenanceMode(ctx context.Context, untilTime *time.Time, kubeClient klient.Client) error {176cmap, err := maintenanceConfigmap(untilTime)177if err != nil {178return err179}180181err = kubeClient.Resources().Create(ctx, cmap)182if err != nil {183if apierrors.IsAlreadyExists(err) {184err = kubeClient.Resources().Update(ctx, cmap)185if err != nil {186return err187}188}189190return err191}192193return nil194}195196func maintenanceConfigmap(untilTime *time.Time) (*corev1.ConfigMap, error) {197mcfg := config.MaintenanceConfig{}198if untilTime != nil {199mcfg.EnabledUntil = untilTime200}201202data, err := json.Marshal(mcfg)203if err != nil {204return nil, err205}206207return &corev1.ConfigMap{208ObjectMeta: metav1.ObjectMeta{209Name: "ws-manager-mk2-maintenance-mode",210Namespace: "default",211},212Data: map[string]string{213"config.json": string(data),214},215}, nil216}217218219