Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
monyarm committed Aug 2, 2024
1 parent a1c5537 commit 4ae6831
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 177 deletions.
141 changes: 95 additions & 46 deletions packages/mcl/src/src/mcl/commands/ci_matrix.d
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ GitHubOS getGHOS(string os)
return os in osMap ? osMap[os] : GitHubOS.selfHosted;
}

void assertGHOS(string input, GitHubOS expected)
{
auto actual = getGHOS(input);
assert(actual == expected, fmt("getGHOS(\"%s\") should return %s, but returned %s", input, expected, actual));
}

@("getGHOS")
unittest
{
assert(getGHOS("ubuntu-latest") == GitHubOS.ubuntuLatest);
assert(getGHOS("macos-14") == GitHubOS.macos14);
assert(getGHOS("crazyos-inator-2000") == GitHubOS.selfHosted);
assertGHOS("ubuntu-latest", GitHubOS.ubuntuLatest);
assertGHOS("macos-14", GitHubOS.macos14);
assertGHOS("crazyos-inator-2000", GitHubOS.selfHosted);
}

immutable SupportedSystem[string] systemMap;
Expand All @@ -91,13 +97,19 @@ SupportedSystem getSystem(string system)
return system in systemMap ? systemMap[system] : SupportedSystem.x86_64_linux;
}

void assertSystem(string input, SupportedSystem expected)
{
auto actual = getSystem(input);
assert(actual == expected, fmt("getSystem(\"%s\") should return %s, but returned %s", input, expected, actual));
}

@("getSystem")
unittest
{
assert(getSystem("x86_64-linux") == SupportedSystem.x86_64_linux);
assert(getSystem("x86_64-darwin") == SupportedSystem.x86_64_darwin);
assert(getSystem("aarch64-darwin") == SupportedSystem.aarch64_darwin);
assert(getSystem("bender-bending-rodriguez-os") == SupportedSystem.x86_64_linux);
assertSystem("x86_64-linux", SupportedSystem.x86_64_linux);
assertSystem("x86_64-darwin", SupportedSystem.x86_64_darwin);
assertSystem("aarch64-darwin", SupportedSystem.aarch64_darwin);
assertSystem("bender-bending-rodriguez-os", SupportedSystem.x86_64_linux);
}

struct Package
Expand Down Expand Up @@ -228,12 +240,18 @@ struct Params
GitHubOS systemToGHPlatform(SupportedSystem os) =>
os == SupportedSystem.x86_64_linux ? GitHubOS.selfHosted : GitHubOS.macos14;

void assertGHPlatform(SupportedSystem system, GitHubOS expected)
{
auto actual = systemToGHPlatform(system);
assert(actual == expected, fmt("`systemToGHPlatform(%s)` should return `%s`, but returned `%s`", system, expected, actual));
}

@("systemToGHPlatform")
unittest
{
assert(systemToGHPlatform(SupportedSystem.x86_64_linux) == GitHubOS.selfHosted);
assert(systemToGHPlatform(SupportedSystem.x86_64_darwin) == GitHubOS.macos14);
assert(systemToGHPlatform(SupportedSystem.aarch64_darwin) == GitHubOS.macos14);
assertGHPlatform(SupportedSystem.x86_64_linux, GitHubOS.selfHosted);
assertGHPlatform(SupportedSystem.x86_64_darwin, GitHubOS.macos14);
assertGHPlatform(SupportedSystem.aarch64_darwin, GitHubOS.macos14);
}

static immutable string[] uselessWarnings =
Expand Down Expand Up @@ -370,7 +388,9 @@ int getNixEvalWorkerCount()
@("getNixEvalWorkerCount")
unittest
{
assert(getNixEvalWorkerCount() == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS));
auto actual = getNixEvalWorkerCount();
assert(actual == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS),
"getNixEvalWorkerCount() should return the number of threads per CPU if it is less than MAX_WORKERS, otherwise it should return MAX_WORKERS, but returned %s".fmt(actual));
}

string[] meminfo;
Expand Down Expand Up @@ -402,11 +422,13 @@ unittest
{
// Test when params.maxMemory is 0
params.maxMemory = 0;
assert(getAvailableMemoryMB() > 0);
auto actual = getAvailableMemoryMB();
assert(actual > 0, "getAvailableMemoryMB() should return a value greater than 0, but returned %s".fmt(actual));

// Test when params.maxMemory is not 0
params.maxMemory = 1024;
assert(getAvailableMemoryMB() == 1024);
actual = getAvailableMemoryMB();
assert(actual == 1024, "getAvailableMemoryMB() should return 1024, but returned %s".fmt(actual));
}

