kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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  	"kubeform.dev/terraform-backend-sdk/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  	t.Parallel()
    22  
    23  	tf := e2e.NewBinary(terraformBin, "testdata/provider-dev-override")
    24  	defer tf.Close()
    25  
    26  	// In order to do a decent end-to-end test for this case we will need a
    27  	// real enough provider plugin to try to run and make sure we are able
    28  	// to actually run it. For now we'll use the "test" provider for that,
    29  	// because it happens to be in this repository and therefore allows
    30  	// us to avoid drawing in anything external, but we might revisit this
    31  	// strategy in future if other needs cause us to evolve the test
    32  	// provider in a way that makes it less suitable for this particular test,
    33  	// such as if it stops being buildable into an independent executable.
    34  	providerExeDir := filepath.Join(tf.WorkDir(), "pkgdir")
    35  	providerExePrefix := filepath.Join(providerExeDir, "terraform-provider-test_")
    36  	providerExe := e2e.GoBuild("github.com/hashicorp/terraform/internal/provider-simple/main", providerExePrefix)
    37  	t.Logf("temporary provider executable is %s", providerExe)
    38  
    39  	err := ioutil.WriteFile(filepath.Join(tf.WorkDir(), "dev.tfrc"), []byte(fmt.Sprintf(`
    40  		provider_installation {
    41  			dev_overrides {
    42  				"example.com/test/test" = %q
    43  			}
    44  		}
    45  	`, providerExeDir)), os.ModePerm)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	tf.AddEnv("TF_CLI_CONFIG_FILE=dev.tfrc")
    51  
    52  	stdout, stderr, err := tf.Run("providers")
    53  	if err != nil {
    54  		t.Fatalf("unexpected error: %s\n%s", err, stderr)
    55  	}
    56  	if got, want := stdout, `provider[example.com/test/test]`; !strings.Contains(got, want) {
    57  		t.Errorf("configuration should depend on %s, but doesn't\n%s", want, got)
    58  	}
    59  
    60  	// NOTE: We're intentionally not running "terraform init" here, because
    61  	// dev overrides are always ready to use and don't need any special action
    62  	// to "install" them. This test is mimicking the a happy path of going
    63  	// directly from "go build" to validate/plan/apply without interacting
    64  	// with any registries, mirrors, lock files, etc. To verify "terraform
    65  	// init" does actually show a warning, that behavior is tested at the end.
    66  	stdout, stderr, err = tf.Run("validate")
    67  	if err != nil {
    68  		t.Fatalf("unexpected error: %s\n%s", err, stderr)
    69  	}
    70  
    71  	if got, want := stdout, `The configuration is valid, but`; !strings.Contains(got, want) {
    72  		t.Errorf("stdout doesn't include the success message\nwant: %s\n%s", want, got)
    73  	}
    74  	if got, want := stdout, `Provider development overrides are in effect`; !strings.Contains(got, want) {
    75  		t.Errorf("stdout doesn't include the warning about development overrides\nwant: %s\n%s", want, got)
    76  	}
    77  
    78  	stdout, stderr, err = tf.Run("init")
    79  	if err == nil {
    80  		t.Fatal("expected error: Failed to query available provider packages")
    81  	}
    82  	if got, want := stdout, `Provider development overrides are in effect`; !strings.Contains(got, want) {
    83  		t.Errorf("stdout doesn't include the warning about development overrides\nwant: %s\n%s", want, got)
    84  	}
    85  	if got, want := stderr, `Failed to query available provider packages`; !strings.Contains(got, want) {
    86  		t.Errorf("stderr doesn't include the error about listing unavailable development provider\nwant: %s\n%s", want, got)
    87  	}
    88  }