mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
ec85fb1960
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.
97 lines
1.8 KiB
Go
97 lines
1.8 KiB
Go
package communicator
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestCommunicator_new(t *testing.T) {
|
|
cfg := map[string]cty.Value{
|
|
"type": cty.StringVal("telnet"),
|
|
"host": cty.StringVal("127.0.0.1"),
|
|
}
|
|
|
|
if _, err := New(cty.ObjectVal(cfg)); err == nil {
|
|
t.Fatalf("expected error with telnet")
|
|
}
|
|
|
|
cfg["type"] = cty.StringVal("ssh")
|
|
if _, err := New(cty.ObjectVal(cfg)); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
|
|
cfg["type"] = cty.StringVal("winrm")
|
|
if _, err := New(cty.ObjectVal(cfg)); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
}
|
|
func TestRetryFunc(t *testing.T) {
|
|
origMax := maxBackoffDelay
|
|
maxBackoffDelay = time.Second
|
|
origStart := initialBackoffDelay
|
|
initialBackoffDelay = 10 * time.Millisecond
|
|
|
|
defer func() {
|
|
maxBackoffDelay = origMax
|
|
initialBackoffDelay = origStart
|
|
}()
|
|
|
|
// succeed on the third try
|
|
errs := []error{io.EOF, &net.OpError{Err: errors.New("ERROR")}, nil}
|
|
count := 0
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
err := Retry(ctx, func() error {
|
|
if count >= len(errs) {
|
|
return errors.New("failed to stop after nil error")
|
|
}
|
|
|
|
err := errs[count]
|
|
count++
|
|
|
|
return err
|
|
})
|
|
|
|
if count != 3 {
|
|
t.Fatal("retry func should have been called 3 times")
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRetryFuncBackoff(t *testing.T) {
|
|
origMax := maxBackoffDelay
|
|
maxBackoffDelay = time.Second
|
|
origStart := initialBackoffDelay
|
|
initialBackoffDelay = 100 * time.Millisecond
|
|
|
|
defer func() {
|
|
maxBackoffDelay = origMax
|
|
initialBackoffDelay = origStart
|
|
}()
|
|
|
|
count := 0
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
Retry(ctx, func() error {
|
|
count++
|
|
return io.EOF
|
|
})
|
|
|
|
if count > 4 {
|
|
t.Fatalf("retry func failed to backoff. called %d times", count)
|
|
}
|
|
}
|