From 7ac95c258e87438959ba98fc3d84386ac4779e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 25 Aug 2022 16:52:55 -0300 Subject: [PATCH] Add tests --- pkg/controllerapi/controllerapi_test.go | 106 ++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 pkg/controllerapi/controllerapi_test.go diff --git a/pkg/controllerapi/controllerapi_test.go b/pkg/controllerapi/controllerapi_test.go new file mode 100644 index 000000000..f7f8ba920 --- /dev/null +++ b/pkg/controllerapi/controllerapi_test.go @@ -0,0 +1,106 @@ +package controllerapi_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tsuru/nginx-operator/api/v1alpha1" + "github.com/tsuru/rpaas-operator/pkg/controllerapi" + extensionsruntime "github.com/tsuru/rpaas-operator/pkg/runtime" + coreV1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestPrometheusDiscover(t *testing.T) { + + nginx1 := &v1alpha1.Nginx{ + ObjectMeta: metaV1.ObjectMeta{ + Name: "test", + Namespace: "default", + }, + Spec: v1alpha1.NginxSpec{ + TLS: []v1alpha1.NginxTLS{ + { + Hosts: []string{"test.internal"}, + }, + { + Hosts: []string{"hello.globo"}, + }, + }, + }, + Status: v1alpha1.NginxStatus{ + Services: []v1alpha1.ServiceStatus{ + { + Name: "test", + }, + }, + }, + } + + svc1 := &coreV1.Service{ + ObjectMeta: metaV1.ObjectMeta{ + Name: "test", + Namespace: "default", + Labels: map[string]string{ + "nginx.tsuru.io/app": "nginx", + "rpaas.extensions.tsuru.io/instance-name": "test", + "rpaas.extensions.tsuru.io/service-name": "rpaasv2", + }, + }, + Status: coreV1.ServiceStatus{ + LoadBalancer: coreV1.LoadBalancerStatus{ + Ingress: []coreV1.LoadBalancerIngress{ + { + IP: "1.1.1.1", + }, + }, + }, + }, + } + + scheme := extensionsruntime.NewScheme() + client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(nginx1, svc1).Build() + api := controllerapi.New(client) + + w := httptest.NewRecorder() + r, _ := http.NewRequest(http.MethodGet, "/v1/prometheus/discover", nil) + + api.ServeHTTP(w, r) + + assert.Equal(t, http.StatusOK, w.Code) + l := []controllerapi.TargetGroup{} + err := json.Unmarshal(w.Body.Bytes(), &l) + require.NoError(t, err) + + assert.Equal(t, []controllerapi.TargetGroup{ + { + Targets: []string{"http://1.1.1.1/_nginx_healthcheck"}, + Labels: map[string]string{ + "service": "rpaasv2", + "service_instance": "test", + }, + }, + { + Targets: []string{"https://1.1.1.1/_nginx_healthcheck"}, + Labels: map[string]string{ + "servername": "test.internal", + "service": "rpaasv2", + "service_instance": "test", + }, + }, + { + Targets: []string{"https://1.1.1.1/_nginx_healthcheck"}, + Labels: map[string]string{ + "servername": "hello.globo", + "service": "rpaasv2", + "service_instance": "test", + }, + }, + }, l) +}