Skip to content

Commit b5f4fbc

Browse files
committed
Improve test around world mounting
1 parent 14fb109 commit b5f4fbc

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎controllers/minecraftserver_controller_test.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,46 @@ func TestMountedPVC(t *testing.T) {
342342
var pod corev1.Pod
343343
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(&server), &pod)
344344
require.NoError(t, err)
345+
346+
// Find the volume on the Pod for this PVC
347+
// Find the Volume for this config file
348+
found := false
349+
volumeName := ""
350+
for _, v := range pod.Spec.Volumes {
351+
if v.VolumeSource.PersistentVolumeClaim != nil &&
352+
v.VolumeSource.PersistentVolumeClaim.ClaimName == pvc.Name {
353+
// Oh hey found it.
354+
found = true
355+
volumeName = v.Name
356+
break
357+
}
358+
}
359+
assert.True(t, found, "Unable to find Pod volume for the world PVC")
360+
// Find the three expected volume mounts from this volume
361+
foundWorldVolumeMount := false
362+
foundNetherVolumeMount := false
363+
foundEndVolumeMount := false
364+
container := pod.Spec.Containers[0]
365+
for _, vm := range container.VolumeMounts {
366+
if vm.Name == volumeName {
367+
// Hey it's a mount of our Volume
368+
switch vm.MountPath {
369+
case "/data/world":
370+
foundWorldVolumeMount = true
371+
assert.Equal(t, "world", vm.SubPath)
372+
case "/data/world_nether":
373+
foundNetherVolumeMount = true
374+
assert.Equal(t, "world_nether", vm.SubPath)
375+
case "/data/world_the_end":
376+
foundEndVolumeMount = true
377+
assert.Equal(t, "world_the_end", vm.SubPath)
378+
}
379+
// No default, we tolerate other mounts of the world that aren't these three. We just don't care about them.
380+
}
381+
}
382+
assert.True(t, foundWorldVolumeMount, "Unable to find world volume mount")
383+
assert.True(t, foundNetherVolumeMount, "Unable to find world_nether volume mount")
384+
assert.True(t, foundEndVolumeMount, "Unable to find world_the_end volume mount")
345385
}
346386

347387
func TestBasicMinecraftServer(t *testing.T) {

‎pkg/reconcile/pod.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func podForServer(server *v1alpha1.MinecraftServer, configMap *corev1.ConfigMap)
210210
}
211211

212212
if server.Spec.World != nil {
213+
// We expect our PVC to have directories for each dimension: world/, world_nether/, and world_the_end/. The
214+
// container will expect these to be mounted at /data/world/, /data/world_nether/, and /data/world_the_end/. We
215+
// don't want to just mount the whole PVC at /data/ as we don't want to persist *other* files that get populated
216+
// into that directory.
217+
// The solution is to add the PVC as a volume on the pod, and then use three volume mounts on the container to
218+
// explicitly mount the PVC three times at each different directory.
213219
const levelName = "world"
214220
const levelNameNether = "world_nether"
215221
const levelNameTheEnd = "world_the_end"

0 commit comments

Comments
 (0)