dependents_test.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  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 dependents
  14. import (
  15. "strings"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestDependentList(t *testing.T) {
  20. containsExactlyOne := func(s, substr string) {
  21. assert.Equal(t, 1, strings.Count(s, substr))
  22. }
  23. isBefore := func(s, before, after string) {
  24. assert.Less(t, strings.Index(s, before), strings.Index(s, after))
  25. }
  26. t.Run("empty", func(t *testing.T) {
  27. d := NewDependentList()
  28. assert.True(t, d.Empty())
  29. })
  30. t.Run("one resource, one dependent", func(t *testing.T) {
  31. d := NewDependentList()
  32. d.Add("MyResources", "my-resource-1")
  33. assert.False(t, d.Empty())
  34. assert.ElementsMatch(t, []string{"MyResources"}, d.PluralKinds())
  35. assert.ElementsMatch(t, []string{"my-resource-1"}, d.OfKind("MyResources"))
  36. toString := d.StringWithHeader("header")
  37. containsExactlyOne(toString, "header:")
  38. containsExactlyOne(toString, "MyResources")
  39. containsExactlyOne(toString, "my-resource-1")
  40. })
  41. t.Run("one resource - multiple dependents", func(t *testing.T) {
  42. d := NewDependentList()
  43. d.Add("MyResources", "my-resource-1")
  44. d.Add("MyResources", "my-resource-2")
  45. d.Add("MyResources", "my-resource-3")
  46. assert.False(t, d.Empty())
  47. assert.ElementsMatch(t, []string{"MyResources"}, d.PluralKinds())
  48. assert.ElementsMatch(t, []string{"my-resource-1", "my-resource-2", "my-resource-3"}, d.OfKind("MyResources"))
  49. assert.ElementsMatch(t, []string{}, d.OfKind("OtherKinds"))
  50. toString := d.StringWithHeader("head with arg %d", 1)
  51. containsExactlyOne(toString, "head with arg 1:")
  52. containsExactlyOne(toString, "MyResources")
  53. containsExactlyOne(toString, "my-resource-1")
  54. containsExactlyOne(toString, "my-resource-2")
  55. containsExactlyOne(toString, "my-resource-3")
  56. })
  57. t.Run("multiple resources - multiple dependents", func(t *testing.T) {
  58. d := NewDependentList()
  59. d.Add("MyResources", "my-resource-2")
  60. d.Add("MyResources", "my-resource-4")
  61. d.Add("YourResources", "your-resource-1")
  62. d.Add("TheirResources", "their-resource-5")
  63. d.Add("TheirResources", "their-resource-6")
  64. assert.False(t, d.Empty())
  65. assert.ElementsMatch(t, []string{"MyResources", "YourResources", "TheirResources"}, d.PluralKinds())
  66. assert.ElementsMatch(t, []string{"my-resource-2", "my-resource-4"}, d.OfKind("MyResources"))
  67. assert.ElementsMatch(t, []string{"your-resource-1"}, d.OfKind("YourResources"))
  68. assert.ElementsMatch(t, []string{"their-resource-5", "their-resource-6"}, d.OfKind("TheirResources"))
  69. assert.ElementsMatch(t, []string{}, d.OfKind("OtherKinds"))
  70. toString := d.StringWithHeader("head with arg %s", "mom")
  71. t.Log(toString)
  72. containsExactlyOne(toString, "head with arg mom:")
  73. containsExactlyOne(toString, "MyResources")
  74. containsExactlyOne(toString, "my-resource-2")
  75. containsExactlyOne(toString, "my-resource-4")
  76. containsExactlyOne(toString, "YourResources")
  77. containsExactlyOne(toString, "your-resource-1")
  78. containsExactlyOne(toString, "TheirResources")
  79. containsExactlyOne(toString, "their-resource-5")
  80. containsExactlyOne(toString, "their-resource-6")
  81. // ensure alphabetical ordering
  82. isBefore(toString, "MyResources", "TheirResources")
  83. isBefore(toString, "TheirResources", "YourResources")
  84. })
  85. }