github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/e2etest/version_test.go (about) 1 package e2etest 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/internal/e2e" 10 "github.com/hashicorp/terraform/version" 11 ) 12 13 func TestVersion(t *testing.T) { 14 // Along with testing the "version" command in particular, this serves 15 // as a good smoke test for whether the Terraform binary can even be 16 // compiled and run, since it doesn't require any external network access 17 // to do its job. 18 19 t.Parallel() 20 21 fixturePath := filepath.Join("testdata", "empty") 22 tf := e2e.NewBinary(t, terraformBin, fixturePath) 23 24 stdout, stderr, err := tf.Run("version") 25 if err != nil { 26 t.Errorf("unexpected error: %s", err) 27 } 28 29 if stderr != "" { 30 t.Errorf("unexpected stderr output:\n%s", stderr) 31 } 32 33 wantVersion := fmt.Sprintf("Terraform v%s", version.String()) 34 if !strings.Contains(stdout, wantVersion) { 35 t.Errorf("output does not contain our current version %q:\n%s", wantVersion, stdout) 36 } 37 } 38 39 func TestVersionWithProvider(t *testing.T) { 40 // This is a more elaborate use of "version" that shows the selected 41 // versions of plugins too. 42 t.Parallel() 43 44 // This test reaches out to releases.hashicorp.com to download the 45 // template and null providers, so it can only run if network access is 46 // allowed. 47 skipIfCannotAccessNetwork(t) 48 49 fixturePath := filepath.Join("testdata", "template-provider") 50 tf := e2e.NewBinary(t, terraformBin, fixturePath) 51 52 // Initial run (before "init") should work without error but will not 53 // include the provider version, since we've not "locked" one yet. 54 { 55 stdout, stderr, err := tf.Run("version") 56 if err != nil { 57 t.Errorf("unexpected error: %s", err) 58 } 59 60 if stderr != "" { 61 t.Errorf("unexpected stderr output:\n%s", stderr) 62 } 63 64 wantVersion := fmt.Sprintf("Terraform v%s", version.String()) 65 if !strings.Contains(stdout, wantVersion) { 66 t.Errorf("output does not contain our current version %q:\n%s", wantVersion, stdout) 67 } 68 } 69 70 { 71 _, _, err := tf.Run("init") 72 if err != nil { 73 t.Errorf("unexpected error: %s", err) 74 } 75 } 76 77 // After running init, we additionally include information about the 78 // selected version of the "template" provider. 79 { 80 stdout, stderr, err := tf.Run("version") 81 if err != nil { 82 t.Errorf("unexpected error: %s", err) 83 } 84 85 if stderr != "" { 86 t.Errorf("unexpected stderr output:\n%s", stderr) 87 } 88 89 wantMsg := "+ provider registry.terraform.io/hashicorp/template v" // we don't know which version we'll get here 90 if !strings.Contains(stdout, wantMsg) { 91 t.Errorf("output does not contain provider information %q:\n%s", wantMsg, stdout) 92 } 93 } 94 }