github.com/pulumi/terraform@v1.4.0/pkg/command/e2etest/provider_dev_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/pulumi/terraform/pkg/e2e"
    12  )
    13  
    14  // TestProviderDevOverrides is a test for the special dev_overrides setting
    15  // in the provider_installation section of the CLI configuration file, which
    16  // is our current answer to smoothing provider development by allowing
    17  // developers to opt out of the version number and checksum verification
    18  // we normally do, so they can just overwrite the same local executable
    19  // in-place to iterate faster.
    20  func TestProviderDevOverrides(t *testing.T) {
    21  	if !canRunGoBuild {
    22  		// We're running in a separate-build-then-run context, so we can't
    23  		// currently execute this test which depends on being able to build
    24  		// new executable at runtime.
    25  		//
    26  		// (See the comment on canRunGoBuild's declaration for more information.)
    27  		t.Skip("can't run without building a new provider executable")
    28  	}
    29  	t.Parallel()
    30  
    31  	tf := e2e.NewBinary(t, terraformBin, "testdata/provider-dev-override")
    32  
    33  	// In order to do a decent end-to-end test for this case we will need a
    34  	// real enough provider plugin to try to run and make sure we are able
    35  	// to actually run it. For now we'll use the "test" provider for that,
    36  	// because it happens to be in this repository and therefore allows
    37  	// us to avoid drawing in anything external, but we might revisit this
    38  	// strategy in future if other needs cause us to evolve the test
    39  	// provider in a way that makes it less suitable for this particular test,
    40  	// such as if it stops being buildable into an independent executable.
    41  	providerExeDir := filepath.Join(tf.WorkDir(), "pkgdir")
    42  	providerExePrefix := filepath.Join(providerExeDir, "terraform-provider-test_")
    43  	providerExe := e2e.GoBuild("github.com/pulumi/terraform/pkg/provider-simple/main", providerExePrefix)
    44  	t.Logf("temporary provider executable is %s", providerExe)
    45  
    46  	err := ioutil.WriteFile(filepath.Join(tf.WorkDir(), "dev.tfrc"), []byte(fmt.Sprintf(`
    47  		provider_installation {
    48  			dev_overrides {
    49  				"example.com/test/test" = %q
    50  			}
    51  		}
    52  	`, providerExeDir)), os.ModePerm)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	tf.AddEnv("TF_CLI_CONFIG_FILE=dev.tfrc")
    58  
    59  	stdout, stderr, err := tf.Run("providers")
    60  	if err != nil {
    61  		t.Fatalf("unexpected error: %s\n%s", err, stderr)
    62  	}
    63  	if got, want := stdout, `provider[example.com/test/test]`; !strings.Contains(got, want) {
    64  		t.Errorf("configuration should depend on %s, but doesn't\n%s", want, got)
    65  	}
    66  
    67  	// NOTE: We're intentionally not running "terraform init" here, because
    68  	// dev overrides are always ready to use and don't need any special action
    69  	// to "install" them. This test is mimicking the a happy path of going
    70  	// directly from "go build" to validate/plan/apply without interacting
    71  	// with any registries, mirrors, lock files, etc. To verify "terraform
    72  	// init" does actually show a warning, that behavior is tested at the end.
    73  	stdout, stderr, err = tf.Run("validate")
    74  	if err != nil {
    75  		t.Fatalf("unexpected error: %s\n%s", err, stderr)
    76  	}
    77  
    78  	if got, want := stdout, `The configuration is valid, but`; !strings.Contains(got, want) {
    79  		t.Errorf("stdout doesn't include the success message\nwant: %s\n%s", want, got)
    80  	}
    81  	if got, want := stdout, `Provider development overrides are in effect`; !strings.Contains(got, want) {
    82  		t.Errorf("stdout doesn't include the warning about development overrides\nwant: %s\n%s", want, got)
    83  	}
    84  
    85  	stdout, stderr, err = tf.Run("init")
    86  	if err == nil {
    87  		t.Fatal("expected error: Failed to query available provider packages")
    88  	}
    89  	if got, want := stdout, `Provider development overrides are in effect`; !strings.Contains(got, want) {
    90  		t.Errorf("stdout doesn't include the warning about development overrides\nwant: %s\n%s", want, got)
    91  	}
    92  	if got, want := stderr, `Failed to query available provider packages`; !strings.Contains(got, want) {
    93  		t.Errorf("stderr doesn't include the error about listing unavailable development provider\nwant: %s\n%s", want, got)
    94  	}
    95  }