prometheus

Форк
0
/
engine_internal_test.go 
82 строки · 2.0 Кб
1
// Copyright 2024 The Prometheus Authors
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 promql
15

16
import (
17
	"errors"
18
	"testing"
19

20
	"github.com/go-kit/log"
21
	"github.com/stretchr/testify/require"
22

23
	"github.com/prometheus/prometheus/promql/parser"
24
	"github.com/prometheus/prometheus/util/annotations"
25
)
26

27
func TestRecoverEvaluatorRuntime(t *testing.T) {
28
	var output []interface{}
29
	logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
30
		output = append(output, keyvals...)
31
		return nil
32
	}))
33
	ev := &evaluator{logger: logger}
34

35
	expr, _ := parser.ParseExpr("sum(up)")
36

37
	var err error
38

39
	defer func() {
40
		require.EqualError(t, err, "unexpected error: runtime error: index out of range [123] with length 0")
41
		require.Contains(t, output, "sum(up)")
42
	}()
43
	defer ev.recover(expr, nil, &err)
44

45
	// Cause a runtime panic.
46
	var a []int
47
	a[123] = 1
48
}
49

50
func TestRecoverEvaluatorError(t *testing.T) {
51
	ev := &evaluator{logger: log.NewNopLogger()}
52
	var err error
53

54
	e := errors.New("custom error")
55

56
	defer func() {
57
		require.EqualError(t, err, e.Error())
58
	}()
59
	defer ev.recover(nil, nil, &err)
60

61
	panic(e)
62
}
63

64
func TestRecoverEvaluatorErrorWithWarnings(t *testing.T) {
65
	ev := &evaluator{logger: log.NewNopLogger()}
66
	var err error
67
	var ws annotations.Annotations
68

69
	warnings := annotations.New().Add(errors.New("custom warning"))
70
	e := errWithWarnings{
71
		err:      errors.New("custom error"),
72
		warnings: warnings,
73
	}
74

75
	defer func() {
76
		require.EqualError(t, err, e.Error())
77
		require.Equal(t, warnings, ws, "wrong warning message")
78
	}()
79
	defer ev.recover(nil, &ws, &err)
80

81
	panic(e)
82
}
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.