configwatcher_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(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. newTestWatcher(t, TestConfigMapName, "single", &firstDidRun),
  50. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun))
  51. f := w.ToWatchFunc()
  52. // Execute watch func with a 'alternate-test-config' configmap
  53. f(newConfigMap(AlternateTestConfigMapName, "oof!"))
  54. // Assert that first did not run
  55. if firstDidRun {
  56. t.Errorf("Executed alternate-test-config map change, but test-config handler, 'single' executed!\n")
  57. }
  58. if !secondDidRun {
  59. t.Errorf("Failed to run configmap handler for 'alternate'\n")
  60. }
  61. }
  62. func TestConfigWatcherMultipleHandlersForSameConfig(t *testing.T) {
  63. // Test that adding two different configmap watchers for the same configmap are both triggered
  64. var firstDidRun bool = false
  65. var secondDidRun bool = false
  66. var thirdDidRun bool = false
  67. w := NewConfigMapWatchers(
  68. newTestWatcher(t, TestConfigMapName, "first", &firstDidRun),
  69. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun),
  70. // third watcher watches for the same configmap as "first"
  71. newTestWatcher(t, TestConfigMapName, "third", &thirdDidRun),
  72. )
  73. f := w.ToWatchFunc()
  74. // Execute watch func with a 'test-config' configmap
  75. f(newConfigMap(TestConfigMapName, "double trouble"))
  76. // Assert that second did not run
  77. if secondDidRun {
  78. t.Errorf("Executed test-config map change, first handler, 'single', executed!\n")
  79. }
  80. if !firstDidRun {
  81. t.Errorf("Failed to run configmap handler for 'first'\n")
  82. }
  83. if !thirdDidRun {
  84. t.Errorf("Failed to run configmap handler for 'third'\n")
  85. }
  86. }
  87. func TestConfigMapWatcherWithAdd(t *testing.T) {
  88. // Test that adding two different configmap watchers for the same configmap are both triggered
  89. // when using Add() and AddWatcher()
  90. var firstDidRun bool = false
  91. var secondDidRun bool = false
  92. var thirdDidRun bool = false
  93. a, b, c := newTestWatcher(t, TestConfigMapName, "first", &firstDidRun),
  94. newTestWatcher(t, AlternateTestConfigMapName, "alternate", &secondDidRun),
  95. // third watcher watches for the same configmap as "first"
  96. newTestWatcher(t, TestConfigMapName, "third", &thirdDidRun)
  97. w := NewConfigMapWatchers()
  98. w.AddWatcher(a)
  99. w.AddWatcher(b)
  100. w.Add(c.ConfigMapName, c.WatchFunc)
  101. f := w.ToWatchFunc()
  102. // Execute watch func with a 'test-config' configmap
  103. f(newConfigMap(TestConfigMapName, "double trouble"))
  104. // Assert that second did not run
  105. if secondDidRun {
  106. t.Errorf("Executed test-config map change, first handler, 'single', executed!\n")
  107. }
  108. if !firstDidRun {
  109. t.Errorf("Failed to run configmap handler for 'first'\n")
  110. }
  111. if !thirdDidRun {
  112. t.Errorf("Failed to run configmap handler for 'third'\n")
  113. }
  114. }