github.com/opentofu/opentofu@v1.7.1/internal/communicator/communicator_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package communicator
     7  
     8  import (
     9  	"context"
    10  	"errors"
    11  	"io"
    12  	"net"
    13  	"sync"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/zclconf/go-cty/cty"
    18  )
    19  
    20  func TestCommunicator_new(t *testing.T) {
    21  	cfg := map[string]cty.Value{
    22  		"type": cty.StringVal("telnet"),
    23  		"host": cty.StringVal("127.0.0.1"),
    24  	}
    25  
    26  	if _, err := New(cty.ObjectVal(cfg)); err == nil {
    27  		t.Fatalf("expected error with telnet")
    28  	}
    29  
    30  	cfg["type"] = cty.StringVal("ssh")
    31  	if _, err := New(cty.ObjectVal(cfg)); err != nil {
    32  		t.Fatalf("err: %v", err)
    33  	}
    34  
    35  	cfg["type"] = cty.StringVal("winrm")
    36  	if _, err := New(cty.ObjectVal(cfg)); err != nil {
    37  		t.Fatalf("err: %v", err)
    38  	}
    39  }
    40  func TestRetryFunc(t *testing.T) {
    41  	origMax := maxBackoffDelay
    42  	maxBackoffDelay = time.Second
    43  	origStart := initialBackoffDelay
    44  	initialBackoffDelay = 10 * time.Millisecond
    45  
    46  	defer func() {
    47  		maxBackoffDelay = origMax
    48  		initialBackoffDelay = origStart
    49  	}()
    50  
    51  	// succeed on the third try
    52  	errs := []error{io.EOF, &net.OpError{Err: errors.New("ERROR")}, nil}
    53  	count := 0
    54  
    55  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    56  	defer cancel()
    57  
    58  	err := Retry(ctx, func() error {
    59  		if count >= len(errs) {
    60  			return errors.New("failed to stop after nil error")
    61  		}
    62  
    63  		err := errs[count]
    64  		count++
    65  
    66  		return err
    67  	})
    68  
    69  	if count != 3 {
    70  		t.Fatal("retry func should have been called 3 times")
    71  	}
    72  
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  }
    77  
    78  func TestRetryFuncBackoff(t *testing.T) {
    79  	origMax := maxBackoffDelay
    80  	maxBackoffDelay = time.Second
    81  	origStart := initialBackoffDelay
    82  	initialBackoffDelay = 100 * time.Millisecond
    83  
    84  	retryTestWg = &sync.WaitGroup{}
    85  	retryTestWg.Add(1)
    86  
    87  	defer func() {
    88  		maxBackoffDelay = origMax
    89  		initialBackoffDelay = origStart
    90  		retryTestWg = nil
    91  	}()
    92  
    93  	count := 0
    94  
    95  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    96  	defer cancel()
    97  
    98  	Retry(ctx, func() error {
    99  		count++
   100  		return io.EOF
   101  	})
   102  	cancel()
   103  	retryTestWg.Wait()
   104  
   105  	if count > 4 {
   106  		t.Fatalf("retry func failed to backoff. called %d times", count)
   107  	}
   108  }