void saveCachixDeploySpec(Package[] packages)
Expand All @@ -429,8 +451,14 @@ unittest
createResultDirs();
saveCachixDeploySpec(cast(Package[]) testPackageArray);
JSONValue deploySpec = parseJSON((resultDir() ~ "/cachix-deploy-spec.json").readText);
assert(testPackageArray[1].name == deploySpec[0]["package"].str);
assert(testPackageArray[1].output == deploySpec[0]["out"].str);
string testPackageName = testPackageArray[1].name;
string deploySpecName = deploySpec[0]["package"].str;
string testPackageOutput = testPackageArray[1].output;
string deploySpecOutput = deploySpec[0]["out"].str;
assert(testPackageName == deploySpecName,
"The name of the package should be %s, but was %s".fmt(testPackageName, deploySpecName));
assert(testPackageOutput == deploySpecOutput,
"The output of the package should be %s, but was %s".fmt(testPackageOutput, deploySpecOutput));
}

void saveGHCIMatrix(Package[] packages)
Expand Down Expand Up @@ -463,7 +491,10 @@ unittest
: "matrix-post.json")).readText);
foreach (i, pkg; testPackageArray)
{
assert(pkg.name == matrix["include"][i]["name"].str);
string pkgName = pkg.name;
string matrixName = matrix["include"][i]["name"].str;
assert(pkgName == matrixName,
"The name of the package should be %s, but was %s".fmt(pkgName, matrixName));
}
}

Expand Down Expand Up @@ -504,10 +535,14 @@ unittest
string comment = (rootDir() ~ "comment.md").readText;
foreach (pkg; testSummaryTableEntryArray)
{
assert(comment.indexOf(pkg.name) != -1);
assert(comment.indexOf(pkg.x86_64.linux) != -1);
assert(comment.indexOf(pkg.x86_64.darwin) != -1);
assert(comment.indexOf(pkg.aarch64.darwin) != -1);
assert(comment.indexOf(pkg.name) != -1,
"The comment should contain the package name %s, the comment is:\n%s".fmt(pkg.name, comment));
assert(comment.indexOf(pkg.x86_64.linux) != -1,
"The comment should contain the x86_64 linux status %s, the comment is:\n%s".fmt(pkg.x86_64.linux, comment));
assert(comment.indexOf(pkg.x86_64.darwin) != -1,
"The comment should contain the x86_64 darwin status %s, the comment is:\n%s".fmt(pkg.x86_64.darwin, comment));
assert(comment.indexOf(pkg.aarch64.darwin) != -1,
"The comment should contain the aarch64 darwin status %s, the comment is:\n%s".fmt(pkg.aarch64.darwin, comment));
}
}

Expand Down Expand Up @@ -561,30 +596,35 @@ SummaryTableEntry createSummaryTableEntry(Package[] group)
return entry;
}

void assertNixTable(SummaryTableEntry[] tableSummary, immutable(Package[]) testPackageArray, int index,
string expectedLinuxStatus = "[" ~ Status.cached ~ "](https://testPackage.com)", string expectedDarwinStatus = Status.notSupported, string expectedAarch64Status = Status.notSupported)
{
string actualName = tableSummary[index].name;
string expectedName = testPackageArray[index].name;
assert(actualName == expectedName, fmt("Expected name to be %s, but got %s", expectedName, actualName));

string actualLinuxStatus = tableSummary[index].x86_64.linux;
assert(actualLinuxStatus == expectedLinuxStatus, fmt("Expected Linux status to be %s, but got %s", expectedLinuxStatus, actualLinuxStatus));

string actualDarwinStatus = tableSummary[index].x86_64.darwin;
assert(actualDarwinStatus == expectedDarwinStatus, fmt("Expected Darwin status to be %s, but got %s", expectedDarwinStatus, actualDarwinStatus));

string actualAarch64Status = tableSummary[index].aarch64.darwin;
assert(actualAarch64Status == expectedAarch64Status, fmt("Expected Aarch64 Darwin status to be %s, but got %s", expectedAarch64Status, actualAarch64Status));
}

@("convertNixEvalToTableSummary/getStatus")
unittest
{
auto tableSummary = convertNixEvalToTableSummary(cast(Package[]) testPackageArray);
assert(tableSummary[0].name == testPackageArray[0].name);
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
assert(tableSummary[1].name == testPackageArray[1].name);
assert(tableSummary[1].x86_64.linux == Status.notSupported);
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
assert(tableSummary[1].aarch64.darwin == Status.buildFailed);
assertNixTable(tableSummary, testPackageArray, 0);
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.buildFailed);

