|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + batchv1 "k8s.io/api/batch/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/client-go/kubernetes/scheme" |
| 12 | +) |
| 13 | + |
| 14 | +func renderExecutorJobWithVolumes(t *testing.T, taskVolumes []string, pvcVolumes []Volume, needsPVC bool) *batchv1.Job { |
| 15 | + t.Helper() |
| 16 | + |
| 17 | + tmplBytes, err := os.ReadFile("../config/kubernetes/executor-job.yaml") |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("read executor-job.yaml: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + tpl, err := template.New("executor").Parse(string(tmplBytes)) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("parse template: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + data := map[string]interface{}{ |
| 28 | + "TaskId": "task-abc", |
| 29 | + "JobId": 0, |
| 30 | + "Namespace": "test-ns", |
| 31 | + "JobsNamespace": "test-ns", |
| 32 | + "Command": []string{"echo ok"}, |
| 33 | + "UseShell": false, |
| 34 | + "Workdir": "/", |
| 35 | + "Volumes": pvcVolumes, |
| 36 | + "TaskVolumes": taskVolumes, |
| 37 | + "Env": map[string]string{}, |
| 38 | + "Cpus": "100m", |
| 39 | + "RamGb": "128Mi", |
| 40 | + "DiskGb": "1Gi", |
| 41 | + "CpusLimit": "0", |
| 42 | + "RamGbLimit": "0", |
| 43 | + "DiskGbLimit": "0", |
| 44 | + "Image": "alpine", |
| 45 | + "NeedsPVC": needsPVC, |
| 46 | + "NodeSelector": nil, |
| 47 | + "Tolerations": nil, |
| 48 | + "ServiceAccountName": "funnel-sa-test-ns", |
| 49 | + } |
| 50 | + |
| 51 | + var buf bytes.Buffer |
| 52 | + if err := tpl.Execute(&buf, data); err != nil { |
| 53 | + t.Fatalf("execute template: %v\nrendered:\n%s", err, buf.String()) |
| 54 | + } |
| 55 | + |
| 56 | + obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(buf.Bytes(), nil, nil) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("decode rendered template: %v\nrendered:\n%s", err, buf.String()) |
| 59 | + } |
| 60 | + |
| 61 | + job, ok := obj.(*batchv1.Job) |
| 62 | + if !ok { |
| 63 | + t.Fatalf("decoded object is not Job: %T", obj) |
| 64 | + } |
| 65 | + return job |
| 66 | +} |
| 67 | + |
| 68 | +// TestTaskVolumesRenderedAsEmptyDir verifies that TES task.Volumes are rendered |
| 69 | +// as emptyDir volumes (not PVC mounts) in the executor job pod spec. |
| 70 | +func TestTaskVolumesRenderedAsEmptyDir(t *testing.T) { |
| 71 | + job := renderExecutorJobWithVolumes(t, |
| 72 | + []string{"/vol", "/tmp/shared"}, |
| 73 | + nil, // no PVC volumes |
| 74 | + false, // no PVC needed |
| 75 | + ) |
| 76 | + |
| 77 | + podSpec := job.Spec.Template.Spec |
| 78 | + |
| 79 | + // Expect two volumes, both emptyDir |
| 80 | + if len(podSpec.Volumes) != 2 { |
| 81 | + t.Fatalf("expected 2 volumes, got %d: %+v", len(podSpec.Volumes), podSpec.Volumes) |
| 82 | + } |
| 83 | + for _, v := range podSpec.Volumes { |
| 84 | + if v.EmptyDir == nil { |
| 85 | + t.Errorf("volume %q should be emptyDir, got: %+v", v.Name, v.VolumeSource) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Expect two volumeMounts in the container |
| 90 | + if len(podSpec.Containers) == 0 { |
| 91 | + t.Fatal("no containers in pod spec") |
| 92 | + } |
| 93 | + mounts := podSpec.Containers[0].VolumeMounts |
| 94 | + if len(mounts) != 2 { |
| 95 | + t.Fatalf("expected 2 volumeMounts, got %d: %+v", len(mounts), mounts) |
| 96 | + } |
| 97 | + |
| 98 | + mountPaths := map[string]bool{} |
| 99 | + for _, m := range mounts { |
| 100 | + mountPaths[m.MountPath] = true |
| 101 | + } |
| 102 | + for _, vol := range []string{"/vol", "/tmp/shared"} { |
| 103 | + if !mountPaths[vol] { |
| 104 | + t.Errorf("expected mountPath %q not found in mounts: %+v", vol, mounts) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// TestTaskVolumesDoNotUsePVC verifies that when a task only has volumes (no |
| 110 | +// inputs/outputs), no PVC volume appears in the pod spec. |
| 111 | +func TestTaskVolumesDoNotUsePVC(t *testing.T) { |
| 112 | + job := renderExecutorJobWithVolumes(t, |
| 113 | + []string{"/data"}, |
| 114 | + nil, |
| 115 | + false, |
| 116 | + ) |
| 117 | + |
| 118 | + for _, v := range job.Spec.Template.Spec.Volumes { |
| 119 | + if v.PersistentVolumeClaim != nil { |
| 120 | + t.Errorf("unexpected PVC volume %q when task has only TES volumes", v.Name) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// TestTaskVolumesAndPVCCoexist verifies that a task with both inputs (PVC) and |
| 126 | +// TES volumes renders both a PVC mount and emptyDir mounts without conflict. |
| 127 | +func TestTaskVolumesAndPVCCoexist(t *testing.T) { |
| 128 | + pvcVols := []Volume{ |
| 129 | + {HostPath: "/work/inputs/file.txt", ContainerPath: "/inputs/file.txt", Readonly: false}, |
| 130 | + } |
| 131 | + job := renderExecutorJobWithVolumes(t, |
| 132 | + []string{"/vol"}, |
| 133 | + pvcVols, |
| 134 | + true, // has PVC for inputs |
| 135 | + ) |
| 136 | + |
| 137 | + podSpec := job.Spec.Template.Spec |
| 138 | + |
| 139 | + var hasEmptyDir, hasPVC bool |
| 140 | + for _, v := range podSpec.Volumes { |
| 141 | + if v.EmptyDir != nil { |
| 142 | + hasEmptyDir = true |
| 143 | + } |
| 144 | + if v.PersistentVolumeClaim != nil { |
| 145 | + hasPVC = true |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if !hasEmptyDir { |
| 150 | + t.Error("expected at least one emptyDir volume for TES task volume") |
| 151 | + } |
| 152 | + if !hasPVC { |
| 153 | + t.Error("expected PVC volume for inputs") |
| 154 | + } |
| 155 | + |
| 156 | + // Verify no mountPath appears twice |
| 157 | + mounts := podSpec.Containers[0].VolumeMounts |
| 158 | + seen := map[string]int{} |
| 159 | + for _, m := range mounts { |
| 160 | + seen[m.MountPath]++ |
| 161 | + } |
| 162 | + for path, count := range seen { |
| 163 | + if count > 1 { |
| 164 | + t.Errorf("mountPath %q appears %d times — duplicate mount", path, count) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Verify the emptyDir mount does not use a subPath (subPath is PVC-specific) |
| 169 | + for _, m := range mounts { |
| 170 | + if m.MountPath == "/vol" && m.SubPath != "" { |
| 171 | + t.Errorf("emptyDir mount at /vol should not have subPath, got %q", m.SubPath) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + _ = corev1.EmptyDirVolumeSource{} // ensure corev1 import used |
| 176 | +} |
0 commit comments