|
| 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 client_test |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + |
| 30 | + . "github.com/onsi/ginkgo/v2" |
| 31 | + . "github.com/onsi/gomega" |
| 32 | + v1 "k8s.io/api/core/v1" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + k8swatch "k8s.io/apimachinery/pkg/watch" |
| 35 | + |
| 36 | + aliecsv1alpha1 "github.com/AliceO2Group/Control/operator/api/v1alpha1" |
| 37 | + "github.com/AliceO2Group/Control/operator/pkg/client" |
| 38 | +) |
| 39 | + |
| 40 | +var _ = Describe("Client", func() { |
| 41 | + var ( |
| 42 | + ctx context.Context |
| 43 | + c *client.Client |
| 44 | + ) |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + ctx = context.Background() |
| 48 | + var err error |
| 49 | + c, err = client.NewFromConfig(cfg, "default") |
| 50 | + Expect(err).NotTo(HaveOccurred()) |
| 51 | + }) |
| 52 | + |
| 53 | + Describe("Task Create, Read, Update, Delete", func() { |
| 54 | + var task *aliecsv1alpha1.Task |
| 55 | + |
| 56 | + BeforeEach(func() { |
| 57 | + task = &aliecsv1alpha1.Task{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{Name: "test-task"}, |
| 59 | + Spec: aliecsv1alpha1.TaskSpec{State: "standby", Pod: v1.PodSpec{Containers: []v1.Container{}}}, |
| 60 | + } |
| 61 | + Expect(c.CreateTask(ctx, task)).To(Succeed()) |
| 62 | + }) |
| 63 | + |
| 64 | + AfterEach(func() { |
| 65 | + _ = c.DeleteTask(ctx, task.Name) |
| 66 | + }) |
| 67 | + |
| 68 | + It("gets a created task", func() { |
| 69 | + got, err := c.GetTask(ctx, "test-task") |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + Expect(got.Name).To(Equal("test-task")) |
| 72 | + Expect(got.Spec.State).To(Equal("standby")) |
| 73 | + }) |
| 74 | + |
| 75 | + It("updates a task", func() { |
| 76 | + got, err := c.GetTask(ctx, "test-task") |
| 77 | + Expect(err).NotTo(HaveOccurred()) |
| 78 | + |
| 79 | + got.Spec.State = "running" |
| 80 | + Expect(c.UpdateTask(ctx, got)).To(Succeed()) |
| 81 | + |
| 82 | + updated, err := c.GetTask(ctx, "test-task") |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + Expect(updated.Spec.State).To(Equal("running")) |
| 85 | + }) |
| 86 | + |
| 87 | + It("deletes a task", func() { |
| 88 | + Expect(c.DeleteTask(ctx, "test-task")).To(Succeed()) |
| 89 | + |
| 90 | + _, err := c.GetTask(ctx, "test-task") |
| 91 | + Expect(err).To(HaveOccurred()) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + Describe("Task Watch", func() { |
| 96 | + It("receives events for task changes", func() { |
| 97 | + watcher, err := c.WatchTasks(ctx) |
| 98 | + Expect(err).NotTo(HaveOccurred()) |
| 99 | + defer watcher.Stop() |
| 100 | + |
| 101 | + task := &aliecsv1alpha1.Task{ |
| 102 | + ObjectMeta: metav1.ObjectMeta{Name: "watch-task"}, |
| 103 | + Spec: aliecsv1alpha1.TaskSpec{State: "standby", Pod: v1.PodSpec{Containers: []v1.Container{}}}, |
| 104 | + } |
| 105 | + Expect(c.CreateTask(ctx, task)).To(Succeed()) |
| 106 | + defer c.DeleteTask(ctx, task.Name) |
| 107 | + |
| 108 | + Eventually(watcher.ResultChan()).Should(Receive(Satisfy(func(e k8swatch.Event) bool { |
| 109 | + t, ok := e.Object.(*aliecsv1alpha1.Task) |
| 110 | + return ok && t.Name == "watch-task" && e.Type == k8swatch.Added |
| 111 | + }))) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + Describe("Environment Create, Read, Update, Delete", func() { |
| 116 | + var env *aliecsv1alpha1.Environment |
| 117 | + |
| 118 | + BeforeEach(func() { |
| 119 | + env = &aliecsv1alpha1.Environment{ |
| 120 | + ObjectMeta: metav1.ObjectMeta{Name: "test-env"}, |
| 121 | + Spec: aliecsv1alpha1.EnvironmentSpec{ |
| 122 | + State: "standby", |
| 123 | + Tasks: map[string][]aliecsv1alpha1.TaskDefinition{}, |
| 124 | + }, |
| 125 | + TaskTemplates: aliecsv1alpha1.TemplateSpecification{ |
| 126 | + Tasks: map[string][]aliecsv1alpha1.TaskReference{}, |
| 127 | + }, |
| 128 | + } |
| 129 | + Expect(c.CreateEnvironment(ctx, env)).To(Succeed()) |
| 130 | + }) |
| 131 | + |
| 132 | + AfterEach(func() { |
| 133 | + _ = c.DeleteEnvironment(ctx, env.Name) |
| 134 | + }) |
| 135 | + |
| 136 | + It("gets a created environment", func() { |
| 137 | + got, err := c.GetEnvironment(ctx, "test-env") |
| 138 | + Expect(err).NotTo(HaveOccurred()) |
| 139 | + Expect(got.Name).To(Equal("test-env")) |
| 140 | + Expect(got.Spec.State).To(Equal("standby")) |
| 141 | + }) |
| 142 | + |
| 143 | + It("updates an environment", func() { |
| 144 | + got, err := c.GetEnvironment(ctx, "test-env") |
| 145 | + Expect(err).NotTo(HaveOccurred()) |
| 146 | + |
| 147 | + got.Spec.State = "running" |
| 148 | + Expect(c.UpdateEnvironment(ctx, got)).To(Succeed()) |
| 149 | + |
| 150 | + updated, err := c.GetEnvironment(ctx, "test-env") |
| 151 | + Expect(err).NotTo(HaveOccurred()) |
| 152 | + Expect(updated.Spec.State).To(Equal("running")) |
| 153 | + }) |
| 154 | + |
| 155 | + It("deletes an environment", func() { |
| 156 | + Expect(c.DeleteEnvironment(ctx, "test-env")).To(Succeed()) |
| 157 | + |
| 158 | + _, err := c.GetEnvironment(ctx, "test-env") |
| 159 | + Expect(err).To(HaveOccurred()) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + Describe("Environment Watch", func() { |
| 164 | + It("receives events for environment changes", func() { |
| 165 | + watcher, err := c.WatchEnvironments(ctx) |
| 166 | + Expect(err).NotTo(HaveOccurred()) |
| 167 | + defer watcher.Stop() |
| 168 | + |
| 169 | + env := &aliecsv1alpha1.Environment{ |
| 170 | + ObjectMeta: metav1.ObjectMeta{Name: "watch-env"}, |
| 171 | + Spec: aliecsv1alpha1.EnvironmentSpec{ |
| 172 | + State: "standby", |
| 173 | + Tasks: map[string][]aliecsv1alpha1.TaskDefinition{}, |
| 174 | + }, |
| 175 | + TaskTemplates: aliecsv1alpha1.TemplateSpecification{ |
| 176 | + Tasks: map[string][]aliecsv1alpha1.TaskReference{}, |
| 177 | + }, |
| 178 | + } |
| 179 | + Expect(c.CreateEnvironment(ctx, env)).To(Succeed()) |
| 180 | + defer c.DeleteEnvironment(ctx, env.Name) |
| 181 | + |
| 182 | + Eventually(watcher.ResultChan()).Should(Receive(Satisfy(func(e k8swatch.Event) bool { |
| 183 | + ev, ok := e.Object.(*aliecsv1alpha1.Environment) |
| 184 | + return ok && ev.Name == "watch-env" && e.Type == k8swatch.Added |
| 185 | + }))) |
| 186 | + }) |
| 187 | + }) |
| 188 | +}) |
0 commit comments