|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package v1 |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + batch "k8s.io/api/batch/v1" |
| 29 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + |
| 31 | + pbSchedulerV1 "github.com/arangodb/kube-arangodb/integrations/scheduler/v1/definition" |
| 32 | + schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
| 35 | + "github.com/arangodb/kube-arangodb/pkg/util/tests" |
| 36 | +) |
| 37 | + |
| 38 | +func Test_BatchJob(t *testing.T) { |
| 39 | + ctx, c := context.WithCancel(context.Background()) |
| 40 | + defer c() |
| 41 | + |
| 42 | + client := kclient.NewFakeClientBuilder().Add( |
| 43 | + tests.NewMetaObject(t, tests.FakeNamespace, "test", func(t *testing.T, obj *schedulerApi.ArangoProfile) { |
| 44 | + obj.Spec = schedulerApi.ProfileSpec{} |
| 45 | + }), |
| 46 | + tests.NewMetaObject(t, tests.FakeNamespace, "test-select-all", func(t *testing.T, obj *schedulerApi.ArangoProfile) { |
| 47 | + obj.Spec = schedulerApi.ProfileSpec{ |
| 48 | + Selectors: &schedulerApi.ProfileSelectors{ |
| 49 | + Label: &meta.LabelSelector{}, |
| 50 | + }, |
| 51 | + Template: &schedulerApi.ProfileTemplate{}, |
| 52 | + } |
| 53 | + }), |
| 54 | + tests.NewMetaObject(t, tests.FakeNamespace, "test-select-specific", func(t *testing.T, obj *schedulerApi.ArangoProfile) { |
| 55 | + obj.Spec = schedulerApi.ProfileSpec{ |
| 56 | + Selectors: &schedulerApi.ProfileSelectors{ |
| 57 | + Label: &meta.LabelSelector{ |
| 58 | + MatchLabels: map[string]string{ |
| 59 | + "A": "B", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Template: &schedulerApi.ProfileTemplate{}, |
| 64 | + } |
| 65 | + }), |
| 66 | + ).Client() |
| 67 | + |
| 68 | + scheduler := Client(t, ctx, client, func(c Configuration) Configuration { |
| 69 | + c.Namespace = tests.FakeNamespace |
| 70 | + c.VerifyAccess = false |
| 71 | + return c |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("Ensure job does not exist - get", func(t *testing.T) { |
| 75 | + resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{ |
| 76 | + Name: "test", |
| 77 | + }) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + require.False(t, resp.GetExists()) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("Ensure job does not exist - list", func(t *testing.T) { |
| 84 | + resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{}) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + require.Len(t, resp.GetBatchJobs(), 0) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("Schedule Job", func(t *testing.T) { |
| 91 | + resp, err := scheduler.CreateBatchJob(context.Background(), &pbSchedulerV1.CreateBatchJobRequest{ |
| 92 | + Spec: &pbSchedulerV1.Spec{ |
| 93 | + Metadata: &pbSchedulerV1.Metadata{ |
| 94 | + Name: "test", |
| 95 | + }, |
| 96 | + Job: &pbSchedulerV1.JobBase{ |
| 97 | + Labels: nil, |
| 98 | + Profiles: []string{ |
| 99 | + "test", |
| 100 | + }, |
| 101 | + }, |
| 102 | + Containers: map[string]*pbSchedulerV1.ContainerBase{ |
| 103 | + "example": { |
| 104 | + Image: util.NewType("ubuntu:20.04"), |
| 105 | + Args: []string{ |
| 106 | + "/bin/bash", |
| 107 | + "-c", |
| 108 | + "true", |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + BatchJob: &pbSchedulerV1.BatchJobSpec{ |
| 114 | + Completions: util.NewType[int32](1), |
| 115 | + }, |
| 116 | + }) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + require.EqualValues(t, "test", resp.GetName()) |
| 120 | + require.Len(t, resp.Profiles, 2) |
| 121 | + require.Contains(t, resp.Profiles, "test") |
| 122 | + require.Contains(t, resp.Profiles, "test-select-all") |
| 123 | + require.NotContains(t, resp.Profiles, "test-select-specific") |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("Ensure job exist - get", func(t *testing.T) { |
| 127 | + resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{ |
| 128 | + Name: "test", |
| 129 | + }) |
| 130 | + require.NoError(t, err) |
| 131 | + |
| 132 | + require.True(t, resp.GetExists()) |
| 133 | + }) |
| 134 | + |
| 135 | + t.Run("Ensure job exist - list", func(t *testing.T) { |
| 136 | + resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{}) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + require.Len(t, resp.GetBatchJobs(), 1) |
| 140 | + require.Contains(t, resp.GetBatchJobs(), "test") |
| 141 | + }) |
| 142 | + |
| 143 | + t.Run("Ensure job details - pre", func(t *testing.T) { |
| 144 | + resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{ |
| 145 | + Name: "test", |
| 146 | + }) |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + require.True(t, resp.GetExists()) |
| 150 | + require.EqualValues(t, 0, resp.GetBatchJob().GetStatus().GetSucceeded()) |
| 151 | + }) |
| 152 | + |
| 153 | + t.Run("Ensure job details - update", func(t *testing.T) { |
| 154 | + job := tests.NewMetaObject[*batch.Job](t, tests.FakeNamespace, "test") |
| 155 | + |
| 156 | + tests.RefreshObjectsC(t, client, &job) |
| 157 | + |
| 158 | + job.Status.Succeeded = 1 |
| 159 | + |
| 160 | + tests.UpdateObjectsC(t, client, &job) |
| 161 | + }) |
| 162 | + |
| 163 | + t.Run("Ensure job details - post", func(t *testing.T) { |
| 164 | + resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{ |
| 165 | + Name: "test", |
| 166 | + }) |
| 167 | + require.NoError(t, err) |
| 168 | + |
| 169 | + require.True(t, resp.GetExists()) |
| 170 | + require.EqualValues(t, 1, resp.GetBatchJob().GetStatus().GetSucceeded()) |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("Delete Job", func(t *testing.T) { |
| 174 | + resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{ |
| 175 | + Name: "test", |
| 176 | + }) |
| 177 | + require.NoError(t, err) |
| 178 | + require.True(t, resp.GetExists()) |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("Re-Delete Job", func(t *testing.T) { |
| 182 | + resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{ |
| 183 | + Name: "test", |
| 184 | + }) |
| 185 | + require.NoError(t, err) |
| 186 | + require.False(t, resp.GetExists()) |
| 187 | + }) |
| 188 | + |
| 189 | + t.Run("Ensure job does not exist after deletion - get", func(t *testing.T) { |
| 190 | + resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{ |
| 191 | + Name: "test", |
| 192 | + }) |
| 193 | + require.NoError(t, err) |
| 194 | + |
| 195 | + require.False(t, resp.GetExists()) |
| 196 | + }) |
| 197 | + |
| 198 | + t.Run("Ensure job does not exist after deletion - list", func(t *testing.T) { |
| 199 | + resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{}) |
| 200 | + require.NoError(t, err) |
| 201 | + |
| 202 | + require.Len(t, resp.GetBatchJobs(), 0) |
| 203 | + }) |
| 204 | +} |
0 commit comments