...

Source file src/edge-infra.dev/pkg/lib/featureflag/testutil/testutil.go

Documentation: edge-infra.dev/pkg/lib/featureflag/testutil

     1  package testutil
     2  
     3  import (
     4  	ld "github.com/launchdarkly/go-server-sdk/v6"
     5  	"github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
     6  	"github.com/launchdarkly/go-server-sdk/v6/testhelpers/ldtestdata"
     7  
     8  	ff "edge-infra.dev/pkg/lib/featureflag"
     9  )
    10  
    11  // TestFlag is a simplistic representation of an LD feature flag targeting rule. If user is empty,
    12  // the variation will apply for all users.
    13  type TestFlag struct {
    14  	FlagContext ff.Contextualizer
    15  	Name        string
    16  	Value       bool
    17  }
    18  
    19  // MustInitTestFeatureFlags is meant for use in test scenarios only.
    20  //
    21  // It sets a custom data source which will force the featureflag client to be recreated.
    22  func MustInitTestFeatureFlags(flagsAndValues map[string]bool) {
    23  	td := ldtestdata.DataSource()
    24  	for feat, enabled := range flagsAndValues {
    25  		td.Update(td.Flag(feat).VariationForAll(enabled))
    26  	}
    27  	var testcfg ld.Config
    28  	testcfg.Events = ldcomponents.NoEvents()
    29  	testcfg.DataSource = td
    30  	ff.SetConfig(testcfg)
    31  }
    32  
    33  // MustInitTestFeatureFlagsForContexts is meant for use in test scenarios only. Parameter flags
    34  // is a set of targeting rules used to configure flags for specific Contexts instead of all.
    35  func MustInitTestFeatureFlagsForContexts(flags []TestFlag) {
    36  	td := ldtestdata.DataSource()
    37  	for _, flag := range flags {
    38  		fbldr := td.Flag(flag.Name)
    39  		if flag.FlagContext.Kind() == "" {
    40  			fbldr = fbldr.VariationForAll(flag.Value)
    41  		} else {
    42  			fbldr = fbldr.VariationForKey(flag.FlagContext.Kind(), flag.FlagContext.Key(), flag.Value).On(true).FallthroughVariation(false)
    43  		}
    44  		td.Update(fbldr)
    45  	}
    46  	var testcfg ld.Config
    47  	testcfg.Events = ldcomponents.NoEvents()
    48  	testcfg.DataSource = td
    49  	ff.SetConfig(testcfg)
    50  }
    51  

View as plain text