provider_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 awsutil
  14. import (
  15. "encoding/json"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestParameterTagsToJSONString(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. tags map[string]string
  23. expected string
  24. wantErr bool
  25. }{
  26. {
  27. name: "Valid tags",
  28. tags: map[string]string{
  29. "key1": "value1",
  30. "key2": "value2",
  31. },
  32. expected: `{"key1":"value1","key2":"value2"}`,
  33. wantErr: false,
  34. },
  35. {
  36. name: "Empty tags",
  37. tags: map[string]string{},
  38. expected: `{}`,
  39. wantErr: false,
  40. },
  41. {
  42. name: "Nil tags",
  43. tags: nil,
  44. wantErr: false,
  45. expected: "null",
  46. },
  47. }
  48. for _, tt := range tests {
  49. t.Run(tt.name, func(t *testing.T) {
  50. result, err := ParameterTagsToJSONString(tt.tags)
  51. if tt.wantErr {
  52. assert.Error(t, err)
  53. } else {
  54. assert.NoError(t, err)
  55. var resultMap map[string]string
  56. err := json.Unmarshal([]byte(result), &resultMap)
  57. assert.NoError(t, err)
  58. assert.Equal(t, tt.expected, result)
  59. }
  60. })
  61. }
  62. }
  63. func TestFindTagKeysToRemove(t *testing.T) {
  64. tests := []struct {
  65. name string
  66. tags map[string]string
  67. metaTags map[string]string
  68. expected []string
  69. }{
  70. {
  71. name: "No tags to remove",
  72. tags: map[string]string{
  73. "key1": "value1",
  74. "key2": "value2",
  75. },
  76. metaTags: map[string]string{
  77. "key1": "value1",
  78. "key2": "value2",
  79. },
  80. expected: []string{},
  81. },
  82. {
  83. name: "Some tags to remove",
  84. tags: map[string]string{
  85. "key1": "value1",
  86. "key2": "value2",
  87. "key3": "value3",
  88. },
  89. metaTags: map[string]string{
  90. "key1": "value1",
  91. "key2": "value2",
  92. },
  93. expected: []string{"key3"},
  94. },
  95. {
  96. name: "All tags to remove",
  97. tags: map[string]string{
  98. "key1": "value1",
  99. "key2": "value2",
  100. },
  101. metaTags: map[string]string{},
  102. expected: []string{"key1", "key2"},
  103. },
  104. {
  105. name: "Empty tags and metaTags",
  106. tags: map[string]string{},
  107. metaTags: map[string]string{},
  108. expected: []string{},
  109. },
  110. {
  111. name: "Empty metaTags with non-empty tags",
  112. tags: map[string]string{
  113. "key1": "value1",
  114. },
  115. metaTags: map[string]string{},
  116. expected: []string{"key1"},
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. result := FindTagKeysToRemove(tt.tags, tt.metaTags)
  122. assert.ElementsMatch(t, tt.expected, result)
  123. })
  124. }
  125. }