11#! /usr/bin/env bats
22# -----------------------------------------------------------------------------
33# Unit tests for platform migration 61 --> 62 (hand the snapshot CRDs over to
4- # the snapshot-controller release and delete the empty v1alpha1
5- # VolumeGroupSnapshot CRDs). See
4+ # the snapshot-controller release and replace the empty v1alpha1
5+ # VolumeGroupSnapshot CRDs with v1beta2 ones ). See
66# packages/core/platform/images/migrations/migrations/61 for the mechanism.
77#
8- # The fake keeps the CRDs in a state file and applies each annotate, label and
9- # delete to it, so a second run reads what the first one wrote.
8+ # The fake keeps the CRDs in a state file and applies each annotate, label,
9+ # delete and create to it, so a second run reads what the first one wrote. The
10+ # v1beta2 CRDs the migration creates come from group-crds/, standing in for the
11+ # copies `make image-migrations` bakes into the image.
1012#
1113# The harness (docker, jq baked onto the migrations base image, the explicit
1214# `return` in run_migration) is the one hack/migration-55-fluxcd-orphan.bats
@@ -43,6 +45,8 @@ run_migration() {
4345 -e FAKE_CRD_FAIL=" ${FAKE_CRD_FAIL-} " \
4446 -e FAKE_DELETE_FAIL=" ${FAKE_DELETE_FAIL-} " \
4547 -e FAKE_RECORD_FAIL=" ${FAKE_RECORD_FAIL-} " \
48+ -e FAKE_CREATE_FAIL=" ${FAKE_CREATE_FAIL-} " \
49+ -e GROUP_CRD_DIR=/fakebin/group-crds \
4650 " $TESTIMG " " /migrations/$1 " || _run_migration_rc=$?
4751 return " $_run_migration_rc "
4852}
@@ -66,7 +70,7 @@ DOCKERFILE
6670 FAKE_CMDLOG=" $WORK /cmdlog"
6771 : > " $FAKE_CMDLOG "
6872 export NAMESPACE=cozy-system
69- unset FAKE_CRD_FAIL FAKE_DELETE_FAIL FAKE_RECORD_FAIL || true
73+ unset FAKE_CRD_FAIL FAKE_DELETE_FAIL FAKE_RECORD_FAIL FAKE_CREATE_FAIL || true
7074 return 0
7175}
7276
@@ -81,7 +85,7 @@ crd_field() {
8185 jq -r --arg n " $1 " " .items[] | select(.metadata.name == \$ n) | $2 " " $WORK /state/crds.json"
8286}
8387
84- @test " adopts the snapshot CRDs and deletes the empty v1alpha1 group snapshot CRDs" {
88+ @test " adopts the snapshot CRDs and replaces the empty v1alpha1 group snapshot CRDs" {
8589 prep
8690 rc=0
8791 run_migration 61 > " $WORK /out" 2>&1 || rc=$?
@@ -97,8 +101,15 @@ crd_field() {
97101 done
98102
99103 for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
100- grep -qxF " DELETE $p .groupsnapshot.storage.k8s.io" " $FAKE_CMDLOG "
104+ crd=" $p .groupsnapshot.storage.k8s.io"
105+ grep -qxF " DELETE $crd " " $FAKE_CMDLOG "
106+ grep -qxF " CREATE $crd " " $FAKE_CMDLOG "
107+ [ " $( crd_field " $crd " ' [.spec.versions[] | select(.served) | .name] | index("v1beta2") != null' ) " = " true" ]
108+ [ " $( crd_field " $crd " ' .metadata.annotations["meta.helm.sh/release-name"]' ) " = " snapshot-controller" ]
101109 done
110+ # Every delete comes before the first create, so a kept CRD is known before
111+ # anything is created.
112+ [ " $( grep -nE ' ^(DELETE|CREATE) ' " $FAKE_CMDLOG " | sed -n ' 3p' | cut -d' ' -f1 | cut -d: -f2) " = " DELETE" ]
102113 if grep -qF " ANNOTATE volumegroupsnapshot" " $FAKE_CMDLOG " ; then echo " a v1alpha1 CRD was adopted" >&2 ; return 1; fi
103114 if grep -qF " RECORD" " $FAKE_CMDLOG " ; then echo " unexpected record" >&2 ; return 1; fi
104115 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
@@ -113,7 +124,7 @@ crd_field() {
113124 run_migration 61 > " $WORK /out" 2>&1 || rc=$?
114125 cat " $WORK /out" ; cat " $FAKE_CMDLOG "
115126 [ " $rc " -eq 0 ]
116- if grep -qE " ^(ANNOTATE|LABEL|DELETE)" " $FAKE_CMDLOG " ; then echo " second run changed a CRD" >&2 ; return 1; fi
127+ if grep -qE " ^(ANNOTATE|LABEL|DELETE|CREATE )" " $FAKE_CMDLOG " ; then echo " second run changed a CRD" >&2 ; return 1; fi
117128 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
118129 rm -rf " $WORK "
119130}
@@ -129,6 +140,7 @@ crd_field() {
129140 echo " a CRD holding objects was deleted" >&2 ; return 1
130141 fi
131142 grep -qxF " DELETE volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io" " $FAKE_CMDLOG "
143+ if grep -qF " CREATE" " $FAKE_CMDLOG " ; then echo " a v1beta2 CRD was created next to a kept v1alpha1 one" >&2 ; return 1; fi
132144 grep -qF " cozystack.io/migration-61-needs-attention=volumegroupsnapshots.groupsnapshot.storage.k8s.io=2" " $FAKE_CMDLOG "
133145 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
134146 rm -rf " $WORK "
@@ -155,7 +167,7 @@ crd_field() {
155167 run_migration 61 > " $WORK /out" 2>&1 || rc=$?
156168 cat " $WORK /out" ; cat " $FAKE_CMDLOG "
157169 [ " $rc " -eq 0 ]
158- if grep -qE " ^(ANNOTATE|LABEL|DELETE)" " $FAKE_CMDLOG " ; then echo " a foreign CRD was changed" >&2 ; return 1; fi
170+ if grep -qE " ^(ANNOTATE|LABEL|DELETE|CREATE )" " $FAKE_CMDLOG " ; then echo " a foreign CRD was changed" >&2 ; return 1; fi
159171 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
160172 rm -rf " $WORK "
161173}
@@ -168,7 +180,7 @@ crd_field() {
168180 run_migration 61 > " $WORK /out" 2>&1 || rc=$?
169181 cat " $WORK /out" ; cat " $FAKE_CMDLOG "
170182 [ " $rc " -eq 0 ]
171- if grep -qF " DELETE" " $FAKE_CMDLOG " ; then echo " a v1beta2 CRD was deleted " >&2 ; return 1; fi
183+ if grep -qE " ^( DELETE|CREATE) " " $FAKE_CMDLOG " ; then echo " a v1beta2 CRD was replaced " >&2 ; return 1; fi
172184 [ " $( crd_field volumegroupsnapshots.groupsnapshot.storage.k8s.io ' .metadata.annotations["meta.helm.sh/release-name"]' ) " = " snapshot-controller" ]
173185 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
174186 rm -rf " $WORK "
@@ -181,7 +193,7 @@ crd_field() {
181193 run_migration 61 > " $WORK /out" 2>&1 || rc=$?
182194 cat " $WORK /out" ; cat " $FAKE_CMDLOG "
183195 [ " $rc " -eq 0 ]
184- if grep -qE " ^(ANNOTATE|LABEL|DELETE)" " $FAKE_CMDLOG " ; then echo " unexpected change" >&2 ; return 1; fi
196+ if grep -qE " ^(ANNOTATE|LABEL|DELETE|CREATE )" " $FAKE_CMDLOG " ; then echo " unexpected change" >&2 ; return 1; fi
185197 grep -qxF " STAMP 62" " $FAKE_CMDLOG "
186198 rm -rf " $WORK "
187199}
@@ -207,3 +219,25 @@ crd_field() {
207219 if grep -qF " STAMP" " $FAKE_CMDLOG " ; then echo " unexpected STAMP after failed delete" >&2 ; return 1; fi
208220 rm -rf " $WORK "
209221}
222+
223+ @test " a failing create aborts before stamping, and the retry creates the CRDs" {
224+ prep
225+ export FAKE_CREATE_FAIL=" Error from server (InternalError): etcdserver: request timed out"
226+ rc=0
227+ run_migration 61 > " $WORK /out" 2>&1 || rc=$?
228+ cat " $WORK /out" ; cat " $FAKE_CMDLOG "
229+ [ " $rc " -ne 0 ]
230+ if grep -qF " STAMP" " $FAKE_CMDLOG " ; then echo " unexpected STAMP after failed create" >&2 ; return 1; fi
231+
232+ unset FAKE_CREATE_FAIL
233+ : > " $FAKE_CMDLOG "
234+ rc=0
235+ run_migration 61 > " $WORK /out" 2>&1 || rc=$?
236+ cat " $WORK /out" ; cat " $FAKE_CMDLOG "
237+ [ " $rc " -eq 0 ]
238+ for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
239+ grep -qxF " CREATE $p .groupsnapshot.storage.k8s.io" " $FAKE_CMDLOG "
240+ done
241+ grep -qxF " STAMP 62" " $FAKE_CMDLOG "
242+ rm -rf " $WORK "
243+ }
0 commit comments