params.isInitial = true;
tableSummary = convertNixEvalToTableSummary(cast(Package[]) testPackageArray);
params.isInitial = false;
assert(tableSummary[0].name == testPackageArray[0].name);
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
assert(tableSummary[1].name == testPackageArray[1].name);
assert(tableSummary[1].x86_64.linux == Status.notSupported);
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
assert(tableSummary[1].aarch64.darwin == Status.building);
assertNixTable(tableSummary, testPackageArray, 0);
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.building);
}

void printTableForCacheStatus(Package[] packages)
Expand Down Expand Up @@ -637,12 +677,11 @@ unittest
cacheUrl: nixosCacheEndpoint ~ storePathHash ~ ".narinfo",
};

assert(!testPackage.isCached);
assert(checkPackage(testPackage).isCached);
assert(checkPackage(testPackage).isCached, "Package %s should be cached".fmt(testPackage.cacheUrl));

testPackage.cacheUrl = nixosCacheEndpoint ~ "nonexistent.narinfo";

assert(!checkPackage(testPackage).isCached);
assert(!checkPackage(testPackage).isCached, "Package %s should not be cached".fmt(testPackage.cacheUrl));
}

Package[] getPrecalcMatrix()
Expand Down Expand Up @@ -674,13 +713,23 @@ unittest
string precalcMatrixStr = "{\"include\": [{\"name\": \"test\", \"allowedToFail\": false, \"attrPath\": \"test\", \"cacheUrl\": \"url\", \"isCached\": true, \"os\": \"linux\", \"system\": \"x86_64-linux\", \"output\": \"output\"}]}";
params.precalcMatrix = precalcMatrixStr;
auto packages = getPrecalcMatrix();
Package testPackage = {
name: "test",
allowedToFail: false,
attrPath: "test",
cacheUrl: "url",
isCached: true,
os: GitHubOS.selfHosted,
system: SupportedSystem.x86_64_linux,
output: "output"
};
assert(packages.length == 1);
assert(packages[0].name == "test");
assert(!packages[0].allowedToFail);
assert(packages[0].attrPath == "test");
assert(packages[0].cacheUrl == "url");
assert(packages[0].name == testPackage.name, "Expected %s, got %s".fmt(testPackage.name, packages[0].name));
assert(!packages[0].allowedToFail, "Expected %s, got %s".fmt(testPackage.allowedToFail, packages[0].allowedToFail));
assert(packages[0].attrPath == testPackage.attrPath, "Expected %s, got %s".fmt(testPackage.attrPath, packages[0].attrPath));
assert(packages[0].cacheUrl == testPackage.cacheUrl, "Expected %s, got %s".fmt(testPackage.cacheUrl, packages[0].cacheUrl));
assert(packages[0].isCached);
assert(packages[0].os == GitHubOS.selfHosted);
assert(packages[0].system == SupportedSystem.x86_64_linux);
assert(packages[0].output == "output");
assert(packages[0].os == testPackage.os, "Expected %s, got %s".fmt(testPackage.os, packages[0].os));
assert(packages[0].system == testPackage.system, "Expected %s, got %s".fmt(testPackage.system, packages[0].system));
assert(packages[0].output == testPackage.output, "Expected %s, got %s".fmt(testPackage.output, packages[0].output));
}
30 changes: 16 additions & 14 deletions packages/mcl/src/src/mcl/commands/shard_matrix.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import std.conv : to, parse;
import std.stdio : writeln;
import std.string : strip;
import std.regex : matchFirst, regex;
import std.format : format;
import std.algorithm : each;
import std.parallelism : parallel;

import mcl.utils.nix : nix;
import mcl.utils.path : createResultDirs, resultDir, rootDir;
Expand Down Expand Up @@ -64,15 +67,21 @@ ShardMatrix generateShardMatrix()

}

void assertShard(Shard shard, int index) {
string expectedPrefix = index == -1 ? "" : "legacyPackages";
string expectedPostfix = index == -1 ? "" : ("shards." ~ index.to!string);
assert(shard.prefix == expectedPrefix, "Expected shard %s to have prefix '%s', but got %s".format(index, expectedPrefix, shard.prefix));
assert(shard.postfix == expectedPostfix, "Expected shard %s to have postfix '%s', but got %s".format(index, expectedPostfix, shard.postfix));
assert(shard.digit == index, "Expected shard %s to have digit %s, but got %s".format(index, index, shard.digit));
}

