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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestProvidersLock(t *testing.T) {
    15  	t.Run("noop", func(t *testing.T) {
    16  		// in the most basic case, running providers lock in a directory with no configuration at all should succeed.
    17  		// create an empty working directory
    18  		td := tempDir(t)
    19  		os.MkdirAll(td, 0755)
    20  		defer os.RemoveAll(td)
    21  		defer testChdir(t, td)()
    22  
    23  		ui := new(cli.MockUi)
    24  		c := &ProvidersLockCommand{
    25  			Meta: Meta{
    26  				Ui: ui,
    27  			},
    28  		}
    29  		code := c.Run([]string{})
    30  		if code != 0 {
    31  			t.Fatalf("wrong exit code; expected 0, got %d", code)
    32  		}
    33  	})
    34  
    35  	// This test depends on the -fs-mirror argument, so we always know what results to expect
    36  	t.Run("basic", func(t *testing.T) {
    37  		td := tempDir(t)
    38  		testCopyDir(t, testFixturePath("providers-lock/basic"), td)
    39  		defer os.RemoveAll(td)
    40  		defer testChdir(t, td)()
    41  
    42  		// Our fixture dir has a generic os_arch dir, which we need to customize
    43  		// to the actual OS/arch where this test is running in order to get the
    44  		// desired result.
    45  		fixtMachineDir := filepath.Join(td, "fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/os_arch")
    46  		wantMachineDir := filepath.Join(td, "fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
    47  		err := os.Rename(fixtMachineDir, wantMachineDir)
    48  		if err != nil {
    49  			t.Fatalf("unexpected error: %s", err)
    50  		}
    51  
    52  		p := testProvider()
    53  		ui := new(cli.MockUi)
    54  		c := &ProvidersLockCommand{
    55  			Meta: Meta{
    56  				Ui:               ui,
    57  				testingOverrides: metaOverridesForProvider(p),
    58  			},
    59  		}
    60  
    61  		args := []string{"-fs-mirror=fs-mirror"}
    62  		code := c.Run(args)
    63  		if code != 0 {
    64  			t.Fatalf("wrong exit code; expected 0, got %d", code)
    65  		}
    66  
    67  		lockfile, err := os.ReadFile(".terraform.lock.hcl")
    68  		if err != nil {
    69  			t.Fatal("error reading lockfile")
    70  		}
    71  
    72  		expected := `# This file is maintained automatically by "terraform init".
    73  # Manual edits may be lost in future updates.
    74  
    75  provider "registry.terraform.io/hashicorp/test" {
    76    version = "1.0.0"
    77    hashes = [
    78      "h1:7MjN4eFisdTv4tlhXH5hL4QQd39Jy4baPhFxwAd/EFE=",
    79    ]
    80  }
    81  `
    82  		if string(lockfile) != expected {
    83  			t.Fatalf("wrong lockfile content")
    84  		}
    85  	})
    86  }
    87  
    88  func TestProvidersLock_args(t *testing.T) {
    89  
    90  	t.Run("mirror collision", func(t *testing.T) {
    91  		ui := new(cli.MockUi)
    92  		c := &ProvidersLockCommand{
    93  			Meta: Meta{
    94  				Ui: ui,
    95  			},
    96  		}
    97  
    98  		// only one of these arguments can be used at a time
    99  		args := []string{
   100  			"-fs-mirror=/foo/",
   101  			"-net-mirror=www.foo.com",
   102  		}
   103  		code := c.Run(args)
   104  
   105  		if code != 1 {
   106  			t.Fatalf("wrong exit code; expected 1, got %d", code)
   107  		}
   108  		output := ui.ErrorWriter.String()
   109  		if !strings.Contains(output, "The -fs-mirror and -net-mirror command line options are mutually-exclusive.") {
   110  			t.Fatalf("missing expected error message: %s", output)
   111  		}
   112  	})
   113  
   114  	t.Run("invalid platform", func(t *testing.T) {
   115  		ui := new(cli.MockUi)
   116  		c := &ProvidersLockCommand{
   117  			Meta: Meta{
   118  				Ui: ui,
   119  			},
   120  		}
   121  
   122  		// not a valid platform
   123  		args := []string{"-platform=arbitrary_nonsense_that_isnt_valid"}
   124  		code := c.Run(args)
   125  
   126  		if code != 1 {
   127  			t.Fatalf("wrong exit code; expected 1, got %d", code)
   128  		}
   129  		output := ui.ErrorWriter.String()
   130  		if !strings.Contains(output, "must be two words separated by an underscore.") {
   131  			t.Fatalf("missing expected error message: %s", output)
   132  		}
   133  	})
   134  
   135  	t.Run("invalid provider argument", func(t *testing.T) {
   136  		ui := new(cli.MockUi)
   137  		c := &ProvidersLockCommand{
   138  			Meta: Meta{
   139  				Ui: ui,
   140  			},
   141  		}
   142  
   143  		// There is no configuration, so it's not valid to use any provider argument
   144  		args := []string{"hashicorp/random"}
   145  		code := c.Run(args)
   146  
   147  		if code != 1 {
   148  			t.Fatalf("wrong exit code; expected 1, got %d", code)
   149  		}
   150  		output := ui.ErrorWriter.String()
   151  		if !strings.Contains(output, "The provider registry.terraform.io/hashicorp/random is not required by the\ncurrent configuration.") {
   152  			t.Fatalf("missing expected error message: %s", output)
   153  		}
   154  	})
   155  }