1package v2 2 3import ( 4 "sort" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8) 9 10func TestFixtureSilenced(t *testing.T) { 11 s := FixtureSilenced("test_subscription:test_check") 12 s.Expire = 60 13 s.ExpireOnResolve = true 14 s.Creator = "creator@example.com" 15 s.Reason = "test reason" 16 s.Namespace = "default" 17 assert.NotNil(t, s) 18 assert.NotNil(t, s.Name) 19 assert.Equal(t, "test_subscription:test_check", s.Name) 20 assert.NotNil(t, s.Expire) 21 assert.NotNil(t, s.ExpireOnResolve) 22 assert.NotNil(t, s.Expire) 23 assert.NotNil(t, s.Creator) 24 assert.NotNil(t, s.Check) 25 assert.NotNil(t, s.Reason) 26 assert.NotNil(t, s.Subscription) 27 assert.NotNil(t, s.Namespace) 28 29 s = FixtureSilenced("entity:test_subscription:test_check") 30 assert.Equal(t, "entity:test_subscription", s.Subscription) 31 assert.Equal(t, "test_check", s.Check) 32} 33 34// Validation should fail when we don't provide a CheckName or Subscription 35func TestSilencedValidate(t *testing.T) { 36 var s Silenced 37 assert.Error(t, s.Validate()) 38} 39 40func TestSortSilencedByID(t *testing.T) { 41 a := FixtureSilenced("Abernathy:*") 42 b := FixtureSilenced("Bernard:*") 43 c := FixtureSilenced("Clementine:*") 44 d := FixtureSilenced("Dolores:*") 45 46 testCases := []struct { 47 name string 48 inRecords []*Silenced 49 expected []*Silenced 50 }{ 51 { 52 name: "d, c, b, a", 53 inRecords: []*Silenced{d, c, b, a}, 54 expected: []*Silenced{a, b, c, d}, 55 }, 56 { 57 name: "c, d, a, b", 58 inRecords: []*Silenced{c, d, a, b}, 59 expected: []*Silenced{a, b, c, d}, 60 }, 61 } 62 63 for _, tc := range testCases { 64 t.Run(tc.name, func(t *testing.T) { 65 sort.Sort(SortSilencedByName(tc.inRecords)) 66 assert.EqualValues(t, tc.expected, tc.inRecords) 67 }) 68 } 69} 70 71func TestSortSilencedByBegin(t *testing.T) { 72 a := FixtureSilenced("Abernathy:*") 73 a.Begin = 5 74 b := FixtureSilenced("Bernard:*") 75 b.Begin = 10 76 c := FixtureSilenced("Clementine:*") 77 c.Begin = 50 78 79 in := []*Silenced{b, a, c} 80 sort.Sort(SortSilencedByBegin(in)) 81 assert.EqualValues(t, []*Silenced{a, b, c}, in) 82} 83