test(cloudflare): clear environment variables before setting test values (#5851)

* test(cloudflare): clear environment variables before setting test values

Signed-off-by: u-kai <76635578+u-kai@users.noreply.github.com>

* refactor(cloudflare): extract environment variable names to package constants

Signed-off-by: u-kai <76635578+u-kai@users.noreply.github.com>

* refactor(cloudflare): use testutils helper for test environment setup

Signed-off-by: u-kai <76635578+u-kai@users.noreply.github.com>

* refactor(cloudflare): simplify token handling and improve test env setup

Signed-off-by: u-kai <76635578+u-kai@users.noreply.github.com>

---------

Signed-off-by: u-kai <76635578+u-kai@users.noreply.github.com>
This commit is contained in:
Kai Udo 2025-09-22 21:10:17 +09:00 committed by GitHub
parent ef621078c2
commit fe753cb8e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 16 deletions

View File

@ -46,6 +46,11 @@ import (
type changeAction int type changeAction int
const ( const (
// Environment variable names for CloudFlare authentication
cfAPIEmailEnvKey = "CF_API_EMAIL"
cfAPIKeyEnvKey = "CF_API_KEY"
cfAPITokenEnvKey = "CF_API_TOKEN"
// cloudFlareCreate is a ChangeAction enum value // cloudFlareCreate is a ChangeAction enum value
cloudFlareCreate changeAction = iota cloudFlareCreate changeAction = iota
// cloudFlareDelete is a ChangeAction enum value // cloudFlareDelete is a ChangeAction enum value
@ -327,12 +332,12 @@ func NewCloudFlareProvider(
configV4 *cloudflare.Client configV4 *cloudflare.Client
err error err error
) )
if os.Getenv("CF_API_TOKEN") != "" { token := os.Getenv(cfAPITokenEnvKey)
token := os.Getenv("CF_API_TOKEN") if token != "" {
if strings.HasPrefix(token, "file:") { if trimed, ok := strings.CutPrefix(token, "file:"); ok {
tokenBytes, err := os.ReadFile(strings.TrimPrefix(token, "file:")) tokenBytes, err := os.ReadFile(trimed)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read CF_API_TOKEN from file: %w", err) return nil, fmt.Errorf("failed to read %s from file: %w", cfAPITokenEnvKey, err)
} }
token = strings.TrimSpace(string(tokenBytes)) token = strings.TrimSpace(string(tokenBytes))
} }
@ -341,10 +346,10 @@ func NewCloudFlareProvider(
option.WithAPIToken(token), option.WithAPIToken(token),
) )
} else { } else {
config, err = cloudflarev0.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL")) config, err = cloudflarev0.New(os.Getenv(cfAPIKeyEnvKey), os.Getenv(cfAPIEmailEnvKey))
configV4 = cloudflare.NewClient( configV4 = cloudflare.NewClient(
option.WithAPIKey(os.Getenv("CF_API_KEY")), option.WithAPIKey(os.Getenv(cfAPIKeyEnvKey)),
option.WithAPIEmail(os.Getenv("CF_API_EMAIL")), option.WithAPIEmail(os.Getenv(cfAPIEmailEnvKey)),
) )
} }
if err != nil { if err != nil {

View File

@ -949,6 +949,13 @@ func TestCloudflareProvider(t *testing.T) {
Value string Value string
} }
// unset environment variables to avoid interference with tests
testutils.TestHelperEnvSetter(t, map[string]string{
cfAPIEmailEnvKey: "",
cfAPIKeyEnvKey: "",
cfAPITokenEnvKey: "",
})
tokenFile := "/tmp/cf_api_token" tokenFile := "/tmp/cf_api_token"
if err := os.WriteFile(tokenFile, []byte("abc123def"), 0o644); err != nil { if err := os.WriteFile(tokenFile, []byte("abc123def"), 0o644); err != nil {
t.Errorf("failed to write token file, %s", err) t.Errorf("failed to write token file, %s", err)
@ -962,22 +969,22 @@ func TestCloudflareProvider(t *testing.T) {
{ {
Name: "use_api_token", Name: "use_api_token",
Environment: []EnvVar{ Environment: []EnvVar{
{Key: "CF_API_TOKEN", Value: "abc123def"}, {Key: cfAPITokenEnvKey, Value: "abc123def"},
}, },
ShouldFail: false, ShouldFail: false,
}, },
{ {
Name: "use_api_token_file_contents", Name: "use_api_token_file_contents",
Environment: []EnvVar{ Environment: []EnvVar{
{Key: "CF_API_TOKEN", Value: tokenFile}, {Key: cfAPITokenEnvKey, Value: tokenFile},
}, },
ShouldFail: false, ShouldFail: false,
}, },
{ {
Name: "use_email_and_key", Name: "use_email_and_key",
Environment: []EnvVar{ Environment: []EnvVar{
{Key: "CF_API_KEY", Value: "xxxxxxxxxxxxxxxxx"}, {Key: cfAPIKeyEnvKey, Value: "xxxxxxxxxxxxxxxxx"},
{Key: "CF_API_EMAIL", Value: "test@test.com"}, {Key: cfAPIEmailEnvKey, Value: "test@test.com"},
}, },
ShouldFail: false, ShouldFail: false,
}, },
@ -989,14 +996,14 @@ func TestCloudflareProvider(t *testing.T) {
{ {
Name: "use_credentials_in_missing_file", Name: "use_credentials_in_missing_file",
Environment: []EnvVar{ Environment: []EnvVar{
{Key: "CF_API_TOKEN", Value: "file://abc"}, {Key: cfAPITokenEnvKey, Value: "file://abc"},
}, },
ShouldFail: true, ShouldFail: true,
}, },
{ {
Name: "use_credentials_in_missing_file", Name: "use_credentials_in_missing_file",
Environment: []EnvVar{ Environment: []EnvVar{
{Key: "CF_API_TOKEN", Value: "file:/tmp/cf_api_token"}, {Key: cfAPITokenEnvKey, Value: "file:/tmp/cf_api_token"},
}, },
ShouldFail: false, ShouldFail: false,
}, },
@ -1809,8 +1816,10 @@ func TestCustomTTLWithEnabledProxyNotChanged(t *testing.T) {
} }
func TestCloudFlareProvider_Region(t *testing.T) { func TestCloudFlareProvider_Region(t *testing.T) {
t.Setenv("CF_API_TOKEN", "abc123def") testutils.TestHelperEnvSetter(t, map[string]string{
t.Setenv("CF_API_EMAIL", "test@test.com") cfAPITokenEnvKey: "abc123def",
cfAPIEmailEnvKey: "test@test.com",
})
provider, err := NewCloudFlareProvider( provider, err := NewCloudFlareProvider(
endpoint.NewDomainFilter([]string{"example.com"}), endpoint.NewDomainFilter([]string{"example.com"}),
provider.ZoneIDFilter{}, provider.ZoneIDFilter{},