Skip to content

fix: Run expect tests on Windows with conpty pseudo-terminal #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
dc71bd8
Get test passing on Linux, w/ new cross-plat pty abstraction
bryphe-coder Feb 11, 2022
03ea70c
Get most of the expect tests working
bryphe-coder Feb 11, 2022
2d1405c
Vendor conpty as well
bryphe-coder Feb 11, 2022
c949d44
Test out pipePty implementation
bryphe-coder Feb 11, 2022
a73476d
Get tests passing using pipePty implementation
bryphe-coder Feb 12, 2022
0144a1b
No need for CR in SendLine
bryphe-coder Feb 12, 2022
8a71158
Get windows tests working with conpty
bryphe-coder Feb 12, 2022
49210ec
Bring back tty check
bryphe-coder Feb 12, 2022
cde3ec2
Run go fmt
bryphe-coder Feb 12, 2022
1df68f3
Run go fmt
bryphe-coder Feb 12, 2022
1bff2f1
Add comment in 'isTTY' function
bryphe-coder Feb 12, 2022
9ea9bff
Remove unused code
bryphe-coder Feb 12, 2022
e23745e
Fix up naming, the input/output pipes are always confusing...
bryphe-coder Feb 12, 2022
f61b2ef
Fix up naming, add some extra comments
bryphe-coder Feb 12, 2022
7b1f5df
Round of lint fixes
bryphe-coder Feb 12, 2022
b949301
More lint fixes
bryphe-coder Feb 12, 2022
c24774a
Remove unused imports
bryphe-coder Feb 12, 2022
8d7d782
Remaining lint fixes
bryphe-coder Feb 12, 2022
9922222
Add force-tty flag
bryphe-coder Feb 12, 2022
1faa215
Add comment describing why --force-tty is neede dfor test
bryphe-coder Feb 12, 2022
908b9cc
Fix typo
bryphe-coder Feb 12, 2022
bfe475e
Merge main
bryphe-coder Feb 14, 2022
2cb7256
Revert expect test changes
bryphe-coder Feb 14, 2022
3c08393
Update clitest to use cross-platform expect
bryphe-coder Feb 14, 2022
36a0d41
Mark force-tty flag as hidden
bryphe-coder Feb 14, 2022
7253eca
Run CLI tests on windows
bryphe-coder Feb 14, 2022
9b78fb7
Bring back force-tty flag for Windows
bryphe-coder Feb 14, 2022
ed2659e
Fix golang lint issue
bryphe-coder Feb 14, 2022
09a86e8
Run clitest_test on windows, too
bryphe-coder Feb 14, 2022
db4d232
Merge branch 'main' into bryphe/experiment/241/cross-plat-expect
bryphe-coder Feb 14, 2022
0e8d4b6
Remove .Then()
bryphe-coder Feb 14, 2022
09e844b
Remove Regexp/RegexpPattern
bryphe-coder Feb 14, 2022
40c97c0
Remove additional unused functionality
bryphe-coder Feb 14, 2022
dabe9e4
Remove unused reader_lease
bryphe-coder Feb 14, 2022
f556c26
Close console after test
bryphe-coder Feb 14, 2022
e76ad95
Move console cleanup to shared place
bryphe-coder Feb 14, 2022
4e4f3e2
Remove unused matchers
bryphe-coder Feb 14, 2022
5cba77d
Remove more unused options
bryphe-coder Feb 14, 2022
40ca4b5
Remove passthrough_pipe
bryphe-coder Feb 14, 2022
f6df631
Remove commented code
bryphe-coder Feb 14, 2022
c0e52dd
Replace test_log with test_console
bryphe-coder Feb 14, 2022
1962e97
Fix naming
bryphe-coder Feb 14, 2022
0ef0f19
Move force-tty check inside isTTY
bryphe-coder Feb 14, 2022
1de0f1b
Fix inverted conditional for forceTty check
bryphe-coder Feb 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Get test passing on Linux, w/ new cross-plat pty abstraction
  • Loading branch information
bryphe-coder committed Feb 11, 2022
commit dc71bd86401f851d19de029392843e5c143e1fe8
6 changes: 3 additions & 3 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/coder/coder/coderd/coderdtest"
"github.com/stretchr/testify/require"

"github.com/Netflix/go-expect"
"github.com/coder/coder/expect"
)

