2014-09-29 12:56:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-07 14:07:13 -06:00
|
|
|
"context"
|
2019-04-29 08:15:26 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync/atomic"
|
2014-09-29 12:56:01 -05:00
|
|
|
"testing"
|
2019-04-29 08:15:26 -05:00
|
|
|
"time"
|
2014-09-29 12:56:01 -05:00
|
|
|
|
2021-05-17 14:46:19 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2014-09-29 12:56:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUIInput_impl(t *testing.T) {
|
|
|
|
var _ terraform.UIInput = new(UIInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUIInputInput(t *testing.T) {
|
|
|
|
i := &UIInput{
|
|
|
|
Reader: bytes.NewBufferString("foo\n"),
|
|
|
|
Writer: bytes.NewBuffer(nil),
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:07:13 -06:00
|
|
|
v, err := i.Input(context.Background(), &terraform.InputOpts{})
|
2014-09-29 12:56:01 -05:00
|
|
|
if err != nil {
|
2019-04-29 08:15:26 -05:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2014-09-29 12:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if v != "foo" {
|
2019-04-29 08:15:26 -05:00
|
|
|
t.Fatalf("unexpected input: %s", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUIInputInput_canceled(t *testing.T) {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
i := &UIInput{
|
|
|
|
Reader: r,
|
|
|
|
Writer: bytes.NewBuffer(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a context that can be canceled.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Cancel the context after 2 seconds.
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Get input until the context is canceled.
|
|
|
|
v, err := i.Input(ctx, &terraform.InputOpts{})
|
|
|
|
if err != context.Canceled {
|
|
|
|
t.Fatalf("expected a context.Canceled error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the context was canceled v should be empty.
|
|
|
|
if v != "" {
|
|
|
|
t.Fatalf("unexpected input: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the context was canceled we should still be listening.
|
|
|
|
listening := atomic.LoadInt32(&i.listening)
|
|
|
|
if listening != 1 {
|
|
|
|
t.Fatalf("expected listening to be 1, got: %d", listening)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Fake input is given after 1 second.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
fmt.Fprint(w, "foo\n")
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
v, err = i.Input(context.Background(), &terraform.InputOpts{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v != "foo" {
|
|
|
|
t.Fatalf("unexpected input: %s", v)
|
2014-09-29 12:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 14:59:48 -05:00
|
|
|
|
|
|
|
func TestUIInputInput_spaces(t *testing.T) {
|
|
|
|
i := &UIInput{
|
|
|
|
Reader: bytes.NewBufferString("foo bar\n"),
|
|
|
|
Writer: bytes.NewBuffer(nil),
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:07:13 -06:00
|
|
|
v, err := i.Input(context.Background(), &terraform.InputOpts{})
|
2016-08-22 14:59:48 -05:00
|
|
|
if err != nil {
|
2019-04-29 08:15:26 -05:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2016-08-22 14:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if v != "foo bar" {
|
2019-04-29 08:15:26 -05:00
|
|
|
t.Fatalf("unexpected input: %s", v)
|
2016-08-22 14:59:48 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 13:10:06 -05:00
|
|
|
|
|
|
|
func TestUIInputInput_Error(t *testing.T) {
|
|
|
|
i := &UIInput{
|
|
|
|
Reader: bytes.NewBuffer(nil),
|
|
|
|
Writer: bytes.NewBuffer(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := i.Input(context.Background(), &terraform.InputOpts{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error is not 'nil'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "EOF" {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v != "" {
|
|
|
|
t.Fatalf("input must be empty")
|
|
|
|
}
|
|
|
|
}
|