login: add e2e tests for oauth + escape hatch

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
(cherry picked from commit a327476f7f)
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
Laura Brehm 2024-08-20 11:49:04 +01:00
parent 1b3fa65759
commit a58af379e1
No known key found for this signature in database
GPG Key ID: 08EC1B0491948487
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package registry
import (
"io"
"os/exec"
"strings"
"syscall"
"testing"
"time"
"github.com/creack/pty"
"gotest.tools/v3/assert"
)
func TestOauthLogin(t *testing.T) {
t.Parallel()
loginCmd := exec.Command("docker", "login")
p, err := pty.Start(loginCmd)
assert.NilError(t, err)
defer func() {
_ = loginCmd.Wait()
_ = p.Close()
}()
time.Sleep(1 * time.Second)
pid := loginCmd.Process.Pid
t.Logf("terminating PID %d", pid)
err = syscall.Kill(pid, syscall.SIGTERM)
assert.NilError(t, err)
output, _ := io.ReadAll(p)
assert.Check(t, strings.Contains(string(output), "USING WEB BASED LOGIN"), string(output))
}
func TestLoginWithEscapeHatch(t *testing.T) {
t.Parallel()
loginCmd := exec.Command("docker", "login")
loginCmd.Env = append(loginCmd.Env, "DOCKER_CLI_DISABLE_OAUTH_LOGIN=1")
p, err := pty.Start(loginCmd)
assert.NilError(t, err)
defer func() {
_ = loginCmd.Wait()
_ = p.Close()
}()
time.Sleep(1 * time.Second)
pid := loginCmd.Process.Pid
t.Logf("terminating PID %d", pid)
err = syscall.Kill(pid, syscall.SIGTERM)
assert.NilError(t, err)
output, _ := io.ReadAll(p)
assert.Check(t, strings.Contains(string(output), "Username:"), string(output))
}

17
e2e/registry/main_test.go Normal file
View File

@ -0,0 +1,17 @@
package registry
import (
"fmt"
"os"
"testing"
"github.com/docker/cli/internal/test/environment"
)
func TestMain(m *testing.M) {
if err := environment.Setup(); err != nil {
fmt.Println(err.Error())
os.Exit(3)
}
os.Exit(m.Run())
}