blob: dcac09bd5c7c10c6b71dbb5afa519ec9ab1e8db0 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// Copyright 2018 The Kubernetes Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package metrics_test
16
17import (
18 "testing"
19 "time"
20
21 . "github.com/onsi/ginkgo"
22 . "github.com/onsi/gomega"
23 "github.com/prometheus/client_golang/prometheus"
24
25 . "github.com/kubernetes-incubator/metrics-server/pkg/metrics"
26)
27
28func TestMetricsUtil(t *testing.T) {
29 RegisterFailHandler(Fail)
30 RunSpecs(t, "Prometheus Metrics Util Test")
31}
32
33var _ = Describe("Prometheus Bucket Estimator", func() {
34 Context("with a scrape timeout longer than the max default bucket", func() {
35 It("should generate buckets in strictly increasing order", func() {
36 buckets := BucketsForScrapeDuration(15 * time.Second)
37 lastBucket := 0.0
38 for _, bucket := range buckets {
39 Expect(bucket).To(BeNumerically(">", lastBucket))
40 lastBucket = bucket
41 }
42 })
43
44 It("should include some buckets around the scrape timeout", func() {
45 Expect(BucketsForScrapeDuration(15 * time.Second)).To(ContainElement(15.0))
46 Expect(BucketsForScrapeDuration(15 * time.Second)).To(ContainElement(30.0))
47 })
48 })
49 Context("with a scrape timeout shorter than the max default bucket", func() {
50 It("should generate buckets in strictly increasing order", func() {
51 buckets := BucketsForScrapeDuration(5 * time.Second)
52 lastBucket := 0.0
53 for _, bucket := range buckets {
54 Expect(bucket).To(BeNumerically(">", lastBucket))
55 lastBucket = bucket
56 }
57 })
58
59 It("should include a bucket for the scrape timeout", func() {
60 Expect(BucketsForScrapeDuration(5 * time.Second)).To(ContainElement(5.0))
61 })
62 })
63 Context("with a scrape timeout equalt to the max default bucket", func() {
64 maxBucket := prometheus.DefBuckets[len(prometheus.DefBuckets)-1]
65 maxBucketDuration := time.Duration(maxBucket) * time.Second
66
67 It("should generate buckets in strictly increasing order", func() {
68 buckets := BucketsForScrapeDuration(maxBucketDuration)
69 lastBucket := 0.0
70 for _, bucket := range buckets {
71 Expect(bucket).To(BeNumerically(">", lastBucket))
72 lastBucket = bucket
73 }
74 })
75
76 It("should include a bucket for the scrape timeout", func() {
77 Expect(BucketsForScrapeDuration(maxBucketDuration)).To(ContainElement(maxBucket))
78 })
79 })
80})