...

Source file src/github.com/prometheus/alertmanager/nflog/nflogpb/set_test.go

Documentation: github.com/prometheus/alertmanager/nflog/nflogpb

     1  // Copyright 2018 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package nflogpb
    15  
    16  import (
    17  	"testing"
    18  )
    19  
    20  func TestIsFiringSubset(t *testing.T) {
    21  	e := &Entry{
    22  		FiringAlerts: []uint64{1, 2, 3},
    23  	}
    24  
    25  	tests := []struct {
    26  		subset   map[uint64]struct{}
    27  		expected bool
    28  	}{
    29  		{newSubset(), true}, // empty subset
    30  		{newSubset(1), true},
    31  		{newSubset(2), true},
    32  		{newSubset(3), true},
    33  		{newSubset(1, 2), true},
    34  		{newSubset(1, 2), true},
    35  		{newSubset(1, 2, 3), true},
    36  		{newSubset(4), false},
    37  		{newSubset(1, 5), false},
    38  		{newSubset(1, 2, 3, 6), false},
    39  	}
    40  
    41  	for _, test := range tests {
    42  		if result := e.IsFiringSubset(test.subset); result != test.expected {
    43  			t.Errorf("Expected %t, got %t for subset %v", test.expected, result, elements(test.subset))
    44  		}
    45  	}
    46  }
    47  
    48  func TestIsResolvedSubset(t *testing.T) {
    49  	e := &Entry{
    50  		ResolvedAlerts: []uint64{1, 2, 3},
    51  	}
    52  
    53  	tests := []struct {
    54  		subset   map[uint64]struct{}
    55  		expected bool
    56  	}{
    57  		{newSubset(), true}, // empty subset
    58  		{newSubset(1), true},
    59  		{newSubset(2), true},
    60  		{newSubset(3), true},
    61  		{newSubset(1, 2), true},
    62  		{newSubset(1, 2), true},
    63  		{newSubset(1, 2, 3), true},
    64  		{newSubset(4), false},
    65  		{newSubset(1, 5), false},
    66  		{newSubset(1, 2, 3, 6), false},
    67  	}
    68  
    69  	for _, test := range tests {
    70  		if result := e.IsResolvedSubset(test.subset); result != test.expected {
    71  			t.Errorf("Expected %t, got %t for subset %v", test.expected, result, elements(test.subset))
    72  		}
    73  	}
    74  }
    75  
    76  func newSubset(elements ...uint64) map[uint64]struct{} {
    77  	subset := make(map[uint64]struct{})
    78  	for _, el := range elements {
    79  		subset[el] = struct{}{}
    80  	}
    81  
    82  	return subset
    83  }
    84  
    85  func elements(m map[uint64]struct{}) []uint64 {
    86  	els := make([]uint64, 0, len(m))
    87  	for k := range m {
    88  		els = append(els, k)
    89  	}
    90  
    91  	return els
    92  }
    93  

View as plain text