Last active
April 20, 2020 21:53
-
-
Save cuotos/9dda943e11196430c7ae756dce5c8b82 to your computer and use it in GitHub Desktop.
jsonnetunit needle haystack lookup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// containsEnvVars checks that all needles (k8s env variable "name" and "value" objs) exist in the haystack (pod env block) object | |
// i.e. asserts structs exist in array | |
containsEnvVars(haystack, needles):: ( | |
// create an array, 1 element per needle | |
local results = [ | |
// check the needle is valid (it contains the two required fields) | |
if (std.objectHas(needle, 'name') && std.objectHas(needle, 'value')) then | |
// needle is valid | |
// create a sub array to contain result of every haystack item for the current needle | |
local h = [ | |
// if the haystack name and value both match the needle, add 'true' to the sub array | |
if (needle.name == haystackItem.name) && needle.value == haystackItem.value then true | |
for haystackItem in haystack | |
]; | |
// strip out the nulls, leaving only a true for the items where the needle and haystack match | |
local prunedResults = std.prune(h); | |
if std.length(prunedResults) == 0 then | |
error "needle: " + needle + " was not found in the haystack" | |
else | |
true | |
else | |
// needle is not value, raise an error | |
error 'needle ' + needle + ': doesnt contain the fields "name" and "value"' | |
// body of the loop is the above logic | |
for needle in needles | |
]; | |
// remove the empty sub arrays, these are where a needle was not matched to a haystackItem | |
// therefore indicate a needle wasn't found | |
local prunedResults = std.prune(results); | |
//the number of results (bool true) remaining should equal the number of needles, indicating they were all found | |
if std.count(prunedResults, true) < std.length(needles) then | |
false | |
else | |
true | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make sure the envVars contain ADDITIONAL_ENV: ADDITIONAL_VALUE without having to compare the entire map | |
testAdditionalEnvVars: { | |
local exampleVars: [ | |
{ "name": "DB_HOST", "value": "thedb.domain.com" }, | |
{ "name": "DB_PORT", "value": 3306 }, | |
{ "name": "REDIS_HOST", "value": "redis.anotherdomain.local" }, | |
{ "name": "REDIS_PORT", "value": 6379 }, | |
{ "name": "ADDITIONAL_ENV", "value": "ADDITIONAL_VALUE" } | |
] | |
actual: utils.containsEnvVars(exampleVars, [{ name: 'ADDITIONAL_ENV', value: 'ADDITIONAL_VALUE' }]), | |
expect: true, | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment