github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sources/manager_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 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 15 package sources 16 17 import ( 18 "testing" 19 "time" 20 21 "k8s.io/heapster/metrics/util" 22 ) 23 24 func TestAllSourcesReplyInTime(t *testing.T) { 25 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 26 util.NewDummyMetricsSource("s1", time.Second), 27 util.NewDummyMetricsSource("s2", time.Second)) 28 29 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 30 now := time.Now() 31 end := now.Truncate(10 * time.Second) 32 dataBatch, err := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 33 if err != nil { 34 t.Fatalf("ScrapeMetrics error. %v", err) 35 } 36 37 elapsed := time.Now().Sub(now) 38 if elapsed > 3*time.Second { 39 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 40 } 41 42 present := make(map[string]bool) 43 for key := range dataBatch.MetricSets { 44 present[key] = true 45 } 46 47 if _, ok := present["s1"]; !ok { 48 t.Fatal("s1 not found") 49 } 50 51 if _, ok := present["s2"]; !ok { 52 t.Fatal("s2 not found") 53 } 54 } 55 56 func TestOneSourcesReplyInTime(t *testing.T) { 57 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 58 util.NewDummyMetricsSource("s1", time.Second), 59 util.NewDummyMetricsSource("s2", 30*time.Second)) 60 61 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 62 now := time.Now() 63 end := now.Truncate(10 * time.Second) 64 dataBatch, err := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 65 if err != nil { 66 t.Fatalf("ScrapeMetrics error. %v", err) 67 } 68 elapsed := time.Now().Sub(now) 69 70 if elapsed > 4*time.Second { 71 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 72 } 73 74 if elapsed < 2*time.Second { 75 t.Fatalf("ScrapeMetrics took too short: %s", elapsed) 76 } 77 78 present := make(map[string]bool) 79 for key := range dataBatch.MetricSets { 80 present[key] = true 81 } 82 83 if _, ok := present["s1"]; !ok { 84 t.Fatal("s1 not found") 85 } 86 87 if _, ok := present["s2"]; ok { 88 t.Fatal("s2 found") 89 } 90 } 91 92 func TestNoSourcesReplyInTime(t *testing.T) { 93 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 94 util.NewDummyMetricsSource("s1", 30*time.Second), 95 util.NewDummyMetricsSource("s2", 30*time.Second)) 96 97 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 98 now := time.Now() 99 end := now.Truncate(10 * time.Second) 100 dataBatch, err := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 101 if err != nil { 102 t.Fatalf("ScrapeMetrics error. %v", err) 103 } 104 elapsed := time.Now().Sub(now) 105 106 if elapsed > 4*time.Second { 107 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 108 } 109 110 if elapsed < 2*time.Second { 111 t.Fatalf("ScrapeMetrics took too short: %s", elapsed) 112 } 113 114 present := make(map[string]bool) 115 for key := range dataBatch.MetricSets { 116 present[key] = true 117 } 118 119 if _, ok := present["s1"]; ok { 120 t.Fatal("s1 found") 121 } 122 123 if _, ok := present["s2"]; ok { 124 t.Fatal("s2 found") 125 } 126 }