|
1 | 1 | package rollout
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "encoding/json"
|
5 | 6 | "fmt"
|
6 | 7 | "strconv"
|
| 8 | + "strings" |
7 | 9 | "testing"
|
8 | 10 | "time"
|
9 | 11 |
|
| 12 | + log "github.com/sirupsen/logrus" |
10 | 13 | "github.com/stretchr/testify/assert"
|
11 | 14 | corev1 "k8s.io/api/core/v1"
|
12 | 15 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
@@ -1007,6 +1010,92 @@ func TestBlueGreenRolloutScaleUpdateActiveRS(t *testing.T) {
|
1007 | 1010 | f.run(getKey(r2, t))
|
1008 | 1011 | }
|
1009 | 1012 |
|
| 1013 | +func TestBlueGreenRolloutScaleUpdateStableRS(t *testing.T) { |
| 1014 | + f := newFixture(t) |
| 1015 | + defer f.Close() |
| 1016 | + |
| 1017 | + r1 := newBlueGreenRollout("foo", 1, nil, "active", "") |
| 1018 | + rs1 := newReplicaSetWithStatus(r1, 1, 1) |
| 1019 | + r2 := bumpVersion(r1) |
| 1020 | + |
| 1021 | + rs2 := newReplicaSetWithStatus(r2, 1, 1) |
| 1022 | + f.kubeobjects = append(f.kubeobjects, rs1, rs2) |
| 1023 | + f.replicaSetLister = append(f.replicaSetLister, rs1, rs2) |
| 1024 | + |
| 1025 | + rs1PodHash := rs1.Labels[v1alpha1.DefaultRolloutUniqueLabelKey] |
| 1026 | + rs2PodHash := rs2.Labels[v1alpha1.DefaultRolloutUniqueLabelKey] |
| 1027 | + |
| 1028 | + // Make the new RS the active and the old one stable to simulate post-promotion analysis step. |
| 1029 | + r2 = updateBlueGreenRolloutStatus(r2, rs2PodHash, rs2PodHash, rs1PodHash, 1, 1, 2, 1, false, true, false) |
| 1030 | + |
| 1031 | + f.rolloutLister = append(f.rolloutLister, r2) |
| 1032 | + |
| 1033 | + f.objects = append(f.objects, r2) |
| 1034 | + activeSvc := newService("active", 80, map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rs2PodHash}, r2) |
| 1035 | + f.kubeobjects = append(f.kubeobjects, activeSvc) |
| 1036 | + f.serviceLister = append(f.serviceLister, activeSvc) |
| 1037 | + |
| 1038 | + f.expectPatchRolloutAction(r1) |
| 1039 | + |
| 1040 | + // Patch the rollout to get it in the state we want (old RS is stable and new is active) |
| 1041 | + f.run(getKey(r2, t)) |
| 1042 | + // Actually update the replicas now that we are in the desired state (old RS is stable and new is active) |
| 1043 | + r2.Spec.Replicas = pointer.Int32Ptr(2) |
| 1044 | + |
| 1045 | + f.expectUpdateReplicaSetAction(rs1) |
| 1046 | + f.expectUpdateReplicaSetAction(rs2) |
| 1047 | + f.run(getKey(r2, t)) |
| 1048 | +} |
| 1049 | + |
| 1050 | +func TestBlueGreenStableRSReconciliationShouldNotScaleOnFirstTimeRollout(t *testing.T) { |
| 1051 | + f := newFixture(t) |
| 1052 | + prevOutput := log.StandardLogger().Out |
| 1053 | + defer func() { |
| 1054 | + log.SetOutput(prevOutput) |
| 1055 | + }() |
| 1056 | + defer f.Close() |
| 1057 | + |
| 1058 | + // Setup Logging capture |
| 1059 | + buf := bytes.NewBufferString("") |
| 1060 | + log.SetOutput(buf) |
| 1061 | + |
| 1062 | + r := newBlueGreenRollout("foo", 1, nil, "active", "preview") |
| 1063 | + r.Status.Conditions = []v1alpha1.RolloutCondition{} |
| 1064 | + f.rolloutLister = append(f.rolloutLister, r) |
| 1065 | + f.objects = append(f.objects, r) |
| 1066 | + previewSvc := newService("preview", 80, nil, r) |
| 1067 | + activeSvc := newService("active", 80, nil, r) |
| 1068 | + f.kubeobjects = append(f.kubeobjects, previewSvc, activeSvc) |
| 1069 | + f.serviceLister = append(f.serviceLister, activeSvc, previewSvc) |
| 1070 | + |
| 1071 | + rs := newReplicaSet(r, 1) |
| 1072 | + rsPodHash := rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey] |
| 1073 | + generatedConditions := generateConditionsPatchWithCompleted(false, conditions.ReplicaSetUpdatedReason, rs, false, "", true) |
| 1074 | + |
| 1075 | + f.expectCreateReplicaSetAction(rs) |
| 1076 | + f.expectPatchServiceAction(previewSvc, rsPodHash) |
| 1077 | + f.expectUpdateReplicaSetAction(rs) // scale up RS |
| 1078 | + f.expectUpdateRolloutStatusAction(r) |
| 1079 | + expectedPatchWithoutSubs := `{ |
| 1080 | + "status":{ |
| 1081 | + "blueGreen" : { |
| 1082 | + "previewSelector": "%s" |
| 1083 | + }, |
| 1084 | + "conditions": %s, |
| 1085 | + "selector": "foo=bar", |
| 1086 | + "stableRS": "%s", |
| 1087 | + "phase": "Progressing", |
| 1088 | + "message": "more replicas need to be updated" |
| 1089 | + } |
| 1090 | + }` |
| 1091 | + expectedPatch := calculatePatch(r, fmt.Sprintf(expectedPatchWithoutSubs, rsPodHash, generatedConditions, rsPodHash)) |
| 1092 | + f.expectPatchRolloutActionWithPatch(r, expectedPatch) |
| 1093 | + f.run(getKey(r, t)) |
| 1094 | + |
| 1095 | + logMessage := buf.String() |
| 1096 | + assert.True(t, strings.Contains(logMessage, "msg=\"Stable ReplicaSet doesn't exist and hence no reconciliation is required.\""), logMessage) |
| 1097 | +} |
| 1098 | + |
1010 | 1099 | func TestPreviewReplicaCountHandleScaleUpPreviewCheckPoint(t *testing.T) {
|
1011 | 1100 | t.Run("TrueAfterMeetingMinAvailable", func(t *testing.T) {
|
1012 | 1101 | f := newFixture(t)
|
|
0 commit comments