Skip to content

Commit

Permalink
add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Oct 15, 2024
1 parent 461e14e commit fe7b668
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
22 changes: 17 additions & 5 deletions integrationtests/api_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (a *apiHelper) createFlag(
return nil
}

// createFlagInEnvironment sets up a flag with two variations. The first
// createFlagWithVariations sets up a flag with two variations. The first
// is served when the flag is off, and the second is served when it is on.
func (a *apiHelper) createFlagWithVariations(
proj projectInfo,
Expand All @@ -377,16 +377,28 @@ func (a *apiHelper) createFlagWithVariations(
on bool,
variation1 ldvalue.Value,
variation2 ldvalue.Value,
) error {
return a.createFlagWithBody(proj, env, flagKey, on, func(body *ldapi.FeatureFlagBody) {
for _, value := range []ldvalue.Value{variation1, variation2} {
valueAsInterface := value.AsArbitraryValue()
body.Variations = append(body.Variations, ldapi.Variation{Value: &valueAsInterface})
}
})
}

func (a *apiHelper) createFlagWithBody(
proj projectInfo,
env environmentInfo,
flagKey string,
on bool,
body func(body *ldapi.FeatureFlagBody),
) error {
flagPost := ldapi.FeatureFlagBody{
Name: flagKey,
Key: flagKey,
}

for _, value := range []ldvalue.Value{variation1, variation2} {
valueAsInterface := value.AsArbitraryValue()
flagPost.Variations = append(flagPost.Variations, ldapi.Variation{Value: &valueAsInterface})
}
body(&flagPost)

_, _, err := a.apiClient.FeatureFlagsApi.
PostFeatureFlag(a.apiContext, proj.key).
Expand Down
61 changes: 61 additions & 0 deletions integrationtests/standard_mode_prerequisite_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,50 @@ func makeTopLevelPrerequisites(api *scopedApiHelper) (map[string][]string, error
}, nil
}

func makeFailedPrerequisites(api *scopedApiHelper) (map[string][]string, error) {

// flagOn -> prereq1
// failedPrereq -> prereq1

if err := api.createFlagWithVariations("prereq1", true, ldvalue.Bool(false), ldvalue.Bool(true)); err != nil {
return nil, err
}

if err := api.createFlagWithVariations("prereq2", true, ldvalue.Bool(false), ldvalue.Bool(true)); err != nil {
return nil, err
}

if err := api.createFlagWithPrerequisites("flagOn", true, ldvalue.Bool(false), ldvalue.Bool(true), []ldapi.Prerequisite{
{Key: "prereq1", Variation: 1},
}); err != nil {
return nil, err
}

if err := api.createFlagWithPrerequisites("flagOff", false, ldvalue.Bool(false), ldvalue.Bool(true), []ldapi.Prerequisite{
{Key: "prereq1", Variation: 1},
}); err != nil {
return nil, err
}

if err := api.createFlagWithPrerequisites("failedPrereq", true, ldvalue.Bool(false), ldvalue.Bool(true), []ldapi.Prerequisite{
{Key: "prereq1", Variation: 0}, // wrong variation!
{Key: "prereq2", Variation: 1}, // correct variation, but we shouldn't see it since the first prereq failed
}); err != nil {
return nil, err
}

return map[string][]string{
"flagOn": {"prereq1"},
"flagOff": {},
"failedPrereq": {"prereq1"},
"prereq1": {},
"prereq2": {},
}, nil

}

// TODO: Make a builder for the API client so that all flag options can be accessed.

func testStandardModeWithPrerequisites(t *testing.T, manager *integrationTestManager) {
t.Run("includes top-level prerequisites", func(t *testing.T) {
api, err := newScopedApiHelper(manager.apiHelper)
Expand All @@ -111,4 +155,21 @@ func testStandardModeWithPrerequisites(t *testing.T, manager *integrationTestMan
})
manager.verifyFlagPrerequisites(t, api.projAndEnvs(), prerequisites)
})

t.Run("ignores prereqs if not evaluated", func(t *testing.T) {
api, err := newScopedApiHelper(manager.apiHelper)
require.NoError(t, err)
defer api.cleanup()

prerequisites, err := makeFailedPrerequisites(api)
require.NoError(t, err)

manager.startRelay(t, api.envVariables())
defer manager.stopRelay(t)

manager.awaitEnvironments(t, api.projAndEnvs(), nil, func(proj projectInfo, env environmentInfo) string {
return env.name
})
manager.verifyFlagPrerequisites(t, api.projAndEnvs(), prerequisites)
})
}

0 comments on commit fe7b668

Please sign in to comment.