metrics_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2019 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 metrics
  14. import (
  15. "fmt"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  18. )
  19. func TestMetricCollection(t *testing.T) {
  20. dsw := cache.NewDesiredStateOfWorld()
  21. asw := cache.NewActualStateOfWorld()
  22. fakePlugin := cache.PluginInfo{
  23. SocketPath: fmt.Sprintf("fake/path/plugin.sock"),
  24. FoundInDeprecatedDir: false,
  25. }
  26. // Add one plugin to DesiredStateOfWorld
  27. err := dsw.AddOrUpdatePlugin(fakePlugin.SocketPath, fakePlugin.FoundInDeprecatedDir)
  28. if err != nil {
  29. t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
  30. }
  31. // Add one plugin to ActualStateOfWorld
  32. err = asw.AddPlugin(fakePlugin)
  33. if err != nil {
  34. t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
  35. }
  36. metricCollector := &totalPluginsCollector{asw, dsw}
  37. // Check if getPluginCount returns correct data
  38. count := metricCollector.getPluginCount()
  39. if len(count) != 2 {
  40. t.Errorf("getPluginCount failed. Expected <2> states, got <%d>", len(count))
  41. }
  42. dswCount, ok := count["desired_state_of_world"]
  43. if !ok {
  44. t.Errorf("getPluginCount failed. Expected <desired_state_of_world>, got nothing")
  45. }
  46. fakePluginCount := dswCount["fake/path/plugin.sock"]
  47. if fakePluginCount != 1 {
  48. t.Errorf("getPluginCount failed. Expected <1> fake/path/plugin.sock in DesiredStateOfWorld, got <%d>",
  49. fakePluginCount)
  50. }
  51. aswCount, ok := count["actual_state_of_world"]
  52. if !ok {
  53. t.Errorf("getPluginCount failed. Expected <actual_state_of_world>, got nothing")
  54. }
  55. fakePluginCount = aswCount["fake/path/plugin.sock"]
  56. if fakePluginCount != 1 {
  57. t.Errorf("getPluginCount failed. Expected <1> fake/path/plugin.sock in ActualStateOfWorld, got <%d>",
  58. fakePluginCount)
  59. }
  60. }