|
| 1 | +/** |
| 2 | +# Copyright (c) NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package image |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestImagePath_ValidSpec(t *testing.T) { |
| 27 | + const envName = "TEST_IMAGE_PATH_VALID" |
| 28 | + |
| 29 | + t.Run("repository, image and tag version resolve to repo/image:tag", func(t *testing.T) { |
| 30 | + path, err := ImagePath("nvcr.io/nvidia", "gpu-operator", "v1.0.0", envName) |
| 31 | + require.NoError(t, err) |
| 32 | + assert.Equal(t, "nvcr.io/nvidia/gpu-operator:v1.0.0", path) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("sha256 version resolves to a digest reference", func(t *testing.T) { |
| 36 | + path, err := ImagePath("nvcr.io/nvidia", "gpu-operator", "sha256:cafe", envName) |
| 37 | + require.NoError(t, err) |
| 38 | + assert.Equal(t, "nvcr.io/nvidia/gpu-operator@sha256:cafe", path) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestImagePath_InvalidSpec(t *testing.T) { |
| 43 | + const envName = "TEST_IMAGE_PATH_ENV_INVALID" |
| 44 | + |
| 45 | + testCases := []struct { |
| 46 | + description string |
| 47 | + repository string |
| 48 | + image string |
| 49 | + version string |
| 50 | + }{ |
| 51 | + { |
| 52 | + description: "empty repository with tag version", |
| 53 | + repository: "", |
| 54 | + image: "gpu-operator", |
| 55 | + version: "v1.0.0", |
| 56 | + }, |
| 57 | + { |
| 58 | + description: "empty repository with digest version", |
| 59 | + repository: "", |
| 60 | + image: "gpu-operator", |
| 61 | + version: "sha256:cafe", |
| 62 | + }, |
| 63 | + { |
| 64 | + description: "empty image with repository and version set", |
| 65 | + repository: "nvcr.io/nvidia", |
| 66 | + image: "", |
| 67 | + version: "v1.0.0", |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "empty repository and image with tag version", |
| 71 | + repository: "", |
| 72 | + image: "", |
| 73 | + version: "v1.0.0", |
| 74 | + }, |
| 75 | + { |
| 76 | + description: "empty repository and image with digest version", |
| 77 | + repository: "", |
| 78 | + image: "", |
| 79 | + version: "sha256:abc", |
| 80 | + }, |
| 81 | + { |
| 82 | + description: "empty version with repository and image set", |
| 83 | + repository: "nvcr.io/nvidia", |
| 84 | + image: "gpu-operator", |
| 85 | + version: "", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range testCases { |
| 90 | + t.Run(tc.description, func(t *testing.T) { |
| 91 | + // An env value must not rescue an incomplete spec. |
| 92 | + t.Setenv(envName, "from-env/op:v9") |
| 93 | + |
| 94 | + path, err := ImagePath(tc.repository, tc.image, tc.version, envName) |
| 95 | + |
| 96 | + require.Error(t, err) |
| 97 | + assert.Empty(t, path) |
| 98 | + assert.ErrorContains(t, err, "invalid image specification") |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestImagePath_KbldPassthrough(t *testing.T) { |
| 104 | + const envName = "TEST_IMAGE_PATH_KBLD" |
| 105 | + |
| 106 | + path, err := ImagePath("", "nvcr.io/nvidia/driver@sha256:cafe", "", envName) |
| 107 | + require.NoError(t, err) |
| 108 | + assert.Equal(t, "nvcr.io/nvidia/driver@sha256:cafe", path) |
| 109 | +} |
| 110 | + |
| 111 | +func TestImagePath_EnvFallback(t *testing.T) { |
| 112 | + const envName = "TEST_IMAGE_PATH_ENV_FALLBACK" |
| 113 | + t.Setenv(envName, "from-env/op:v9") |
| 114 | + |
| 115 | + path, err := ImagePath("", "", "", envName) |
| 116 | + require.NoError(t, err) |
| 117 | + assert.Equal(t, "from-env/op:v9", path) |
| 118 | +} |
| 119 | + |
| 120 | +func TestImagePath_EmptyError(t *testing.T) { |
| 121 | + const envName = "TEST_IMAGE_PATH_EMPTY" |
| 122 | + |
| 123 | + path, err := ImagePath("", "", "", envName) |
| 124 | + require.Error(t, err) |
| 125 | + assert.Empty(t, path) |
| 126 | + assert.ErrorContains(t, err, "empty image path provided through both CR and ENV "+envName) |
| 127 | +} |
0 commit comments