log_metrics_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package collectors
  14. import (
  15. "strings"
  16. "testing"
  17. "github.com/prometheus/client_golang/prometheus"
  18. "github.com/prometheus/client_golang/prometheus/testutil"
  19. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  20. )
  21. func TestNoMetricsCollected(t *testing.T) {
  22. ch := make(chan prometheus.Metric)
  23. collector := &logMetricsCollector{
  24. podStats: func() ([]statsapi.PodStats, error) {
  25. return []statsapi.PodStats{}, nil
  26. },
  27. }
  28. collector.Collect(ch)
  29. num := len(ch)
  30. if num != 0 {
  31. t.Fatalf("Channel expected to be empty, but received %d", num)
  32. }
  33. }
  34. func TestMetricsCollected(t *testing.T) {
  35. size := uint64(18)
  36. collector := &logMetricsCollector{
  37. podStats: func() ([]statsapi.PodStats, error) {
  38. return []statsapi.PodStats{
  39. {
  40. PodRef: statsapi.PodReference{
  41. Namespace: "some-namespace",
  42. Name: "podName1",
  43. },
  44. Containers: []statsapi.ContainerStats{
  45. {
  46. Name: "containerName1",
  47. Logs: &statsapi.FsStats{
  48. UsedBytes: &size,
  49. },
  50. },
  51. },
  52. },
  53. }, nil
  54. },
  55. }
  56. err := testutil.CollectAndCompare(collector, strings.NewReader(`
  57. # HELP kubelet_container_log_filesystem_used_bytes Bytes used by the container's logs on the filesystem.
  58. # TYPE kubelet_container_log_filesystem_used_bytes gauge
  59. kubelet_container_log_filesystem_used_bytes{container="containerName1",namespace="some-namespace",pod="podName1"} 18
  60. `), "kubelet_container_log_filesystem_used_bytes")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }