|
| 1 | +package deviceid |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGet_DisabledByEnv(t *testing.T) { |
| 10 | + t.Setenv(EnvDisable, "1") |
| 11 | + reset() |
| 12 | + assert.Equal(t, "", Get(), "Get must return empty when DOCTL_DISABLE_DEVICE_ID is set") |
| 13 | +} |
| 14 | + |
| 15 | +func TestGet_DisabledByEnv_VariantsAreCaseInsensitive(t *testing.T) { |
| 16 | + for _, v := range []string{"1", "true", "TRUE", "Yes", " yes "} { |
| 17 | + t.Run(v, func(t *testing.T) { |
| 18 | + t.Setenv(EnvDisable, v) |
| 19 | + reset() |
| 20 | + assert.Equal(t, "", Get()) |
| 21 | + }) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestGet_NotDisabledByEnv_FalsyValuesIgnored(t *testing.T) { |
| 26 | + // "0", "false", "" must not be treated as opt-out. We can't assert what |
| 27 | + // Get returns (it depends on the host), but we can assert isDisabled |
| 28 | + // behavior directly, which is the only env-driven branch in Get. |
| 29 | + for _, v := range []string{"", "0", "false", "no", "off", "anything-else"} { |
| 30 | + t.Run(v, func(t *testing.T) { |
| 31 | + t.Setenv(EnvDisable, v) |
| 32 | + assert.False(t, isDisabled(), "value %q must not trip opt-out", v) |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestGet_Cached(t *testing.T) { |
| 38 | + // Two calls must yield the same value; sync.Once guarantees read() is |
| 39 | + // invoked at most once. We rely on Get being deterministic for the |
| 40 | + // lifetime of the process. |
| 41 | + t.Setenv(EnvDisable, "") |
| 42 | + reset() |
| 43 | + first := Get() |
| 44 | + second := Get() |
| 45 | + assert.Equal(t, first, second) |
| 46 | +} |
0 commit comments