mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
36d0a50427
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
120 lines
2.3 KiB
Go
120 lines
2.3 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
|
)
|
|
|
|
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),
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestUIInputInput_spaces(t *testing.T) {
|
|
i := &UIInput{
|
|
Reader: bytes.NewBufferString("foo bar\n"),
|
|
Writer: bytes.NewBuffer(nil),
|
|
}
|
|
|
|
v, err := i.Input(context.Background(), &terraform.InputOpts{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if v != "foo bar" {
|
|
t.Fatalf("unexpected input: %s", v)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|