2019-07-20 04:18:50 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-10-02 02:49:53 +08:00
|
|
|
"k8s.io/api/core/v1"
|
2021-03-18 05:35:25 +08:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-04-09 00:43:39 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-20 04:18:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestEndpoint(t *testing.T) {
|
|
|
|
|
cli := &client{}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location())
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
handler *mockHandler
|
2021-04-09 00:43:39 +08:00
|
|
|
output []telegraf.Metric
|
2019-07-20 04:18:50 +08:00
|
|
|
hasError bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no endpoints",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
|
|
|
|
"/endpoints/": &v1.EndpointsList{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "collect ready endpoints",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
|
|
|
|
"/endpoints/": &v1.EndpointsList{
|
2021-03-18 05:35:25 +08:00
|
|
|
Items: []v1.Endpoints{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Subsets: []v1.EndpointSubset{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Addresses: []v1.EndpointAddress{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Hostname: "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
NodeName: toStrPtr("b.storage.internal"),
|
|
|
|
|
TargetRef: &v1.ObjectReference{
|
2021-03-18 05:35:25 +08:00
|
|
|
Kind: "pod",
|
|
|
|
|
Name: "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
Ports: []v1.EndpointPort{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Name: "server",
|
|
|
|
|
Protocol: "TCP",
|
|
|
|
|
Port: 8080,
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Generation: 12,
|
|
|
|
|
Namespace: "ns1",
|
|
|
|
|
Name: "storage",
|
|
|
|
|
CreationTimestamp: metav1.Time{Time: now},
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-04-09 00:43:39 +08:00
|
|
|
output: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"kubernetes_endpoint",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"endpoint_name": "storage",
|
|
|
|
|
"namespace": "ns1",
|
|
|
|
|
"hostname": "storage-6",
|
|
|
|
|
"node_name": "b.storage.internal",
|
|
|
|
|
"port_name": "server",
|
|
|
|
|
"port_protocol": "TCP",
|
|
|
|
|
"pod": "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
2021-04-09 00:43:39 +08:00
|
|
|
map[string]interface{}{
|
|
|
|
|
"ready": true,
|
|
|
|
|
"port": int32(8080),
|
|
|
|
|
"generation": int64(12),
|
|
|
|
|
"created": now.UnixNano(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "collect notready endpoints",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
|
|
|
|
"/endpoints/": &v1.EndpointsList{
|
2021-03-18 05:35:25 +08:00
|
|
|
Items: []v1.Endpoints{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Subsets: []v1.EndpointSubset{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
NotReadyAddresses: []v1.EndpointAddress{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Hostname: "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
NodeName: toStrPtr("b.storage.internal"),
|
|
|
|
|
TargetRef: &v1.ObjectReference{
|
2021-03-18 05:35:25 +08:00
|
|
|
Kind: "pod",
|
|
|
|
|
Name: "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
Ports: []v1.EndpointPort{
|
2019-07-20 04:18:50 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Name: "server",
|
|
|
|
|
Protocol: "TCP",
|
|
|
|
|
Port: 8080,
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Generation: 12,
|
|
|
|
|
Namespace: "ns1",
|
|
|
|
|
Name: "storage",
|
|
|
|
|
CreationTimestamp: metav1.Time{Time: now},
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-04-09 00:43:39 +08:00
|
|
|
output: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"kubernetes_endpoint",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"endpoint_name": "storage",
|
|
|
|
|
"namespace": "ns1",
|
|
|
|
|
"hostname": "storage-6",
|
|
|
|
|
"node_name": "b.storage.internal",
|
|
|
|
|
"port_name": "server",
|
|
|
|
|
"port_protocol": "TCP",
|
|
|
|
|
"pod": "storage-6",
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
2021-04-09 00:43:39 +08:00
|
|
|
map[string]interface{}{
|
|
|
|
|
"ready": false,
|
|
|
|
|
"port": int32(8080),
|
|
|
|
|
"generation": int64(12),
|
|
|
|
|
"created": now.UnixNano(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
2019-07-20 04:18:50 +08:00
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
2021-07-07 03:57:52 +08:00
|
|
|
{
|
|
|
|
|
name: "endpoints missing node_name",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
|
|
|
|
"/endpoints/": &v1.EndpointsList{
|
|
|
|
|
Items: []v1.Endpoints{
|
|
|
|
|
{
|
|
|
|
|
Subsets: []v1.EndpointSubset{
|
|
|
|
|
{
|
|
|
|
|
NotReadyAddresses: []v1.EndpointAddress{
|
|
|
|
|
{
|
|
|
|
|
Hostname: "storage-6",
|
|
|
|
|
TargetRef: &v1.ObjectReference{
|
|
|
|
|
Kind: "pod",
|
|
|
|
|
Name: "storage-6",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
|
{
|
|
|
|
|
Name: "server",
|
|
|
|
|
Protocol: "TCP",
|
|
|
|
|
Port: 8080,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Addresses: []v1.EndpointAddress{
|
|
|
|
|
{
|
|
|
|
|
Hostname: "storage-12",
|
|
|
|
|
TargetRef: &v1.ObjectReference{
|
|
|
|
|
Kind: "pod",
|
|
|
|
|
Name: "storage-12",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
|
{
|
|
|
|
|
Name: "server",
|
|
|
|
|
Protocol: "TCP",
|
|
|
|
|
Port: 8080,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Generation: 12,
|
|
|
|
|
Namespace: "ns1",
|
|
|
|
|
Name: "storage",
|
|
|
|
|
CreationTimestamp: metav1.Time{Time: now},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
output: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"kubernetes_endpoint",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"endpoint_name": "storage",
|
|
|
|
|
"namespace": "ns1",
|
|
|
|
|
"hostname": "storage-6",
|
|
|
|
|
"port_name": "server",
|
|
|
|
|
"port_protocol": "TCP",
|
|
|
|
|
"pod": "storage-6",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"ready": false,
|
|
|
|
|
"port": int32(8080),
|
|
|
|
|
"generation": int64(12),
|
|
|
|
|
"created": now.UnixNano(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"kubernetes_endpoint",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"endpoint_name": "storage",
|
|
|
|
|
"namespace": "ns1",
|
|
|
|
|
"hostname": "storage-12",
|
|
|
|
|
"port_name": "server",
|
|
|
|
|
"port_protocol": "TCP",
|
|
|
|
|
"pod": "storage-12",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"ready": true,
|
|
|
|
|
"port": int32(8080),
|
|
|
|
|
"generation": int64(12),
|
|
|
|
|
"created": now.UnixNano(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range tests {
|
|
|
|
|
ks := &KubernetesInventory{
|
|
|
|
|
client: cli,
|
|
|
|
|
}
|
|
|
|
|
acc := new(testutil.Accumulator)
|
|
|
|
|
for _, endpoint := range ((v.handler.responseMap["/endpoints/"]).(*v1.EndpointsList)).Items {
|
2021-03-18 05:35:25 +08:00
|
|
|
ks.gatherEndpoint(endpoint, acc)
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := acc.FirstError()
|
2021-04-09 00:43:39 +08:00
|
|
|
if v.hasError {
|
|
|
|
|
require.Errorf(t, err, "%s failed, should have error", v.name)
|
|
|
|
|
continue
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
2021-04-09 00:43:39 +08:00
|
|
|
|
|
|
|
|
// No error case
|
|
|
|
|
require.NoErrorf(t, err, "%s failed, err: %v", v.name, err)
|
|
|
|
|
|
|
|
|
|
require.Len(t, acc.Metrics, len(v.output))
|
|
|
|
|
testutil.RequireMetricsEqual(t, acc.GetTelegrafMetrics(), v.output, testutil.IgnoreTime())
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
}
|