@@ -2,6 +2,8 @@ package controllers
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
6
+ "strings"
5
7
"testing"
6
8
7
9
"github.com/google/go-cmp/cmp"
@@ -64,3 +66,165 @@ func TestSetStatusProgressing(t *testing.T) {
64
66
})
65
67
}
66
68
}
69
+
70
+ func TestTruncateMessage (t * testing.T ) {
71
+ tests := []struct {
72
+ name string
73
+ message string
74
+ expected string
75
+ }{
76
+ {
77
+ name : "short message unchanged" ,
78
+ message : "This is a short message" ,
79
+ expected : "This is a short message" ,
80
+ },
81
+ {
82
+ name : "empty message unchanged" ,
83
+ message : "" ,
84
+ expected : "" ,
85
+ },
86
+ {
87
+ name : "exact max length message unchanged" ,
88
+ message : strings .Repeat ("a" , maxConditionMessageLength ),
89
+ expected : strings .Repeat ("a" , maxConditionMessageLength ),
90
+ },
91
+ {
92
+ name : "message just over limit gets truncated" ,
93
+ message : strings .Repeat ("a" , maxConditionMessageLength + 1 ),
94
+ expected : strings .Repeat ("a" , maxConditionMessageLength - len (truncationSuffix )) + truncationSuffix ,
95
+ },
96
+ {
97
+ name : "very long message gets truncated" ,
98
+ message : strings .Repeat ("word " , 10000 ) + "finalword" ,
99
+ expected : strings .Repeat ("word " , 10000 )[:maxConditionMessageLength - len (truncationSuffix )] + truncationSuffix ,
100
+ },
101
+ }
102
+
103
+ for _ , tc := range tests {
104
+ t .Run (tc .name , func (t * testing.T ) {
105
+ result := truncateMessage (tc .message )
106
+ require .Equal (t , tc .expected , result )
107
+
108
+ // Verify the result is within the limit
109
+ require .LessOrEqual (t , len (result ), maxConditionMessageLength ,
110
+ "truncated message should not exceed max length" )
111
+
112
+ // If the original message was over the limit, verify truncation occurred
113
+ if len (tc .message ) > maxConditionMessageLength {
114
+ require .Contains (t , result , truncationSuffix ,
115
+ "long messages should contain truncation suffix" )
116
+ require .Less (t , len (result ), len (tc .message ),
117
+ "truncated message should be shorter than original" )
118
+ }
119
+ })
120
+ }
121
+ }
122
+
123
+ func TestSetStatusProgressingWithLongMessage (t * testing.T ) {
124
+ // Create a very long CRD validation error message
125
+ longCRDError := fmt .Sprintf ("validating upgrade for CRD \" applications.argoproj.io\" : %s" ,
126
+ strings .Repeat ("v1alpha1: ^.spec.generators: unhandled changes found\n " , 500 ))
127
+
128
+ ext := & ocv1.ClusterExtension {}
129
+ err := errors .New (longCRDError )
130
+ setStatusProgressing (ext , err )
131
+
132
+ cond := meta .FindStatusCondition (ext .Status .Conditions , ocv1 .TypeProgressing )
133
+ require .NotNil (t , cond )
134
+ require .LessOrEqual (t , len (cond .Message ), maxConditionMessageLength )
135
+ require .Contains (t , cond .Message , truncationSuffix )
136
+ require .Contains (t , cond .Message , "validating upgrade for CRD" )
137
+ }
138
+
139
+ func TestSetInstalledStatusConditionUnknownWithLongMessage (t * testing.T ) {
140
+ longError := strings .Repeat ("some long error message " , 2000 )
141
+
142
+ ext := & ocv1.ClusterExtension {}
143
+ setInstalledStatusConditionUnknown (ext , longError )
144
+
145
+ cond := meta .FindStatusCondition (ext .Status .Conditions , ocv1 .TypeInstalled )
146
+ require .NotNil (t , cond )
147
+ require .LessOrEqual (t , len (cond .Message ), maxConditionMessageLength )
148
+ require .Contains (t , cond .Message , truncationSuffix )
149
+ }
150
+
151
+ func TestCRDValidationErrorScenario (t * testing.T ) {
152
+ // Simulate a large CRD validation error like the one in the user's issue
153
+ crdError := "validating upgrade for CRD \" applications.argoproj.io\" : " +
154
+ strings .Repeat ("v1alpha1: ^.spec.generators: unhandled changes found in JSONSchemaProps\n " , 1000 )
155
+
156
+ ext := & ocv1.ClusterExtension {ObjectMeta : metav1.ObjectMeta {Name : "test-extension" }}
157
+ err := errors .New (crdError )
158
+ setStatusProgressing (ext , err )
159
+
160
+ cond := meta .FindStatusCondition (ext .Status .Conditions , ocv1 .TypeProgressing )
161
+ require .NotNil (t , cond )
162
+ require .LessOrEqual (t , len (cond .Message ), maxConditionMessageLength )
163
+ require .Contains (t , cond .Message , truncationSuffix )
164
+ require .Contains (t , cond .Message , "validating upgrade for CRD" )
165
+ require .Equal (t , metav1 .ConditionTrue , cond .Status )
166
+ require .Equal (t , ocv1 .ReasonRetrying , cond .Reason )
167
+ }
168
+
169
+ func TestSetStatusConditionWrapper (t * testing.T ) {
170
+ tests := []struct {
171
+ name string
172
+ message string
173
+ expectedTruncated bool
174
+ }{
175
+ {
176
+ name : "short message not truncated" ,
177
+ message : "This is a short message" ,
178
+ expectedTruncated : false ,
179
+ },
180
+ {
181
+ name : "long message gets truncated" ,
182
+ message : strings .Repeat ("This is a very long message. " , 2000 ),
183
+ expectedTruncated : true ,
184
+ },
185
+ {
186
+ name : "message at exact limit not truncated" ,
187
+ message : strings .Repeat ("a" , maxConditionMessageLength ),
188
+ expectedTruncated : false ,
189
+ },
190
+ {
191
+ name : "message over limit gets truncated" ,
192
+ message : strings .Repeat ("a" , maxConditionMessageLength + 1 ),
193
+ expectedTruncated : true ,
194
+ },
195
+ }
196
+
197
+ for _ , tc := range tests {
198
+ t .Run (tc .name , func (t * testing.T ) {
199
+ var conditions []metav1.Condition
200
+
201
+ // Use our wrapper function
202
+ SetStatusCondition (& conditions , metav1.Condition {
203
+ Type : "TestCondition" ,
204
+ Status : metav1 .ConditionTrue ,
205
+ Reason : "Testing" ,
206
+ Message : tc .message ,
207
+ })
208
+
209
+ require .Len (t , conditions , 1 , "should have exactly one condition" )
210
+ cond := conditions [0 ]
211
+
212
+ // Verify message is within limits
213
+ require .LessOrEqual (t , len (cond .Message ), maxConditionMessageLength ,
214
+ "condition message should not exceed max length" )
215
+
216
+ // Check if truncation occurred as expected
217
+ if tc .expectedTruncated {
218
+ require .Contains (t , cond .Message , truncationSuffix ,
219
+ "long messages should contain truncation suffix" )
220
+ require .Less (t , len (cond .Message ), len (tc .message ),
221
+ "truncated message should be shorter than original" )
222
+ } else {
223
+ require .Equal (t , tc .message , cond .Message ,
224
+ "short messages should remain unchanged" )
225
+ require .NotContains (t , cond .Message , truncationSuffix ,
226
+ "short messages should not contain truncation suffix" )
227
+ }
228
+ })
229
+ }
230
+ }
0 commit comments