mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Make sure `flagValue.String()` can be called even when flagValue is
created by `reflect.New`
```go
import (
"flag"
"log"
"go.opentelemetry.io/collector/featuregate"
)
func main() {
fs := new(flag.FlagSet)
featuregate.GlobalRegistry().RegisterFlags(fs)
if err := fs.Parse([]string{"--help"}); err != nil {
log.Fatalf("failed to parse flags: %v", err)
}
}
```
Before this PR
```
Usage:
-feature-gates value
Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.
panic calling String method on zero featuregate.flagValue for flag feature-gates: runtime error: invalid memory address or nil pointer dereference
failed to parse flags: flag: help requested
```
Note that there is a panic in the message.
After this PR
```
Usage:
-feature-gates value
Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.
failed to parse flags: flag: help requested
```
---------
Co-authored-by: Yang Song <[email protected]>
140 lines
4.5 KiB
Go
140 lines
4.5 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package featuregate
|
|
|
|
import (
|
|
"flag"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewFlag(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
input string
|
|
expectedSetErr bool
|
|
expected map[string]bool
|
|
expectedStr string
|
|
}{
|
|
{
|
|
name: "empty item",
|
|
input: "",
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "simple enable alpha",
|
|
input: "alpha",
|
|
expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "plus enable alpha",
|
|
input: "+alpha",
|
|
expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "disabled beta",
|
|
input: "-beta",
|
|
expected: map[string]bool{"alpha": false, "beta": false, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,-beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "multiple items",
|
|
input: "-beta,alpha",
|
|
expected: map[string]bool{"alpha": true, "beta": false, "deprecated": false, "stable": true},
|
|
expectedStr: "alpha,-beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "multiple items with plus",
|
|
input: "-beta,+alpha",
|
|
expected: map[string]bool{"alpha": true, "beta": false, "deprecated": false, "stable": true},
|
|
expectedStr: "alpha,-beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "repeated items",
|
|
input: "alpha,-beta,-alpha",
|
|
expected: map[string]bool{"alpha": false, "beta": false, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,-beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "multiple plus items",
|
|
input: "+alpha,+beta",
|
|
expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "enable stable",
|
|
input: "stable",
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "disable stable",
|
|
input: "-stable",
|
|
expectedSetErr: true,
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "enable deprecated",
|
|
input: "deprecated",
|
|
expectedSetErr: true,
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "disable deprecated",
|
|
input: "-deprecated",
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "enable missing",
|
|
input: "missing",
|
|
expectedSetErr: true,
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
{
|
|
name: "disable missing",
|
|
input: "missing",
|
|
expectedSetErr: true,
|
|
expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
|
|
expectedStr: "-alpha,beta,-deprecated,stable",
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
reg := NewRegistry()
|
|
reg.MustRegister("alpha", StageAlpha)
|
|
reg.MustRegister("beta", StageBeta)
|
|
reg.MustRegister("deprecated", StageDeprecated, WithRegisterToVersion("1.0.0"))
|
|
reg.MustRegister("stable", StageStable, WithRegisterToVersion("1.0.0"))
|
|
fs := flag.NewFlagSet("test", flag.ContinueOnError)
|
|
reg.RegisterFlags(fs)
|
|
registrationFlag := fs.Lookup(featureGatesFlag)
|
|
require.NotNil(t, registrationFlag)
|
|
if tt.expectedSetErr {
|
|
require.Error(t, registrationFlag.Value.Set(tt.input))
|
|
} else {
|
|
require.NoError(t, registrationFlag.Value.Set(tt.input))
|
|
}
|
|
got := map[string]bool{}
|
|
reg.VisitAll(func(g *Gate) {
|
|
got[g.ID()] = g.IsEnabled()
|
|
})
|
|
assert.Equal(t, tt.expected, got)
|
|
assert.Equal(t, tt.expectedStr, registrationFlag.Value.String())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFlagStringNotInitialize(t *testing.T) {
|
|
flag := &flagValue{}
|
|
assert.Empty(t, flag.String())
|
|
}
|