2
0

configwatcher_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package watcher
  2. import (
  3. "testing"
  4. v1 "k8s.io/api/core/v1"
  5. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  6. )
  7. const (
  8. TestConfigMapName = "test-config"
  9. AlternateTestConfigMapName = "alternate-test-config"
  10. TestDataProperty = "test-prop"
  11. )
  12. func newTestWatcher(t *testing.T, configMapName string, instanceName string, didRun *bool) *ConfigMapWatcher {
  13. return &ConfigMapWatcher{
  14. ConfigMapName: configMapName,
  15. WatchFunc: func(cmn string, data map[string]string) error {
  16. t.Logf("ConfigMapWatcher[%s] triggered for ConfigMap: %s, data[\"test\"] = %s\n", instanceName, cmn, data[TestDataProperty])
  17. *didRun = true
  18. return nil
  19. },
  20. }
  21. }
  22. func newConfigMap(configMapName string, dataValue string) *v1.ConfigMap {
  23. return &v1.ConfigMap{
  24. Data: map[string]string{
  25. TestDataProperty: dataValue,
  26. },
  27. ObjectMeta: metav1.ObjectMeta{
  28. Name: configMapName,
  29. },
  30. }
  31. }
  32. func TestConfigWatcherSingleHandler(t *testing.T) {
  33. // Test that a single watcher added for the configmap test-config is executed when
  34. // triggered
  35. var didRun bool = false
  36. w := NewConfigMapWatchers(nil, "", newTestWatcher(t, TestConfigMapName, "single", &didRun))
  37. f := w.toWatchFunc()
  38. // Execute watch func with a 'test-config' configmap
  39. f(newConfigMap(TestConfigMapName, "testing 1 2 3"))
  40. if !didRun {
  41. t.Errorf("Failed to run configmap handler for 'single'\n")
  42. }
  43. }
  44. func TestConfigWatcherMultipleHandlers(t *testing.T) {
  45. // Test that adding two different configmap watchers aren't both triggered on a configmap update
  46. var firstDidRun bool = false
  47. var secondDidRun bool = false
  48. w := NewConfigMapWatchers(
  49. nil,
  50. "",
  51. newTestWatcher(t, TestConfigMapName, "single", &firstDidRun),
  52. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun))
  53. f := w.toWatchFunc()
  54. // Execute watch func with a 'alternate-test-config' configmap
  55. f(newConfigMap(AlternateTestConfigMapName, "oof!"))
  56. // Assert that first did not run
  57. if firstDidRun {
  58. t.Errorf("Executed alternate-test-config map change, but test-config handler, 'single' executed!\n")
  59. }
  60. if !secondDidRun {
  61. t.Errorf("Failed to run configmap handler for 'alternate'\n")
  62. }
  63. }
  64. func TestConfigWatcherMultipleHandlersForSameConfig(t *testing.T) {
  65. // Test that adding two different configmap watchers for the same configmap are both triggered
  66. var firstDidRun bool = false
  67. var secondDidRun bool = false
  68. var thirdDidRun bool = false
  69. w := NewConfigMapWatchers(
  70. nil,
  71. "",
  72. newTestWatcher(t, TestConfigMapName, "first", &firstDidRun),
  73. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun),
  74. // third watcher watches for the same configmap as "first"
  75. newTestWatcher(t, TestConfigMapName, "third", &thirdDidRun),
  76. )
  77. f := w.toWatchFunc()
  78. // Execute watch func with a 'test-config' configmap
  79. f(newConfigMap(TestConfigMapName, "double trouble"))
  80. // Assert that second did not run
  81. if secondDidRun {
  82. t.Errorf("Executed test-config map change, first handler, 'single', executed!\n")
  83. }
  84. if !firstDidRun {
  85. t.Errorf("Failed to run configmap handler for 'first'\n")
  86. }
  87. if !thirdDidRun {
  88. t.Errorf("Failed to run configmap handler for 'third'\n")
  89. }
  90. }
  91. func TestConfigMapWatcherWithAdd(t *testing.T) {
  92. // Test that adding two different configmap watchers for the same configmap are both triggered
  93. // when using Add() and AddWatcher()
  94. var firstDidRun bool = false
  95. var secondDidRun bool = false
  96. var thirdDidRun bool = false
  97. a, b, c := newTestWatcher(t, TestConfigMapName, "first", &firstDidRun),
  98. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun),
  99. // third watcher watches for the same configmap as "first"
  100. newTestWatcher(t, TestConfigMapName, "third", &thirdDidRun)
  101. w := NewConfigMapWatchers(nil, "")
  102. w.AddWatcher(a)
  103. w.AddWatcher(b)
  104. w.Add(c.ConfigMapName, c.WatchFunc)
  105. f := w.toWatchFunc()
  106. // Execute watch func with a 'test-config' configmap
  107. f(newConfigMap(TestConfigMapName, "double trouble"))
  108. // Assert that second did not run
  109. if secondDidRun {
  110. t.Errorf("Executed test-config map change, first handler, 'single', executed!\n")
  111. }
  112. if !firstDidRun {
  113. t.Errorf("Failed to run configmap handler for 'first'\n")
  114. }
  115. if !thirdDidRun {
  116. t.Errorf("Failed to run configmap handler for 'third'\n")
  117. }
  118. }