2014-05-24 21:04:43 +02:00
package command
import (
2020-10-03 01:41:56 +02:00
"io/ioutil"
2020-06-15 20:45:27 +02:00
"os"
"strings"
2014-05-24 21:04:43 +02:00
"testing"
2020-10-03 01:41:56 +02:00
"github.com/google/go-cmp/cmp"
2021-05-17 21:00:50 +02:00
"github.com/hashicorp/terraform/internal/addrs"
2020-10-03 01:41:56 +02:00
"github.com/hashicorp/terraform/internal/depsfile"
"github.com/hashicorp/terraform/internal/getproviders"
2014-05-24 21:04:43 +02:00
"github.com/mitchellh/cli"
)
func TestVersionCommand_implements ( t * testing . T ) {
var _ cli . Command = & VersionCommand { }
}
2020-06-15 20:45:27 +02:00
2020-06-15 20:55:51 +02:00
func TestVersion ( t * testing . T ) {
2020-10-03 01:41:56 +02:00
td , err := ioutil . TempDir ( "" , "terraform-test-version" )
if err != nil {
t . Fatal ( err )
}
2020-06-15 20:55:51 +02:00
defer os . RemoveAll ( td )
defer testChdir ( t , td ) ( )
2020-10-03 01:41:56 +02:00
// We'll create a fixed dependency lock file in our working directory
// so we can verify that the version command shows the information
// from it.
locks := depsfile . NewLocks ( )
locks . SetProvider (
addrs . NewDefaultProvider ( "test2" ) ,
getproviders . MustParseVersion ( "1.2.3" ) ,
nil ,
nil ,
)
locks . SetProvider (
addrs . NewDefaultProvider ( "test1" ) ,
getproviders . MustParseVersion ( "7.8.9-beta.2" ) ,
nil ,
nil ,
)
ui := cli . NewMockUi ( )
2020-06-15 20:55:51 +02:00
c := & VersionCommand {
2020-10-03 01:41:56 +02:00
Meta : Meta {
Ui : ui ,
} ,
2020-06-15 20:55:51 +02:00
Version : "4.5.6" ,
VersionPrerelease : "foo" ,
2020-11-19 00:43:27 +01:00
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-06-15 20:55:51 +02:00
}
2020-10-03 01:41:56 +02:00
if err := c . replaceLockedDependencies ( locks ) ; err != nil {
t . Fatal ( err )
}
2020-06-15 20:55:51 +02:00
if code := c . Run ( [ ] string { } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual := strings . TrimSpace ( ui . OutputWriter . String ( ) )
2020-11-19 00:43:27 +01:00
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"
2020-06-15 20:55:51 +02:00
if actual != expected {
2020-10-03 01:41:56 +02:00
t . Fatalf ( "wrong output\ngot:\n%s\nwant:\n%s" , actual , expected )
2020-06-15 20:55:51 +02:00
}
}
2020-06-17 21:24:15 +02:00
func TestVersion_flags ( t * testing . T ) {
ui := new ( cli . MockUi )
m := Meta {
Ui : ui ,
}
// `terraform version`
c := & VersionCommand {
Meta : m ,
Version : "4.5.6" ,
VersionPrerelease : "foo" ,
2020-11-19 00:43:27 +01:00
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-06-17 21:24:15 +02:00
}
if code := c . Run ( [ ] string { "-v" , "-version" } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual := strings . TrimSpace ( ui . OutputWriter . String ( ) )
2020-11-19 00:43:27 +01:00
expected := "Terraform v4.5.6-foo\non aros_riscv64"
2020-06-17 21:24:15 +02:00
if actual != expected {
t . Fatalf ( "wrong output\ngot: %#v\nwant: %#v" , actual , expected )
}
}
2020-08-12 22:22:39 +02:00
func TestVersion_outdated ( t * testing . T ) {
ui := new ( cli . MockUi )
m := Meta {
Ui : ui ,
}
c := & VersionCommand {
Meta : m ,
Version : "4.5.6" ,
CheckFunc : mockVersionCheckFunc ( true , "4.5.7" ) ,
2020-11-19 00:43:27 +01:00
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-08-12 22:22:39 +02:00
}
if code := c . Run ( [ ] string { } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual := strings . TrimSpace ( ui . OutputWriter . String ( ) )
2020-11-19 00:43:27 +01:00
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"
2020-08-12 22:22:39 +02:00
if actual != expected {
t . Fatalf ( "wrong output\ngot: %#v\nwant: %#v" , actual , expected )
}
}
2020-06-15 20:45:27 +02:00
func TestVersion_json ( t * testing . T ) {
2020-10-03 01:41:56 +02:00
td , err := ioutil . TempDir ( "" , "terraform-test-version" )
if err != nil {
t . Fatal ( err )
}
2020-06-15 20:45:27 +02:00
defer os . RemoveAll ( td )
defer testChdir ( t , td ) ( )
2020-10-03 01:41:56 +02:00
ui := cli . NewMockUi ( )
meta := Meta {
Ui : ui ,
2020-06-15 20:45:27 +02:00
}
// `terraform version -json` without prerelease
c := & VersionCommand {
2020-11-19 00:43:27 +01:00
Meta : meta ,
Version : "4.5.6" ,
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-06-15 20:45:27 +02:00
}
if code := c . Run ( [ ] string { "-json" } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual := strings . TrimSpace ( ui . OutputWriter . String ( ) )
2020-10-03 01:41:56 +02:00
expected := strings . TrimSpace ( `
{
"terraform_version" : "4.5.6" ,
2020-11-19 00:43:27 +01:00
"platform" : "aros_riscv64" ,
2020-10-03 01:41:56 +02:00
"provider_selections" : { } ,
"terraform_outdated" : false
}
` )
if diff := cmp . Diff ( expected , actual ) ; diff != "" {
t . Fatalf ( "wrong output\n%s" , diff )
2020-06-15 20:45:27 +02:00
}
// flush the output from the mock ui
ui . OutputWriter . Reset ( )
2020-10-03 01:41:56 +02:00
// Now we'll create a fixed dependency lock file in our working directory
// so we can verify that the version command shows the information
// from it.
locks := depsfile . NewLocks ( )
locks . SetProvider (
addrs . NewDefaultProvider ( "test2" ) ,
getproviders . MustParseVersion ( "1.2.3" ) ,
nil ,
nil ,
)
locks . SetProvider (
addrs . NewDefaultProvider ( "test1" ) ,
getproviders . MustParseVersion ( "7.8.9-beta.2" ) ,
nil ,
nil ,
)
// `terraform version -json` with prerelease and provider dependencies
2020-06-15 20:45:27 +02:00
c = & VersionCommand {
2020-10-03 01:41:56 +02:00
Meta : meta ,
2020-06-15 20:45:27 +02:00
Version : "4.5.6" ,
VersionPrerelease : "foo" ,
2020-11-19 00:43:27 +01:00
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-06-15 20:45:27 +02:00
}
2020-10-03 01:41:56 +02:00
if err := c . replaceLockedDependencies ( locks ) ; err != nil {
t . Fatal ( err )
}
2020-06-15 20:45:27 +02:00
if code := c . Run ( [ ] string { "-json" } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual = strings . TrimSpace ( ui . OutputWriter . String ( ) )
2020-10-03 01:41:56 +02:00
expected = strings . TrimSpace ( `
{
"terraform_version" : "4.5.6-foo" ,
2020-11-19 00:43:27 +01:00
"platform" : "aros_riscv64" ,
2020-10-03 01:41:56 +02:00
"provider_selections" : {
"registry.terraform.io/hashicorp/test1" : "7.8.9-beta.2" ,
"registry.terraform.io/hashicorp/test2" : "1.2.3"
} ,
"terraform_outdated" : false
}
` )
if diff := cmp . Diff ( expected , actual ) ; diff != "" {
t . Fatalf ( "wrong output\n%s" , diff )
2020-06-15 20:45:27 +02:00
}
}
2020-08-12 22:22:39 +02:00
func TestVersion_jsonoutdated ( t * testing . T ) {
ui := new ( cli . MockUi )
m := Meta {
Ui : ui ,
}
c := & VersionCommand {
Meta : m ,
Version : "4.5.6" ,
CheckFunc : mockVersionCheckFunc ( true , "4.5.7" ) ,
2020-11-19 00:43:27 +01:00
Platform : getproviders . Platform { OS : "aros" , Arch : "riscv64" } ,
2020-08-12 22:22:39 +02:00
}
if code := c . Run ( [ ] string { "-json" } ) ; code != 0 {
t . Fatalf ( "bad: \n%s" , ui . ErrorWriter . String ( ) )
}
actual := strings . TrimSpace ( ui . OutputWriter . String ( ) )
2021-01-12 22:35:30 +01:00
expected := "{\n \"terraform_version\": \"4.5.6\",\n \"platform\": \"aros_riscv64\",\n \"provider_selections\": {},\n \"terraform_outdated\": true\n}"
2020-08-12 22:22:39 +02:00
if actual != expected {
t . Fatalf ( "wrong output\ngot: %#v\nwant: %#v" , actual , expected )
}
}
func mockVersionCheckFunc ( outdated bool , latest string ) VersionCheckFunc {
return func ( ) ( VersionCheckInfo , error ) {
return VersionCheckInfo {
Outdated : outdated ,
Latest : latest ,
// Alerts is not used by version command
} , nil
}
}