...

Source file src/github.com/prometheus/alertmanager/api/v2/testing.go

Documentation: github.com/prometheus/alertmanager/api/v2

     1  // Copyright 2022 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 v2
    15  
    16  import (
    17  	"encoding/json"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/go-openapi/strfmt"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	open_api_models "github.com/prometheus/alertmanager/api/v2/models"
    25  	"github.com/prometheus/alertmanager/pkg/labels"
    26  	"github.com/prometheus/alertmanager/silence/silencepb"
    27  )
    28  
    29  func createSilence(t *testing.T, ID, creator string, start, ends time.Time) (open_api_models.PostableSilence, []byte) {
    30  	t.Helper()
    31  
    32  	comment := "test"
    33  	matcherName := "a"
    34  	matcherValue := "b"
    35  	isRegex := false
    36  	startsAt := strfmt.DateTime(start)
    37  	endsAt := strfmt.DateTime(ends)
    38  
    39  	sil := open_api_models.PostableSilence{
    40  		ID: ID,
    41  		Silence: open_api_models.Silence{
    42  			Matchers:  open_api_models.Matchers{&open_api_models.Matcher{Name: &matcherName, Value: &matcherValue, IsRegex: &isRegex}},
    43  			StartsAt:  &startsAt,
    44  			EndsAt:    &endsAt,
    45  			CreatedBy: &creator,
    46  			Comment:   &comment,
    47  		},
    48  	}
    49  	b, err := json.Marshal(&sil)
    50  	require.NoError(t, err)
    51  
    52  	return sil, b
    53  }
    54  
    55  func createSilenceMatcher(t *testing.T, name, pattern string, matcherType silencepb.Matcher_Type) *silencepb.Matcher {
    56  	t.Helper()
    57  
    58  	return &silencepb.Matcher{
    59  		Name:    name,
    60  		Pattern: pattern,
    61  		Type:    matcherType,
    62  	}
    63  }
    64  
    65  func createLabelMatcher(t *testing.T, name, value string, matchType labels.MatchType) *labels.Matcher {
    66  	t.Helper()
    67  
    68  	matcher, _ := labels.NewMatcher(matchType, name, value)
    69  	return matcher
    70  }
    71  

View as plain text