JFIF ( %!1"%)-...383.7(-.+  -%&--------------------------------------------------"J !1"AQaq2BR#r3Sbs4T$Dd(!1"2AQaq# ?q& JX"-` Es?Bl 1( H6fX[vʆEiB!j{hu85o%TI/*T `WTXط8%ɀt*$PaSIa9gkG$t h&)ٞ)O.4uCm!w*:K*I&bDl"+ ӹ=<Ӷ|FtI{7_/,/T ̫ԷC ȷMq9[1w!R{ U<?СCԀdc8'124,I'3-G s4IcWq$Ro瓩!"j']VӤ'B4H8n)iv$Hb=B:B=YݚXZILcA g$ΕzuPD? !զIEÁ $D'l"gp`+6֏$1Ľ˫EjUpܣvDت\2Wڰ_iIْ/~'cŧE:ɝBn9&rt,H`*Tf֙LK$#d "p/n$J oJ@'I0B+NRwj2GH.BWLOiGP W@#"@ę| 2@P D2[Vj!VE11pHn,c~T;U"H㤑EBxHClTZ7:х5,w=.`,:Lt1tE9""@pȠb\I_IƝpe &܏/ 3, WE2aDK &cy(3nI7'0W էΠ\&@:נ!oZIܻ1j@=So LJ{5UĜiʒP H{^iaH?U2j@<'13nXkdP&%ɰ&-(<]Vlya7 6c1HJcmǸ!˗GB3Ԏߏ\=qIPNĉA)JeJtEJbIxWbdóT V'0 WH*|D u6ӈHZh[8e  $v>p!rIWeB,i '佧 )g#[)m!tahm_<6nL/ BcT{"HSfp7|ybi8'.ih%,wm  403WebShell
403Webshell
Server IP : 88.223.91.99  /  Your IP : 216.73.216.254
Web Server : LiteSpeed
System : Linux id-dci-web1986.main-hosting.eu 5.14.0-611.26.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jan 29 05:24:47 EST 2026 x86_64
User : u686484674 ( 686484674)
PHP Version : 8.0.30
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /proc/./self/root/opt/go/pkg/mod/github.com/prometheus/common@v0.61.0/model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /proc/./self/root/opt/go/pkg/mod/github.com/prometheus/common@v0.61.0/model/alert_test.go
// Copyright 2013 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 model

import (
	"fmt"
	"sort"
	"strings"
	"testing"
	"time"
)

func TestAlertValidate(t *testing.T) {
	ts := time.Now()

	cases := []struct {
		alert *Alert
		err   string
	}{
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b"},
				StartsAt: ts,
			},
		},
		{
			alert: &Alert{
				Labels: LabelSet{"a": "b"},
			},
			err: "start time missing",
		},
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b"},
				StartsAt: ts,
				EndsAt:   ts,
			},
		},
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b"},
				StartsAt: ts,
				EndsAt:   ts.Add(1 * time.Minute),
			},
		},
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b"},
				StartsAt: ts,
				EndsAt:   ts.Add(-1 * time.Minute),
			},
			err: "start time must be before end time",
		},
		{
			alert: &Alert{
				StartsAt: ts,
			},
			err: "at least one label pair required",
		},
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b", "!bad": "label"},
				StartsAt: ts,
			},
			err: "invalid label set: invalid name",
		},
		{
			alert: &Alert{
				Labels:   LabelSet{"a": "b", "bad": "\xfflabel"},
				StartsAt: ts,
			},
			err: "invalid label set: invalid value",
		},
		{
			alert: &Alert{
				Labels:      LabelSet{"a": "b"},
				Annotations: LabelSet{"!bad": "label"},
				StartsAt:    ts,
			},
			err: "invalid annotations: invalid name",
		},
		{
			alert: &Alert{
				Labels:      LabelSet{"a": "b"},
				Annotations: LabelSet{"bad": "\xfflabel"},
				StartsAt:    ts,
			},
			err: "invalid annotations: invalid value",
		},
	}

	for i, c := range cases {
		err := c.alert.Validate()
		if err == nil {
			if c.err == "" {
				continue
			}
			t.Errorf("%d. Expected error %q but got none", i, c.err)
			continue
		}
		if c.err == "" {
			t.Errorf("%d. Expected no error but got %q", i, err)
			continue
		}
		if !strings.Contains(err.Error(), c.err) {
			t.Errorf("%d. Expected error to contain %q but got %q", i, c.err, err)
		}
	}
}

