bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/sched/grouping_test.go (about)

     1  package sched
     2  
     3  import (
     4  	"testing"
     5  
     6  	"bosun.org/models"
     7  	"bosun.org/opentsdb"
     8  )
     9  
    10  func TestGroupSets_Single(t *testing.T) {
    11  	ak := models.AlertKey("a{host=foo}")
    12  	states := States{ak: &models.IncidentState{AlertKey: ak, Alert: "a", Tags: opentsdb.TagSet{"host": "foo"}.Tags(), Subject: "aaa"}}
    13  	groups := states.GroupSets(5)
    14  	if len(groups) != 1 {
    15  		t.Fatalf("Expected 1 group. Found %d.", len(groups))
    16  	}
    17  	if len(groups["a{host=foo}"]) == 0 {
    18  		t.Fatal("Expected alert key but couldn't find it.")
    19  	}
    20  }
    21  
    22  func TestGroupSets_AboveAndBelow(t *testing.T) {
    23  	aks := map[string]string{
    24  		"a1{host=a}": "a1 on a",
    25  		"a2{host=a}": "a2 on a",
    26  		"a3{host=a}": "a3 on a",
    27  		"a4{host=a}": "a4 on a",
    28  	}
    29  	states := States{}
    30  	for a, sub := range aks {
    31  		ak, err := models.ParseAlertKey(a)
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  		states[ak] = &models.IncidentState{AlertKey: models.AlertKey(a), Alert: ak.Name(), Tags: ak.Group().Tags(), Subject: sub}
    36  	}
    37  
    38  	groups := states.GroupSets(5)
    39  	if len(groups) != 4 {
    40  		t.Fatalf("Expected 4 unique groups, but found %d.", len(groups))
    41  	}
    42  
    43  	groups = states.GroupSets(4)
    44  	if len(groups) != 1 {
    45  		t.Fatalf("Expected 1 unique group, but found %d.", len(groups))
    46  	}
    47  }
    48  
    49  func TestGroupSets_ByAlert(t *testing.T) {
    50  	aks := map[string]string{
    51  		"a{host=a}": "a on a",
    52  		"a{host=b}": "a on b",
    53  		"a{host=c}": "a on c",
    54  		"a{host=d}": "a on d",
    55  	}
    56  	states := States{}
    57  	for a, sub := range aks {
    58  		ak, err := models.ParseAlertKey(a)
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		states[ak] = &models.IncidentState{AlertKey: models.AlertKey(a), Alert: ak.Name(), Tags: ak.Group().Tags(), Subject: sub}
    63  	}
    64  
    65  	groups := states.GroupSets(5)
    66  	if len(groups) != 4 {
    67  		t.Fatalf("Expected 4 unique groups, but found %d.", len(groups))
    68  	}
    69  
    70  	groups = states.GroupSets(4)
    71  	if len(groups) != 1 {
    72  		t.Fatalf("Expected 1 unique group, but found %d.", len(groups))
    73  	}
    74  }