func TestLogin(t *testing.T) {
Expand All @@ -28,8 +28,8 @@ func TestLogin(t *testing.T) {
require.NoError(t, err)
client := coderdtest.New(t)
root, _ := clitest.New(t, "login", client.URL.String())
root.SetIn(console.Tty())
root.SetOut(console.Tty())
root.SetIn(console.InTty())
root.SetOut(console.OutTty())
go func() {
err := root.Execute()
require.NoError(t, err)
Expand Down
233 changes: 233 additions & 0 deletions expect/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Copyright 2018 Netflix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package expect

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"time"
"unicode/utf8"

"github.com/coder/coder/expect/pty"
)

// Console is an interface to automate input and output for interactive
// applications. Console can block until a specified output is received and send
// input back on it's tty. Console can also multiplex other sources of input
// and multiplex its output to other writers.
type Console struct {
opts ConsoleOpts
pty pty.Pty
passthroughPipe *PassthroughPipe
runeReader *bufio.Reader
closers []io.Closer
}

// ConsoleOpt allows setting Console options.
type ConsoleOpt func(*ConsoleOpts) error

// ConsoleOpts provides additional options on creating a Console.
type ConsoleOpts struct {
Logger *log.Logger
Stdouts []io.Writer
Closers []io.Closer
ExpectObservers []ExpectObserver
SendObservers []SendObserver
ReadTimeout *time.Duration
}

// ExpectObserver provides an interface for a function callback that will
// be called after each Expect operation.
// matchers will be the list of active matchers when an error occurred,
// or a list of matchers that matched `buf` when err is nil.
// buf is the captured output that was matched against.
// err is error that might have occurred. May be nil.
type ExpectObserver func(matchers []Matcher, buf string, err error)

// SendObserver provides an interface for a function callback that will
// be called after each Send operation.
// msg is the string that was sent.
// num is the number of bytes actually sent.
// err is the error that might have occured. May be nil.
type SendObserver func(msg string, num int, err error)

// WithStdout adds writers that Console duplicates writes to, similar to the
// Unix tee(1) command.
//
// Each write is written to each listed writer, one at a time. Console is the
// last writer, writing to it's internal buffer for matching expects.
// If a listed writer returns an error, that overall write operation stops and
// returns the error; it does not continue down the list.
func WithStdout(writers ...io.Writer) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.Stdouts = append(opts.Stdouts, writers...)
return nil
}
}

// WithCloser adds closers that are closed in order when Console is closed.
func WithCloser(closer ...io.Closer) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.Closers = append(opts.Closers, closer...)
return nil
}
}

// WithLogger adds a logger for Console to log debugging information to. By
// default Console will discard logs.
func WithLogger(logger *log.Logger) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.Logger = logger
return nil
}
}

// WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations.
func WithExpectObserver(observers ...ExpectObserver) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.ExpectObservers = append(opts.ExpectObservers, observers...)
return nil
}
}

// WithSendObserver adds a SendObserver to allow monitoring Send operations.
func WithSendObserver(observers ...SendObserver) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.SendObservers = append(opts.SendObservers, observers...)
return nil
}
}

// WithDefaultTimeout sets a default read timeout during Expect statements.
func WithDefaultTimeout(timeout time.Duration) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.ReadTimeout = &timeout
return nil
}
}

// NewConsole returns a new Console with the given options.
func NewConsole(opts ...ConsoleOpt) (*Console, error) {
options := ConsoleOpts{
Logger: log.New(ioutil.Discard, "", 0),
}

for _, opt := range opts {
if err := opt(&options); err != nil {
return nil, err
}
}

pty, err := pty.New()
if err != nil {
return nil, err
}
closers := append(options.Closers, pty)
reader := pty.Reader()

passthroughPipe, err := NewPassthroughPipe(reader)
if err != nil {
return nil, err
}
closers = append(closers, passthroughPipe)

c := &Console{
opts: options,
pty: pty,
passthroughPipe: passthroughPipe,
runeReader: bufio.NewReaderSize(passthroughPipe, utf8.UTFMax),
closers: closers,
}

/*for _, stdin := range options.Stdins {
go func(stdin io.Reader) {
_, err := io.Copy(c, stdin)
if err != nil {
c.Logf("failed to copy stdin: %s", err)
}
}(stdin)
}*/

return c, nil
}

// Tty returns an input Tty for accepting input
func (c *Console) InTty() *os.File {
return c.pty.InPipe()
}

// OutTty returns an output tty for writing
func (c *Console) OutTty() *os.File {
return c.pty.OutPipe()
}

// Read reads bytes b from Console's tty.
/*func (c *Console) Read(b []byte) (int, error) {
return c.ptm.Read(b)
}*/

// Write writes bytes b to Console's tty.
/*func (c *Console) Write(b []byte) (int, error) {
c.Logf("console write: %q", b)
return c.ptm.Write(b)
}*/

// Fd returns Console's file descripting referencing the master part of its
// pty.
/*func (c *Console) Fd() uintptr {
return c.ptm.Fd()
}*/

// Close closes Console's tty. Calling Close will unblock Expect and ExpectEOF.
func (c *Console) Close() error {
for _, fd := range c.closers {
err := fd.Close()
if err != nil {
c.Logf("failed to close: %s", err)
}
}
return nil
}

// Send writes string s to Console's tty.
func (c *Console) Send(s string) (int, error) {
c.Logf("console send: %q", s)
n, err := c.pty.WriteString(s)
for _, observer := range c.opts.SendObservers {
observer(s, n, err)
}
return n, err
}

// SendLine writes string s to Console's tty with a trailing newline.
func (c *Console) SendLine(s string) (int, error) {
return c.Send(fmt.Sprintf("%s\n", s))
}

// Log prints to Console's logger.
// Arguments are handled in the manner of fmt.Print.
func (c *Console) Log(v ...interface{}) {
c.opts.Logger.Print(v...)
}

// Logf prints to Console's logger.
// Arguments are handled in the manner of fmt.Printf.
func (c *Console) Logf(format string, v ...interface{}) {
c.opts.Logger.Printf(format, v...)
}
19 changes: 19 additions & 0 deletions expect/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 Netflix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package expect provides an expect-like interface to automate control of
// applications. It is unlike expect in that it does not spawn or manage
// process lifecycle. This package only focuses on expecting output and sending
// input through it's psuedoterminal.
package expect
Loading