forked from renoki-co/php-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-volumesnapshot-live.sh
More file actions
executable file
·266 lines (225 loc) · 8.47 KB
/
test-volumesnapshot-live.sh
File metadata and controls
executable file
·266 lines (225 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
set -e
echo "🚀 Starting VolumeSnapshot Live Cluster Testing"
echo "=============================================="
# Function to cleanup on exit
cleanup() {
echo "🧹 Cleaning up..."
pkill -f "kubectl proxy" || true
minikube delete || true
}
# Set up cleanup trap
trap cleanup EXIT
# Step 1: Delete existing minikube cluster
echo "🗑️ Deleting existing minikube cluster..."
minikube delete || true
# Step 2: Start fresh minikube cluster (use defaults for local system)
echo "🆕 Starting fresh minikube cluster..."
minikube start
# Step 3: Enable required addons (matching CI config)
echo "🔧 Enabling VolumeSnapshots and CSI hostpath driver..."
minikube addons enable volumesnapshots
minikube addons enable csi-hostpath-driver
# Step 4: Wait for cluster to be ready
echo "⏳ Waiting for cluster to be ready..."
kubectl wait --for=condition=ready node --all --timeout=300s
# Step 5: Set up in-cluster config (matching CI config)
echo "🔐 Setting up in-cluster config..."
sudo mkdir -p /var/run/secrets/kubernetes.io/serviceaccount
echo "some-token" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/token
echo "c29tZS1jZXJ0Cg==" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
echo "some-namespace" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/namespace
sudo chmod -R 777 /var/run/secrets/kubernetes.io/serviceaccount/
# Step 6: Apply CRDs (matching CI config)
echo "📋 Setting up CRDs for testing..."
kubectl apply -f https://raw.githubusercontent.com/bitnami-labs/sealed-secrets/main/helm/sealed-secrets/crds/bitnami.com_sealedsecrets.yaml
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.3.0/standard-install.yaml
# Step 7: Start kubectl proxy (matching CI config)
echo "🔌 Starting kubectl proxy on port 8080..."
kubectl proxy --port=8080 --reject-paths="^/non-existent-path" &
# The shell variable $! captures the PID of the most recently started background process.
# shellcheck disable=SC2034
PROXY_PID=$!
# Wait for proxy to be ready
echo "⏳ Waiting for kubectl proxy to be ready..."
sleep 5
# Test proxy connection
echo "🧪 Testing proxy connection..."
curl -s http://127.0.0.1:8080/api/v1/namespaces/default > /dev/null || {
echo "❌ Proxy connection failed"
exit 1
}
echo "✅ Proxy connection successful"
# Step 8: Verify VolumeSnapshot CRDs are available
echo "🔍 Verifying VolumeSnapshot CRDs..."
kubectl get crd volumesnapshots.snapshot.storage.k8s.io || {
echo "❌ VolumeSnapshot CRD not found"
exit 1
}
echo "✅ VolumeSnapshot CRD found"
# Step 9: Verify CSI driver is running
echo "🔍 Verifying CSI hostpath driver..."
kubectl get pods -n kube-system | grep csi-hostpath || {
echo "❌ CSI hostpath driver not running"
exit 1
}
echo "✅ CSI hostpath driver is running"
# Step 10: Check VolumeSnapshotClass
echo "🔍 Checking VolumeSnapshotClass..."
kubectl get volumesnapshotclass || {
echo "⚠️ No VolumeSnapshotClass found, creating one..."
cat <<EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: csi-hostpath-snapclass
driver: hostpath.csi.k8s.io
deletionPolicy: Delete
EOF
}
echo "✅ VolumeSnapshotClass ready"
# Step 11: Run VolumeSnapshot unit tests
echo "🧪 Running VolumeSnapshot unit tests..."
composer test -- --filter="VolumeSnapshotTest" --exclude-group=integration
# Step 12: Run VolumeSnapshot integration tests
echo "🧪 Running VolumeSnapshot integration tests..."
CI=1 composer test -- --filter="VolumeSnapshotIntegrationTest"
# Step 13: Manual validation test
echo "🔬 Running manual validation test..."
# Create a test namespace
kubectl create namespace volume-snapshot-manual-test || true
# Create StorageClass
cat <<EOF | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: csi-hostpath-sc-manual
provisioner: hostpath.csi.k8s.io
volumeBindingMode: Immediate
allowVolumeExpansion: true
parameters:
storagePool: "default"
EOF
# Create PVC
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pvc-manual
namespace: volume-snapshot-manual-test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: csi-hostpath-sc-manual
EOF
# Wait for PVC to be bound
echo "⏳ Waiting for PVC to be bound..."
kubectl wait --for=condition=bound pvc/test-pvc-manual -n volume-snapshot-manual-test --timeout=120s
# Create Pod to write data
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: data-writer-manual
namespace: volume-snapshot-manual-test
spec:
containers:
- name: writer
image: busybox
command: ['sh', '-c', 'echo "Manual test data written at $(date)" > /data/test.txt && sleep 30']
volumeMounts:
- name: data-volume
mountPath: /data
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: test-pvc-manual
restartPolicy: Never
EOF
# Wait for pod to complete
echo "⏳ Waiting for data writer pod to complete..."
kubectl wait --for=condition=completed pod/data-writer-manual -n volume-snapshot-manual-test --timeout=60s
# Create VolumeSnapshot
cat <<EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: test-snapshot-manual
namespace: volume-snapshot-manual-test
spec:
volumeSnapshotClassName: csi-hostpath-snapclass
source:
persistentVolumeClaimName: test-pvc-manual
EOF
# Wait for snapshot to be ready
echo "⏳ Waiting for VolumeSnapshot to be ready..."
timeout=180
counter=0
while [ "$counter" -lt "$timeout" ]; do
ready=$(kubectl get volumesnapshot test-snapshot-manual -n volume-snapshot-manual-test -o jsonpath='{.status.readyToUse}' 2>/dev/null || echo "false")
if [ "$ready" = "true" ]; then
echo "✅ VolumeSnapshot is ready!"
break
fi
error=$(kubectl get volumesnapshot test-snapshot-manual -n volume-snapshot-manual-test -o jsonpath='{.status.error.message}' 2>/dev/null || echo "")
if [ -n "$error" ]; then
echo "❌ VolumeSnapshot failed: $error"
break
fi
sleep 5
counter=$((counter + 5))
echo "⏳ Waiting for snapshot (${counter}s/${timeout}s)..."
done
# Show snapshot status
echo "📊 VolumeSnapshot status:"
kubectl get volumesnapshot test-snapshot-manual -n volume-snapshot-manual-test -o yaml
# Test PHP SDK integration
echo "🐘 Testing PHP SDK integration..."
php -r "
require 'vendor/autoload.php';
use MonoVM\PhpK8s\Test\Kinds\VolumeSnapshot;
\$cluster = new \MonoVM\PhpK8s\KubernetesCluster('http://127.0.0.1:8080');
\$cluster->withoutSslChecks();
echo \"Testing VolumeSnapshot CRD PHP SDK...\n\";
// Register the VolumeSnapshot CRD
VolumeSnapshot::register();
// Test creating a new snapshot via PHP SDK CRD
try {
\$newSnapshot = \$cluster->volumeSnapshot()
->setName('php-sdk-snapshot')
->setNamespace('volume-snapshot-manual-test')
->setVolumeSnapshotClassName('csi-hostpath-snapclass')
->setSourcePvcName('test-pvc-manual');
echo \"✅ Successfully created VolumeSnapshot CRD object: \" . \$newSnapshot->getName() . \"\n\";
echo \" - Type: \" . get_class(\$newSnapshot) . \"\n\";
echo \" - API Version: \" . \$newSnapshot->getApiVersion() . \"\n\";
echo \" - Namespace: \" . \$newSnapshot->getNamespace() . \"\n\";
echo \" - Source PVC: \" . \$newSnapshot->getSourcePvcName() . \"\n\";
// Create it on the cluster
\$createdSnapshot = \$newSnapshot->create();
echo \"✅ Successfully created snapshot on cluster: \" . \$createdSnapshot->getName() . \"\n\";
} catch (Exception \$e) {
echo \"❌ Failed to create snapshot via PHP SDK: \" . \$e->getMessage() . \"\n\";
}
echo \"\n📝 Note: VolumeSnapshot is implemented as a CRD (Custom Resource Definition).\n\";
echo \" Cluster-level methods like getAllVolumeSnapshots() are not available for CRDs.\n\";
echo \" Use direct resource creation and Kubernetes API calls instead.\n\";
"
# Clean up manual test resources
echo "🧹 Cleaning up manual test resources..."
kubectl delete namespace volume-snapshot-manual-test || true
kubectl delete storageclass csi-hostpath-sc-manual || true
echo ""
echo "🎉 VolumeSnapshot live cluster testing completed!"
echo "✅ All tests passed successfully"
echo ""
echo "Summary:"
echo "- ✅ Minikube cluster started with VolumeSnapshots enabled"
echo "- ✅ CSI hostpath driver configured"
echo "- ✅ Unit tests passed"
echo "- ✅ Integration tests passed"
echo "- ✅ Manual validation completed"
echo "- ✅ PHP SDK integration verified"