|
| 1 | +/* |
| 2 | + * === This file is part of ALICE O² === |
| 3 | + * |
| 4 | + * Copyright 2026 CERN and copyright holders of ALICE O². |
| 5 | + * Author: Michal Tichak <michal.tichak@cern.ch> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * In applying this license CERN does not waive the privileges and |
| 21 | + * immunities granted to it by virtue of its status as an |
| 22 | + * Intergovernmental Organization or submit itself to any jurisdiction. |
| 23 | + */ |
| 24 | + |
| 25 | +package controller |
| 26 | + |
| 27 | +import ( |
| 28 | + "testing" |
| 29 | + |
| 30 | + v1 "k8s.io/api/core/v1" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/runtime" |
| 33 | + "k8s.io/apimachinery/pkg/types" |
| 34 | + |
| 35 | + aliecsv1alpha1 "github.com/AliceO2Group/Control/control-operator/api/v1alpha1" |
| 36 | +) |
| 37 | + |
| 38 | +// newTestScheme is used to register owner of pod properly in TestPodForTask |
| 39 | +func newTestScheme(t *testing.T) *runtime.Scheme { |
| 40 | + t.Helper() |
| 41 | + scheme := runtime.NewScheme() |
| 42 | + if err := aliecsv1alpha1.AddToScheme(scheme); err != nil { |
| 43 | + t.Fatalf("failed to add aliecsv1alpha1 to scheme: %v", err) |
| 44 | + } |
| 45 | + return scheme |
| 46 | +} |
| 47 | + |
| 48 | +func TestPodForTask(t *testing.T) { |
| 49 | + r := &TaskReconciler{Scheme: newTestScheme(t)} |
| 50 | + |
| 51 | + task := &aliecsv1alpha1.Task{ |
| 52 | + ObjectMeta: metav1.ObjectMeta{ |
| 53 | + Name: "my-task", |
| 54 | + Namespace: "my-namespace", |
| 55 | + UID: types.UID("test-uid"), |
| 56 | + }, |
| 57 | + Spec: aliecsv1alpha1.TaskSpec{ |
| 58 | + Pod: v1.PodSpec{ |
| 59 | + RestartPolicy: v1.RestartPolicyAlways, |
| 60 | + Containers: []v1.Container{ |
| 61 | + {Name: "main", Image: "example.io/image:latest"}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + pod := r.podForTask(task) |
| 68 | + |
| 69 | + if pod.Name != "aliecs-task-pod-my-task" { |
| 70 | + t.Errorf("pod.Name = %q, want %q", pod.Name, "aliecs-task-pod-my-task") |
| 71 | + } |
| 72 | + if pod.Namespace != task.Namespace { |
| 73 | + t.Errorf("pod.Namespace = %q, want %q", pod.Namespace, task.Namespace) |
| 74 | + } |
| 75 | + |
| 76 | + wantLabels := map[string]string{ |
| 77 | + "task_name": "my-task", |
| 78 | + "application": "ControlOperator", |
| 79 | + } |
| 80 | + for k, v := range wantLabels { |
| 81 | + if got := pod.Labels[k]; got != v { |
| 82 | + t.Errorf("pod.Labels[%q] = %q, want %q", k, got, v) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if len(pod.Spec.Containers) != 1 || pod.Spec.Containers[0].Name != "main" { |
| 87 | + t.Errorf("pod.Spec.Containers not copied from task.Spec.Pod, got %+v", pod.Spec.Containers) |
| 88 | + } |
| 89 | + |
| 90 | + // podForTask always forces RestartPolicyNever, regardless of what's set on the Task spec. |
| 91 | + if pod.Spec.RestartPolicy != v1.RestartPolicyNever { |
| 92 | + t.Errorf("pod.Spec.RestartPolicy = %q, want %q", pod.Spec.RestartPolicy, v1.RestartPolicyNever) |
| 93 | + } |
| 94 | + |
| 95 | + ownerRefs := pod.GetOwnerReferences() |
| 96 | + if len(ownerRefs) != 1 { |
| 97 | + t.Fatalf("expected exactly one owner reference, got %d: %+v", len(ownerRefs), ownerRefs) |
| 98 | + } |
| 99 | + owner := ownerRefs[0] |
| 100 | + if owner.Kind != "Task" { |
| 101 | + t.Errorf("owner.Kind = %q, want %q", owner.Kind, "Task") |
| 102 | + } |
| 103 | + if owner.Name != task.Name { |
| 104 | + t.Errorf("owner.Name = %q, want %q", owner.Name, task.Name) |
| 105 | + } |
| 106 | + if owner.UID != task.UID { |
| 107 | + t.Errorf("owner.UID = %q, want %q", owner.UID, task.UID) |
| 108 | + } |
| 109 | + if owner.Controller == nil || !*owner.Controller { |
| 110 | + t.Errorf("owner.Controller = %v, want true", owner.Controller) |
| 111 | + } |
| 112 | +} |
0 commit comments