mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 04:27:54 +00:00
Replace os.Setenv with testing.T.Setenv in tests
T.Setenv ensures that the environment is returned to its prior state when the test ends. It also panics when called from a parallel test to prevent racy test interdependencies.
This commit is contained in:
parent
53cccbe4f9
commit
d38ac7e7c6
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -705,8 +704,6 @@ func TestGCEPDLimits(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMaxVols(t *testing.T) {
|
func TestGetMaxVols(t *testing.T) {
|
||||||
previousValue := os.Getenv(KubeMaxPDVols)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
rawMaxVols string
|
rawMaxVols string
|
||||||
expected int
|
expected int
|
||||||
@ -731,18 +728,13 @@ func TestGetMaxVols(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
os.Setenv(KubeMaxPDVols, test.rawMaxVols)
|
t.Setenv(KubeMaxPDVols, test.rawMaxVols)
|
||||||
result := getMaxVolLimitFromEnv()
|
result := getMaxVolLimitFromEnv()
|
||||||
if result != test.expected {
|
if result != test.expected {
|
||||||
t.Errorf("expected %v got %v", test.expected, result)
|
t.Errorf("expected %v got %v", test.expected, result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Unsetenv(KubeMaxPDVols)
|
|
||||||
if previousValue != "" {
|
|
||||||
os.Setenv(KubeMaxPDVols, previousValue)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFakePVCLister(filterName string) fakeframework.PersistentVolumeClaimLister {
|
func getFakePVCLister(filterName string) fakeframework.PersistentVolumeClaimLister {
|
||||||
|
11
pkg/util/env/env_test.go
vendored
11
pkg/util/env/env_test.go
vendored
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package env
|
package env
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ func TestGetEnvAsStringOrFallback(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
key := "FLOCKER_SET_VAR"
|
key := "FLOCKER_SET_VAR"
|
||||||
os.Setenv(key, expected)
|
t.Setenv(key, expected)
|
||||||
assert.Equal(expected, GetEnvAsStringOrFallback(key, "~"+expected))
|
assert.Equal(expected, GetEnvAsStringOrFallback(key, "~"+expected))
|
||||||
|
|
||||||
key = "FLOCKER_UNSET_VAR"
|
key = "FLOCKER_UNSET_VAR"
|
||||||
@ -43,7 +42,7 @@ func TestGetEnvAsIntOrFallback(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
key := "FLOCKER_SET_VAR"
|
key := "FLOCKER_SET_VAR"
|
||||||
os.Setenv(key, strconv.Itoa(expected))
|
t.Setenv(key, strconv.Itoa(expected))
|
||||||
returnVal, _ := GetEnvAsIntOrFallback(key, 1)
|
returnVal, _ := GetEnvAsIntOrFallback(key, 1)
|
||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
|
|
||||||
@ -52,7 +51,7 @@ func TestGetEnvAsIntOrFallback(t *testing.T) {
|
|||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
|
|
||||||
key = "FLOCKER_SET_VAR"
|
key = "FLOCKER_SET_VAR"
|
||||||
os.Setenv(key, "not-an-int")
|
t.Setenv(key, "not-an-int")
|
||||||
returnVal, err := GetEnvAsIntOrFallback(key, 1)
|
returnVal, err := GetEnvAsIntOrFallback(key, 1)
|
||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -66,7 +65,7 @@ func TestGetEnvAsFloat64OrFallback(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
key := "FLOCKER_SET_VAR"
|
key := "FLOCKER_SET_VAR"
|
||||||
os.Setenv(key, "1.0")
|
t.Setenv(key, "1.0")
|
||||||
returnVal, _ := GetEnvAsFloat64OrFallback(key, 2.0)
|
returnVal, _ := GetEnvAsFloat64OrFallback(key, 2.0)
|
||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
|
|
||||||
@ -75,7 +74,7 @@ func TestGetEnvAsFloat64OrFallback(t *testing.T) {
|
|||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
|
|
||||||
key = "FLOCKER_SET_VAR"
|
key = "FLOCKER_SET_VAR"
|
||||||
os.Setenv(key, "not-a-float")
|
t.Setenv(key, "not-a-float")
|
||||||
returnVal, err := GetEnvAsFloat64OrFallback(key, 1.0)
|
returnVal, err := GetEnvAsFloat64OrFallback(key, 1.0)
|
||||||
assert.Equal(expected, returnVal)
|
assert.Equal(expected, returnVal)
|
||||||
assert.EqualError(err, "strconv.ParseFloat: parsing \"not-a-float\": invalid syntax")
|
assert.EqualError(err, "strconv.ParseFloat: parsing \"not-a-float\": invalid syntax")
|
||||||
|
Loading…
Reference in New Issue
Block a user