teleport/integration/terminal_test.go
Trent Clarke d710424ae7
Ports some integration tests to Testify/Subtests (#6884)
In an attempt to make it easier to
 1) navigate the integration test output,
 2) find the cause of test failures, and
 3) run individual tests, make it easier to run individual
    integration tests from the command line,

...this change ports some of the OSS integration tests away from
GoCheck and implements them in terms of the standard `testing`
package.

The main changes are:
 * Test suites are now constructed as a normal Test function
   with many subtests.
 * The GoCheck assertions have been replaced with equivalent
   assertions from `testify/require`, for example:
     `c.Assert(err, check.IsNil)`
   becomes
     `require.NoError(t, err)`

   ... and so on
2021-05-26 19:05:46 -07:00

79 lines
1.8 KiB
Go

/*
Copyright 2021 Gravitational, 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 integration
import (
"bytes"
"strings"
"sync"
"time"
)
// Terminal emulates stdin+stdout for integration testing
type Terminal struct {
typed chan byte
mu *sync.Mutex
written *bytes.Buffer
}
func NewTerminal(capacity int) *Terminal {
return &Terminal{
typed: make(chan byte, capacity),
mu: &sync.Mutex{},
written: bytes.NewBuffer(nil),
}
}
func (t *Terminal) Type(data string) {
for _, b := range []byte(data) {
t.typed <- b
}
}
// Output returns a number of first 'limit' bytes printed into this fake terminal
func (t *Terminal) Output(limit int) string {
t.mu.Lock()
defer t.mu.Unlock()
buff := t.written.Bytes()
if len(buff) > limit {
buff = buff[:limit]
}
// clean up white space for easier comparison:
return strings.TrimSpace(string(buff))
}
func (t *Terminal) Write(data []byte) (n int, err error) {
t.mu.Lock()
defer t.mu.Unlock()
return t.written.Write(data)
}
func (t *Terminal) Read(p []byte) (n int, err error) {
for n = 0; n < len(p); n++ {
p[n] = <-t.typed
if p[n] == '\r' {
break
}
if p[n] == '\a' { // 'alert' used for debugging, means 'pause for 1 second'
time.Sleep(time.Second)
n--
}
time.Sleep(time.Millisecond * 10)
}
return n, nil
}