mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Refactor discovery tests to use testify.
Signed-off-by: Paweł Szulik <paul.szulik@gmail.com>
This commit is contained in:
parent
34875ae8c7
commit
7f24efccdb
|
@ -56,15 +56,12 @@ func TestConfiguredService(t *testing.T) {
|
||||||
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
||||||
|
|
||||||
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
||||||
if err != nil {
|
require.NoError(t, err, "when initializing discovery")
|
||||||
t.Errorf("Unexpected error when initializing discovery %v", err)
|
require.True(t, consulDiscovery.shouldWatch("configuredServiceName", []string{""}),
|
||||||
}
|
"Expected service %s to be watched", "configuredServiceName")
|
||||||
if !consulDiscovery.shouldWatch("configuredServiceName", []string{""}) {
|
require.False(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}),
|
||||||
t.Errorf("Expected service %s to be watched", "configuredServiceName")
|
"Expected service %s to not be watched", "nonConfiguredServiceName")
|
||||||
}
|
|
||||||
if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) {
|
|
||||||
t.Errorf("Expected service %s to not be watched", "nonConfiguredServiceName")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfiguredServiceWithTag(t *testing.T) {
|
func TestConfiguredServiceWithTag(t *testing.T) {
|
||||||
|
@ -76,21 +73,18 @@ func TestConfiguredServiceWithTag(t *testing.T) {
|
||||||
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
||||||
|
|
||||||
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
||||||
if err != nil {
|
require.NoError(t, err, "when initializing discovery")
|
||||||
t.Errorf("Unexpected error when initializing discovery %v", err)
|
require.False(t, consulDiscovery.shouldWatch("configuredServiceName", []string{""}),
|
||||||
}
|
"Expected service %s to not be watched without tag", "configuredServiceName")
|
||||||
if consulDiscovery.shouldWatch("configuredServiceName", []string{""}) {
|
|
||||||
t.Errorf("Expected service %s to not be watched without tag", "configuredServiceName")
|
require.True(t, consulDiscovery.shouldWatch("configuredServiceName", []string{"http"}),
|
||||||
}
|
"Expected service %s to be watched with tag %s", "configuredServiceName", "http")
|
||||||
if !consulDiscovery.shouldWatch("configuredServiceName", []string{"http"}) {
|
|
||||||
t.Errorf("Expected service %s to be watched with tag %s", "configuredServiceName", "http")
|
require.False(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}),
|
||||||
}
|
"Expected service %s to not be watched without tag", "nonConfiguredServiceName")
|
||||||
if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) {
|
|
||||||
t.Errorf("Expected service %s to not be watched without tag", "nonConfiguredServiceName")
|
require.False(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{"http"}),
|
||||||
}
|
"Expected service %s to not be watched with tag %s", "nonConfiguredServiceName", "http")
|
||||||
if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{"http"}) {
|
|
||||||
t.Errorf("Expected service %s to not be watched with tag %s", "nonConfiguredServiceName", "http")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfiguredServiceWithTags(t *testing.T) {
|
func TestConfiguredServiceWithTags(t *testing.T) {
|
||||||
|
@ -173,13 +167,10 @@ func TestConfiguredServiceWithTags(t *testing.T) {
|
||||||
metrics := NewTestMetrics(t, tc.conf, prometheus.NewRegistry())
|
metrics := NewTestMetrics(t, tc.conf, prometheus.NewRegistry())
|
||||||
|
|
||||||
consulDiscovery, err := NewDiscovery(tc.conf, nil, metrics)
|
consulDiscovery, err := NewDiscovery(tc.conf, nil, metrics)
|
||||||
if err != nil {
|
require.NoError(t, err, "when initializing discovery")
|
||||||
t.Errorf("Unexpected error when initializing discovery %v", err)
|
|
||||||
}
|
|
||||||
ret := consulDiscovery.shouldWatch(tc.serviceName, tc.serviceTags)
|
ret := consulDiscovery.shouldWatch(tc.serviceName, tc.serviceTags)
|
||||||
if ret != tc.shouldWatch {
|
require.Equal(t, tc.shouldWatch, ret, "Watched service and tags: %s %+v, input was %s %+v",
|
||||||
t.Errorf("Expected should watch? %t, got %t. Watched service and tags: %s %+v, input was %s %+v", tc.shouldWatch, ret, tc.conf.Services, tc.conf.ServiceTags, tc.serviceName, tc.serviceTags)
|
tc.conf.Services, tc.conf.ServiceTags, tc.serviceName, tc.serviceTags)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,12 +180,8 @@ func TestNonConfiguredService(t *testing.T) {
|
||||||
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
metrics := NewTestMetrics(t, conf, prometheus.NewRegistry())
|
||||||
|
|
||||||
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
consulDiscovery, err := NewDiscovery(conf, nil, metrics)
|
||||||
if err != nil {
|
require.NoError(t, err, "when initializing discovery")
|
||||||
t.Errorf("Unexpected error when initializing discovery %v", err)
|
require.True(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}), "Expected service %s to be watched", "nonConfiguredServiceName")
|
||||||
}
|
|
||||||
if !consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) {
|
|
||||||
t.Errorf("Expected service %s to be watched", "nonConfiguredServiceName")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -267,7 +254,7 @@ func newServer(t *testing.T) (*httptest.Server, *SDConfig) {
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
response = ServicesTestAnswer
|
response = ServicesTestAnswer
|
||||||
default:
|
default:
|
||||||
t.Errorf("Unhandled consul call: %s", r.URL)
|
require.FailNow(t, "Unhandled consul call: %s", r.URL)
|
||||||
}
|
}
|
||||||
w.Header().Add("X-Consul-Index", "1")
|
w.Header().Add("X-Consul-Index", "1")
|
||||||
w.Write([]byte(response))
|
w.Write([]byte(response))
|
||||||
|
@ -502,13 +489,10 @@ oauth2:
|
||||||
var config SDConfig
|
var config SDConfig
|
||||||
err := config.UnmarshalYAML(unmarshal([]byte(test.config)))
|
err := config.UnmarshalYAML(unmarshal([]byte(test.config)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
require.Equalf(t, err.Error(), test.errMessage, "Expected error '%s', got '%v'", test.errMessage, err)
|
require.EqualError(t, err, test.errMessage)
|
||||||
return
|
|
||||||
}
|
|
||||||
if test.errMessage != "" {
|
|
||||||
t.Errorf("Expected error %s, got none", test.errMessage)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
require.Empty(t, test.errMessage, "Expected error.")
|
||||||
|
|
||||||
require.Equal(t, test.expected, config)
|
require.Equal(t, test.expected, config)
|
||||||
})
|
})
|
||||||
|
|
|
@ -358,9 +358,7 @@ func TestInvalidFile(t *testing.T) {
|
||||||
|
|
||||||
// Verify that we've received nothing.
|
// Verify that we've received nothing.
|
||||||
time.Sleep(defaultWait)
|
time.Sleep(defaultWait)
|
||||||
if runner.lastReceive().After(now) {
|
require.False(t, runner.lastReceive().After(now), "Unexpected targets received.")
|
||||||
t.Fatalf("unexpected targets received: %v", runner.targets())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,14 +131,8 @@ func (d k8sDiscoveryTest) Run(t *testing.T) {
|
||||||
go readResultWithTimeout(t, ch, d.expectedMaxItems, time.Second, resChan)
|
go readResultWithTimeout(t, ch, d.expectedMaxItems, time.Second, resChan)
|
||||||
|
|
||||||
dd, ok := d.discovery.(hasSynced)
|
dd, ok := d.discovery.(hasSynced)
|
||||||
if !ok {
|
require.True(t, ok, "discoverer does not implement hasSynced interface")
|
||||||
t.Errorf("discoverer does not implement hasSynced interface")
|
require.True(t, cache.WaitForCacheSync(ctx.Done(), dd.hasSynced), "discoverer failed to sync: %v", dd)
|
||||||
return
|
|
||||||
}
|
|
||||||
if !cache.WaitForCacheSync(ctx.Done(), dd.hasSynced) {
|
|
||||||
t.Errorf("discoverer failed to sync: %v", dd)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.afterStart != nil {
|
if d.afterStart != nil {
|
||||||
d.afterStart()
|
d.afterStart()
|
||||||
|
|
|
@ -694,7 +694,7 @@ func TestTargetUpdatesOrder(t *testing.T) {
|
||||||
for x := 0; x < totalUpdatesCount; x++ {
|
for x := 0; x < totalUpdatesCount; x++ {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
t.Fatalf("%d: no update arrived within the timeout limit", x)
|
require.FailNow(t, "%d: no update arrived within the timeout limit", x)
|
||||||
case tgs := <-provUpdates:
|
case tgs := <-provUpdates:
|
||||||
discoveryManager.updateGroup(poolKey{setName: strconv.Itoa(i), provider: tc.title}, tgs)
|
discoveryManager.updateGroup(poolKey{setName: strconv.Itoa(i), provider: tc.title}, tgs)
|
||||||
for _, got := range discoveryManager.allGroups() {
|
for _, got := range discoveryManager.allGroups() {
|
||||||
|
@ -756,10 +756,8 @@ func verifySyncedPresence(t *testing.T, tGroups map[string][]*targetgroup.Group,
|
||||||
|
|
||||||
func verifyPresence(t *testing.T, tSets map[poolKey]map[string]*targetgroup.Group, poolKey poolKey, label string, present bool) {
|
func verifyPresence(t *testing.T, tSets map[poolKey]map[string]*targetgroup.Group, poolKey poolKey, label string, present bool) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if _, ok := tSets[poolKey]; !ok {
|
_, ok := tSets[poolKey]
|
||||||
t.Fatalf("'%s' should be present in Pool keys: %v", poolKey, tSets)
|
require.True(t, ok, "'%s' should be present in Pool keys: %v", poolKey, tSets)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
match := false
|
match := false
|
||||||
var mergedTargets string
|
var mergedTargets string
|
||||||
|
@ -776,7 +774,7 @@ func verifyPresence(t *testing.T, tSets map[poolKey]map[string]*targetgroup.Grou
|
||||||
if !present {
|
if !present {
|
||||||
msg = "not"
|
msg = "not"
|
||||||
}
|
}
|
||||||
t.Fatalf("%q should %s be present in Targets labels: %q", label, msg, mergedTargets)
|
require.FailNow(t, "%q should %s be present in Targets labels: %q", label, msg, mergedTargets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1088,22 +1086,14 @@ func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) {
|
||||||
syncedTargets = <-discoveryManager.SyncCh()
|
syncedTargets = <-discoveryManager.SyncCh()
|
||||||
p = pk("static", "prometheus", 1)
|
p = pk("static", "prometheus", 1)
|
||||||
targetGroups, ok := discoveryManager.targets[p]
|
targetGroups, ok := discoveryManager.targets[p]
|
||||||
if !ok {
|
require.True(t, ok, "'%v' should be present in target groups", p)
|
||||||
t.Fatalf("'%v' should be present in target groups", p)
|
|
||||||
}
|
|
||||||
group, ok := targetGroups[""]
|
group, ok := targetGroups[""]
|
||||||
if !ok {
|
require.True(t, ok, "missing '' key in target groups %v", targetGroups)
|
||||||
t.Fatalf("missing '' key in target groups %v", targetGroups)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(group.Targets) != 0 {
|
require.Empty(t, group.Targets, "Invalid number of targets.")
|
||||||
t.Fatalf("Invalid number of targets: expected 0, got %d", len(group.Targets))
|
|
||||||
}
|
|
||||||
require.Len(t, syncedTargets, 1)
|
require.Len(t, syncedTargets, 1)
|
||||||
require.Len(t, syncedTargets["prometheus"], 1)
|
require.Len(t, syncedTargets["prometheus"], 1)
|
||||||
if lbls := syncedTargets["prometheus"][0].Labels; lbls != nil {
|
require.Nil(t, syncedTargets["prometheus"][0].Labels)
|
||||||
t.Fatalf("Unexpected Group: expected nil Labels, got %v", lbls)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {
|
func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {
|
||||||
|
@ -1131,9 +1121,7 @@ func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {
|
||||||
syncedTargets := <-discoveryManager.SyncCh()
|
syncedTargets := <-discoveryManager.SyncCh()
|
||||||
verifyPresence(t, discoveryManager.targets, pk("static", "prometheus", 0), "{__address__=\"foo:9090\"}", true)
|
verifyPresence(t, discoveryManager.targets, pk("static", "prometheus", 0), "{__address__=\"foo:9090\"}", true)
|
||||||
verifyPresence(t, discoveryManager.targets, pk("static", "prometheus2", 0), "{__address__=\"foo:9090\"}", true)
|
verifyPresence(t, discoveryManager.targets, pk("static", "prometheus2", 0), "{__address__=\"foo:9090\"}", true)
|
||||||
if len(discoveryManager.providers) != 1 {
|
require.Len(t, discoveryManager.providers, 1, "Invalid number of providers.")
|
||||||
t.Fatalf("Invalid number of providers: expected 1, got %d", len(discoveryManager.providers))
|
|
||||||
}
|
|
||||||
require.Len(t, syncedTargets, 2)
|
require.Len(t, syncedTargets, 2)
|
||||||
verifySyncedPresence(t, syncedTargets, "prometheus", "{__address__=\"foo:9090\"}", true)
|
verifySyncedPresence(t, syncedTargets, "prometheus", "{__address__=\"foo:9090\"}", true)
|
||||||
require.Len(t, syncedTargets["prometheus"], 1)
|
require.Len(t, syncedTargets["prometheus"], 1)
|
||||||
|
@ -1231,9 +1219,7 @@ func TestGaugeFailedConfigs(t *testing.T) {
|
||||||
<-discoveryManager.SyncCh()
|
<-discoveryManager.SyncCh()
|
||||||
|
|
||||||
failedCount := client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs)
|
failedCount := client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs)
|
||||||
if failedCount != 3 {
|
require.Equal(t, 3.0, failedCount, "Expected to have 3 failed configs.")
|
||||||
t.Fatalf("Expected to have 3 failed configs, got: %v", failedCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
c["prometheus"] = Configs{
|
c["prometheus"] = Configs{
|
||||||
staticConfig("foo:9090"),
|
staticConfig("foo:9090"),
|
||||||
|
@ -1242,9 +1228,7 @@ func TestGaugeFailedConfigs(t *testing.T) {
|
||||||
<-discoveryManager.SyncCh()
|
<-discoveryManager.SyncCh()
|
||||||
|
|
||||||
failedCount = client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs)
|
failedCount = client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs)
|
||||||
if failedCount != 0 {
|
require.Equal(t, 0.0, failedCount, "Expected to get no failed config.")
|
||||||
t.Fatalf("Expected to get no failed config, got: %v", failedCount)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCoordinationWithReceiver(t *testing.T) {
|
func TestCoordinationWithReceiver(t *testing.T) {
|
||||||
|
@ -1388,19 +1372,14 @@ func TestCoordinationWithReceiver(t *testing.T) {
|
||||||
time.Sleep(expected.delay)
|
time.Sleep(expected.delay)
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
t.Fatalf("step %d: no update received in the expected timeframe", i)
|
require.FailNow(t, "step %d: no update received in the expected timeframe", i)
|
||||||
case tgs, ok := <-mgr.SyncCh():
|
case tgs, ok := <-mgr.SyncCh():
|
||||||
if !ok {
|
require.True(t, ok, "step %d: discovery manager channel is closed", i)
|
||||||
t.Fatalf("step %d: discovery manager channel is closed", i)
|
require.Equal(t, len(expected.tgs), len(tgs), "step %d: targets mismatch", i)
|
||||||
}
|
|
||||||
if len(tgs) != len(expected.tgs) {
|
|
||||||
t.Fatalf("step %d: target groups mismatch, got: %d, expected: %d\ngot: %#v\nexpected: %#v",
|
|
||||||
i, len(tgs), len(expected.tgs), tgs, expected.tgs)
|
|
||||||
}
|
|
||||||
for k := range expected.tgs {
|
for k := range expected.tgs {
|
||||||
if _, ok := tgs[k]; !ok {
|
_, ok := tgs[k]
|
||||||
t.Fatalf("step %d: target group not found: %s\ngot: %#v", i, k, tgs)
|
require.True(t, ok, "step %d: target group not found: %s", i, k)
|
||||||
}
|
|
||||||
assertEqualGroups(t, tgs[k], expected.tgs[k])
|
assertEqualGroups(t, tgs[k], expected.tgs[k])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,23 +69,15 @@ func TestMarathonSDHandleError(t *testing.T) {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if !errors.Is(err, errTesting) {
|
require.ErrorIs(t, err, errTesting)
|
||||||
t.Fatalf("Expected error: %s", err)
|
require.Empty(t, tgs, "Expected no target groups.")
|
||||||
}
|
|
||||||
if len(tgs) != 0 {
|
|
||||||
t.Fatalf("Got group: %s", tgs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarathonSDEmptyList(t *testing.T) {
|
func TestMarathonSDEmptyList(t *testing.T) {
|
||||||
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return &appList{}, nil }
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return &appList{}, nil }
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Empty(t, tgs, "Expected no target groups.")
|
||||||
}
|
|
||||||
if len(tgs) > 0 {
|
|
||||||
t.Fatalf("Got group: %v", tgs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppList(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppList(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -119,28 +111,16 @@ func TestMarathonSDSendGroup(t *testing.T) {
|
||||||
return marathonTestAppList(marathonValidLabel, 1), nil
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
|
||||||
}
|
|
||||||
|
|
||||||
tg := tgs[0]
|
tg := tgs[0]
|
||||||
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
|
require.Equal(t, 1, len(tg.Targets), "Expected 1 target.")
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 1 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarathonSDRemoveApp(t *testing.T) {
|
func TestMarathonSDRemoveApp(t *testing.T) {
|
||||||
|
@ -153,40 +133,27 @@ func TestMarathonSDRemoveApp(t *testing.T) {
|
||||||
defer refreshMetrics.Unregister()
|
defer refreshMetrics.Unregister()
|
||||||
|
|
||||||
md, err := NewDiscovery(cfg, nil, metrics)
|
md, err := NewDiscovery(cfg, nil, metrics)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("%s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
||||||
return marathonTestAppList(marathonValidLabel, 1), nil
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := md.refresh(context.Background())
|
tgs, err := md.refresh(context.Background())
|
||||||
if err != nil {
|
require.NoError(t, err, "Got error on first update.")
|
||||||
t.Fatalf("Got error on first update: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 targetgroup.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
|
||||||
t.Fatal("Expected 1 targetgroup, got", len(tgs))
|
|
||||||
}
|
|
||||||
tg1 := tgs[0]
|
tg1 := tgs[0]
|
||||||
|
|
||||||
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
||||||
return marathonTestAppList(marathonValidLabel, 0), nil
|
return marathonTestAppList(marathonValidLabel, 0), nil
|
||||||
}
|
}
|
||||||
tgs, err = md.refresh(context.Background())
|
tgs, err = md.refresh(context.Background())
|
||||||
if err != nil {
|
require.NoError(t, err, "Got error on second update.")
|
||||||
t.Fatalf("Got error on second update: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 targetgroup.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
|
||||||
t.Fatal("Expected 1 targetgroup, got", len(tgs))
|
|
||||||
}
|
|
||||||
tg2 := tgs[0]
|
tg2 := tgs[0]
|
||||||
|
|
||||||
if tg2.Source != tg1.Source {
|
require.Equal(t, tg1.Source, tg2.Source, "Source is different.")
|
||||||
if len(tg2.Targets) > 0 {
|
require.NotEmpty(t, tg2.Targets, "Got a non-empty target set.")
|
||||||
t.Errorf("Got a non-empty target set: %s", tg2.Targets)
|
|
||||||
}
|
|
||||||
t.Fatalf("Source is different: %s != %s", tg1.Source, tg2.Source)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithMultiplePorts(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithMultiplePorts(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -221,34 +188,22 @@ func TestMarathonSDSendGroupWithMultiplePort(t *testing.T) {
|
||||||
return marathonTestAppListWithMultiplePorts(marathonValidLabel, 1), nil
|
return marathonTestAppListWithMultiplePorts(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
||||||
}
|
"Wrong portMappings label from the first port: %s", tgt[model.AddressLabel])
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
||||||
}
|
"Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *appList {
|
func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -278,20 +233,12 @@ func TestMarathonZeroTaskPorts(t *testing.T) {
|
||||||
return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil
|
return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
|
||||||
}
|
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service-zero-ports" {
|
tg := tgs[0]
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
require.Equal(t, "test-service-zero-ports", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Empty(t, tg.Targets, "Wrong number of targets.")
|
||||||
if len(tg.Targets) != 0 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
|
func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
|
||||||
|
@ -306,9 +253,7 @@ func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
// Execute test case and validate behavior.
|
// Execute test case and validate behavior.
|
||||||
_, err := testUpdateServices(nil)
|
_, err := testUpdateServices(nil)
|
||||||
if err == nil {
|
require.Error(t, err, "Expected error for 5xx HTTP response from marathon server.")
|
||||||
t.Fatalf("Expected error for 5xx HTTP response from marathon server, got nil")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithPortDefinitions(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithPortDefinitions(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -346,40 +291,24 @@ func TestMarathonSDSendGroupWithPortDefinitions(t *testing.T) {
|
||||||
return marathonTestAppListWithPortDefinitions(marathonValidLabel, 1), nil
|
return marathonTestAppListWithPortDefinitions(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:1234" {
|
require.Equal(t, "mesos-slave1:1234", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
||||||
}
|
"Wrong portMappings label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]),
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
"Wrong portDefinitions label from the first port.")
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:5678" {
|
require.Equal(t, "mesos-slave1:5678", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "yes", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithPortDefinitionsRequirePorts(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithPortDefinitionsRequirePorts(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -416,40 +345,22 @@ func TestMarathonSDSendGroupWithPortDefinitionsRequirePorts(t *testing.T) {
|
||||||
return marathonTestAppListWithPortDefinitionsRequirePorts(marathonValidLabel, 1), nil
|
return marathonTestAppListWithPortDefinitionsRequirePorts(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "yes", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithPorts(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithPorts(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -481,40 +392,22 @@ func TestMarathonSDSendGroupWithPorts(t *testing.T) {
|
||||||
return marathonTestAppListWithPorts(marathonValidLabel, 1), nil
|
return marathonTestAppListWithPorts(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -555,40 +448,22 @@ func TestMarathonSDSendGroupWithContainerPortMappings(t *testing.T) {
|
||||||
return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil
|
return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:12345" {
|
require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithDockerContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithDockerContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -629,40 +504,22 @@ func TestMarathonSDSendGroupWithDockerContainerPortMappings(t *testing.T) {
|
||||||
return marathonTestAppListWithDockerContainerPortMappings(marathonValidLabel, 1), nil
|
return marathonTestAppListWithDockerContainerPortMappings(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "mesos-slave1:12345" {
|
require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marathonTestAppListWithContainerNetworkAndPortMappings(labels map[string]string, runningTasks int) *appList {
|
func marathonTestAppListWithContainerNetworkAndPortMappings(labels map[string]string, runningTasks int) *appList {
|
||||||
|
@ -707,38 +564,20 @@ func TestMarathonSDSendGroupWithContainerNetworkAndPortMapping(t *testing.T) {
|
||||||
return marathonTestAppListWithContainerNetworkAndPortMappings(marathonValidLabel, 1), nil
|
return marathonTestAppListWithContainerNetworkAndPortMappings(marathonValidLabel, 1), nil
|
||||||
}
|
}
|
||||||
tgs, err := testUpdateServices(client)
|
tgs, err := testUpdateServices(client)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Got error: %s", err)
|
require.Equal(t, 1, len(tgs), "Expected 1 target group.")
|
||||||
}
|
|
||||||
if len(tgs) != 1 {
|
tg := tgs[0]
|
||||||
t.Fatal("Expected 1 target group, got", len(tgs))
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
||||||
}
|
require.Equal(t, 2, len(tg.Targets), "Wrong number of targets.")
|
||||||
tg := tgs[0]
|
|
||||||
|
|
||||||
if tg.Source != "test-service" {
|
|
||||||
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
||||||
}
|
|
||||||
if len(tg.Targets) != 2 {
|
|
||||||
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
||||||
}
|
|
||||||
tgt := tg.Targets[0]
|
tgt := tg.Targets[0]
|
||||||
if tgt[model.AddressLabel] != "1.2.3.4:8080" {
|
require.Equal(t, "1.2.3.4:8080", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
||||||
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
tgt = tg.Targets[1]
|
tgt = tg.Targets[1]
|
||||||
if tgt[model.AddressLabel] != "1.2.3.4:1234" {
|
require.Equal(t, "1.2.3.4:1234", string(tgt[model.AddressLabel]), "Wrong target address.")
|
||||||
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
||||||
}
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
||||||
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
||||||
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SDMock is the interface for the OpenStack mock.
|
// SDMock is the interface for the OpenStack mock.
|
||||||
|
@ -49,15 +51,13 @@ func (m *SDMock) Setup() {
|
||||||
const tokenID = "cbc36478b0bd8e67e89469c7749d4127"
|
const tokenID = "cbc36478b0bd8e67e89469c7749d4127"
|
||||||
|
|
||||||
func testMethod(t *testing.T, r *http.Request, expected string) {
|
func testMethod(t *testing.T, r *http.Request, expected string) {
|
||||||
if expected != r.Method {
|
require.Equal(t, expected, r.Method, "Unexpected request method.")
|
||||||
t.Errorf("Request method = %v, expected %v", r.Method, expected)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHeader(t *testing.T, r *http.Request, header, expected string) {
|
func testHeader(t *testing.T, r *http.Request, header, expected string) {
|
||||||
if actual := r.Header.Get(header); expected != actual {
|
t.Helper()
|
||||||
t.Errorf("Header %s = %s, expected %s", header, actual, expected)
|
actual := r.Header.Get(header)
|
||||||
}
|
require.Equal(t, expected, actual, "Unexpected value for request header %s.", header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleVersionsSuccessfully mocks version call.
|
// HandleVersionsSuccessfully mocks version call.
|
||||||
|
|
|
@ -97,7 +97,7 @@ func TestRefresh(t *testing.T) {
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
select {
|
select {
|
||||||
case <-ch:
|
case <-ch:
|
||||||
t.Fatal("Unexpected target group")
|
require.FailNow(t, "Unexpected target group")
|
||||||
case <-tick.C:
|
case <-tick.C:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/goleak"
|
"go.uber.org/goleak"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,7 +32,5 @@ func TestNewDiscoveryError(t *testing.T) {
|
||||||
time.Second, []string{"/"},
|
time.Second, []string{"/"},
|
||||||
nil,
|
nil,
|
||||||
func(data []byte, path string) (model.LabelSet, error) { return nil, nil })
|
func(data []byte, path string) (model.LabelSet, error) { return nil, nil })
|
||||||
if err == nil {
|
require.Error(t, err)
|
||||||
t.Fatalf("expected error, got nil")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue