github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/varimport_test.go (about) 1 package taskrun_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/swaros/contxt/module/configure" 8 "github.com/swaros/contxt/module/systools" 9 "github.com/swaros/contxt/module/taskrun" 10 ) 11 12 func clearStrings(compare string) string { 13 compare = strings.ReplaceAll(compare, " ", "") 14 compare = strings.ReplaceAll(compare, "\n", "") 15 compare = strings.ReplaceAll(compare, "\t", "") 16 compare = strings.ReplaceAll(compare, "\r", "") 17 return compare 18 } 19 20 func TestIssue82(t *testing.T) { 21 jsonFileName := "../../docs/test/issue82/docker-inspect.json" 22 data, jerr := taskrun.ImportJSONFile(jsonFileName) 23 if jerr != nil { 24 t.Error(jerr) 25 } 26 27 if _, ok := data["0"]; !ok { 28 t.Error("could not get the key") 29 } 30 31 jsonFileName = "../../docs/test/issue82/mapped-inspect.json" 32 data, jerr = taskrun.ImportJSONFile(jsonFileName) 33 if jerr != nil { 34 t.Error(jerr) 35 } 36 37 if _, ok := data["image"]; !ok { 38 t.Error("could not get the key") 39 } 40 } 41 42 type testingGsonKey struct { 43 varName string 44 path string 45 expected string 46 } 47 48 func TestIssue82Usage(t *testing.T) { 49 assertTestFolderVarFn(t, "issue82", "issue82", func() { 50 assertVarStrNotEquals(t, "IMAGE-INFO", "") 51 52 var alltests []testingGsonKey = []testingGsonKey{ 53 { 54 varName: "json_b", 55 path: "image.0.Id", 56 expected: "sha256:14d13f9624cdefd69d648dec8ec436a08dd50004530199ae8f1a2b88c36755d6", 57 }, 58 { 59 varName: "json_b", 60 path: "image.0.RepoTags.0", 61 expected: "golib-local:latest", 62 }, 63 { 64 varName: "json_a", 65 path: "0.Id", 66 expected: "sha256:14d13f9624cdefd69d648dec8ec436a08dd50004530199ae8f1a2b88c36755d6", 67 }, 68 { 69 varName: "json_a", 70 path: "0.Config.WorkingDir", 71 expected: "/usr/src/golibdb", 72 }, 73 { 74 varName: "json_a", 75 path: "0.Config.Entrypoint.0", 76 expected: "docker-php-entrypoint", 77 }, 78 } 79 80 for _, testCase := range alltests { 81 82 eqCheckVarMapValue( 83 testCase.varName, 84 testCase.path, 85 testCase.expected, 86 func(result string) { /* nothing to do ..we found it */ }, 87 func(result string) { 88 t.Error("not found expected key: ", testCase.varName, testCase.path, testCase.expected, " -> in -> ", result) 89 }, 90 func(result string) { t.Error("the while data on not exists. check key ", testCase.varName) }, 91 ) 92 93 } 94 95 }) 96 } 97 98 func TestIssue82_2(t *testing.T) { 99 jsonFileName := "../../docs/test/issue82/mapped-inspect.json" 100 _, jerr := taskrun.ImportJSONFile(jsonFileName) 101 if jerr != nil { 102 t.Error(jerr) 103 } 104 105 } 106 107 func TestParseArgLine(t *testing.T) { 108 param1 := `i sayed 'Hello you' and got the response 'fuck you'` 109 params, found := taskrun.GetArgQuotedEntries(param1) 110 if !found { 111 t.Error("nothing found. that should not happens") 112 } 113 if len(params) > 2 { 114 t.Error("unexcpected amout if entries", params) 115 } 116 117 allFounds := taskrun.SplitQuoted(param1, " ") 118 if len(allFounds) != 8 { 119 t.Error("unexpected amount of strings ", len(allFounds)) 120 } 121 122 } 123 124 func TestParsingFile(t *testing.T) { 125 fileName := "../../docs/test/varimport/test1.json" 126 jsonMap, err := taskrun.ImportJSONFile(fileName) 127 if err != nil { 128 t.Error("loading file error:", fileName, err) 129 } 130 131 if jsonMap["msg"] != "hello world" { 132 t.Error("msg should be 'hello world'") 133 } 134 } 135 136 func TestParseTemplateFile2(t *testing.T) { 137 jsonFileName := "../../docs/test/varimport/test2.json" 138 templateFilename := "../../docs/test/varimport/test2.yaml" 139 140 template, err := taskrun.ImportFileContent(templateFilename) 141 if err != nil { 142 t.Error("loading file error:", templateFilename, err) 143 } 144 145 jsonMap, jerr := taskrun.ImportJSONFile(jsonFileName) 146 if jerr != nil { 147 t.Error("loading file error:", jsonFileName, jerr) 148 } 149 150 result, herr := taskrun.HandleJSONMap(template, jsonMap) 151 if herr != nil { 152 t.Error("error while parsing:[", herr, "]") 153 } 154 155 compare := `testcase: 156 imports: 157 testint: thomas` 158 // remove all spaces 159 compare2 := clearStrings(compare) 160 result2 := clearStrings(result) 161 162 if compare2 != result2 { 163 t.Error("result is not matching expected result\n", result, "\n", compare) 164 } 165 } 166 167 func TestParseTemplateFile3(t *testing.T) { 168 ymlFileName := "../../docs/test/varimport/values.yaml" 169 templateFilename := "../../docs/test/varimport/contxt_template.yml" 170 171 template, err := taskrun.ImportFileContent(templateFilename) 172 if err != nil { 173 t.Error("loading file error:", templateFilename, err) 174 } 175 176 jsonMap, jerr := taskrun.ImportYAMLFile(ymlFileName) 177 if jerr != nil { 178 t.Error("loading file error:", ymlFileName, jerr) 179 } 180 181 result, herr := taskrun.HandleJSONMap(template, jsonMap) 182 if herr != nil { 183 t.Error("error while parsing:[", herr, "]") 184 } 185 186 compare := `config:import:-"values.yaml"task:-id:testvarsscript:-echotest-run-echo'next'-echoinstance1258` 187 // remove all spaces 188 compare2 := clearStrings(compare) 189 result2 := clearStrings(result) 190 if compare2 != result2 { 191 t.Error("result is not matching expected result\n", result, "\n", compare, "\n short:[", result2, "]") 192 } 193 } 194 195 func TestFolderCheck(t *testing.T) { 196 value, err := taskrun.ImportFolders("../../docs/test/01template/contxt.yml", "../../docs/test/01values/") 197 if err != nil { 198 t.Error(err) 199 } 200 201 check1 := clearStrings(value) 202 compare := `config:version:1.45.06sequencially:truecoloroff:truevariables:checkApi:context.democheckName:awesometask:-id:scriptscript:-echo'hallowelt'-ls-ga-echo'tagout_1valuehello'-echo'tagout_2valueworld'listener:-trigger:onerror:trueonoutcountLess:0onoutcountMore:0onoutContains:-context.json-fatalaction:target:""stopall:falsescript:-echo'triggeredbyoutputparsing'` 203 if check1 != compare { 204 t.Error("result is not matching with expected\n", check1, "\n", compare) 205 } 206 207 } 208 209 func TestTryParse(t *testing.T) { 210 err := taskrun.ImportDataFromYAMLFile("test1", "../../docs/test/foreach/importFile.yaml") 211 if err != nil { 212 t.Error(err) 213 } 214 var script []string 215 script = append(script, "not changed") 216 script = append(script, "#@foreach test1 testData.section.simple") 217 script = append(script, "#@- output:[__LINE__]") 218 script = append(script, "#@end") 219 script = append(script, "not changed too") 220 hitCounter := 0 221 _, _, newScript := taskrun.TryParse(script, func(line string) (bool, int) { 222 hitCounter++ 223 fails := false 224 switch hitCounter { 225 case 1: 226 if line != "not changed" { 227 fails = true 228 } 229 case 2: 230 if line != "output:[firstLine]" { 231 fails = true 232 } 233 case 3: 234 if line != "output:[secondLine]" { 235 fails = true 236 } 237 case 4: 238 if line != "output:[thirdLine]" { 239 fails = true 240 } 241 case 5: 242 if line != "not changed too" { 243 fails = true 244 } 245 } 246 if fails { 247 t.Error("failing because line", hitCounter, "have unexpected content [", line, "]") 248 } 249 return false, systools.ExitOk 250 }) 251 252 if len(newScript) < 1 { 253 t.Error("generated script should not be empty") 254 } 255 if len(newScript) != 5 { 256 t.Error("unexpected result length ", len(newScript)) 257 } 258 259 } 260 261 func TestTryParseError(t *testing.T) { 262 err := taskrun.ImportDataFromYAMLFile("test1", "../../docs/test/foreach/importFile.yaml") 263 if err != nil { 264 t.Error(err) 265 } 266 var script []string 267 script = append(script, "not changed") 268 script = append(script, "#@foreach test1 testData.section.simple") 269 script = append(script, "#@- output:[__LINE__]") 270 script = append(script, "#@end") 271 script = append(script, "not changed too") 272 hitCounter := 0 273 rAbort, rCode, newScript := taskrun.TryParse(script, func(line string) (bool, int) { 274 hitCounter++ 275 fails := false 276 abort := false 277 returnCode := systools.ExitOk 278 switch hitCounter { 279 case 1: 280 if line != "not changed" { 281 fails = true 282 } 283 case 2: 284 if line != "output:[firstLine]" { 285 fails = true 286 } 287 case 3: 288 if line != "output:[secondLine]" { 289 fails = true 290 } 291 abort = true 292 returnCode = systools.ExitByStopReason 293 } 294 if fails { 295 t.Error("failing because line", hitCounter, "have unexpected content [", line, "]") 296 } 297 return abort, returnCode 298 }) 299 300 if len(newScript) < 1 { 301 t.Error("generated script should not be empty") 302 } 303 if len(newScript) != 3 { 304 t.Error("unexpected result length ", len(newScript)) 305 } 306 307 if rAbort == false { 308 t.Error("unexpected abort result ") 309 } 310 311 if rCode != systools.ExitByStopReason { 312 t.Error("unexpected return code") 313 } 314 315 } 316 317 func TestTryParseJsonImport(t *testing.T) { 318 var script []string 319 script = append(script, "#@import-json json-data {\"hello\":\"world\"}") 320 321 taskrun.TryParse(script, func(line string) (bool, int) { 322 return false, systools.ExitOk 323 }) 324 325 have, data := taskrun.GetData("json-data") 326 if have == false { 327 t.Error("json was not imported") 328 } 329 330 if data["hello"] != "world" { 331 t.Error("import was not working as expected") 332 } 333 334 } 335 336 func TestTryParseJsonImportByExec(t *testing.T) { 337 338 var script []string 339 if configure.GetOs() == "windows" { 340 return // skip on windows 341 } else { 342 script = append(script, "#@import-json-exec exec-import-data cat ../../docs/test/foreach/forcat.json") 343 } 344 345 taskrun.TryParse(script, func(line string) (bool, int) { 346 return false, systools.ExitOk 347 }) 348 349 have, data := taskrun.GetData("exec-import-data") 350 if have == false { 351 t.Error("json was not imported") 352 } 353 354 if data["main"] == nil { 355 t.Error("import was not working as expected") 356 } 357 358 } 359 360 func TestTryParseVar(t *testing.T) { 361 if configure.GetOs() == "windows" { 362 return // skip on windows 363 } 364 var script []string 365 taskrun.SetPH("test-var-out", "first") 366 script = append(script, "#@var check-replace-out echo test-${test-var-out}-case") 367 368 taskrun.TryParse(script, func(line string) (bool, int) { 369 return false, systools.ExitOk 370 }) 371 372 teststr := taskrun.GetPH("check-replace-out") 373 if teststr != "test-first-case" { 374 t.Error("set var by command is not working. got [", teststr, "]") 375 } 376 } 377 378 func TestSetVar(t *testing.T) { 379 var script []string 380 script = append(script, "#@set test-var-set hello") 381 taskrun.TryParse(script, func(line string) (bool, int) { 382 return false, systools.ExitOk 383 }) 384 teststr := taskrun.GetPH("test-var-set") 385 if teststr != "hello" { 386 t.Error("set var by command is not working. got [", teststr, "]") 387 } 388 } 389 390 func TestVariablesSet(t *testing.T) { 391 fileName := "docs/test/03values/values.yml" 392 err := taskrun.ImportDataFromYAMLFile("case_a", "../../"+fileName) 393 if err != nil { 394 t.Error(err) 395 } 396 397 // create script section 398 var script []string 399 script = append(script, "#@set-in-map case_a root.first.name rudolf") 400 taskrun.TryParse(script, func(line string) (bool, int) { 401 return false, systools.ExitOk 402 }) 403 404 teststr := taskrun.GetJSONPathValueString("case_a", "root.first.name") 405 if teststr != "rudolf" { 406 t.Error("replace var by path is not working. got [", teststr, "]") 407 } 408 409 } 410 411 func TestExportAsYaml(t *testing.T) { 412 fileName := "docs/test/03values/values.yml" 413 err := taskrun.ImportDataFromYAMLFile("case_a", "../../"+fileName) 414 if err != nil { 415 t.Error(err) 416 } 417 // create script section 418 var script []string 419 script = append(script, "#@export-to-yaml case_a yamlstring") 420 taskrun.TryParse(script, func(line string) (bool, int) { 421 return false, systools.ExitOk 422 }) 423 expect := ` 424 root: 425 first: 426 name: charly 427 ` 428 assertVarStrEquals(t, "yamlstring", expect) 429 } 430 431 func TestDynamicVarReplace(t *testing.T) { 432 if runError := folderRunner("./../../docs/test/03values", t, func(t *testing.T) { 433 // note. they keys are sorted in root. so version must be at the end 434 expectedYaml := ` 435 services: 436 website: 437 host: myhost 438 port: 8080 439 version: 5 440 ` 441 442 taskrun.RunTargets("main", false) 443 assertVarStrEquals(t, "host", "myhost") 444 assertVarStrEquals(t, "A", "myhost:8080") 445 assertVarStrEquals(t, "B", "myhost") 446 assertVarStrEquals(t, "C", expectedYaml) 447 448 }); runError != nil { 449 t.Error(runError) 450 } 451 } 452 453 func TestImportFileAsTemplae(t *testing.T) { 454 if err := folderRunner("./../../docs/test/01tplImport", t, func(t *testing.T) { 455 taskrun.RunTargets("tpl-test", false) 456 mapData := taskrun.GetOriginMap() 457 if mapData == nil { 458 t.Error("task data should be exists") 459 } 460 expectedYaml := ` 461 testing: 462 checks: 463 check-out-ABC: set 464 check-out-CHECK: set 465 check-out-something: set 466 ` 467 assertVarStrEquals(t, "OUT-TO-YAML", expectedYaml) 468 }); err != nil { 469 t.Error(err) 470 } 471 } 472 473 /* 474 func TestTryParseWithKeys(t *testing.T) { 475 err := taskrun.ImportDataFromYAMLFile("test-with-key", "../../docs/test/foreach/importFile.yaml") 476 if err != nil { 477 t.Error(err) 478 } 479 var script []string 480 script = append(script, "not changed") 481 script = append(script, "#@foreach test-with-key testData.section.import.keyValue") 482 script = append(script, "#@- output:[__LINE__]") 483 script = append(script, "#@end") 484 script = append(script, "not changed too") 485 hitCounter := 0 486 _, _, newScript := taskrun.TryParse(script, func(line string) (bool, int) { 487 hitCounter++ 488 fails := false 489 switch hitCounter { 490 case 1: 491 if line != "not changed" { 492 fails = true 493 } 494 case 2: 495 if line != "output:[firstLine]" { 496 fails = true 497 } 498 case 3: 499 if line != "output:[secondLine]" { 500 fails = true 501 } 502 case 4: 503 if line != "output:[thirdLine]" { 504 fails = true 505 } 506 case 5: 507 if line != "not changed too" { 508 fails = true 509 } 510 } 511 if fails { 512 t.Error("failing because line", hitCounter, "have unexpected content [", line, "]") 513 } 514 return false, taskrun.ExitOk 515 }) 516 517 if len(newScript) < 1 { 518 t.Error("generated script should not be empty") 519 } 520 if len(newScript) != 5 { 521 t.Error("unexpected result length ", len(newScript)) 522 } 523 524 } 525 */