Skip to content

Commit

Permalink
fixup: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert committed Jan 5, 2024
1 parent f582a48 commit 69e4f99
Show file tree
Hide file tree
Showing 16 changed files with 350 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}
- name: Validate Schema
run: go test -v ./json
run: make test

# Ensure there is no diff when make gen-schema-json is run
- run: make gen-schema-json
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ gen-schema-json: install-yq
yq eval -o=json json/flagd-definitions.yaml > json/flagd-definitions.json
yq eval -o=json json/targeting.yaml > json/targeting.json

.PHONY: test
test:
go test -v ./json

ajv-validate-flagd-schema:
@if ! npm ls ajv-cli; then npm ci; fi
npx ajv compile -s json/targeting.json
Expand Down
4 changes: 2 additions & 2 deletions json/flagd-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"patternProperties": {
"^.{1,}$": {
"$comment": "this relative ref means that targeting.json MUST be in the same dir, or available on the same HTTP path",
"$ref": "https://flagd.dev/schema/targeting.json#/$defs/targeting"
"$ref": "./targeting.json#/$defs/targeting"
}
}
}
Expand All @@ -71,7 +71,7 @@
"type": "string"
},
"targeting": {
"$ref": "https://flagd.dev/schema/targeting.json#/$defs/targeting"
"$ref": "./targeting.json#/$defs/targeting"
}
},
"required": [
Expand Down
4 changes: 2 additions & 2 deletions json/flagd-definitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ properties:
"^.{1,}$":
$comment: this relative ref means that targeting.json MUST be in the same
dir, or available on the same HTTP path
$ref: "https://flagd.dev/schema/targeting.json#/$defs/targeting"
$ref: "./targeting.json#/$defs/targeting"
"$defs":
flag:
$comment: base flag object; no title/description here, allows for better UX,
Expand All @@ -56,7 +56,7 @@ properties:
if the targeting returns null).
type: string
targeting:
$ref: "https://flagd.dev/schema/targeting.json#/$defs/targeting"
$ref: "./targeting.json#/$defs/targeting"
required:
- state
- defaultVariant
Expand Down
2 changes: 0 additions & 2 deletions json/flagd_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ var s *gojsonschema.Schema

