@@ -11,10 +11,13 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"strconv"
14
+ "strings"
14
15
"testing"
16
+ "time"
15
17
16
18
"github.com/google/go-cmp/cmp"
17
19
"github.com/google/go-cmp/cmp/cmpopts"
20
+ "github.com/prometheus/client_golang/prometheus/testutil"
18
21
corev1 "k8s.io/api/core/v1"
19
22
apierrors "k8s.io/apimachinery/pkg/api/errors"
20
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,6 +31,7 @@ import (
28
31
29
32
fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
30
33
"go.goms.io/fleet/pkg/utils/controller"
34
+ "go.goms.io/fleet/pkg/utils/controller/metrics"
31
35
"go.goms.io/fleet/pkg/utils/defaulter"
32
36
"go.goms.io/fleet/test/utils/resource"
33
37
)
@@ -3073,7 +3077,9 @@ func TestHandleDelete(t *testing.T) {
3073
3077
}
3074
3078
3075
3079
func TestIsRolloutComplete (t * testing.T ) {
3080
+ metricMetadata := "\n # HELP fleet_workload_placement_status Placement status\n # TYPE fleet_workload_placement_status gauge"
3076
3081
crpGeneration := int64 (25 )
3082
+ currentTime := metav1 .Now ()
3077
3083
tests := []struct {
3078
3084
name string
3079
3085
conditions []metav1.Condition
@@ -3086,31 +3092,37 @@ func TestIsRolloutComplete(t *testing.T) {
3086
3092
Status : metav1 .ConditionTrue ,
3087
3093
Type : string (fleetv1beta1 .ClusterResourcePlacementAppliedConditionType ),
3088
3094
ObservedGeneration : crpGeneration ,
3095
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 5 )),
3089
3096
},
3090
3097
{
3091
3098
Status : metav1 .ConditionTrue ,
3092
3099
Type : string (fleetv1beta1 .ClusterResourcePlacementAvailableConditionType ),
3093
3100
ObservedGeneration : crpGeneration ,
3101
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 4 )),
3094
3102
},
3095
3103
{
3096
3104
Status : metav1 .ConditionTrue ,
3097
3105
Type : string (fleetv1beta1 .ClusterResourcePlacementOverriddenConditionType ),
3098
3106
ObservedGeneration : crpGeneration ,
3107
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 2 )),
3099
3108
},
3100
3109
{
3101
3110
Status : metav1 .ConditionTrue ,
3102
3111
Type : string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType ),
3103
3112
ObservedGeneration : crpGeneration ,
3113
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second )),
3104
3114
},
3105
3115
{
3106
3116
Status : metav1 .ConditionTrue ,
3107
3117
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3108
3118
ObservedGeneration : crpGeneration ,
3119
+ LastTransitionTime : currentTime ,
3109
3120
},
3110
3121
{
3111
3122
Status : metav1 .ConditionTrue ,
3112
3123
Type : string (fleetv1beta1 .ClusterResourcePlacementWorkSynchronizedConditionType ),
3113
3124
ObservedGeneration : crpGeneration ,
3125
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 3 )),
3114
3126
},
3115
3127
},
3116
3128
want : true ,
@@ -3122,6 +3134,7 @@ func TestIsRolloutComplete(t *testing.T) {
3122
3134
Status : metav1 .ConditionUnknown ,
3123
3135
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3124
3136
ObservedGeneration : crpGeneration ,
3137
+ LastTransitionTime : currentTime ,
3125
3138
},
3126
3139
},
3127
3140
want : false ,
@@ -3133,6 +3146,7 @@ func TestIsRolloutComplete(t *testing.T) {
3133
3146
Status : metav1 .ConditionTrue ,
3134
3147
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3135
3148
ObservedGeneration : crpGeneration ,
3149
+ LastTransitionTime : currentTime ,
3136
3150
},
3137
3151
},
3138
3152
want : false ,
@@ -3144,16 +3158,19 @@ func TestIsRolloutComplete(t *testing.T) {
3144
3158
Status : metav1 .ConditionTrue ,
3145
3159
Type : string (fleetv1beta1 .ClusterResourcePlacementOverriddenConditionType ),
3146
3160
ObservedGeneration : 1 ,
3161
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 2 )),
3147
3162
},
3148
3163
{
3149
3164
Status : metav1 .ConditionTrue ,
3150
3165
Type : string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType ),
3151
3166
ObservedGeneration : crpGeneration ,
3167
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second )),
3152
3168
},
3153
3169
{
3154
3170
Status : metav1 .ConditionTrue ,
3155
3171
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3156
3172
ObservedGeneration : crpGeneration ,
3173
+ LastTransitionTime : currentTime ,
3157
3174
},
3158
3175
},
3159
3176
want : false ,
@@ -3165,21 +3182,25 @@ func TestIsRolloutComplete(t *testing.T) {
3165
3182
Status : metav1 .ConditionTrue ,
3166
3183
Type : string (fleetv1beta1 .ClusterResourcePlacementOverriddenConditionType ),
3167
3184
ObservedGeneration : crpGeneration ,
3185
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 2 )),
3168
3186
},
3169
3187
{
3170
3188
Status : metav1 .ConditionTrue ,
3171
3189
Type : string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType ),
3172
3190
ObservedGeneration : crpGeneration ,
3191
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second )),
3173
3192
},
3174
3193
{
3175
3194
Status : metav1 .ConditionTrue ,
3176
3195
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3177
3196
ObservedGeneration : crpGeneration ,
3197
+ LastTransitionTime : currentTime ,
3178
3198
},
3179
3199
{
3180
3200
Status : metav1 .ConditionFalse ,
3181
3201
Type : string (fleetv1beta1 .ClusterResourcePlacementWorkSynchronizedConditionType ),
3182
3202
ObservedGeneration : crpGeneration ,
3203
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 3 )),
3183
3204
},
3184
3205
},
3185
3206
want : false ,
@@ -3191,21 +3212,25 @@ func TestIsRolloutComplete(t *testing.T) {
3191
3212
Status : metav1 .ConditionTrue ,
3192
3213
Type : string (fleetv1beta1 .ClusterResourcePlacementOverriddenConditionType ),
3193
3214
ObservedGeneration : crpGeneration ,
3215
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 2 )),
3194
3216
},
3195
3217
{
3196
3218
Status : metav1 .ConditionTrue ,
3197
3219
Type : string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType ),
3198
3220
ObservedGeneration : crpGeneration ,
3221
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second )),
3199
3222
},
3200
3223
{
3201
3224
Status : metav1 .ConditionTrue ,
3202
3225
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3203
3226
ObservedGeneration : crpGeneration ,
3227
+ LastTransitionTime : currentTime ,
3204
3228
},
3205
3229
{
3206
3230
Status : metav1 .ConditionTrue ,
3207
3231
Type : string (fleetv1beta1 .ClusterResourcePlacementWorkSynchronizedConditionType ),
3208
3232
ObservedGeneration : crpGeneration ,
3233
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 3 )),
3209
3234
},
3210
3235
},
3211
3236
want : false ,
@@ -3217,41 +3242,47 @@ func TestIsRolloutComplete(t *testing.T) {
3217
3242
Status : metav1 .ConditionTrue ,
3218
3243
Type : string (fleetv1beta1 .ClusterResourcePlacementAppliedConditionType ),
3219
3244
ObservedGeneration : crpGeneration ,
3245
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 5 )),
3220
3246
},
3221
3247
{
3222
3248
Status : metav1 .ConditionFalse ,
3223
3249
Type : string (fleetv1beta1 .ClusterResourcePlacementAvailableConditionType ),
3224
3250
ObservedGeneration : crpGeneration ,
3251
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 4 )),
3225
3252
},
3226
3253
{
3227
3254
Status : metav1 .ConditionTrue ,
3228
3255
Type : string (fleetv1beta1 .ClusterResourcePlacementOverriddenConditionType ),
3229
3256
ObservedGeneration : crpGeneration ,
3257
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 2 )),
3230
3258
},
3231
3259
{
3232
3260
Status : metav1 .ConditionTrue ,
3233
3261
Type : string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType ),
3234
3262
ObservedGeneration : crpGeneration ,
3263
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second )),
3235
3264
},
3236
3265
{
3237
3266
Status : metav1 .ConditionTrue ,
3238
3267
Type : string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType ),
3239
3268
ObservedGeneration : crpGeneration ,
3269
+ LastTransitionTime : currentTime ,
3240
3270
},
3241
3271
{
3242
3272
Status : metav1 .ConditionTrue ,
3243
3273
Type : string (fleetv1beta1 .ClusterResourcePlacementWorkSynchronizedConditionType ),
3244
3274
ObservedGeneration : crpGeneration ,
3275
+ LastTransitionTime : metav1 .NewTime (currentTime .Add (time .Second * 3 )),
3245
3276
},
3246
3277
},
3247
3278
want : false ,
3248
3279
},
3249
3280
}
3250
- for _ , tc := range tests {
3281
+ for i , tc := range tests {
3251
3282
t .Run (tc .name , func (t * testing.T ) {
3252
3283
crp := & fleetv1beta1.ClusterResourcePlacement {
3253
3284
ObjectMeta : metav1.ObjectMeta {
3254
- Name : testName ,
3285
+ Name : testName + strconv . Itoa ( i ) ,
3255
3286
Generation : crpGeneration ,
3256
3287
},
3257
3288
Status : fleetv1beta1.ClusterResourcePlacementStatus {
@@ -3262,6 +3293,18 @@ func TestIsRolloutComplete(t *testing.T) {
3262
3293
if got != tc .want {
3263
3294
t .Errorf ("isRolloutCompleted() got %v, want %v" , got , tc .want )
3264
3295
}
3296
+
3297
+ var wantMetrics string
3298
+ for _ , cond := range tc .conditions {
3299
+ lastTransitionTime := float64 (cond .LastTransitionTime .UnixNano () / 1e9 )
3300
+ wantMetrics += fmt .Sprintf ("\n fleet_workload_placement_status{conditionType=\" %[1]s\" ,generation=\" %[2]d\" ,name=\" %[3]s\" ,observedGeneration=\" %[4]d\" ,reason=\" %[5]s\" ,status=\" %[6]s\" } %f" ,
3301
+ cond .Type , crp .Generation , crp .Name , cond .ObservedGeneration , cond .Reason , cond .Status , lastTransitionTime )
3302
+ }
3303
+
3304
+ if err := testutil .CollectAndCompare (metrics .FleetPlacementStatus , strings .NewReader (metricMetadata + wantMetrics + "\n " )); err != nil {
3305
+ t .Errorf ("%s" , err )
3306
+ }
3307
+ metrics .FleetPlacementStatus .Reset ()
3265
3308
})
3266
3309
}
3267
3310
}
0 commit comments