Skip to content

Commit

Permalink
fix tenv linter
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Apr 30, 2024
1 parent dd4f5f4 commit 6fc8034
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 72 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ issues:
- contextcheck
- promlinter
- errname
- tenv
- exhaustive
- nilerr
- interfacebloat
Expand Down
13 changes: 3 additions & 10 deletions pkg/issuer/acme/dns/clouddns/clouddns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,28 @@ func init() {
}
}

func restoreGCloudEnv() {
os.Setenv("GCE_PROJECT", gcloudProject)
}

func TestNewDNSProviderValid(t *testing.T) {
if !gcloudLiveTest {
t.Skip("skipping live test (requires credentials)")
}
os.Setenv("GCE_PROJECT", "")
t.Setenv("GCE_PROJECT", "")
_, err := NewDNSProviderCredentials("my-project", util.RecursiveNameservers, "")
assert.NoError(t, err)
restoreGCloudEnv()
}

func TestNewDNSProviderValidEnv(t *testing.T) {
if !gcloudLiveTest {
t.Skip("skipping live test (requires credentials)")
}
os.Setenv("GCE_PROJECT", "my-project")
t.Setenv("GCE_PROJECT", "my-project")
_, err := NewDNSProviderEnvironment(util.RecursiveNameservers, "")
assert.NoError(t, err)
restoreGCloudEnv()
}

func TestNewDNSProviderMissingCredErr(t *testing.T) {
os.Setenv("GCE_PROJECT", "")
t.Setenv("GCE_PROJECT", "")
_, err := NewDNSProviderEnvironment(util.RecursiveNameservers, "")
assert.EqualError(t, err, "Google Cloud project name missing")
restoreGCloudEnv()
}

func TestLiveGoogleCloudPresent(t *testing.T) {
Expand Down
30 changes: 10 additions & 20 deletions pkg/issuer/acme/dns/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,39 @@ func init() {
}
}

func restoreCloudFlareEnv() {
os.Setenv("CLOUDFLARE_EMAIL", cflareEmail)
os.Setenv("CLOUDFLARE_API_KEY", cflareAPIKey)
}

func TestNewDNSProviderValidAPIKey(t *testing.T) {
os.Setenv("CLOUDFLARE_EMAIL", "")
os.Setenv("CLOUDFLARE_API_KEY", "")
t.Setenv("CLOUDFLARE_EMAIL", "")
t.Setenv("CLOUDFLARE_API_KEY", "")
_, err := NewDNSProviderCredentials("123", "123", "", util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err)
restoreCloudFlareEnv()
}

func TestNewDNSProviderValidAPIToken(t *testing.T) {
os.Setenv("CLOUDFLARE_EMAIL", "")
os.Setenv("CLOUDFLARE_API_KEY", "")
t.Setenv("CLOUDFLARE_EMAIL", "")
t.Setenv("CLOUDFLARE_API_KEY", "")
_, err := NewDNSProviderCredentials("123", "", "123", util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err)
restoreCloudFlareEnv()
}

func TestNewDNSProviderKeyAndTokenProvided(t *testing.T) {
os.Setenv("CLOUDFLARE_EMAIL", "")
os.Setenv("CLOUDFLARE_API_KEY", "")
t.Setenv("CLOUDFLARE_EMAIL", "")
t.Setenv("CLOUDFLARE_API_KEY", "")
_, err := NewDNSProviderCredentials("123", "123", "123", util.RecursiveNameservers, "cert-manager-test")
assert.EqualError(t, err, "the Cloudflare API key and API token cannot be both present simultaneously")
restoreCloudFlareEnv()
}

func TestNewDNSProviderValidApiKeyEnv(t *testing.T) {
os.Setenv("CLOUDFLARE_EMAIL", "test@example.com")
os.Setenv("CLOUDFLARE_API_KEY", "123")
t.Setenv("CLOUDFLARE_EMAIL", "test@example.com")
t.Setenv("CLOUDFLARE_API_KEY", "123")
_, err := NewDNSProvider(util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err)
restoreCloudFlareEnv()
}

func TestNewDNSProviderMissingCredErr(t *testing.T) {
os.Setenv("CLOUDFLARE_EMAIL", "")
os.Setenv("CLOUDFLARE_API_KEY", "")
t.Setenv("CLOUDFLARE_EMAIL", "")
t.Setenv("CLOUDFLARE_API_KEY", "")
_, err := NewDNSProvider(util.RecursiveNameservers, "cert-manager-test")
assert.EqualError(t, err, "no Cloudflare credential has been given (can be either an API key or an API token)")
restoreCloudFlareEnv()
}

