Skip to content

Commit 3ee2d8d

Browse files
committed
feat: allow disabling tests using checkin
1 parent 30ca197 commit 3ee2d8d

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

internal/checkincache/checkincache.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,24 @@ func Store(kvStore model.KeyValueStore, resp *model.OOAPICheckInResult) error {
4242

4343
// GetFeatureFlag returns the value of a check-in feature flag. In case of any
4444
// error this function will always return a false value.
45-
func GetFeatureFlag(kvStore model.KeyValueStore, name string) bool {
45+
func GetFeatureFlag(kvStore model.KeyValueStore, name string, defaultFlag bool) bool {
4646
data, err := kvStore.Get(CheckInFlagsState)
4747
if err != nil {
48-
return false // as documented
48+
return defaultFlag // as documented
4949
}
5050
var wrapper checkInFlagsWrapper
5151
if err := json.Unmarshal(data, &wrapper); err != nil {
52-
return false // as documented
52+
return defaultFlag // as documented
5353
}
54+
fmt.Println(wrapper)
5455
if time.Now().After(wrapper.Expire) {
55-
return false // as documented
56+
return defaultFlag // as documented
5657
}
57-
return wrapper.Flags[name] // works even if map is nil
58+
flag, ok := wrapper.Flags[name]
59+
if ok {
60+
return flag
61+
}
62+
return defaultFlag // default to true if the flag is not present
5863
}
5964

6065
// ExperimentEnabledKey returns the [model.KeyValueStore] key to use to
@@ -66,6 +71,6 @@ func ExperimentEnabledKey(name string) string {
6671
// ExperimentEnabled returns whether a given experiment has been enabled by a previous
6772
// execution of check-in. Some experiments are disabled by default for different reasons
6873
// and we use the check-in API to control whether and when they should be enabled.
69-
func ExperimentEnabled(kvStore model.KeyValueStore, name string) bool {
70-
return GetFeatureFlag(kvStore, ExperimentEnabledKey(name))
74+
func ExperimentEnabled(kvStore model.KeyValueStore, name string, defaultFlag bool) bool {
75+
return GetFeatureFlag(kvStore, ExperimentEnabledKey(name), defaultFlag)
7176
}

internal/checkincache/checkincache_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestGetFeatureFlag(t *testing.T) {
6565
return nil, expectedErr
6666
},
6767
}
68-
if GetFeatureFlag(memstore, "antani") {
68+
if GetFeatureFlag(memstore, "antani", false) {
6969
t.Fatal("expected to see false here")
7070
}
7171
})
@@ -76,7 +76,7 @@ func TestGetFeatureFlag(t *testing.T) {
7676
return []byte(`{`), nil
7777
},
7878
}
79-
if GetFeatureFlag(memstore, "antani") {
79+
if GetFeatureFlag(memstore, "antani", false) {
8080
t.Fatal("expected to see false here")
8181
}
8282
})
@@ -88,7 +88,7 @@ func TestGetFeatureFlag(t *testing.T) {
8888
return []byte(response), nil
8989
},
9090
}
91-
if GetFeatureFlag(memstore, "antani") {
91+
if GetFeatureFlag(memstore, "antani", false) {
9292
t.Fatal("expected to see false here")
9393
}
9494
})
@@ -109,7 +109,7 @@ func TestGetFeatureFlag(t *testing.T) {
109109
return data, nil
110110
},
111111
}
112-
if !GetFeatureFlag(memstore, "antani") {
112+
if !GetFeatureFlag(memstore, "antani", false) {
113113
t.Fatal("expected to see true here")
114114
}
115115
})
@@ -128,7 +128,7 @@ func TestGetFeatureFlag(t *testing.T) {
128128
return data, nil
129129
},
130130
}
131-
if GetFeatureFlag(memstore, "antani") {
131+
if GetFeatureFlag(memstore, "antani", false) {
132132
t.Fatal("expected to see false here")
133133
}
134134
})

internal/probeservices/checkin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ func TestCheckIn(t *testing.T) {
391391
}
392392

393393
// make sure we have written non-default values into the key-value store
394-
if !checkincache.GetFeatureFlag(client.KVStore, "torsf_enabled") {
394+
if !checkincache.GetFeatureFlag(client.KVStore, "torsf_enabled", false) {
395395
t.Fatal("expected to see true here")
396396
}
397-
if !checkincache.GetFeatureFlag(client.KVStore, "vanilla_tor_enabled") {
397+
if !checkincache.GetFeatureFlag(client.KVStore, "vanilla_tor_enabled", false) {
398398
t.Fatal("expected to see true here")
399399
}
400400
checkinrawdata, err := client.KVStore.Get(checkincache.CheckInFlagsState)
@@ -502,10 +502,10 @@ func TestCheckIn(t *testing.T) {
502502
}
503503

504504
// make sure we are still getting the default values here
505-
if checkincache.GetFeatureFlag(client.KVStore, "torsf_enabled") {
505+
if checkincache.GetFeatureFlag(client.KVStore, "torsf_enabled", false) {
506506
t.Fatal("expected to see false here")
507507
}
508-
if checkincache.GetFeatureFlag(client.KVStore, "vanilla_tor_enabled") {
508+
if checkincache.GetFeatureFlag(client.KVStore, "vanilla_tor_enabled", false) {
509509
t.Fatal("expected to see false here")
510510
}
511511
checkinrawdata, err := client.KVStore.Get(checkincache.CheckInFlagsState)

internal/registry/factory.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func NewFactory(name string, kvStore model.KeyValueStore, logger model.Logger) (
345345
// and improve the LTE implementation so that we can always use it. See the actual
346346
// issue test for additional details on this planned A/B test.
347347
switch {
348-
case name == "web_connectivity" && checkincache.GetFeatureFlag(kvStore, "webconnectivity_0.5"):
348+
case name == "web_connectivity" && checkincache.GetFeatureFlag(kvStore, "webconnectivity_0.5", false):
349349
// use LTE rather than the normal webconnectivity when the
350350
// feature flag has been set through the check-in API
351351
logger.Infof("using webconnectivity LTE")
@@ -367,16 +367,19 @@ func NewFactory(name string, kvStore model.KeyValueStore, logger model.Logger) (
367367
//
368368
// Note: check-in flags expire after 24h.
369369
//
370-
// TODO(https://github.com/ooni/probe/issues/2554): we need to restructure
371-
// how we run experiments to make sure check-in flags are always fresh.
370+
//
371+
//
372372
if factory.enabledByDefault {
373-
return factory, nil // enabled by default
373+
if !checkincache.ExperimentEnabled(kvStore, name, true) {
374+
return nil, fmt.Errorf("%s: %w", name, ErrRequiresForceEnable)
375+
}
376+
return factory, nil
374377
}
375378
if os.Getenv(OONI_FORCE_ENABLE_EXPERIMENT) == "1" {
376379
return factory, nil // enabled by environment variable
377380
}
378-
if checkincache.ExperimentEnabled(kvStore, name) {
379-
return factory, nil // enabled by check-in
381+
if checkincache.ExperimentEnabled(kvStore, name, false) {
382+
return factory, nil
380383
}
381384

382385
logger.Warnf(experimentDisabledByCheckInWarning, name)

internal/registry/torsf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func init() {
2020
},
2121
canonicalName: canonicalName,
2222
config: &torsf.Config{},
23-
enabledByDefault: false,
23+
enabledByDefault: true,
2424
inputPolicy: model.InputNone,
2525
}
2626
}

0 commit comments

Comments
 (0)