Skip to content

Commit dee4e10

Browse files
committed
add e2e test on backupjob
Signed-off-by: Andrey Kolkov <[email protected]>
1 parent 263afb4 commit dee4e10

1 file changed

Lines changed: 213 additions & 0 deletions

File tree

‎hack/e2e-apps/backup.bats‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#!/usr/bin/env bats
2+
3+
@test "Create Backup for Virtual Machine" {
4+
# Test variables
5+
bucket_name='test-backup-bucket'
6+
vm_name='test-backup-vm'
7+
backupjob_name='test-backup-job'
8+
namespace='tenant-test'
9+
10+
# Step 1: Create the bucket resource
11+
kubectl apply -f - <<EOF
12+
apiVersion: apps.cozystack.io/v1alpha1
13+
kind: Bucket
14+
metadata:
15+
name: ${bucket_name}
16+
namespace: ${namespace}
17+
spec: {}
18+
EOF
19+
20+
# Wait for the bucket to be ready
21+
kubectl -n ${namespace} wait hr bucket-${bucket_name} --timeout=100s --for=condition=ready
22+
kubectl -n ${namespace} wait bucketclaims.objectstorage.k8s.io bucket-${bucket_name} --timeout=300s --for=jsonpath='{.status.bucketReady}'=true
23+
kubectl -n ${namespace} wait bucketaccesses.objectstorage.k8s.io bucket-${bucket_name} --timeout=300s --for=jsonpath='{.status.accessGranted}'=true
24+
25+
# Get bucket credentials for later S3 verification
26+
kubectl -n ${namespace} get secret bucket-${bucket_name} -ojsonpath='{.data.BucketInfo}' | base64 -d > /tmp/bucket-backup-credentials.json
27+
ACCESS_KEY=$(jq -r '.spec.secretS3.accessKeyID' /tmp/bucket-backup-credentials.json)
28+
SECRET_KEY=$(jq -r '.spec.secretS3.accessSecretKey' /tmp/bucket-backup-credentials.json)
29+
BUCKET_NAME=$(jq -r '.spec.bucketName' /tmp/bucket-backup-credentials.json)
30+
ENDPOINT=$(jq -r '.spec.secretS3.endpoint' /tmp/bucket-backup-credentials.json)
31+
32+
# Step 2: Create the Virtual Machine
33+
kubectl apply -f - <<EOF
34+
apiVersion: apps.cozystack.io/v1alpha1
35+
kind: VirtualMachine
36+
metadata:
37+
name: ${vm_name}
38+
namespace: ${namespace}
39+
spec:
40+
external: false
41+
externalMethod: PortList
42+
externalPorts:
43+
- 22
44+
instanceType: "u1.medium"
45+
instanceProfile: ubuntu
46+
systemDisk:
47+
image: ubuntu
48+
storage: 5Gi
49+
storageClass: replicated
50+
gpus: []
51+
resources: {}
52+
sshKeys:
53+
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPht0dPk5qQ+54g1hSX7A6AUxXJW5T6n/3d7Ga2F8gTF
54+
test@test
55+
cloudInit: |
56+
#cloud-config
57+
users:
58+
- name: test
59+
shell: /bin/bash
60+
sudo: ['ALL=(ALL) NOPASSWD: ALL']
61+
groups: sudo
62+
ssh_authorized_keys:
63+
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPht0dPk5qQ+54g1hSX7A6AUxXJW5T6n/3d7Ga2F8gTF test@test
64+
cloudInitSeed: ""
65+
EOF
66+
67+
# Wait for VM to be ready
68+
sleep 5
69+
kubectl -n ${namespace} wait hr virtual-machine-${vm_name} --timeout=10s --for=condition=ready
70+
kubectl -n ${namespace} wait dv virtual-machine-${vm_name} --timeout=150s --for=condition=ready
71+
kubectl -n ${namespace} wait pvc virtual-machine-${vm_name} --timeout=100s --for=jsonpath='{.status.phase}'=Bound
72+
kubectl -n ${namespace} wait vm virtual-machine-${vm_name} --timeout=100s --for=condition=ready
73+
74+
# Step 3: Create BackupJob
75+
kubectl apply -f - <<EOF
76+
apiVersion: backups.cozystack.io/v1alpha1
77+
kind: BackupJob
78+
metadata:
79+
name: ${backupjob_name}
80+
namespace: ${namespace}
81+
labels:
82+
backups.cozystack.io/triggered-by: e2e-test
83+
spec:
84+
applicationRef:
85+
apiGroup: apps.cozystack.io
86+
kind: VirtualMachine
87+
name: ${vm_name}
88+
storageRef:
89+
apiGroup: apps.cozystack.io
90+
kind: Bucket
91+
name: ${bucket_name}
92+
strategyRef:
93+
apiGroup: strategy.backups.cozystack.io
94+
kind: Velero
95+
name: velero-strategy-default
96+
EOF
97+
98+
# Wait for BackupJob to start (phase should be Running after Velero backup is created)
99+
kubectl -n ${namespace} wait backupjob ${backupjob_name} --timeout=60s --for=jsonpath='{.status.phase}'=Running
100+
101+
# Wait for BackupJob to complete (Succeeded phase)
102+
kubectl -n ${namespace} wait backupjob ${backupjob_name} --timeout=300s --for=jsonpath='{.status.phase}'=Succeeded
103+
104+
# Verify BackupJob status
105+
PHASE=$(kubectl -n ${namespace} get backupjob ${backupjob_name} -o jsonpath='{.status.phase}')
106+
[ "$PHASE" = "Succeeded" ]
107+
108+
# Verify BackupJob has a backupRef
109+
BACKUP_REF=$(kubectl -n ${namespace} get backupjob ${backupjob_name} -o jsonpath='{.status.backupRef.name}')
110+
[ -n "$BACKUP_REF" ]
111+
112+
# Find the Velero backup by searching for backups matching the namespace-backupjob pattern
113+
# Format: namespace-backupjob-timestamp
114+
VELERO_BACKUP_NAME=""
115+
VELERO_BACKUP_PHASE=""
116+
117+
# Wait a bit for the backup to be created and appear in the API
118+
sleep 5
119+
120+
# Find backup by pattern matching namespace-backupjob
121+
for backup in $(kubectl -n cozy-velero get backups.velero.io -o jsonpath='{.items[*].metadata.name}' 2>/dev/null); do
122+
if echo "$backup" | grep -q "^${namespace}-${backupjob_name}-"; then
123+
VELERO_BACKUP_NAME=$backup
124+
VELERO_BACKUP_PHASE=$(kubectl -n cozy-velero get backups.velero.io $backup -o jsonpath='{.status.phase}' 2>/dev/null || echo "")
125+
break
126+
fi
127+
done
128+
129+
# Verify Velero Backup was found
130+
[ -n "$VELERO_BACKUP_NAME" ]
131+
132+
# Wait for Velero Backup to complete (with timeout)
133+
timeout 300 sh -ec "
134+
until kubectl -n cozy-velero get backups.velero.io ${VELERO_BACKUP_NAME} -o jsonpath='{.status.phase}' | grep -q 'Completed\|Failed'; do
135+
sleep 5
136+
done
137+
"
138+
139+
# Verify Velero Backup is Completed
140+
VELERO_BACKUP_PHASE=$(kubectl -n cozy-velero get backups.velero.io ${VELERO_BACKUP_NAME} -o jsonpath='{.status.phase}')
141+
[ "$VELERO_BACKUP_PHASE" = "Completed" ]
142+
143+
# Step 4: Verify S3 has backup data
144+
# Extract endpoint host and port for port-forwarding
145+
ENDPOINT_HOST=$(echo $ENDPOINT | sed 's|https\?://||' | cut -d: -f1)
146+
ENDPOINT_PORT=$(echo $ENDPOINT | sed 's|https\?://||' | cut -d: -f2)
147+
if [ -z "$ENDPOINT_PORT" ]; then
148+
ENDPOINT_PORT=8333
149+
fi
150+
151+
# Start port-forwarding to S3 endpoint
152+
kubectl -n tenant-root port-forward service/seaweedfs-s3 ${ENDPOINT_PORT}:${ENDPOINT_PORT} > /dev/null 2>&1 &
153+
PORT_FORWARD_PID=$!
154+
155+
# Wait for port-forward to be ready
156+
timeout 30 sh -ec "until nc -z localhost ${ENDPOINT_PORT}; do sleep 1; done"
157+
158+
# Set up MinIO alias
159+
mc alias set backup-test https://localhost:${ENDPOINT_PORT} $ACCESS_KEY $SECRET_KEY --insecure
160+
161+
# Check if backup data exists in S3 bucket
162+
# Velero stores backups in a structure like: backups/<backup-name>/
163+
BACKUP_PREFIX="backups/${VELERO_BACKUP_NAME}"
164+
165+
# Wait a bit for backup data to be written to S3
166+
sleep 10
167+
168+
# List backup directory in S3
169+
BACKUP_FILES=$(mc ls backup-test/${BUCKET_NAME}/${BACKUP_PREFIX}/ 2>/dev/null | wc -l || echo "0")
170+
171+
# Verify backup files exist (should have at least metadata files)
172+
if [ "$BACKUP_FILES" -eq "0" ]; then
173+
# Try alternative paths - Velero might use different structure
174+
BACKUP_FILES=$(mc ls backup-test/${BUCKET_NAME}/backups/ 2>/dev/null | grep "${VELERO_BACKUP_NAME}" | wc -l || echo "0")
175+
fi
176+
177+
# At minimum, verify the bucket has some Velero-related data
178+
VELERO_DATA=$(mc ls backup-test/${BUCKET_NAME}/backups/ 2>/dev/null | wc -l || echo "0")
179+
[ "$VELERO_DATA" -gt "0" ]
180+
181+
# Clean up port-forward
182+
kill $PORT_FORWARD_PID 2>/dev/null || true
183+
wait $PORT_FORWARD_PID 2>/dev/null || true
184+
185+
# Step 5: Clean up resources (in reverse order)
186+
187+
# Clean up Velero resources in cozy-velero namespace
188+
if [ -n "$VELERO_BACKUP_NAME" ]; then
189+
kubectl -n cozy-velero delete backups.velero.io ${VELERO_BACKUP_NAME} --wait=false 2>/dev/null || true
190+
fi
191+
192+
# Clean up BackupStorageLocation and VolumeSnapshotLocation (named: namespace-backupjob)
193+
BSL_NAME="${namespace}-${backupjob_name}"
194+
kubectl -n cozy-velero delete backupstoragelocations.velero.io ${BSL_NAME} --wait=false 2>/dev/null || true
195+
kubectl -n cozy-velero delete volumesnapshotlocations.velero.io ${BSL_NAME} --wait=false 2>/dev/null || true
196+
197+
# Clean up Velero credentials secret
198+
SECRET_NAME="backup-${namespace}-${backupjob_name}-s3-credentials"
199+
kubectl -n cozy-velero delete secret ${SECRET_NAME} --wait=false 2>/dev/null || true
200+
201+
# Clean up BackupJob
202+
kubectl -n ${namespace} delete backupjob ${backupjob_name} --wait=false
203+
204+
# Clean up Virtual Machine
205+
kubectl -n ${namespace} delete virtualmachines.apps.cozystack.io ${vm_name} --wait=false
206+
207+
# Clean up Bucket
208+
kubectl -n ${namespace} delete bucket.apps.cozystack.io ${bucket_name} --wait=false
209+
210+
# Clean up temporary files
211+
rm -f /tmp/bucket-backup-credentials.json
212+
}
213+

0 commit comments

Comments
 (0)