func TestAlert(t *testing.T) {
	// Verifying that an alert with no EndsAt field is unresolved and has firing status.
	alert := &Alert{
		Labels:   LabelSet{"foo": "bar", "lorem": "ipsum"},
		StartsAt: time.Now(),
	}

	actual := fmt.Sprint(alert)
	expected := "[d181d0f][active]"

	if actual != expected {
		t.Errorf("expected %s, but got %s", expected, actual)
	}

	actualStatus := alert.Status()
	expectedStatus := AlertStatus("firing")

	if actualStatus != expectedStatus {
		t.Errorf("expected alertStatus %s, but got %s", expectedStatus, actualStatus)
	}

	// Verifying that an alert with an EndsAt time before the current time is resolved and has resolved status.
	ts := time.Now()
	ts1 := ts.Add(-2 * time.Minute)
	ts2 := ts.Add(-1 * time.Minute)
	alert = &Alert{
		Labels:   LabelSet{"foo": "bar", "lorem": "ipsum"},
		StartsAt: ts1,
		EndsAt:   ts2,
	}

	if !alert.Resolved() {
		t.Error("expected alert to be resolved, but it was not")
	}

	actual = fmt.Sprint(alert)
	expected = "[d181d0f][resolved]"

	if actual != expected {
		t.Errorf("expected %s, but got %s", expected, actual)
	}

	actualStatus = alert.Status()
	expectedStatus = "resolved"

	if actualStatus != expectedStatus {
		t.Errorf("expected alertStatus %s, but got %s", expectedStatus, actualStatus)
	}

	// Verifying that ResolvedAt works for different times
	if alert.ResolvedAt(ts1) {
		t.Error("unexpected alert was resolved at start time")
	}
	if alert.ResolvedAt(ts2.Add(-time.Millisecond)) {
		t.Error("unexpected alert was resolved before it ended")
	}
	if !alert.ResolvedAt(ts2) {
		t.Error("expected alert to be resolved at end time")
	}
	if !alert.ResolvedAt(ts2.Add(time.Millisecond)) {
		t.Error("expected alert to be resolved after it ended")
	}

	// Verifying that StatusAt works for different times
	actualStatus = alert.StatusAt(ts1)
	if actualStatus != "firing" {
		t.Errorf("expected alert to be firing at start time, but got %s", actualStatus)
	}
	actualStatus = alert.StatusAt(ts1.Add(-time.Millisecond))
	if actualStatus != "firing" {
		t.Errorf("expected alert to be firing before it ended, but got %s", actualStatus)
	}
	actualStatus = alert.StatusAt(ts2)
	if actualStatus != "resolved" {
		t.Errorf("expected alert to be resolved at end time, but got %s", actualStatus)
	}
	actualStatus = alert.StatusAt(ts2.Add(time.Millisecond))
	if actualStatus != "resolved" {
		t.Errorf("expected alert to be resolved after it ended, but got %s", actualStatus)
	}
}

func TestSortAlerts(t *testing.T) {
	ts := time.Now()
	alerts := Alerts{
		{
			Labels: LabelSet{
				"alertname": "InternalError",
				"dev":       "sda3",
			},
			StartsAt: ts.Add(-6 * time.Minute),
			EndsAt:   ts.Add(-3 * time.Minute),
		},
		{
			Labels: LabelSet{
				"alertname": "DiskFull",
				"dev":       "sda1",
			},
			StartsAt: ts.Add(-5 * time.Minute),
			EndsAt:   ts.Add(-4 * time.Minute),
		},
		{
			Labels: LabelSet{
				"alertname": "OutOfMemory",
				"dev":       "sda1",
			},
			StartsAt: ts.Add(-2 * time.Minute),
			EndsAt:   ts.Add(-1 * time.Minute),
		},
		{
			Labels: LabelSet{
				"alertname": "DiskFull",
				"dev":       "sda2",
			},
			StartsAt: ts.Add(-2 * time.Minute),
			EndsAt:   ts.Add(-3 * time.Minute),
		},
		{
			Labels: LabelSet{
				"alertname": "OutOfMemory",
				"dev":       "sda2",
			},
			StartsAt: ts.Add(-5 * time.Minute),
			EndsAt:   ts.Add(-2 * time.Minute),
		},
	}

	sort.Sort(alerts)

	expected := []string{
		"DiskFull[5ffe595][resolved]",
		"InternalError[09cfd46][resolved]",
		"OutOfMemory[d43a602][resolved]",
		"DiskFull[5ff4595][resolved]",
		"OutOfMemory[d444602][resolved]",
	}

	for i := range alerts {
		if alerts[i].String() != expected[i] {
			t.Errorf("expected alert %s at index %d, but got %s", expected[i], i, alerts[i].String())
		}
	}
}

func TestAlertsStatus(t *testing.T) {
	ts := time.Now()
	firingAlerts := Alerts{
		{
			Labels: LabelSet{
				"foo": "bar",
			},
			StartsAt: ts,
		},
		{
			Labels: LabelSet{
				"bar": "baz",
			},
			StartsAt: ts,
		},
	}

	actualStatus := firingAlerts.Status()
	expectedStatus := AlertFiring

	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	actualStatus = firingAlerts.StatusAt(ts)
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	ts = time.Now()
	resolvedAlerts := Alerts{
		{
			Labels: LabelSet{
				"foo": "bar",
			},
			StartsAt: ts.Add(-1 * time.Minute),
			EndsAt:   ts,
		},
		{
			Labels: LabelSet{
				"bar": "baz",
			},
			StartsAt: ts.Add(-1 * time.Minute),
			EndsAt:   ts,
		},
	}

	actualStatus = resolvedAlerts.Status()
	expectedStatus = AlertResolved
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	actualStatus = resolvedAlerts.StatusAt(ts)
	expectedStatus = AlertResolved
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	ts = time.Now()
	mixedAlerts := Alerts{
		{
			Labels: LabelSet{
				"foo": "bar",
			},
			StartsAt: ts.Add(-1 * time.Minute),
			EndsAt:   ts.Add(5 * time.Minute),
		},
		{
			Labels: LabelSet{
				"bar": "baz",
			},
			StartsAt: ts.Add(-1 * time.Minute),
			EndsAt:   ts,
		},
	}

	actualStatus = mixedAlerts.Status()
	expectedStatus = AlertFiring
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	actualStatus = mixedAlerts.StatusAt(ts)
	expectedStatus = AlertFiring
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}

	actualStatus = mixedAlerts.StatusAt(ts.Add(5 * time.Minute))
	expectedStatus = AlertResolved
	if actualStatus != expectedStatus {
		t.Errorf("expected status %s, but got %s", expectedStatus, actualStatus)
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit