From c91d092f25985a7a67c0ac3eb3713140c983d6db Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Mon, 20 Nov 2023 17:31:19 +0100 Subject: [PATCH] more tests Signed-off-by: Matthias Bertschy --- config/config_test.go | 168 ++++++++++++++++++++++++++++++++++++ configuration/services.json | 11 +++ 2 files changed, 179 insertions(+) create mode 100644 config/config_test.go create mode 100644 configuration/services.json diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..12b1b4a --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,168 @@ +package config + +import ( + "os" + "reflect" + "testing" + + "github.com/armosec/utils-k8s-go/armometadata" + "github.com/kubescape/backend/pkg/servicediscovery/schema" + v2 "github.com/kubescape/backend/pkg/servicediscovery/v2" + pulsarconfig "github.com/kubescape/messaging/pulsar/config" + "github.com/kubescape/synchronizer/domain" + "github.com/stretchr/testify/assert" +) + +func TestLoadClusterConfig(t *testing.T) { + tests := []struct { + name string + want armometadata.ClusterConfig + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := LoadClusterConfig() + if (err != nil) != tt.wantErr { + t.Errorf("LoadClusterConfig() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("LoadClusterConfig() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestLoadConfig(t *testing.T) { + tests := []struct { + name string + path string + want Config + }{ + { + name: "client config", + path: "../configuration/client", + want: Config{ + InCluster: InCluster{ + ServerUrl: "ws://127.0.0.1:8080/", + ClusterName: "cluster-1", + Account: "11111111-2222-3333-4444-11111111", + AccessKey: "xxxxxxxx-1111-1111-1111-xxxxxxxx", + Resources: []Resource{ + {Group: "apps", Version: "v1", Resource: "deployments", Strategy: "patch"}, + {Group: "", Version: "v1", Resource: "pods", Strategy: "patch"}, + {Group: "spdx.softwarecomposition.kubescape.io", Version: "v1beta1", Resource: "sbomspdxv2p3s", Strategy: "copy"}, + {Group: "spdx.softwarecomposition.kubescape.io", Version: "v1beta1", Resource: "sbomspdxv2p3filtereds", Strategy: "copy"}, + }, + }, + }, + }, + { + name: "server config", + path: "../configuration/server", + want: Config{ + Backend: Backend{ + AuthenticationServer: &AuthenticationServerConfig{ + Url: "https://api.armosec.io/api/v1", + HeaderToQueryParamMapping: map[string]string{"x-api-account": "customerGUID"}, + HeaderToHeaderMapping: map[string]string{"x-api-key": "X-API-KEY"}, + }, + Subscription: "subscription", + PulsarConfig: &pulsarconfig.PulsarConfig{ + URL: "pulsar://localhost:6650", + Tenant: "kubescape", + Namespace: "kubescape", + AdminUrl: "http://localhost:8081", + Clusters: []string{"standalone"}, + RedeliveryDelaySeconds: 0, + MaxDeliveryAttempts: 2, + }, + Topic: "synchronizer", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := LoadConfig(tt.path) + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestLoadServiceURLs(t *testing.T) { + tests := []struct { + name string + env map[string]string + filePath string + want schema.IBackendServices + }{ + { + name: "via filePath", + filePath: "../configuration/services.json", + want: &v2.ServicesV2{ + EventReceiverHttpUrl: "https://er-test.com", + EventReceiverWebsocketUrl: "wss://er-test.com", + GatewayUrl: "https://gw.test.com", + ApiServerUrl: "https://api.test.com", + MetricsUrl: "https://metrics.test.com", + SynchronizerUrl: "wss://synchronizer.test.com", + }, + }, + { + name: "via env", + env: map[string]string{"SERVICES": "../configuration/services.json"}, + want: &v2.ServicesV2{ + EventReceiverHttpUrl: "https://er-test.com", + EventReceiverWebsocketUrl: "wss://er-test.com", + GatewayUrl: "https://gw.test.com", + ApiServerUrl: "https://api.test.com", + MetricsUrl: "https://metrics.test.com", + SynchronizerUrl: "wss://synchronizer.test.com", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + err := os.Setenv(k, v) + assert.NoError(t, err) + } + got, err := LoadServiceURLs(tt.filePath) + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestResource_String(t *testing.T) { + type fields struct { + Group string + Version string + Resource string + Strategy domain.Strategy + } + tests := []struct { + name string + fields fields + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := Resource{ + Group: tt.fields.Group, + Version: tt.fields.Version, + Resource: tt.fields.Resource, + Strategy: tt.fields.Strategy, + } + if got := r.String(); got != tt.want { + t.Errorf("String() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/configuration/services.json b/configuration/services.json new file mode 100644 index 0000000..1050402 --- /dev/null +++ b/configuration/services.json @@ -0,0 +1,11 @@ +{ + "version": "v2", + "response": { + "event-receiver-http": "https://er-test.com", + "event-receiver-ws": "wss://er-test.com", + "gateway": "https://gw.test.com", + "api-server": "https://api.test.com", + "metrics": "https://metrics.test.com", + "synchronizer": "wss://synchronizer.test.com" + } +}