1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package args
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"gotest.tools/v3/assert"
"github.com/sylabs/singularity/v4/pkg/build/types/parser"
)
func TestGetKeyVal(t *testing.T) {
tests := []struct {
name string
input string
expect []string
err string
}{
{
name: "normal case",
input: " k1= v1\n",
expect: []string{
"k1", "v1",
},
err: "",
},
{
name: "normal case 2",
input: "\n k1 = v1\n",
expect: []string{
"k1", "v1",
},
err: "",
},
{
name: "normal case 3",
input: "k1=1.0",
expect: []string{
"k1", "1.0",
},
err: "",
},
{
name: "normal case 4",
input: "k1= a whitespace ",
expect: []string{
"k1", "a whitespace",
},
err: "",
},
{
name: "wrong case because of missing =",
input: "\n k1 v1\n",
expect: []string{},
err: "is not a key=value pair",
},
{
name: "wrong case because of missing key",
input: "\n =v1\n",
expect: []string{},
err: "missing key portion in",
},
{
name: "wrong case because of missing value",
input: "\n k1 =\n",
expect: []string{},
err: "missing value portion in",
},
}
for _, test := range tests {
t.Logf("Starting %s", test.name)
k, v, err := getKeyVal(test.input)
if test.err != "" {
assert.ErrorContains(t, err, test.err)
} else {
assert.Equal(t, k, test.expect[0])
assert.Equal(t, v, test.expect[1])
}
}
}
func TestReader(t *testing.T) {
tests := []struct {
name string
input string
output string
argsMap map[string]string
defaultArgsMap map[string]string
err string
}{
{
name: "normal case",
input: "/script-{{ APP_VER }}",
output: "/script-1.0",
argsMap: map[string]string{
"OS_VER": "1",
"APP_VER": "1.0",
},
defaultArgsMap: map[string]string{},
err: "",
},
{
name: "normal case 2",
input: "/script-{{ OS_VER }}.sh {{ APP_VER }}",
output: "/script-1.sh 1.0",
argsMap: map[string]string{
"OS_VER": "1",
"APP_VER": "1.0",
},
defaultArgsMap: map[string]string{},
err: "",
},
{
name: "normal case 3",
input: "/script-1.sh 1.0",
output: "/script-1.sh 1.0",
argsMap: map[string]string{},
defaultArgsMap: map[string]string{},
err: "",
},
{
name: "normal case 4",
input: "/script-{{ OS_VER }}.sh {{ APP_VER }}",
output: "/script-1.sh 1.0",
argsMap: map[string]string{
"OS_VER": "1",
},
defaultArgsMap: map[string]string{
"APP_VER": "1.0",
},
err: "",
},
{
name: "normal case 5",
input: "/script-{{ OS_VER }}.sh {{ APP_VER }}",
output: "/script-1.sh 1.0",
argsMap: map[string]string{
"OS_VER": "1",
"APP_VER": "1.0",
},
defaultArgsMap: map[string]string{
"APP_VER": "0.0",
},
err: "",
},
{
name: "normal case 6",
input: "/script-{{ \nAPP_VER }}",
output: "/script-1.0",
argsMap: map[string]string{
"OS_VER": "1",
"APP_VER": "1.0",
},
defaultArgsMap: map[string]string{},
err: "",
},
{
name: "wrong case because of missing variable",
input: "/script-{{ OS_VER }}.sh {{ APP_VER }}",
output: "",
argsMap: map[string]string{
"OS_VER": "1",
},
defaultArgsMap: map[string]string{},
err: "is not defined through either --build-arg (--build-arg-file) or 'arguments' section",
},
{
name: "wrong case because of missing variable 2",
input: "/script-{{ OS_VER }}.sh {{ APP_VER }}",
output: "",
argsMap: map[string]string{
"OS_VE": "1",
},
defaultArgsMap: map[string]string{},
err: "is not defined through either --build-arg (--build-arg-file) or 'arguments' section",
},
}
for _, test := range tests {
t.Logf("Starting %s", test.name)
var consumedArgs []string
reader, err := NewReader(
bytes.NewReader([]byte(test.input)),
test.argsMap,
test.defaultArgsMap,
&consumedArgs,
)
var output []byte
if err == nil {
output, err = io.ReadAll(reader)
}
if test.err != "" {
assert.ErrorContains(t, err, test.err)
} else {
assert.Equal(t, string(output), test.output)
}
}
}
func TestReadDefaults(t *testing.T) {
defFilePath := filepath.Join("..", "..", "..", "..", "test", "build-args", "single-stage-unit-test.def")
defFile, err := os.Open(defFilePath)
if err != nil {
t.Fatalf("while trying to open def file %q: %s", defFilePath, err)
}
defer defFile.Close()
defs, err := parser.All(defFile)
if err != nil {
t.Fatalf("while trying to read def file %q: %s", defFilePath, err)
}
assert.Equal(t, len(defs), 1)
defaultArgsMap := ReadDefaults(defs[0])
assert.DeepEqual(t, defaultArgsMap, map[string]string{
"OS_VER": "3.17",
"DEMO": "a demo",
"AUTHOR": "jason",
})
defFilePath = filepath.Join("..", "..", "..", "..", "test", "build-args", "multiple-stage-unit-test.def")
defFile, err = os.Open(defFilePath)
if err != nil {
t.Fatalf("while trying to open def file %q: %s", defFilePath, err)
}
defer defFile.Close()
defs, err = parser.All(defFile)
if err != nil {
t.Fatalf("while trying to read def file %q: %s", defFilePath, err)
}
assert.Equal(t, len(defs), 2)
defaultArgsMap = ReadDefaults(defs[0])
assert.DeepEqual(t, defaultArgsMap, map[string]string{
"HOME": "/root",
})
defaultArgsMap = ReadDefaults(defs[1])
assert.DeepEqual(t, defaultArgsMap, map[string]string{
"FINAL_IMAGE": "alpine:3.17",
"HOME": "/root",
})
}
|