@("generateShardMatrix")
unittest
{
auto shards = generateShardMatrix();
//this repo doesn't include shards, so we should get the error message
assert(shards.include.length == 1);
assert(shards.include[0].prefix == "");
assert(shards.include[0].postfix == "");
assert(shards.include[0].digit == -1);
assert(shards.include.length == 1, "generateShardMatrix should return 1 shard, but got %s".format(shards.include.length));
assertShard(shards.include[0], -1);

}

Expand All @@ -92,16 +101,9 @@ ShardMatrix splitToShards(int shardCount)
unittest
{
auto shards = splitToShards(3);
assert(shards.include.length == 3);
assert(shards.include[0].prefix == "legacyPackages");
assert(shards.include[0].postfix == "shards.0");
assert(shards.include[0].digit == 0);
assert(shards.include[1].prefix == "legacyPackages");
assert(shards.include[1].postfix == "shards.1");
assert(shards.include[1].digit == 1);
assert(shards.include[2].prefix == "legacyPackages");
assert(shards.include[2].postfix == "shards.2");
assert(shards.include[2].digit == 2);
assert(shards.include.length == 3, "Expectes splitToShards(3) to return 3 shards, but got %s".format(shards.include.length));
foreach(i, shard; shards.include.parallel)
assertShard(shard, i.to!int);

}

Expand Down
25 changes: 15 additions & 10 deletions packages/mcl/src/src/mcl/utils/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ T[] uniqIfSame(T)(T[] arr)
@("uniqIfSame")
unittest
{
assert(uniqIfSame([1, 1, 1, 1]) == [1]);
assert(uniqIfSame([1, 2, 3, 4]) == [1, 2, 3, 4]);
assert(uniqIfSame(["a", "a", "a", "a"]) == ["a"]);
assert(uniqIfSame(["a", "b", "c", "d"]) == ["a", "b", "c", "d"]);
assert(uniqIfSame([1, 1, 1, 1]) == [1],
"uniqIfSame should return [1] for [1, 1, 1, 1], but got " ~ uniqIfSame([1, 1, 1, 1]).to!string);
assert(uniqIfSame([1, 2, 3, 4]) == [1, 2, 3, 4],
"uniqIfSame should return [1, 2, 3, 4] for [1, 2, 3, 4], but got " ~ uniqIfSame([1, 2, 3, 4]).to!string);
assert(uniqIfSame(["a", "a", "a", "a"]) == ["a"],
"uniqIfSame should return [\"a\"] for [\"a\", \"a\", \"a\", \"a\"], but got " ~ uniqIfSame(["a", "a", "a", "a"]).to!string);
assert(uniqIfSame(["a", "b", "c", "d"]) == ["a", "b", "c", "d"],
"uniqIfSame should return [\"a\", \"b\", \"c\", \"d\"] for [\"a\", \"b\", \"c\", \"d\"], but got " ~ uniqIfSame(["a", "b", "c", "d"]).to!string);
}

T uniqArrays(T)(T s)
Expand All @@ -39,16 +43,17 @@ T uniqArrays(T)(T s)
@("uniqArrays")
unittest
{
assert(uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]) == [1, 2, 3, 4]);
assert(uniqArrays("aabbccdd") == "aabbccdd");
assert(uniqArrays(5) == 5);
assert(uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]) == [1, 2, 3, 4],
"uniqArrays should return [1, 2, 3, 4] for [1, 2, 3, 4, 1, 2, 3, 4], but got " ~ uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]).to!string);
assert(uniqArrays("aabbccdd") == "aabbccdd",
"uniqArrays should return \"aabbccdd\" for \"aabbccdd\", but got " ~ uniqArrays("aabbccdd").to!string);
assert(uniqArrays(5) == 5, "uniqArrays should return 5 for 5, but got " ~ uniqArrays(5).to!string);
struct TestStruct
{
int[] a;
string b;
}

assert(uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")) == TestStruct([
1, 2, 3, 4
], "aabbccdd"));
assert(uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")) == TestStruct([1, 2, 3, 4], "aabbccdd"),
"uniqArrays should return TestStruct([1, 2, 3, 4], \"aabbccdd\") for TestStruct([1, 2, 3, 4, 1, 2, 3, 4], \"aabbccdd\"), but got " ~ uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")).to!string);
}
4 changes: 3 additions & 1 deletion packages/mcl/src/src/mcl/utils/cachix.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ in (workspace && machine && deploymentId) =>
unittest
{
assert(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123) ==
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123");
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123",
"getCachixDeploymentApiUrl(\"my-workspace\", \"my-machine\", 123) should return \"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123\", but returned %s"
.fmt(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123)));

}

Expand Down
Loading

0 comments on commit 4ae6831

Please sign in to comment.