Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Aug 26, 2022
1 parent 14bedfd commit 7ac95c2
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions pkg/controllerapi/controllerapi_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7ac95c2

Please sign in to comment.