Path: blob/main/components/content-service/pkg/initializer/initializer_test.go
2499 views
// Copyright (c) 2022 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 initializer_test56import (7"context"8"fmt"9"testing"1011csapi "github.com/gitpod-io/gitpod/content-service/api"12"github.com/gitpod-io/gitpod/content-service/pkg/archive"13"github.com/gitpod-io/gitpod/content-service/pkg/initializer"14)1516type InitializerFunc func(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error)1718func (f InitializerFunc) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {19return f(ctx, mappings)20}2122type RecordingInitializer struct {23CallCount int24}2526func (f *RecordingInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {27f.CallCount++28return csapi.WorkspaceInitFromOther, nil, nil29}3031func TestCompositeInitializer(t *testing.T) {32tests := []struct {33Name string34Children []initializer.Initializer35Eval func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer)36}{37{38Name: "empty",39Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {40if err != nil {41t.Fatalf("unexpected error: %v", err)42}43},44},45{46Name: "single",47Children: []initializer.Initializer{48&RecordingInitializer{},49},50Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {51if cc := children[0].(*RecordingInitializer).CallCount; cc != 1 {52t.Errorf("unexpected call count: expected 1, got %d", cc)53}54},55},56{57Name: "multiple",58Children: []initializer.Initializer{59&RecordingInitializer{},60&RecordingInitializer{},61&RecordingInitializer{},62},63Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {64for i := range children {65if cc := children[i].(*RecordingInitializer).CallCount; cc != 1 {66t.Errorf("unexpected call count on initializer %d: expected 1, got %d", i, cc)67}68}69},70},71{72Name: "error propagation",73Children: []initializer.Initializer{74&RecordingInitializer{},75InitializerFunc(func(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {76return csapi.WorkspaceInitFromOther, nil, fmt.Errorf("error happened here")77}),78&RecordingInitializer{},79},80Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {81if err == nil {82t.Errorf("expected error, got nothing")83}84if cc := children[0].(*RecordingInitializer).CallCount; cc != 1 {85t.Errorf("unexpected call count on first initializer: expected 1, got %d", cc)86}87if cc := children[2].(*RecordingInitializer).CallCount; cc != 0 {88t.Errorf("unexpected call count on last initializer: expected 0, got %d", cc)89}90},91},92}93for _, test := range tests {94t.Run(test.Name, func(t *testing.T) {95comp := initializer.CompositeInitializer(test.Children)96src, _, err := comp.Run(context.Background(), nil)97test.Eval(t, src, err, test.Children)98})99}100}101102103