docker_cli_logout_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "github.com/docker/docker/pkg/integration/checker"
  10. "github.com/go-check/check"
  11. )
  12. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *check.C) {
  13. osPath := os.Getenv("PATH")
  14. defer os.Setenv("PATH", osPath)
  15. workingDir, err := os.Getwd()
  16. c.Assert(err, checker.IsNil)
  17. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  18. c.Assert(err, checker.IsNil)
  19. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  20. os.Setenv("PATH", testPath)
  21. repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
  22. tmp, err := ioutil.TempDir("", "integration-cli-")
  23. c.Assert(err, checker.IsNil)
  24. externalAuthConfig := `{ "credsStore": "shell-test" }`
  25. configPath := filepath.Join(tmp, "config.json")
  26. err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
  27. c.Assert(err, checker.IsNil)
  28. dockerCmd(c, "--config", tmp, "login", "-u", s.reg.username, "-p", s.reg.password, privateRegistryURL)
  29. b, err := ioutil.ReadFile(configPath)
  30. c.Assert(err, checker.IsNil)
  31. c.Assert(string(b), checker.Not(checker.Contains), "\"auth\":")
  32. c.Assert(string(b), checker.Contains, privateRegistryURL)
  33. dockerCmd(c, "--config", tmp, "tag", "busybox", repoName)
  34. dockerCmd(c, "--config", tmp, "push", repoName)
  35. dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
  36. b, err = ioutil.ReadFile(configPath)
  37. c.Assert(err, checker.IsNil)
  38. c.Assert(string(b), checker.Not(checker.Contains), privateRegistryURL)
  39. // check I cannot pull anymore
  40. out, _, err := dockerCmdWithError("--config", tmp, "pull", repoName)
  41. c.Assert(err, check.NotNil, check.Commentf(out))
  42. c.Assert(out, checker.Contains, "Error: image dockercli/busybox:authtest not found")
  43. }
  44. // #23100
  45. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *check.C) {
  46. osPath := os.Getenv("PATH")
  47. defer os.Setenv("PATH", osPath)
  48. workingDir, err := os.Getwd()
  49. c.Assert(err, checker.IsNil)
  50. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  51. c.Assert(err, checker.IsNil)
  52. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  53. os.Setenv("PATH", testPath)
  54. cmd := exec.Command("docker-credential-shell-test", "store")
  55. stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": "%s", "Secret": "%s"}`, privateRegistryURL, s.reg.username, s.reg.password)))
  56. cmd.Stdin = stdin
  57. c.Assert(cmd.Run(), checker.IsNil)
  58. tmp, err := ioutil.TempDir("", "integration-cli-")
  59. c.Assert(err, checker.IsNil)
  60. externalAuthConfig := fmt.Sprintf(`{ "auths": {"https://%s": {}}, "credsStore": "shell-test" }`, privateRegistryURL)
  61. configPath := filepath.Join(tmp, "config.json")
  62. err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
  63. c.Assert(err, checker.IsNil)
  64. dockerCmd(c, "--config", tmp, "login", "-u", s.reg.username, "-p", s.reg.password, privateRegistryURL)
  65. b, err := ioutil.ReadFile(configPath)
  66. c.Assert(err, checker.IsNil)
  67. c.Assert(string(b), checker.Contains, fmt.Sprintf("\"https://%s\": {}", privateRegistryURL))
  68. c.Assert(string(b), checker.Contains, fmt.Sprintf("\"%s\": {}", privateRegistryURL))
  69. dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
  70. b, err = ioutil.ReadFile(configPath)
  71. c.Assert(err, checker.IsNil)
  72. c.Assert(string(b), checker.Not(checker.Contains), fmt.Sprintf("\"https://%s\": {}", privateRegistryURL))
  73. c.Assert(string(b), checker.Not(checker.Contains), fmt.Sprintf("\"%s\": {}", privateRegistryURL))
  74. }