Path: blob/main/components/supervisor/pkg/ports/ports-config_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 ports56import (7"context"8"testing"910"github.com/golang/mock/gomock"11"github.com/google/go-cmp/cmp"1213gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"14)1516func TestPortsConfig(t *testing.T) {17tests := []struct {18Desc string19GitpodConfig *gitpod.GitpodConfig20Expectation *PortConfigTestExpectations21}{22{23Desc: "no configs",24Expectation: &PortConfigTestExpectations{},25},26{27Desc: "instance port config",28GitpodConfig: &gitpod.GitpodConfig{29Ports: []*gitpod.PortsItems{30{31Port: 9229,32OnOpen: "ignore",33Visibility: "public",34Name: "Nice Port Name",35Description: "Nice Port Description",36},37},38},39Expectation: &PortConfigTestExpectations{40InstancePortConfigs: []*gitpod.PortConfig{41{42Port: 9229,43OnOpen: "ignore",44Visibility: "public",45Name: "Nice Port Name",46Description: "Nice Port Description",47},48},49},50},51{52Desc: "instance range config",53GitpodConfig: &gitpod.GitpodConfig{54Ports: []*gitpod.PortsItems{55{56Port: "9229-9339",57OnOpen: "ignore",58Visibility: "public",59Name: "Nice Port Name",60Description: "Nice Port Description",61},62},63},64Expectation: &PortConfigTestExpectations{65InstanceRangeConfigs: []*RangeConfig{66{67PortsItems: gitpod.PortsItems{68Port: "9229-9339",69OnOpen: "ignore",70Visibility: "public",71Description: "Nice Port Description",72Name: "Nice Port Name",73},74Start: 9229,75End: 9339,76},77},78},79},80}81for _, test := range tests {82t.Run(test.Desc, func(t *testing.T) {83configService := &testGitpodConfigService{84configs: make(chan *gitpod.GitpodConfig),85changes: make(chan *struct{}),86}87defer close(configService.configs)88defer close(configService.changes)8990context, cancel := context.WithCancel(context.Background())91defer cancel()9293workspaceID := "test"9495ctrl := gomock.NewController(t)96defer ctrl.Finish()9798service := NewConfigService(workspaceID, configService)99updates, errors := service.Observe(context)100101actual := &PortConfigTestExpectations{}102103if test.GitpodConfig != nil {104go func() {105configService.configs <- test.GitpodConfig106}()107select {108case err := <-errors:109t.Fatal(err)110case change := <-updates:111actual.InstanceRangeConfigs = change.instanceRangeConfigs112for _, config := range change.instancePortConfigs {113actual.InstancePortConfigs = append(actual.InstancePortConfigs, &config.PortConfig)114}115}116}117118if diff := cmp.Diff(test.Expectation, actual); diff != "" {119t.Errorf("unexpected output (-want +got):\n%s", diff)120}121})122}123}124125type PortConfigTestExpectations struct {126InstancePortConfigs []*gitpod.PortConfig127InstanceRangeConfigs []*RangeConfig128}129130type testGitpodConfigService struct {131configs chan *gitpod.GitpodConfig132changes chan *struct{}133}134135func (service *testGitpodConfigService) Watch(ctx context.Context) {136}137138func (service *testGitpodConfigService) Observe(ctx context.Context) <-chan *gitpod.GitpodConfig {139return service.configs140}141142func (service *testGitpodConfigService) ObserveImageFile(ctx context.Context) <-chan *struct{} {143return service.changes144}145146147