mirror of https://github.com/docker/cli.git
test: make sure environment vars are reset after tests
The trust tests were not resetting the environment after they ran, which could result in tests following those tests to fail. While at it, I also updated some other tests to use gotest.tools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
2f494e1181
commit
19bcebd122
|
@ -11,19 +11,12 @@ import (
|
||||||
"github.com/theupdateframework/notary/passphrase"
|
"github.com/theupdateframework/notary/passphrase"
|
||||||
"github.com/theupdateframework/notary/trustpinning"
|
"github.com/theupdateframework/notary/trustpinning"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
"gotest.tools/v3/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
func unsetENV() {
|
|
||||||
os.Unsetenv("DOCKER_CONTENT_TRUST")
|
|
||||||
os.Unsetenv("DOCKER_CONTENT_TRUST_SERVER")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestENVTrustServer(t *testing.T) {
|
func TestENVTrustServer(t *testing.T) {
|
||||||
defer unsetENV()
|
defer env.PatchAll(t, map[string]string{"DOCKER_CONTENT_TRUST_SERVER": "https://notary-test.com:5000"})()
|
||||||
indexInfo := ®istrytypes.IndexInfo{Name: "testserver"}
|
indexInfo := ®istrytypes.IndexInfo{Name: "testserver"}
|
||||||
if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "https://notary-test.com:5000"); err != nil {
|
|
||||||
t.Fatal("Failed to set ENV variable")
|
|
||||||
}
|
|
||||||
output, err := trust.Server(indexInfo)
|
output, err := trust.Server(indexInfo)
|
||||||
expectedStr := "https://notary-test.com:5000"
|
expectedStr := "https://notary-test.com:5000"
|
||||||
if err != nil || output != expectedStr {
|
if err != nil || output != expectedStr {
|
||||||
|
@ -32,11 +25,8 @@ func TestENVTrustServer(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPENVTrustServer(t *testing.T) {
|
func TestHTTPENVTrustServer(t *testing.T) {
|
||||||
defer unsetENV()
|
defer env.PatchAll(t, map[string]string{"DOCKER_CONTENT_TRUST_SERVER": "http://notary-test.com:5000"})()
|
||||||
indexInfo := ®istrytypes.IndexInfo{Name: "testserver"}
|
indexInfo := ®istrytypes.IndexInfo{Name: "testserver"}
|
||||||
if err := os.Setenv("DOCKER_CONTENT_TRUST_SERVER", "http://notary-test.com:5000"); err != nil {
|
|
||||||
t.Fatal("Failed to set ENV variable")
|
|
||||||
}
|
|
||||||
_, err := trust.Server(indexInfo)
|
_, err := trust.Server(indexInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected error with invalid scheme")
|
t.Fatal("Expected error with invalid scheme")
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
|
"gotest.tools/v3/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
var homeKey = "HOME"
|
var homeKey = "HOME"
|
||||||
|
@ -120,12 +121,7 @@ email`: "Invalid auth configuration file",
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
defer env.Patch(t, homeKey, tmpHome)()
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeDir) }()
|
|
||||||
os.Setenv(homeKey, tmpHome)
|
|
||||||
|
|
||||||
for content, expectedError := range invalids {
|
for content, expectedError := range invalids {
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
|
@ -141,12 +137,7 @@ func TestOldValidAuth(t *testing.T) {
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
defer env.Patch(t, homeKey, tmpHome)()
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeDir) }()
|
|
||||||
os.Setenv(homeKey, tmpHome)
|
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
js := `username = am9lam9lOmhlbGxv
|
js := `username = am9lam9lOmhlbGxv
|
||||||
|
@ -180,12 +171,7 @@ func TestOldJSONInvalid(t *testing.T) {
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
defer env.Patch(t, homeKey, tmpHome)()
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeDir) }()
|
|
||||||
os.Setenv(homeKey, tmpHome)
|
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}`
|
js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}`
|
||||||
|
@ -204,12 +190,7 @@ func TestOldJSON(t *testing.T) {
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
defer env.Patch(t, homeKey, tmpHome)()
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeDir) }()
|
|
||||||
os.Setenv(homeKey, tmpHome)
|
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
||||||
|
|
Loading…
Reference in New Issue