Skip to content

Commit

Permalink
update the schema to support metadata property and clean tests
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Dmytrenko <[email protected]>
  • Loading branch information
erka committed Jul 24, 2024
1 parent 8f9c515 commit a7ad122
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 24 deletions.
3 changes: 3 additions & 0 deletions core/validation/flipt.cue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ close({
type: "BOOLEAN_FLAG_TYPE" | *"VARIANT_FLAG_TYPE"
#FlagBoolean | *{}
}
if version == "1.3" {
metadata: [string]: (string | int | bool | float)
}
}

#FlagBoolean: {
Expand Down
6 changes: 6 additions & 0 deletions core/validation/flipt.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@
"items": {
"$ref": "#/$defs/rule"
}
},
"metadata": {
"type": "object",
"additionalProperties": {
"type": ["string", "number", "boolean"]
}
}
},
"allOf": [
Expand Down
41 changes: 41 additions & 0 deletions core/validation/testdata/valid_metadata_v3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: "1.3"
namespace: default
flags:
- key: flipt
name: flipt
description: flipt
enabled: false
variants:
- key: flipt
name: flipt
- key: flipt
name: flipt
default: true
rules:
- segment: internal-users
distributions:
- variant: fromFlipt
rollout: 100
- segment: all-users
distributions:
- variant: fromFlipt2
rollout: 100
metadata:
label: ui
area: 32
hidden: true
radar: 3.2
segments:
- key: all-users
name: All Users
description: All Users
match_type: ALL_MATCH_TYPE
- key: internal-users
name: Internal Users
description: All internal users at flipt.
constraints:
- type: STRING_COMPARISON_TYPE
property: organization
operator: eq
value: flipt
match_type: ALL_MATCH_TYPE
14 changes: 14 additions & 0 deletions core/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ func TestValidate_DefaultVariant_V3(t *testing.T) {
assert.NoError(t, err)
}

func TestValidate_Metadata_V3(t *testing.T) {
const file = "testdata/valid_metadata_v3.yaml"
f, err := os.Open(file)
require.NoError(t, err)

defer f.Close()

v, err := NewFeaturesValidator()
require.NoError(t, err)

err = v.Validate(file, f)
assert.NoError(t, err)
}

func TestValidate_YAML_Stream(t *testing.T) {
const file = "testdata/valid_yaml_stream.yaml"
f, err := os.Open(file)
Expand Down
17 changes: 9 additions & 8 deletions internal/ext/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (m mockLister) ListRollouts(_ context.Context, listRequest *flipt.ListRollo
return &flipt.RolloutList{}, nil
}

func newStruct(t testing.TB, m map[string]any) *structpb.Struct {
t.Helper()
value, err := structpb.NewStruct(m)
require.NoError(t, err)
return value
}

func TestExport(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -118,13 +125,15 @@ func TestExport(t *testing.T) {
Key: "foo",
},
},
Metadata: newStruct(t, map[string]any{"label": "variant", "area": true}),
},
{
Key: "flag2",
Name: "flag2",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Enabled: false,
Metadata: newStruct(t, map[string]any{"label": "bool", "area": 12}),
},
},
},
Expand Down Expand Up @@ -232,7 +241,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -265,7 +273,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
"foo": {
Expand All @@ -275,7 +282,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -308,7 +314,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
},
Expand Down Expand Up @@ -508,7 +513,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -540,7 +544,6 @@ func TestExport(t *testing.T) {
Name: "flag2",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Enabled: false,
},
},
Expand All @@ -551,7 +554,6 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -583,7 +585,6 @@ func TestExport(t *testing.T) {
Name: "flag2",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Enabled: false,
},
},
Expand Down
89 changes: 89 additions & 0 deletions internal/ext/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,95 @@ func TestImport(t *testing.T) {
listSegmentResps: []*flipt.SegmentList{},
},
},
{
name: "import v1.3",
path: "testdata/import_v1_3",
expected: &mockCreator{
createflagReqs: []*flipt.CreateFlagRequest{
{
Key: "flag1",
Name: "flag1",
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: newStruct(t, map[string]any{"label": "variant", "area": true}),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: newStruct(t, map[string]any{"label": "bool", "area": 12}),
},
},
variantReqs: []*flipt.CreateVariantRequest{
{
FlagKey: "flag1",
Key: "variant1",
Name: "variant1",
Description: "variant description",
Attachment: compact(t, variantAttachment),
},
},
segmentReqs: []*flipt.CreateSegmentRequest{
{
Key: "segment1",
Name: "segment1",
Description: "description",
MatchType: flipt.MatchType_ANY_MATCH_TYPE,
},
},
constraintReqs: []*flipt.CreateConstraintRequest{
{
SegmentKey: "segment1",
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "fizz",
Operator: "neq",
Value: "buzz",
},
},
ruleReqs: []*flipt.CreateRuleRequest{
{
FlagKey: "flag1",
SegmentKey: "segment1",
Rank: 1,
},
},
distributionReqs: []*flipt.CreateDistributionRequest{
{
RuleId: "static_rule_id",
VariantId: "static_variant_id",
FlagKey: "flag1",
Rollout: 100,
},
},
rolloutReqs: []*flipt.CreateRolloutRequest{
{
FlagKey: "flag2",
Description: "enabled for internal users",
Rank: 1,
Rule: &flipt.CreateRolloutRequest_Segment{
Segment: &flipt.RolloutSegment{
SegmentKey: "internal_users",
Value: true,
},
},
},
{
FlagKey: "flag2",
Description: "enabled for 50%",
Rank: 2,
Rule: &flipt.CreateRolloutRequest_Threshold{
Threshold: &flipt.RolloutThreshold{
Percentage: 50.0,
Value: true,
},
},
},
},
},
},
}

