github.com/opentofu/opentofu@v1.7.1/internal/command/e2etest/provider_functions_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/opentofu/opentofu/internal/e2e"
     9  )
    10  
    11  func TestFunction_Simple(t *testing.T) {
    12  	// This test reaches out to registry.opentofu.org to download the
    13  	// test functions provider, so it can only run if network access is allowed
    14  	skipIfCannotAccessNetwork(t)
    15  
    16  	fixturePath := filepath.Join("testdata", "functions")
    17  	tf := e2e.NewBinary(t, tofuBin, fixturePath)
    18  
    19  	// tofu init
    20  	_, stderr, err := tf.Run("init")
    21  	if err != nil {
    22  		t.Errorf("unexpected error: %s", err)
    23  	}
    24  	if stderr != "" {
    25  		t.Errorf("unexpected stderr output:\n%s", stderr)
    26  	}
    27  
    28  	_, stderr, err = tf.Run("plan", "-out=fnplan")
    29  	if err != nil {
    30  		t.Errorf("unexpected error: %s", err)
    31  	}
    32  	if stderr != "" {
    33  		t.Errorf("unexpected stderr output:\n%s", stderr)
    34  	}
    35  
    36  	plan, err := tf.Plan("fnplan")
    37  	if err != nil {
    38  		t.Errorf("unexpected error: %s", err)
    39  	}
    40  
    41  	if len(plan.Changes.Outputs) != 1 {
    42  		t.Fatalf("expected 1 outputs, got %d", len(plan.Changes.Outputs))
    43  	}
    44  	for _, out := range plan.Changes.Outputs {
    45  		if !strings.Contains(string(out.After), "Hello Functions") {
    46  			t.Fatalf("unexpected plan output: %s", string(out.After))
    47  		}
    48  	}
    49  }
    50  
    51  func TestFunction_Error(t *testing.T) {
    52  	// This test reaches out to registry.opentofu.org to download the
    53  	// test functions provider, so it can only run if network access is allowed
    54  	skipIfCannotAccessNetwork(t)
    55  	fixturePath := filepath.Join("testdata", "functions-error")
    56  	tf := e2e.NewBinary(t, tofuBin, fixturePath)
    57  
    58  	// tofu init
    59  	_, stderr, err := tf.Run("init")
    60  	if err != nil {
    61  		t.Errorf("unexpected error: %s", err)
    62  	}
    63  	if stderr != "" {
    64  		t.Errorf("unexpected stderr output:\n%s", stderr)
    65  	}
    66  
    67  	// tofu plan -out=fnplan
    68  	_, stderr, err = tf.Run("plan", "-out=fnplan")
    69  	if err == nil {
    70  		t.Errorf("expected error: %s", err)
    71  	}
    72  	if !strings.Contains(stderr, "Call to function \"provider::example::error\" failed") {
    73  		t.Errorf("unexpected stderr output:\n%s", stderr)
    74  	}
    75  }