func init() {
schemaLoader = gojsonschema.NewSchemaLoader()
//gojsonschema.NewStringLoader(flagd_definitions.FlagdDefinitions)
//schemaLoader.AddSchemas()
schemaLoader.AddSchemas(gojsonschema.NewStringLoader(flagd_definitions.Targeting))
var err error
s, err = schemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.FlagdDefinitions))
Expand Down
10 changes: 7 additions & 3 deletions json/targeting.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@
"pattern": "^\\$flagd\\.((timestamp)|(flagKey))$"
},
{
"type": "string",
"pattern": "^(?!\\$flagd.).*$"
"not": {
"$comment": "this is a negated (not) match of \"$flagd.{some-key}\", which is faster and more compatible that a negative lookahead regex",
"type": "string",
"description": "flagd automatically injects \"$flagd.timestamp\" (unix epoch) and \"$flagd.flagKey\" (the key of the flag in evaluation) into the context.",
"pattern": "^\\$flagd\\..*$"
}
},
{
"type": "array",
Expand Down Expand Up @@ -513,7 +517,7 @@
]
},
"anyRule": {
"oneOf": [
"anyOf": [
{
"$ref": "#/$defs/varRule"
},
Expand Down
10 changes: 7 additions & 3 deletions json/targeting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ type: object
description: flagd automatically injects "$flagd.timestamp" (unix epoch)
and "$flagd.flagKey" (the key of the flag in evaluation) into the context.
pattern: "^\\$flagd\\.((timestamp)|(flagKey))$"
- type: string
pattern: "^(?!\\$flagd\\.).+"
- not:
$comment: this is a negated (not) match of "$flagd.{some-key}", which is faster and more compatible that a negative lookahead regex
type: string
description: flagd automatically injects "$flagd.timestamp" (unix epoch) and "$flagd.flagKey"
(the key of the flag in evaluation) into the context.
pattern: "^\\$flagd\\..*$"
- type: array
$comment: this is to support the form of var with a default... there seems to be a bug here, where ajv gives a warning (not an error) because maxItems doesn't equal the number of entries in items, though this is valid in this case
minItems: 1
Expand Down Expand Up @@ -393,7 +397,7 @@ type: object
- $ref: "#/$defs/anyRule"
- $ref: "#/$defs/primitive"
anyRule:
oneOf:
anyOf:
- $ref: "#/$defs/varRule"
- $ref: "#/$defs/missingRule"
- $ref: "#/$defs/missingSomeRule"
Expand Down
26 changes: 26 additions & 0 deletions json/test/negative/fractional-invalid-bucketing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an invalid bucking value is invalid",
"flags": {
"fractional-invalid-bucketing": {
"state": "ENABLED",
"variants": {
"clubs": "clubs",
"diamonds": "diamonds",
"hearts": "hearts",
"spades": "spades",
"none": "none"
},
"defaultVariant": "none",
"targeting": {
"fractional": [
"invalid",
["clubs", 25],
["diamonds", 25],
["hearts", 25],
["spades", 25]
]
}
}
}
}
25 changes: 25 additions & 0 deletions json/test/negative/fractional-invalid-weighting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an invalid weight value is invalid",
"flags": {
"fractional-invalid-weighting": {
"state": "ENABLED",
"variants": {
"clubs": "clubs",
"diamonds": "diamonds",
"hearts": "hearts",
"spades": "spades",
"none": "none"
},
"defaultVariant": "none",
"targeting": {
"fractional": [
["clubs", 25],
["diamonds", "25"],
["hearts", 25],
["spades", 25]
]
}
}
}
}
23 changes: 23 additions & 0 deletions json/test/negative/invalid-ends-with-param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an an int is not a valid ends_with param",
"flags": {
"invalid-ends-with-param": {
"state": "ENABLED",
"variants": {
"prefix": 1,
"postfix": 2
},
"defaultVariant": "none",
"targeting": {
"if": [
{
"ends_with": [{ "var": "id" }, 0]
},
"postfix",
"prefix"
]
}
}
}
}
23 changes: 23 additions & 0 deletions json/test/negative/invalid-flagd-props.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an unsupported $flagd property is invalid",
"flags": {
"invalid-flagd-props": {
"state": "ENABLED",
"variants": {
"yes": "1",
"no": "2"
},
"defaultVariant": "none",
"targeting": {
"if": [
{
"==": [{ "var": "$flagd.myprop" }, { "var": "someprop" }]
},
"yes",
"no"
]
}
}
}
}
23 changes: 23 additions & 0 deletions json/test/negative/invalid-starts-with-param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an an int is not a valid starts_with param",
"flags": {
"invalid-starts-with-param": {
"state": "ENABLED",
"variants": {
"prefix": 1,
"postfix": 2
},
"defaultVariant": "none",
"targeting": {
"if": [
{
"starts_with": [{ "var": "id" }, 0]
},
"prefix",
"postfix"
]
}
}
}
}
23 changes: 23 additions & 0 deletions json/test/negative/sem-ver-invalid-range-specifier.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an invalid range specifier is invalid",
"flags": {
"sem-ver-invalid-range-specifier": {
"state": "ENABLED",
"variants": {
"equal": "equal",
"not": "not"
},
"defaultVariant": "none",
"targeting": {
"if": [
{
"sem_ver": [{ "var": "version" }, "*", "2.0.0"]
},
"equal",
"not"
]
}
}
}
}
23 changes: 23 additions & 0 deletions json/test/negative/sem-ver-invalid-ver-expression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "../../flagd-definitions.json",
"$comments": "tests that an invalid version is invalid",
"flags": {
"sem-ver-invalid-ver-expression": {
"state": "ENABLED",
"variants": {
"equal": "equal",
"not": "not"
},
"defaultVariant": "none",
"targeting": {
"if": [
{
"sem_ver": [{ "var": "version" }, "=", "2.0.0.0"]
},
"equal",
"not"
]
}
}
}
}
Loading

0 comments on commit 69e4f99

Please sign in to comment.