for _, tc := range tests {
Expand Down
12 changes: 10 additions & 2 deletions internal/ext/testdata/export.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
"operator": "AND_SEGMENT_OPERATOR"
}
}
]
],
"metadata": {
"label": "variant",
"area": true
}
},
{
"key": "flag2",
Expand All @@ -55,7 +59,11 @@
"description": "enabled for 50%",
"threshold": { "percentage": 50, "value": true }
}
]
],
"metadata": {
"label": "bool",
"area": 12
}
}
],
"segments": [
Expand Down
6 changes: 6 additions & 0 deletions internal/ext/testdata/export.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ flags:
- segment1
- segment2
operator: AND_SEGMENT_OPERATOR
metadata:
label: variant
area: true
- key: flag2
name: flag2
type: "BOOLEAN_FLAG_TYPE"
Expand All @@ -49,6 +52,9 @@ flags:
threshold:
percentage: 50
value: true
metadata:
label: bool
area: 12
segments:
- key: segment1
name: segment1
Expand Down
75 changes: 75 additions & 0 deletions internal/ext/testdata/import_v1_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"version": "1.3",
"flags": [
{
"key": "flag1",
"name": "flag1",
"type": "VARIANT_FLAG_TYPE",
"description": "description",
"enabled": true,
"variants": [
{
"key": "variant1",
"name": "variant1",
"description": "variant description",
"attachment": {
"pi": 3.141,
"happy": true,
"name": "Niels",
"answer": { "everything": 42 },
"list": [1, 0, 2],
"object": { "currency": "USD", "value": 42.99 }
}
}
],
"rules": [
{
"segment": "segment1",
"rank": 1,
"distributions": [{ "variant": "variant1", "rollout": 100 }]
}
],
"metadata": {
"label": "variant",
"area": true
}
},
{
"key": "flag2",
"name": "flag2",
"type": "BOOLEAN_FLAG_TYPE",
"description": "a boolean flag",
"enabled": false,
"rollouts": [
{
"description": "enabled for internal users",
"segment": { "key": "internal_users", "value": true }
},
{
"description": "enabled for 50%",
"threshold": { "percentage": 50, "value": true }
}
],
"metadata": {
"label": "bool",
"area": 12
}
}
],
"segments": [
{
"key": "segment1",
"name": "segment1",
"match_type": "ANY_MATCH_TYPE",
"description": "description",
"constraints": [
{
"type": "STRING_COMPARISON_TYPE",
"property": "fizz",
"operator": "neq",
"value": "buzz"
}
]
}
]
}
Loading

0 comments on commit a7ad122

Please sign in to comment.