github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/command/version_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/cycloidio/terraform/addrs"
    11  	"github.com/cycloidio/terraform/depsfile"
    12  	"github.com/cycloidio/terraform/getproviders"
    13  	"github.com/mitchellh/cli"
    14  )
    15  
    16  func TestVersionCommand_implements(t *testing.T) {
    17  	var _ cli.Command = &VersionCommand{}
    18  }
    19  
    20  func TestVersion(t *testing.T) {
    21  	td, err := ioutil.TempDir("", "terraform-test-version")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer os.RemoveAll(td)
    26  	defer testChdir(t, td)()
    27  
    28  	// We'll create a fixed dependency lock file in our working directory
    29  	// so we can verify that the version command shows the information
    30  	// from it.
    31  	locks := depsfile.NewLocks()
    32  	locks.SetProvider(
    33  		addrs.NewDefaultProvider("test2"),
    34  		getproviders.MustParseVersion("1.2.3"),
    35  		nil,
    36  		nil,
    37  	)
    38  	locks.SetProvider(
    39  		addrs.NewDefaultProvider("test1"),
    40  		getproviders.MustParseVersion("7.8.9-beta.2"),
    41  		nil,
    42  		nil,
    43  	)
    44  
    45  	ui := cli.NewMockUi()
    46  	c := &VersionCommand{
    47  		Meta: Meta{
    48  			Ui: ui,
    49  		},
    50  		Version:           "4.5.6",
    51  		VersionPrerelease: "foo",
    52  		Platform:          getproviders.Platform{OS: "aros", Arch: "riscv64"},
    53  	}
    54  	if err := c.replaceLockedDependencies(locks); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if code := c.Run([]string{}); code != 0 {
    58  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    59  	}
    60  
    61  	actual := strings.TrimSpace(ui.OutputWriter.String())
    62  	expected := "Terraform v4.5.6-foo\non aros_riscv64\n+ provider registry.terraform.io/hashicorp/test1 v7.8.9-beta.2\n+ provider registry.terraform.io/hashicorp/test2 v1.2.3"
    63  	if actual != expected {
    64  		t.Fatalf("wrong output\ngot:\n%s\nwant:\n%s", actual, expected)
    65  	}
    66  
    67  }
    68  
    69  func TestVersion_flags(t *testing.T) {
    70  	ui := new(cli.MockUi)
    71  	m := Meta{
    72  		Ui: ui,
    73  	}
    74  
    75  	// `terraform version`
    76  	c := &VersionCommand{
    77  		Meta:              m,
    78  		Version:           "4.5.6",
    79  		VersionPrerelease: "foo",
    80  		Platform:          getproviders.Platform{OS: "aros", Arch: "riscv64"},
    81  	}
    82  
    83  	if code := c.Run([]string{"-v", "-version"}); code != 0 {
    84  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    85  	}
    86  
    87  	actual := strings.TrimSpace(ui.OutputWriter.String())
    88  	expected := "Terraform v4.5.6-foo\non aros_riscv64"
    89  	if actual != expected {
    90  		t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected)
    91  	}
    92  }
    93  
    94  func TestVersion_outdated(t *testing.T) {
    95  	ui := new(cli.MockUi)
    96  	m := Meta{
    97  		Ui: ui,
    98  	}
    99  
   100  	c := &VersionCommand{
   101  		Meta:      m,
   102  		Version:   "4.5.6",
   103  		CheckFunc: mockVersionCheckFunc(true, "4.5.7"),
   104  		Platform:  getproviders.Platform{OS: "aros", Arch: "riscv64"},
   105  	}
   106  
   107  	if code := c.Run([]string{}); code != 0 {
   108  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   109  	}
   110  
   111  	actual := strings.TrimSpace(ui.OutputWriter.String())
   112  	expected := "Terraform v4.5.6\non aros_riscv64\n\nYour version of Terraform is out of date! The latest version\nis 4.5.7. You can update by downloading from https://www.terraform.io/downloads.html"
   113  	if actual != expected {
   114  		t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected)
   115  	}
   116  }
   117  
   118  func TestVersion_json(t *testing.T) {
   119  	td, err := ioutil.TempDir("", "terraform-test-version")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	defer os.RemoveAll(td)
   124  	defer testChdir(t, td)()
   125  
   126  	ui := cli.NewMockUi()
   127  	meta := Meta{
   128  		Ui: ui,
   129  	}
   130  
   131  	// `terraform version -json` without prerelease
   132  	c := &VersionCommand{
   133  		Meta:     meta,
   134  		Version:  "4.5.6",
   135  		Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"},
   136  	}
   137  	if code := c.Run([]string{"-json"}); code != 0 {
   138  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   139  	}
   140  
   141  	actual := strings.TrimSpace(ui.OutputWriter.String())
   142  	expected := strings.TrimSpace(`
   143  {
   144    "terraform_version": "4.5.6",
   145    "platform": "aros_riscv64",
   146    "provider_selections": {},
   147    "terraform_outdated": false
   148  }
   149  `)
   150  	if diff := cmp.Diff(expected, actual); diff != "" {
   151  		t.Fatalf("wrong output\n%s", diff)
   152  	}
   153  
   154  	// flush the output from the mock ui
   155  	ui.OutputWriter.Reset()
   156  
   157  	// Now we'll create a fixed dependency lock file in our working directory
   158  	// so we can verify that the version command shows the information
   159  	// from it.
   160  	locks := depsfile.NewLocks()
   161  	locks.SetProvider(
   162  		addrs.NewDefaultProvider("test2"),
   163  		getproviders.MustParseVersion("1.2.3"),
   164  		nil,
   165  		nil,
   166  	)
   167  	locks.SetProvider(
   168  		addrs.NewDefaultProvider("test1"),
   169  		getproviders.MustParseVersion("7.8.9-beta.2"),
   170  		nil,
   171  		nil,
   172  	)
   173  
   174  	// `terraform version -json` with prerelease and provider dependencies
   175  	c = &VersionCommand{
   176  		Meta:              meta,
   177  		Version:           "4.5.6",
   178  		VersionPrerelease: "foo",
   179  		Platform:          getproviders.Platform{OS: "aros", Arch: "riscv64"},
   180  	}
   181  	if err := c.replaceLockedDependencies(locks); err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	if code := c.Run([]string{"-json"}); code != 0 {
   185  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   186  	}
   187  
   188  	actual = strings.TrimSpace(ui.OutputWriter.String())
   189  	expected = strings.TrimSpace(`
   190  {
   191    "terraform_version": "4.5.6-foo",
   192    "platform": "aros_riscv64",
   193    "provider_selections": {
   194      "registry.terraform.io/hashicorp/test1": "7.8.9-beta.2",
   195      "registry.terraform.io/hashicorp/test2": "1.2.3"
   196    },
   197    "terraform_outdated": false
   198  }
   199  `)
   200  	if diff := cmp.Diff(expected, actual); diff != "" {
   201  		t.Fatalf("wrong output\n%s", diff)
   202  	}
   203  
   204  }
   205  
   206  func TestVersion_jsonoutdated(t *testing.T) {
   207  	ui := new(cli.MockUi)
   208  	m := Meta{
   209  		Ui: ui,
   210  	}
   211  
   212  	c := &VersionCommand{
   213  		Meta:      m,
   214  		Version:   "4.5.6",
   215  		CheckFunc: mockVersionCheckFunc(true, "4.5.7"),
   216  		Platform:  getproviders.Platform{OS: "aros", Arch: "riscv64"},
   217  	}
   218  
   219  	if code := c.Run([]string{"-json"}); code != 0 {
   220  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   221  	}
   222  
   223  	actual := strings.TrimSpace(ui.OutputWriter.String())
   224  	expected := "{\n  \"terraform_version\": \"4.5.6\",\n  \"platform\": \"aros_riscv64\",\n  \"provider_selections\": {},\n  \"terraform_outdated\": true\n}"
   225  	if actual != expected {
   226  		t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected)
   227  	}
   228  }
   229  
   230  func mockVersionCheckFunc(outdated bool, latest string) VersionCheckFunc {
   231  	return func() (VersionCheckInfo, error) {
   232  		return VersionCheckInfo{
   233  			Outdated: outdated,
   234  			Latest:   latest,
   235  			// Alerts is not used by version command
   236  		}, nil
   237  	}
   238  }