discovery_test.go 6.4 KB
Newer Older
P
Patrick Bogen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kubernetes

import (
	"flag"
18
	"math/rand"
P
Patrick Bogen 已提交
19 20 21 22 23 24 25 26 27 28 29 30
	"os"
	"testing"

	_ "github.com/prometheus/common/log"
	"github.com/prometheus/common/model"
)

func TestMain(m *testing.M) {
	flag.Parse()
	os.Exit(m.Run())
}

31 32 33 34 35
var portsA = []ContainerPort{
	ContainerPort{
		Name:          "http",
		ContainerPort: 80,
		Protocol:      "TCP",
P
Patrick Bogen 已提交
36 37 38
	},
}

39 40 41 42 43
var portsB = []ContainerPort{
	ContainerPort{
		Name:          "https",
		ContainerPort: 443,
		Protocol:      "TCP",
P
Patrick Bogen 已提交
44 45 46
	},
}

47 48 49 50 51
var portsNoTcp = []ContainerPort{
	ContainerPort{
		Name:          "dns",
		ContainerPort: 53,
		Protocol:      "UDP",
P
Patrick Bogen 已提交
52 53 54
	},
}

55 56 57 58 59 60 61 62 63 64
var portsMultiA = []ContainerPort{
	ContainerPort{
		Name:          "http",
		ContainerPort: 80,
		Protocol:      "TCP",
	},
	ContainerPort{
		Name:          "ssh",
		ContainerPort: 22,
		Protocol:      "TCP",
P
Patrick Bogen 已提交
65 66 67
	},
}

68 69 70 71 72
var portsMultiB = []ContainerPort{
	ContainerPort{
		Name:          "http",
		ContainerPort: 80,
		Protocol:      "TCP",
P
Patrick Bogen 已提交
73
	},
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	ContainerPort{
		Name:          "https",
		ContainerPort: 443,
		Protocol:      "TCP",
	},
}

func container(name string, ports []ContainerPort) Container {
	p := make([]ContainerPort, len(ports))
	copy(p, ports)

	// Shuffle order of ports to ensure code enforces determinism
	for i := range p {
		j := rand.Intn(i + 1)
		p[i], p[j] = p[j], p[i]
	}

	return Container{
		Name:  name,
		Ports: p,
	}
P
Patrick Bogen 已提交
95 96 97
}

func pod(name string, containers []Container) *Pod {
98 99 100 101 102 103 104 105 106
	c := make([]Container, len(containers))
	copy(c, containers)

	// Shuffle order of containers to ensure code enforces determinism
	for i := range c {
		j := rand.Intn(i + 1)
		c[i], c[j] = c[j], c[i]
	}

P
Patrick Bogen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	return &Pod{
		ObjectMeta: ObjectMeta{
			Name: name,
		},
		PodStatus: PodStatus{
			PodIP: "1.1.1.1",
			Phase: "Running",
			Conditions: []PodCondition{
				PodCondition{
					Type:   "Ready",
					Status: "True",
				},
			},
		},
		PodSpec: PodSpec{
122
			Containers: c,
P
Patrick Bogen 已提交
123 124 125 126 127 128 129
		},
	}
}

func TestUpdatePodTargets(t *testing.T) {
	var result []model.LabelSet

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	// Multiple iterations help ensure that we'll see different permutations via the various randomizations that occur
	for i := 0; i < 100; i++ {
		// Return no targets for a pod that isn't "Running"
		result = updatePodTargets(&Pod{PodStatus: PodStatus{PodIP: "1.1.1.1"}}, true)
		if len(result) > 0 {
			t.Fatalf("expected 0 targets, received %d", len(result))
		}

		// Return no targets for a pod with no IP
		result = updatePodTargets(&Pod{PodStatus: PodStatus{Phase: "Running"}}, true)
		if len(result) > 0 {
			t.Fatalf("expected 0 targets, received %d", len(result))
		}

		// A pod with no containers (?!) should not produce any targets
		result = updatePodTargets(pod("empty", []Container{}), true)
		if len(result) > 0 {
			t.Fatalf("expected 0 targets, received %d", len(result))
		}

		// A pod with all valid containers should return one target per container with allContainers=true
		result = updatePodTargets(pod("easy", []Container{container("a", portsA), container("b", portsB)}), true)
		if len(result) != 2 {
			t.Fatalf("expected 2 targets, received %d", len(result))
		}
		if result[0][podReadyLabel] != "true" {
			t.Fatalf("expected result[0] podReadyLabel 'true', received '%s'", result[0][podReadyLabel])
		}
		if _, ok := result[0][podContainerPortMapPrefix+"http"]; !ok {
			t.Fatalf("expected result[0][podContainerPortMapPrefix + 'http'] to be '80', but was missing")
		}
		if result[0][podContainerPortMapPrefix+"http"] != "80" {
			t.Fatalf("expected result[0][podContainerPortMapPrefix + 'http'] to be '80', but was %s", result[0][podContainerPortMapPrefix+"http"])
		}
		if _, ok := result[1][podContainerPortMapPrefix+"https"]; !ok {
			t.Fatalf("expected result[1][podContainerPortMapPrefix + 'https'] to be '443', but was missing")
		}
		if result[1][podContainerPortMapPrefix+"https"] != "443" {
			t.Fatalf("expected result[1][podContainerPortMapPrefix + 'https'] to be '443', but was %s", result[1][podContainerPortMapPrefix+"https"])
		}

		// A pod with all valid containers should return one target with allContainers=false, and it should be the alphabetically first container
		result = updatePodTargets(pod("easy", []Container{container("a", portsA), container("b", portsB)}), false)
		if len(result) != 1 {
			t.Fatalf("expected 1 targets, received %d", len(result))
		}
		if _, ok := result[0][podContainerNameLabel]; !ok {
			t.Fatalf("expected result[0][podContainerNameLabel] to be 'a', but was missing")
		}
		if result[0][podContainerNameLabel] != "a" {
			t.Fatalf("expected result[0][podContainerNameLabel] to be 'a', but was '%s'", result[0][podContainerNameLabel])
		}

		// A pod with some non-targetable containers should return one target per targetable container with allContainers=true
		result = updatePodTargets(pod("mixed", []Container{container("a", portsA), container("no-tcp", portsNoTcp), container("b", portsB)}), true)
		if len(result) != 2 {
			t.Fatalf("expected 2 targets, received %d", len(result))
		}

		// A pod with a container with multiple ports should return the numerically smallest port
		result = updatePodTargets(pod("hard", []Container{container("multiA", portsMultiA), container("multiB", portsMultiB)}), true)
		if len(result) != 2 {
			t.Fatalf("expected 2 targets, received %d", len(result))
		}
		if result[0][model.AddressLabel] != "1.1.1.1:22" {
			t.Fatalf("expected result[0] address to be 1.1.1.1:22, received %s", result[0][model.AddressLabel])
		}
		if result[0][podContainerPortListLabel] != ",ssh=22,http=80," {
			t.Fatalf("expected result[0] podContainerPortListLabel to be ',ssh=22,http=80,', received '%s'", result[0][podContainerPortListLabel])
		}
		if result[1][model.AddressLabel] != "1.1.1.1:80" {
			t.Fatalf("expected result[1] address to be 1.1.1.1:80, received %s", result[1][model.AddressLabel])
		}
		if result[1][podContainerPortListLabel] != ",http=80,https=443," {
			t.Fatalf("expected result[1] podContainerPortListLabel to be ',http=80,https=443,', received '%s'", result[1][podContainerPortListLabel])
		}
P
Patrick Bogen 已提交
206 207
	}
}