github.com/kuoss/venti@v0.2.20/pkg/service/discovery/kubernetes/kubernetes_test.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kuoss/venti/pkg/model"
     7  	"github.com/stretchr/testify/assert"
     8  	v1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/runtime"
    11  	"k8s.io/client-go/kubernetes/fake"
    12  )
    13  
    14  func makeService(name string, namespace string, multiport bool, annotation map[string]string) runtime.Object {
    15  	ports := []v1.ServicePort{
    16  		{
    17  			Name:     "testport",
    18  			Protocol: v1.ProtocolTCP,
    19  			Port:     int32(30900),
    20  		},
    21  	}
    22  	if multiport {
    23  		ports = append(ports, v1.ServicePort{
    24  			Name:     "http",
    25  			Protocol: v1.ProtocolTCP,
    26  			Port:     int32(8080),
    27  		})
    28  	}
    29  
    30  	return runtime.Object(&v1.Service{
    31  		ObjectMeta: metav1.ObjectMeta{
    32  			Name:        name,
    33  			Namespace:   namespace,
    34  			Annotations: annotation,
    35  		},
    36  		Spec: v1.ServiceSpec{
    37  			Ports:     ports,
    38  			Type:      v1.ServiceTypeClusterIP,
    39  			ClusterIP: "10.0.0.1",
    40  		},
    41  	})
    42  }
    43  
    44  var servicesWithoutAnnotation = []runtime.Object{
    45  	makeService("prometheus", "namespace1", false, nil),
    46  	makeService("prometheus", "namespace2", false, nil),
    47  	makeService("prometheus", "kube-system", false, nil),
    48  	makeService("lethe", "kuoss", true, nil),
    49  	makeService("lethe", "kube-system", true, nil),
    50  }
    51  
    52  var servicesWithAnnotation = []runtime.Object{
    53  	makeService("prometheus", "namespace1", false, map[string]string{
    54  		"kuoss.org/datasource-type": "prometheus",
    55  	}),
    56  	makeService("prometheus", "namespace2", false, map[string]string{
    57  		"kuoss.org/datasource-type": "prometheus",
    58  	}),
    59  	makeService("prometheus", "kube-system", false, map[string]string{
    60  		"kuoss.org/datasource-type": "prometheus",
    61  	}),
    62  	makeService("lethe", "kuoss", true, map[string]string{
    63  		"kuoss.org/datasource-type": "lethe",
    64  	}),
    65  	makeService("lethe", "kube-system", true, map[string]string{
    66  		"kuoss.org/datasource-type": "lethe",
    67  	}),
    68  }
    69  
    70  func TestDoDiscoveryWithoutAnnotationKey(t *testing.T) {
    71  	want := []model.Datasource{
    72  		{
    73  			Type:         "lethe",
    74  			Name:         "lethe.kube-system",
    75  			URL:          "http://lethe.kube-system:8080",
    76  			IsMain:       false,
    77  			IsDiscovered: true,
    78  		},
    79  		{
    80  			Type:         "prometheus",
    81  			Name:         "prometheus.kube-system",
    82  			URL:          "http://prometheus.kube-system:30900",
    83  			IsMain:       false,
    84  			IsDiscovered: true,
    85  		},
    86  		{
    87  			Type:         "lethe",
    88  			Name:         "lethe.kuoss",
    89  			URL:          "http://lethe.kuoss:8080",
    90  			IsMain:       false,
    91  			IsDiscovered: true,
    92  		},
    93  		{
    94  			Type:         "prometheus",
    95  			Name:         "prometheus.namespace1",
    96  			URL:          "http://prometheus.namespace1:30900",
    97  			IsMain:       false,
    98  			IsDiscovered: true,
    99  		},
   100  		{
   101  			Type:         "prometheus",
   102  			Name:         "prometheus.namespace2",
   103  			URL:          "http://prometheus.namespace2:30900",
   104  			IsMain:       false,
   105  			IsDiscovered: true,
   106  		}}
   107  
   108  	k8sService := &k8sService{fake.NewSimpleClientset(servicesWithoutAnnotation...)}
   109  	discovered, err := k8sService.Do(model.Discovery{
   110  		Enabled:          true,
   111  		ByNamePrometheus: true,
   112  		ByNameLethe:      true,
   113  	})
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	assert.ElementsMatch(t, want, discovered)
   118  }
   119  
   120  func TestDoDiscoveryAnnotationKey(t *testing.T) {
   121  	want := []model.Datasource{
   122  		{
   123  			Type:         "lethe",
   124  			Name:         "lethe.kube-system",
   125  			URL:          "http://lethe.kube-system:8080",
   126  			IsMain:       false,
   127  			IsDiscovered: true,
   128  		},
   129  		{
   130  			Type:         "prometheus",
   131  			Name:         "prometheus.kube-system",
   132  			URL:          "http://prometheus.kube-system:30900",
   133  			IsMain:       false,
   134  			IsDiscovered: true,
   135  		},
   136  		{
   137  			Type:         "lethe",
   138  			Name:         "lethe.kuoss",
   139  			URL:          "http://lethe.kuoss:8080",
   140  			IsMain:       false,
   141  			IsDiscovered: true,
   142  		},
   143  		{
   144  			Type:         "prometheus",
   145  			Name:         "prometheus.namespace1",
   146  			URL:          "http://prometheus.namespace1:30900",
   147  			IsMain:       false,
   148  			IsDiscovered: true,
   149  		},
   150  		{
   151  			Type:         "prometheus",
   152  			Name:         "prometheus.namespace2",
   153  			URL:          "http://prometheus.namespace2:30900",
   154  			IsMain:       false,
   155  			IsDiscovered: true,
   156  		}}
   157  
   158  	k8sService := &k8sService{fake.NewSimpleClientset(servicesWithAnnotation...)}
   159  	discovered, err := k8sService.Do(model.Discovery{
   160  		Enabled:          true,
   161  		AnnotationKey:    "kuoss.org/datasource-type",
   162  		ByNamePrometheus: false,
   163  		ByNameLethe:      false,
   164  	})
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  	assert.ElementsMatch(t, want, discovered)
   169  }