github.com/jingleWang/moby@v1.13.1/integration-cli/docker_cli_logout_test.go (about)

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