func TestFindNearestZoneForFQDN(t *testing.T) {
Expand Down
13 changes: 3 additions & 10 deletions pkg/issuer/acme/dns/digitalocean/digitalocean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,22 @@ func init() {
}
}

func restoreEnv() {
os.Setenv("DIGITALOCEAN_TOKEN", doToken)
}

func TestNewDNSProviderValid(t *testing.T) {
os.Setenv("DIGITALOCEAN_TOKEN", "")
t.Setenv("DIGITALOCEAN_TOKEN", "")
_, err := NewDNSProviderCredentials("123", util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err)
restoreEnv()
}

func TestNewDNSProviderValidEnv(t *testing.T) {
os.Setenv("DIGITALOCEAN_TOKEN", "123")
t.Setenv("DIGITALOCEAN_TOKEN", "123")
_, err := NewDNSProvider(util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err)
restoreEnv()
}

func TestNewDNSProviderMissingCredErr(t *testing.T) {
os.Setenv("DIGITALOCEAN_TOKEN", "")
t.Setenv("DIGITALOCEAN_TOKEN", "")
_, err := NewDNSProvider(util.RecursiveNameservers, "cert-manager-test")
assert.EqualError(t, err, "DigitalOcean token missing")
restoreEnv()
}

func TestDigitalOceanPresent(t *testing.T) {
Expand Down
39 changes: 8 additions & 31 deletions pkg/issuer/acme/dns/route53/route53_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -33,24 +32,6 @@ import (
logf "github.com/cert-manager/cert-manager/pkg/logs"
)

var (
route53Secret string
route53Key string
route53Region string
)

func init() {
route53Key = os.Getenv("AWS_ACCESS_KEY_ID")
route53Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
route53Region = os.Getenv("AWS_REGION")
}

func restoreRoute53Env() {
os.Setenv("AWS_ACCESS_KEY_ID", route53Key)
os.Setenv("AWS_SECRET_ACCESS_KEY", route53Secret)
os.Setenv("AWS_REGION", route53Region)
}

func makeRoute53Provider(ts *httptest.Server) (*DNSProvider, error) {
cfg, err := config.LoadDefaultConfig(
context.TODO(),
Expand All @@ -73,10 +54,9 @@ func makeRoute53Provider(ts *httptest.Server) (*DNSProvider, error) {
}

func TestAmbientCredentialsFromEnv(t *testing.T) {
os.Setenv("AWS_ACCESS_KEY_ID", "123")
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
os.Setenv("AWS_REGION", "us-east-1")
defer restoreRoute53Env()
t.Setenv("AWS_ACCESS_KEY_ID", "123")
t.Setenv("AWS_SECRET_ACCESS_KEY", "123")
t.Setenv("AWS_REGION", "us-east-1")

provider, err := NewDNSProvider("", "", "", "", "", true, util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err, "Expected no error constructing DNSProvider")
Expand All @@ -88,18 +68,16 @@ func TestAmbientCredentialsFromEnv(t *testing.T) {
}

func TestNoCredentialsFromEnv(t *testing.T) {
os.Setenv("AWS_ACCESS_KEY_ID", "123")
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
os.Setenv("AWS_REGION", "us-east-1")
defer restoreRoute53Env()
t.Setenv("AWS_ACCESS_KEY_ID", "123")
t.Setenv("AWS_SECRET_ACCESS_KEY", "123")
t.Setenv("AWS_REGION", "us-east-1")

_, err := NewDNSProvider("", "", "", "", "", false, util.RecursiveNameservers, "cert-manager-test")
assert.Error(t, err, "Expected error constructing DNSProvider with no credentials and not ambient")
}

func TestAmbientRegionFromEnv(t *testing.T) {
os.Setenv("AWS_REGION", "us-east-1")
defer restoreRoute53Env()
t.Setenv("AWS_REGION", "us-east-1")

provider, err := NewDNSProvider("", "", "", "", "", true, util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err, "Expected no error constructing DNSProvider")
Expand All @@ -108,8 +86,7 @@ func TestAmbientRegionFromEnv(t *testing.T) {
}

func TestNoRegionFromEnv(t *testing.T) {
os.Setenv("AWS_REGION", "us-east-1")
defer restoreRoute53Env()
t.Setenv("AWS_REGION", "us-east-1")

provider, err := NewDNSProvider("marx", "swordfish", "", "", "", false, util.RecursiveNameservers, "cert-manager-test")
assert.NoError(t, err, "Expected no error constructing DNSProvider")
Expand Down

0 comments on commit 6fc8034

Please sign in to comment.