|
| 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 resources |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + core "k8s.io/api/core/v1" |
| 28 | +) |
| 29 | + |
| 30 | +func applyImage(t *testing.T, template *core.PodTemplateSpec, ns ...*Image) func(in func(t *testing.T, pod *core.PodTemplateSpec)) { |
| 31 | + var i *Image |
| 32 | + |
| 33 | + for _, n := range ns { |
| 34 | + require.NoError(t, n.Validate()) |
| 35 | + |
| 36 | + i = i.With(n) |
| 37 | + |
| 38 | + require.NoError(t, i.Validate()) |
| 39 | + } |
| 40 | + |
| 41 | + template = template.DeepCopy() |
| 42 | + |
| 43 | + if template == nil { |
| 44 | + template = &core.PodTemplateSpec{} |
| 45 | + } |
| 46 | + |
| 47 | + require.NoError(t, i.Apply(template)) |
| 48 | + |
| 49 | + return func(in func(t *testing.T, spec *core.PodTemplateSpec)) { |
| 50 | + t.Run("Validate", func(t *testing.T) { |
| 51 | + in(t, template) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func Test_Image(t *testing.T) { |
| 57 | + t.Run("With Nil", func(t *testing.T) { |
| 58 | + applyImage(t, nil, nil)(func(t *testing.T, pod *core.PodTemplateSpec) { |
| 59 | + require.Len(t, pod.Spec.ImagePullSecrets, 0) |
| 60 | + }) |
| 61 | + }) |
| 62 | + t.Run("With Empty", func(t *testing.T) { |
| 63 | + applyImage(t, &core.PodTemplateSpec{})(func(t *testing.T, pod *core.PodTemplateSpec) { |
| 64 | + require.Len(t, pod.Spec.ImagePullSecrets, 0) |
| 65 | + }) |
| 66 | + }) |
| 67 | + t.Run("With PS", func(t *testing.T) { |
| 68 | + applyImage(t, &core.PodTemplateSpec{}, &Image{ |
| 69 | + ImagePullSecrets: []string{ |
| 70 | + "secret", |
| 71 | + }, |
| 72 | + })(func(t *testing.T, pod *core.PodTemplateSpec) { |
| 73 | + require.Len(t, pod.Spec.ImagePullSecrets, 1) |
| 74 | + require.Equal(t, "secret", pod.Spec.ImagePullSecrets[0].Name) |
| 75 | + }) |
| 76 | + }) |
| 77 | + t.Run("With PS2", func(t *testing.T) { |
| 78 | + applyImage(t, &core.PodTemplateSpec{}, &Image{ |
| 79 | + ImagePullSecrets: []string{ |
| 80 | + "secret", |
| 81 | + "secret", |
| 82 | + }, |
| 83 | + })(func(t *testing.T, pod *core.PodTemplateSpec) { |
| 84 | + require.Len(t, pod.Spec.ImagePullSecrets, 1) |
| 85 | + require.Equal(t, "secret", pod.Spec.ImagePullSecrets[0].Name) |
| 86 | + }) |
| 87 | + }) |
| 88 | +} |
0 commit comments