strawboss/src/test/nim/unit/tconfiguration.nim
Jonathan Bernard b2d4df0aac WIP Upgrading to Nim 0.19. Getting docker pieces compiling.
* Addressing breaking changes in migration from Nim 0.18 to 0.19.
* Finishing the initial pass at the refactor required to include
  docker-based builds.
* Regaining confidence in the existing functionality by getting all
  tests passing again after docker introduction (still need new tests to
  cover new docker functionality).
2018-12-09 07:09:23 -06:00

153 lines
5.0 KiB
Nim

import json, strtabs, times, tables, unittest, uuids
from langutils import sameContents
from timeutils import trimNanoSec
import ../../../main/nim/strawbosspkg/configuration
suite "load and save configuration objects":
# suite setup & common data
let testProjDefStr = """{ "name": "dummy-project", "repo":
"/non-existent/dir",
"cfgFilePath": "strawhat.json",
"defaultBranch": "deploy",
"envVars": { "VAR1": "value" } }"""
let testProjDef = ProjectDef(
name: "dummy-project",
repo: "/non-existent/dir",
cfgFilePath: "strawhat.json",
defaultBranch: "deploy",
envVars: newStringTable("VAR1", "value", modeCaseInsensitive))
test "parseRunRequest":
let rr1 = RunRequest(
runId: genUUID(),
projectName: testProjDef.name,
stepName: "build",
buildRef: "master",
workspaceDir: "/no-real/dir",
timestamp: getTime().local.trimNanoSec,
forceRebuild: true)
let rrStr = $rr1
let rr2 = parseRunRequest(parseJson(rrStr))
check rr1 == rr2
test "parseProjectDef":
let pd = parseProjectDef(parseJson(testProjDefStr))
check:
pd.name == "dummy-project"
pd.repo == "/non-existent/dir"
pd.cfgFilePath == "strawhat.json"
pd.defaultBranch == "deploy"
pd.envVars.len == 1
pd.envVars.hasKey("VAR1")
pd.envVars["VAR1"] == "value"
test "ProjectDef ==":
let pd1 = parseProjectDef(parseJson(testProjDefStr))
check pd1 == testProjDef
test "ProjectDef != (name)":
var pd1 = testProjDef
pd1.name = "different"
check pd1 != testProjDef
test "ProjectDef != (repo)":
var pd1 = testProjDef
pd1.repo = "different"
check pd1 != testProjDef
test "ProjectDef != (cfgFilePath)":
var pd1 = testProjDef
pd1.cfgFilePath = "different"
check pd1 != testProjDef
test "ProjectDef != (defaultBranch)":
var pd1 = testProjDef
pd1.defaultBranch = "different"
check pd1 != testProjDef
test "loadStrawBossConfig":
let cfg = loadStrawBossConfig("src/test/json/strawboss.config.json")
let expectedUsers = @[UserRef(name: "bob@builder.com", hashedPwd: "testvalue"),
UserRef(name: "sam@sousa.com", hashedPwd: "testvalue")]
let expectedProjects = @[
ProjectDef(name: "dummy-project",
repo: "/non-existent/dir",
defaultBranch: "deploy",
cfgFilePath: "strawhat.json",
envVars: newStringTable("VAR1", "value", modeCaseSensitive)),
ProjectDef(name: "test-project",
repo: "",
defaultBranch: "master",
cfgFilePath: "strawboss.json",
envVars: newStringTable(modeCaseSensitive))]
check:
cfg.buildDataDir == "build-data"
cfg.authSecret == "change me"
cfg.pwdCost == 11
sameContents(expectedUsers, cfg.users)
sameContents(expectedProjects, cfg.projects)
test "loadProjectConfig":
let pc = loadProjectConfig("src/test/json/dummy-project.config.json")
check:
pc.name == "dummy-project"
pc.versionCmd == "git describe --all --always"
pc.containerImage == "ubuntu"
pc.steps.len == 2
# Explicitly set properties
pc.steps["build"].name == "build"
pc.steps["build"].dontSkip == true
pc.steps["build"].stepCmd == "cust-build"
pc.steps["build"].workingDir == "dir1"
pc.steps["build"].containerImage == "alpine"
sameContents(pc.steps["build"].artifacts, @["bin1", "doc1"])
sameContents(pc.steps["build"].depends, @["test"])
sameContents(pc.steps["build"].expectedEnv, @["VAR1"])
sameContents(pc.steps["build"].cmdInput, @["test", "this"])
# Step with defaulted properties
pc.steps["test"].name == "test"
pc.steps["test"].dontSkip == false
pc.steps["test"].stepCmd == "true"
pc.steps["test"].workingDir == "."
pc.steps["test"].containerImage.len == 0
sameContents(pc.steps["test"].artifacts, @[])
sameContents(pc.steps["test"].depends, @[])
sameContents(pc.steps["test"].expectedEnv, @[])
sameContents(pc.steps["test"].cmdInput, @[])
test "serialze StrawBossConfig to/from string":
let cfg = loadStrawBossConfig("src/test/json/strawboss.config.json")
let cfgStr = $cfg
check cfg == parseStrawBossConfig(parseJson(cfgStr))
test "%step":
let step = Step(
name: "build", stepCmd: "true", workingDir: "dirA",
artifacts: @[], depends: @["compile"], cmdInput: @[],
expectedEnv: @["CWD", "TERM"], dontSkip: true)
let stepJS = %step
for k in @["name", "stepCmd", "workingDir", "artifacts", "cmdInput",
"depends", "expectedEnv", "dontSkip"]:
check stepJS.hasKey(k)
test "loadBuildStatus":
let st = loadBuildStatus("src/test/json/test-status.json")
check:
st.runId == "90843e0c-6113-4462-af33-a89ff9731031"
st.state == BuildState.failed
st.details == "some very good reason"