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