diff --git a/discovery/consul/consul_test.go b/discovery/consul/consul_test.go index 040040ae9..e3bc7938f 100644 --- a/discovery/consul/consul_test.go +++ b/discovery/consul/consul_test.go @@ -56,15 +56,11 @@ func TestConfiguredService(t *testing.T) { metrics := NewTestMetrics(t, conf, prometheus.NewRegistry()) consulDiscovery, err := NewDiscovery(conf, nil, metrics) - if err != nil { - t.Errorf("Unexpected error when initializing discovery %v", err) - } - if !consulDiscovery.shouldWatch("configuredServiceName", []string{""}) { - t.Errorf("Expected service %s to be watched", "configuredServiceName") - } - if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) { - t.Errorf("Expected service %s to not be watched", "nonConfiguredServiceName") - } + require.NoError(t, err, "when initializing discovery") + require.True(t, consulDiscovery.shouldWatch("configuredServiceName", []string{""}), + "Expected service %s to be watched", "configuredServiceName") + require.False(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}), + "Expected service %s to not be watched", "nonConfiguredServiceName") } func TestConfiguredServiceWithTag(t *testing.T) { @@ -76,21 +72,18 @@ func TestConfiguredServiceWithTag(t *testing.T) { metrics := NewTestMetrics(t, conf, prometheus.NewRegistry()) consulDiscovery, err := NewDiscovery(conf, nil, metrics) - if err != nil { - t.Errorf("Unexpected error when initializing discovery %v", err) - } - if consulDiscovery.shouldWatch("configuredServiceName", []string{""}) { - t.Errorf("Expected service %s to not be watched without tag", "configuredServiceName") - } - if !consulDiscovery.shouldWatch("configuredServiceName", []string{"http"}) { - t.Errorf("Expected service %s to be watched with tag %s", "configuredServiceName", "http") - } - if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) { - t.Errorf("Expected service %s to not be watched without tag", "nonConfiguredServiceName") - } - if consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{"http"}) { - t.Errorf("Expected service %s to not be watched with tag %s", "nonConfiguredServiceName", "http") - } + require.NoError(t, err, "when initializing discovery") + require.False(t, consulDiscovery.shouldWatch("configuredServiceName", []string{""}), + "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") + + require.False(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}), + "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") } func TestConfiguredServiceWithTags(t *testing.T) { @@ -173,13 +166,10 @@ func TestConfiguredServiceWithTags(t *testing.T) { metrics := NewTestMetrics(t, tc.conf, prometheus.NewRegistry()) consulDiscovery, err := NewDiscovery(tc.conf, nil, metrics) - if err != nil { - t.Errorf("Unexpected error when initializing discovery %v", err) - } + require.NoError(t, err, "when initializing discovery") ret := consulDiscovery.shouldWatch(tc.serviceName, tc.serviceTags) - if ret != tc.shouldWatch { - 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) - } + require.Equal(t, tc.shouldWatch, ret, "Watched service and tags: %s %+v, input was %s %+v", + tc.conf.Services, tc.conf.ServiceTags, tc.serviceName, tc.serviceTags) } } @@ -189,12 +179,8 @@ func TestNonConfiguredService(t *testing.T) { metrics := NewTestMetrics(t, conf, prometheus.NewRegistry()) consulDiscovery, err := NewDiscovery(conf, nil, metrics) - if err != nil { - t.Errorf("Unexpected error when initializing discovery %v", err) - } - if !consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}) { - t.Errorf("Expected service %s to be watched", "nonConfiguredServiceName") - } + require.NoError(t, err, "when initializing discovery") + require.True(t, consulDiscovery.shouldWatch("nonConfiguredServiceName", []string{""}), "Expected service %s to be watched", "nonConfiguredServiceName") } const ( @@ -502,13 +488,10 @@ oauth2: var config SDConfig err := config.UnmarshalYAML(unmarshal([]byte(test.config))) if err != nil { - require.Equalf(t, err.Error(), test.errMessage, "Expected error '%s', got '%v'", test.errMessage, err) - return - } - if test.errMessage != "" { - t.Errorf("Expected error %s, got none", test.errMessage) + require.EqualError(t, err, test.errMessage) return } + require.Empty(t, test.errMessage, "Expected error.") require.Equal(t, test.expected, config) }) diff --git a/discovery/file/file_test.go b/discovery/file/file_test.go index 8a5057089..521a3c0f1 100644 --- a/discovery/file/file_test.go +++ b/discovery/file/file_test.go @@ -358,9 +358,7 @@ func TestInvalidFile(t *testing.T) { // Verify that we've received nothing. time.Sleep(defaultWait) - if runner.lastReceive().After(now) { - t.Fatalf("unexpected targets received: %v", runner.targets()) - } + require.False(t, runner.lastReceive().After(now), "unexpected targets received: %v", runner.targets()) }) } } diff --git a/discovery/kubernetes/kubernetes_test.go b/discovery/kubernetes/kubernetes_test.go index 4071ebc34..f1f8070e8 100644 --- a/discovery/kubernetes/kubernetes_test.go +++ b/discovery/kubernetes/kubernetes_test.go @@ -131,14 +131,8 @@ func (d k8sDiscoveryTest) Run(t *testing.T) { go readResultWithTimeout(t, ch, d.expectedMaxItems, time.Second, resChan) dd, ok := d.discovery.(hasSynced) - if !ok { - t.Errorf("discoverer does not implement hasSynced interface") - return - } - if !cache.WaitForCacheSync(ctx.Done(), dd.hasSynced) { - t.Errorf("discoverer failed to sync: %v", dd) - return - } + require.True(t, ok, "discoverer does not implement hasSynced interface") + require.True(t, cache.WaitForCacheSync(ctx.Done(), dd.hasSynced), "discoverer failed to sync: %v", dd) if d.afterStart != nil { d.afterStart() diff --git a/discovery/manager_test.go b/discovery/manager_test.go index 2f6e03aa5..52159d94f 100644 --- a/discovery/manager_test.go +++ b/discovery/manager_test.go @@ -694,7 +694,7 @@ func TestTargetUpdatesOrder(t *testing.T) { for x := 0; x < totalUpdatesCount; x++ { select { 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: discoveryManager.updateGroup(poolKey{setName: strconv.Itoa(i), provider: tc.title}, tgs) 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) { t.Helper() - if _, ok := tSets[poolKey]; !ok { - t.Fatalf("'%s' should be present in Pool keys: %v", poolKey, tSets) - return - } + _, ok := tSets[poolKey] + require.True(t, ok, "'%s' should be present in Pool keys: %v", poolKey, tSets) match := false var mergedTargets string @@ -776,7 +774,7 @@ func verifyPresence(t *testing.T, tSets map[poolKey]map[string]*targetgroup.Grou if !present { 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() p = pk("static", "prometheus", 1) targetGroups, ok := discoveryManager.targets[p] - if !ok { - t.Fatalf("'%v' should be present in target groups", p) - } + require.True(t, ok, "'%v' should be present in target groups", p) group, ok := targetGroups[""] - if !ok { - t.Fatalf("missing '' key in target groups %v", targetGroups) - } + require.True(t, ok, "missing '' key in target groups %v", targetGroups) - if len(group.Targets) != 0 { - t.Fatalf("Invalid number of targets: expected 0, got %d", len(group.Targets)) - } + require.Empty(t, group.Targets, "Invalid number of targets.") require.Len(t, syncedTargets, 1) require.Len(t, syncedTargets["prometheus"], 1) - if lbls := syncedTargets["prometheus"][0].Labels; lbls != nil { - t.Fatalf("Unexpected Group: expected nil Labels, got %v", lbls) - } + require.Nil(t, syncedTargets["prometheus"][0].Labels) } func TestIdenticalConfigurationsAreCoalesced(t *testing.T) { @@ -1131,9 +1121,7 @@ func TestIdenticalConfigurationsAreCoalesced(t *testing.T) { syncedTargets := <-discoveryManager.SyncCh() verifyPresence(t, discoveryManager.targets, pk("static", "prometheus", 0), "{__address__=\"foo:9090\"}", true) verifyPresence(t, discoveryManager.targets, pk("static", "prometheus2", 0), "{__address__=\"foo:9090\"}", true) - if len(discoveryManager.providers) != 1 { - t.Fatalf("Invalid number of providers: expected 1, got %d", len(discoveryManager.providers)) - } + require.Len(t, discoveryManager.providers, 1, "Invalid number of providers.") require.Len(t, syncedTargets, 2) verifySyncedPresence(t, syncedTargets, "prometheus", "{__address__=\"foo:9090\"}", true) require.Len(t, syncedTargets["prometheus"], 1) @@ -1231,9 +1219,7 @@ func TestGaugeFailedConfigs(t *testing.T) { <-discoveryManager.SyncCh() failedCount := client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs) - if failedCount != 3 { - t.Fatalf("Expected to have 3 failed configs, got: %v", failedCount) - } + require.Equal(t, 3.0, failedCount, "Expected to have 3 failed configs.") c["prometheus"] = Configs{ staticConfig("foo:9090"), @@ -1242,9 +1228,7 @@ func TestGaugeFailedConfigs(t *testing.T) { <-discoveryManager.SyncCh() failedCount = client_testutil.ToFloat64(discoveryManager.metrics.FailedConfigs) - if failedCount != 0 { - t.Fatalf("Expected to get no failed config, got: %v", failedCount) - } + require.Equal(t, 0.0, failedCount, "Expected to get no failed config.") } func TestCoordinationWithReceiver(t *testing.T) { @@ -1388,19 +1372,14 @@ func TestCoordinationWithReceiver(t *testing.T) { time.Sleep(expected.delay) select { 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(): - if !ok { - t.Fatalf("step %d: discovery manager channel is closed", 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) - } + require.True(t, ok, "step %d: discovery manager channel is closed", i) + require.Equal(t, len(expected.tgs), len(tgs), "step %d: targets mismatch", i) + for k := range expected.tgs { - if _, ok := tgs[k]; !ok { - t.Fatalf("step %d: target group not found: %s\ngot: %#v", i, k, tgs) - } + _, ok := tgs[k] + require.True(t, ok, "step %d: target group not found: %s", i, k) assertEqualGroups(t, tgs[k], expected.tgs[k]) } } diff --git a/discovery/marathon/marathon_test.go b/discovery/marathon/marathon_test.go index c78cc1e3c..659899f16 100644 --- a/discovery/marathon/marathon_test.go +++ b/discovery/marathon/marathon_test.go @@ -69,23 +69,15 @@ func TestMarathonSDHandleError(t *testing.T) { } ) tgs, err := testUpdateServices(client) - if !errors.Is(err, errTesting) { - t.Fatalf("Expected error: %s", err) - } - if len(tgs) != 0 { - t.Fatalf("Got group: %s", tgs) - } + require.ErrorIs(t, err, errTesting) + require.Empty(t, tgs, "Expected no target groups.") } func TestMarathonSDEmptyList(t *testing.T) { client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return &appList{}, nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) > 0 { - t.Fatalf("Got group: %v", tgs) - } + require.NoError(t, err) + require.Empty(t, tgs, "Expected no target groups.") } func marathonTestAppList(labels map[string]string, runningTasks int) *appList { @@ -119,28 +111,16 @@ func TestMarathonSDSendGroup(t *testing.T) { return marathonTestAppList(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 1, "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] - if tgt[model.AddressLabel] != "mesos-slave1:31000" { - t.Fatalf("Wrong target address: %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]) - } + require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.") + require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.") } func TestMarathonSDRemoveApp(t *testing.T) { @@ -153,40 +133,27 @@ func TestMarathonSDRemoveApp(t *testing.T) { defer refreshMetrics.Unregister() md, err := NewDiscovery(cfg, nil, metrics) - if err != nil { - t.Fatalf("%s", err) - } + require.NoError(t, err) md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return marathonTestAppList(marathonValidLabel, 1), nil } tgs, err := md.refresh(context.Background()) - if err != nil { - t.Fatalf("Got error on first update: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 targetgroup, got", len(tgs)) - } + require.NoError(t, err, "Got error on first update.") + require.Len(t, tgs, 1, "Expected 1 targetgroup.") tg1 := tgs[0] md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return marathonTestAppList(marathonValidLabel, 0), nil } tgs, err = md.refresh(context.Background()) - if err != nil { - t.Fatalf("Got error on second update: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 targetgroup, got", len(tgs)) - } + require.NoError(t, err, "Got error on second update.") + require.Len(t, tgs, 1, "Expected 1 targetgroup.") + tg2 := tgs[0] - if tg2.Source != tg1.Source { - if len(tg2.Targets) > 0 { - t.Errorf("Got a non-empty target set: %s", tg2.Targets) - } - t.Fatalf("Source is different: %s != %s", tg1.Source, tg2.Source) - } + require.NotEmpty(t, tg2.Targets, "Got a non-empty target set.") + require.Equal(t, tg1.Source, tg2.Source, "Source is different.") } func marathonTestAppListWithMultiplePorts(labels map[string]string, runningTasks int) *appList { @@ -221,34 +188,22 @@ func TestMarathonSDSendGroupWithMultiplePort(t *testing.T) { return marathonTestAppListWithMultiplePorts(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:31000" { - t.Fatalf("Wrong target address: %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]) - } + require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.") + require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), + "Wrong portMappings label from the first port: %s", tgt[model.AddressLabel]) + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:32000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" { - t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel]) - } + require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.") + require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), + "Wrong portMappings label from the second port: %s", tgt[model.AddressLabel]) } func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *appList { @@ -278,20 +233,12 @@ func TestMarathonZeroTaskPorts(t *testing.T) { return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } - tg := tgs[0] + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") - if tg.Source != "test-service-zero-ports" { - t.Fatalf("Wrong target group name: %s", tg.Source) - } - if len(tg.Targets) != 0 { - t.Fatalf("Wrong number of targets: %v", tg.Targets) - } + tg := tgs[0] + require.Equal(t, "test-service-zero-ports", tg.Source, "Wrong target group name.") + require.Empty(t, tg.Targets, "Wrong number of targets.") } func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) { @@ -306,9 +253,7 @@ func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) { defer ts.Close() // Execute test case and validate behavior. _, err := testUpdateServices(nil) - if err == nil { - t.Fatalf("Expected error for 5xx HTTP response from marathon server, got nil") - } + require.Error(t, err, "Expected error for 5xx HTTP response from marathon server.") } func marathonTestAppListWithPortDefinitions(labels map[string]string, runningTasks int) *appList { @@ -346,40 +291,24 @@ func TestMarathonSDSendGroupWithPortDefinitions(t *testing.T) { return marathonTestAppListWithPortDefinitions(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:1234" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:1234", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:5678" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:5678", string(tgt[model.AddressLabel]), "Wrong target address.") + require.Empty(t, 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.") } func marathonTestAppListWithPortDefinitionsRequirePorts(labels map[string]string, runningTasks int) *appList { @@ -416,40 +345,22 @@ func TestMarathonSDSendGroupWithPortDefinitionsRequirePorts(t *testing.T) { return marathonTestAppListWithPortDefinitionsRequirePorts(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:31000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:32000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") } func marathonTestAppListWithPorts(labels map[string]string, runningTasks int) *appList { @@ -481,40 +392,22 @@ func TestMarathonSDSendGroupWithPorts(t *testing.T) { return marathonTestAppListWithPorts(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:31000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:32000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") } func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *appList { @@ -555,40 +448,22 @@ func TestMarathonSDSendGroupWithContainerPortMappings(t *testing.T) { return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:12345" { - t.Fatalf("Wrong target address: %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]) - } - if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" { - t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel]) - } + require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:32000" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") } func marathonTestAppListWithDockerContainerPortMappings(labels map[string]string, runningTasks int) *appList { @@ -629,40 +504,22 @@ func TestMarathonSDSendGroupWithDockerContainerPortMappings(t *testing.T) { return marathonTestAppListWithDockerContainerPortMappings(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "mesos-slave1:31000" { - t.Fatalf("Wrong target address: %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]) - } - if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" { - t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel]) - } + require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "mesos-slave1:12345" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") } func marathonTestAppListWithContainerNetworkAndPortMappings(labels map[string]string, runningTasks int) *appList { @@ -707,38 +564,20 @@ func TestMarathonSDSendGroupWithContainerNetworkAndPortMapping(t *testing.T) { return marathonTestAppListWithContainerNetworkAndPortMappings(marathonValidLabel, 1), nil } tgs, err := testUpdateServices(client) - if err != nil { - t.Fatalf("Got error: %s", err) - } - if len(tgs) != 1 { - t.Fatal("Expected 1 target group, got", len(tgs)) - } + require.NoError(t, err) + require.Len(t, tgs, 1, "Expected 1 target group.") + tg := tgs[0] + require.Equal(t, "test-service", tg.Source, "Wrong target group name.") + require.Len(t, tg.Targets, 2, "Wrong number of targets.") - 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] - if tgt[model.AddressLabel] != "1.2.3.4:8080" { - t.Fatalf("Wrong target address: %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]) - } - if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" { - t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel]) - } + require.Equal(t, "1.2.3.4:8080", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") + tgt = tg.Targets[1] - if tgt[model.AddressLabel] != "1.2.3.4:1234" { - t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel]) - } - 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]) - } + require.Equal(t, "1.2.3.4:1234", string(tgt[model.AddressLabel]), "Wrong target address.") + 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.") } diff --git a/discovery/openstack/mock_test.go b/discovery/openstack/mock_test.go index e279b0ca8..4aa871e11 100644 --- a/discovery/openstack/mock_test.go +++ b/discovery/openstack/mock_test.go @@ -18,6 +18,8 @@ import ( "net/http" "net/http/httptest" "testing" + + "github.com/stretchr/testify/require" ) // SDMock is the interface for the OpenStack mock. @@ -49,15 +51,13 @@ func (m *SDMock) Setup() { const tokenID = "cbc36478b0bd8e67e89469c7749d4127" func testMethod(t *testing.T, r *http.Request, expected string) { - if expected != r.Method { - t.Errorf("Request method = %v, expected %v", r.Method, expected) - } + require.Equal(t, expected, r.Method, "Unexpected request method.") } func testHeader(t *testing.T, r *http.Request, header, expected string) { - if actual := r.Header.Get(header); expected != actual { - t.Errorf("Header %s = %s, expected %s", header, actual, expected) - } + t.Helper() + actual := r.Header.Get(header) + require.Equal(t, expected, actual, "Unexpected value for request header %s.", header) } // HandleVersionsSuccessfully mocks version call. diff --git a/discovery/refresh/refresh_test.go b/discovery/refresh/refresh_test.go index 407f0a7fa..b70a32635 100644 --- a/discovery/refresh/refresh_test.go +++ b/discovery/refresh/refresh_test.go @@ -97,7 +97,7 @@ func TestRefresh(t *testing.T) { defer tick.Stop() select { case <-ch: - t.Fatal("Unexpected target group") + require.FailNow(t, "Unexpected target group") case <-tick.C: } } diff --git a/discovery/zookeeper/zookeeper_test.go b/discovery/zookeeper/zookeeper_test.go index d0e67b50a..c2b41ce7a 100644 --- a/discovery/zookeeper/zookeeper_test.go +++ b/discovery/zookeeper/zookeeper_test.go @@ -18,6 +18,7 @@ import ( "time" "github.com/prometheus/common/model" + "github.com/stretchr/testify/require" "go.uber.org/goleak" ) @@ -31,7 +32,5 @@ func TestNewDiscoveryError(t *testing.T) { time.Second, []string{"/"}, nil, func(data []byte, path string) (model.LabelSet, error) { return nil, nil }) - if err == nil { - t.Fatalf("expected error, got nil") - } + require.Error(t, err) }