start using t.SetEnv instead of os.Setenv (#14787)

This commit is contained in:
Harshavardhana 2022-04-23 15:33:45 -07:00 committed by GitHub
parent 96adfaebe1
commit d087e28dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 18 deletions

View File

@ -25,6 +25,8 @@ linters:
- varcheck - varcheck
- gocritic - gocritic
- gofumpt - gofumpt
- tenv
- durationcheck
linters-settings: linters-settings:
gofumpt: gofumpt:

View File

@ -115,19 +115,17 @@ func TestDownloadURL(t *testing.T) {
} }
} }
os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5") t.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
durl = getDownloadURL(minioVersion1) durl = getDownloadURL(minioVersion1)
if durl != kubernetesDeploymentDoc { if durl != kubernetesDeploymentDoc {
t.Errorf("Expected %s, got %s", kubernetesDeploymentDoc, durl) t.Errorf("Expected %s, got %s", kubernetesDeploymentDoc, durl)
} }
os.Unsetenv("KUBERNETES_SERVICE_HOST")
os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111") t.Setenv("MESOS_CONTAINER_NAME", "mesos-1111")
durl = getDownloadURL(minioVersion1) durl = getDownloadURL(minioVersion1)
if durl != mesosDeploymentDoc { if durl != mesosDeploymentDoc {
t.Errorf("Expected %s, got %s", mesosDeploymentDoc, durl) t.Errorf("Expected %s, got %s", mesosDeploymentDoc, durl)
} }
os.Unsetenv("MESOS_CONTAINER_NAME")
} }
// Tests user agent string. // Tests user agent string.
@ -162,10 +160,13 @@ func TestUserAgent(t *testing.T) {
sci := globalIsCICD sci := globalIsCICD
globalIsCICD = false globalIsCICD = false
os.Setenv(testCase.envName, testCase.envValue) if testCase.envName != "" {
t.Setenv(testCase.envName, testCase.envValue)
if testCase.envName == "MESOS_CONTAINER_NAME" { if testCase.envName == "MESOS_CONTAINER_NAME" {
os.Setenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION", "mesos-1111") t.Setenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION", "mesos-1111")
} }
}
str := getUserAgent(testCase.mode) str := getUserAgent(testCase.mode)
expectedStr := testCase.expectedStr expectedStr := testCase.expectedStr
if IsDocker() { if IsDocker() {
@ -188,12 +189,11 @@ func TestIsDCOS(t *testing.T) {
globalIsCICD = sci globalIsCICD = sci
}() }()
os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111") t.Setenv("MESOS_CONTAINER_NAME", "mesos-1111")
dcos := IsDCOS() dcos := IsDCOS()
if !dcos { if !dcos {
t.Fatalf("Expected %t, got %t", true, dcos) t.Fatalf("Expected %t, got %t", true, dcos)
} }
os.Unsetenv("MESOS_CONTAINER_NAME") os.Unsetenv("MESOS_CONTAINER_NAME")
dcos = IsDCOS() dcos = IsDCOS()
if dcos { if dcos {
@ -209,12 +209,13 @@ func TestIsKubernetes(t *testing.T) {
globalIsCICD = sci globalIsCICD = sci
}() }()
os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5") t.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
kubernetes := IsKubernetes() kubernetes := IsKubernetes()
if !kubernetes { if !kubernetes {
t.Fatalf("Expected %t, got %t", true, kubernetes) t.Fatalf("Expected %t, got %t", true, kubernetes)
} }
os.Unsetenv("KUBERNETES_SERVICE_HOST") os.Unsetenv("KUBERNETES_SERVICE_HOST")
kubernetes = IsKubernetes() kubernetes = IsKubernetes()
if kubernetes { if kubernetes {
t.Fatalf("Expected %t, got %t", false, kubernetes) t.Fatalf("Expected %t, got %t", false, kubernetes)

View File

@ -181,6 +181,9 @@ M9ofSEt/bdRD
} }
func TestLoadX509KeyPair(t *testing.T) { func TestLoadX509KeyPair(t *testing.T) {
t.Cleanup(func() {
os.Unsetenv(EnvCertPassword)
})
for i, testCase := range loadX509KeyPairTests { for i, testCase := range loadX509KeyPairTests {
privateKey, err := createTempFile("private.key", testCase.privateKey) privateKey, err := createTempFile("private.key", testCase.privateKey)
if err != nil { if err != nil {
@ -192,9 +195,8 @@ func TestLoadX509KeyPair(t *testing.T) {
t.Fatalf("Test %d: failed to create tmp certificate file: %v", i, err) t.Fatalf("Test %d: failed to create tmp certificate file: %v", i, err)
} }
os.Unsetenv(EnvCertPassword)
if testCase.password != "" { if testCase.password != "" {
os.Setenv(EnvCertPassword, testCase.password) t.Setenv(EnvCertPassword, testCase.password)
} }
_, err = LoadX509KeyPair(certificate, privateKey) _, err = LoadX509KeyPair(certificate, privateKey)
if err != nil && !testCase.shouldFail { if err != nil && !testCase.shouldFail {

View File

@ -1655,8 +1655,7 @@ func TestCSVRanges(t *testing.T) {
} }
func TestParquetInput(t *testing.T) { func TestParquetInput(t *testing.T) {
os.Setenv("MINIO_API_SELECT_PARQUET", "on") t.Setenv("MINIO_API_SELECT_PARQUET", "on")
defer os.Setenv("MINIO_API_SELECT_PARQUET", "off")
testTable := []struct { testTable := []struct {
requestXML []byte requestXML []byte
@ -1757,8 +1756,7 @@ func TestParquetInput(t *testing.T) {
} }
func TestParquetInputSchema(t *testing.T) { func TestParquetInputSchema(t *testing.T) {
os.Setenv("MINIO_API_SELECT_PARQUET", "on") t.Setenv("MINIO_API_SELECT_PARQUET", "on")
defer os.Setenv("MINIO_API_SELECT_PARQUET", "off")
testTable := []struct { testTable := []struct {
requestXML []byte requestXML []byte
@ -1860,8 +1858,7 @@ func TestParquetInputSchema(t *testing.T) {
} }
func TestParquetInputSchemaCSV(t *testing.T) { func TestParquetInputSchemaCSV(t *testing.T) {
os.Setenv("MINIO_API_SELECT_PARQUET", "on") t.Setenv("MINIO_API_SELECT_PARQUET", "on")
defer os.Setenv("MINIO_API_SELECT_PARQUET", "off")
testTable := []struct { testTable := []struct {
requestXML []byte requestXML []byte