github.com/hashicorp/go-getter/v2@v2.2.2/get_file_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	testing_helper "github.com/hashicorp/go-getter/v2/helper/testing"
    10  	urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
    11  )
    12  
    13  func TestFileGetter_impl(t *testing.T) {
    14  	var _ Getter = new(FileGetter)
    15  }
    16  
    17  func TestFileGetter(t *testing.T) {
    18  	g := new(FileGetter)
    19  	dst := testing_helper.TempDir(t)
    20  	ctx := context.Background()
    21  
    22  	req := &Request{
    23  		Dst: dst,
    24  		u:   testModuleURL("basic"),
    25  	}
    26  
    27  	// With a dir that doesn't exist
    28  	if err := g.Get(ctx, req); err != nil {
    29  		t.Fatalf("err: %s", err)
    30  	}
    31  
    32  	// Verify the destination folder is a symlink
    33  	fi, err := os.Lstat(dst)
    34  	if err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  	if fi.Mode()&os.ModeSymlink == 0 {
    38  		t.Fatal("destination is not a symlink")
    39  	}
    40  
    41  	// Verify the main file exists
    42  	mainPath := filepath.Join(dst, "main.tf")
    43  	if _, err := os.Stat(mainPath); err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  }
    47  
    48  func TestFileGetter_sourceFile(t *testing.T) {
    49  	g := new(FileGetter)
    50  	dst := testing_helper.TempDir(t)
    51  	ctx := context.Background()
    52  
    53  	// With a source URL that is a path to a file
    54  	u := testModuleURL("basic")
    55  	u.Path += "/main.tf"
    56  
    57  	req := &Request{
    58  		Dst: dst,
    59  		u:   u,
    60  	}
    61  	if err := g.Get(ctx, req); err == nil {
    62  		t.Fatal("should error")
    63  	}
    64  }
    65  
    66  func TestFileGetter_sourceNoExist(t *testing.T) {
    67  	g := new(FileGetter)
    68  	dst := testing_helper.TempDir(t)
    69  	ctx := context.Background()
    70  
    71  	// With a source URL that doesn't exist
    72  	u := testModuleURL("basic")
    73  	u.Path += "/main"
    74  
    75  	req := &Request{
    76  		Dst: dst,
    77  		u:   u,
    78  	}
    79  	if err := g.Get(ctx, req); err == nil {
    80  		t.Fatal("should error")
    81  	}
    82  }
    83  
    84  func TestFileGetter_dir(t *testing.T) {
    85  	g := new(FileGetter)
    86  	dst := testing_helper.TempDir(t)
    87  	ctx := context.Background()
    88  
    89  	if err := os.MkdirAll(dst, 0755); err != nil {
    90  		t.Fatalf("err: %s", err)
    91  	}
    92  
    93  	req := &Request{
    94  		Dst: dst,
    95  		u:   testModuleURL("basic"),
    96  	}
    97  	// With a dir that exists that isn't a symlink
    98  	if err := g.Get(ctx, req); err == nil {
    99  		t.Fatal("should error")
   100  	}
   101  }
   102  
   103  func TestFileGetter_dirSymlink(t *testing.T) {
   104  	g := new(FileGetter)
   105  	dst := testing_helper.TempDir(t)
   106  	ctx := context.Background()
   107  
   108  	dst2 := testing_helper.TempDir(t)
   109  
   110  	// Make parents
   111  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  	if err := os.MkdirAll(dst2, 0755); err != nil {
   115  		t.Fatalf("err: %s", err)
   116  	}
   117  
   118  	// Make a symlink
   119  	if err := os.Symlink(dst2, dst); err != nil {
   120  		t.Fatalf("err: %s", err)
   121  	}
   122  
   123  	req := &Request{
   124  		Dst: dst,
   125  		u:   testModuleURL("basic"),
   126  	}
   127  
   128  	// With a dir that exists that isn't a symlink
   129  	if err := g.Get(ctx, req); err != nil {
   130  		t.Fatalf("err: %s", err)
   131  	}
   132  
   133  	// Verify the main file exists
   134  	mainPath := filepath.Join(dst, "main.tf")
   135  	if _, err := os.Stat(mainPath); err != nil {
   136  		t.Fatalf("err: %s", err)
   137  	}
   138  }
   139  
   140  func TestFileGetter_GetFile(t *testing.T) {
   141  	g := new(FileGetter)
   142  	dst := testing_helper.TempTestFile(t)
   143  	defer os.RemoveAll(filepath.Dir(dst))
   144  	ctx := context.Background()
   145  
   146  	req := &Request{
   147  		Dst: dst,
   148  		u:   testModuleURL("basic-file/foo.txt"),
   149  	}
   150  
   151  	// With a dir that doesn't exist
   152  	if err := g.GetFile(ctx, req); err != nil {
   153  		t.Fatalf("err: %s", err)
   154  	}
   155  
   156  	// Verify the destination folder is a symlink
   157  	fi, err := os.Lstat(dst)
   158  	if err != nil {
   159  		t.Fatalf("err: %s", err)
   160  	}
   161  	if fi.Mode()&os.ModeSymlink == 0 {
   162  		t.Fatal("destination is not a symlink")
   163  	}
   164  
   165  	// Verify the main file exists
   166  	testing_helper.AssertContents(t, dst, "Hello\n")
   167  }
   168  
   169  func TestFileGetter_GetFile_Copy(t *testing.T) {
   170  	g := new(FileGetter)
   171  
   172  	dst := testing_helper.TempTestFile(t)
   173  	defer os.RemoveAll(filepath.Dir(dst))
   174  	ctx := context.Background()
   175  
   176  	req := &Request{
   177  		Dst:  dst,
   178  		u:    testModuleURL("basic-file/foo.txt"),
   179  		Copy: true,
   180  	}
   181  
   182  	// With a dir that doesn't exist
   183  	if err := g.GetFile(ctx, req); err != nil {
   184  		t.Fatalf("err: %s", err)
   185  	}
   186  
   187  	// Verify the destination folder is a symlink
   188  	fi, err := os.Lstat(dst)
   189  	if err != nil {
   190  		t.Fatalf("err: %s", err)
   191  	}
   192  	if fi.Mode()&os.ModeSymlink != 0 {
   193  		t.Fatal("destination is a symlink")
   194  	}
   195  
   196  	// Verify the main file exists
   197  	testing_helper.AssertContents(t, dst, "Hello\n")
   198  }
   199  
   200  // https://github.com/hashicorp/terraform/issues/8418
   201  func TestFileGetter_percent2F(t *testing.T) {
   202  	g := new(FileGetter)
   203  	dst := testing_helper.TempDir(t)
   204  	ctx := context.Background()
   205  
   206  	req := &Request{
   207  		Dst: dst,
   208  		u:   testModuleURL("basic%2Ftest"),
   209  	}
   210  
   211  	// With a dir that doesn't exist
   212  	if err := g.Get(ctx, req); err != nil {
   213  		t.Fatalf("err: %s", err)
   214  	}
   215  
   216  	// Verify the main file exists
   217  	mainPath := filepath.Join(dst, "main.tf")
   218  	if _, err := os.Stat(mainPath); err != nil {
   219  		t.Fatalf("err: %s", err)
   220  	}
   221  }
   222  
   223  func TestFileGetter_Mode_notexist(t *testing.T) {
   224  	g := new(FileGetter)
   225  	ctx := context.Background()
   226  
   227  	u := urlhelper.MustParse("nonexistent")
   228  	if _, err := g.Mode(ctx, u); err == nil {
   229  		t.Fatal("expect source file error")
   230  	}
   231  }
   232  
   233  func TestFileGetter_Mode_file(t *testing.T) {
   234  	g := new(FileGetter)
   235  	ctx := context.Background()
   236  
   237  	// Check the client mode when pointed at a file.
   238  	mode, err := g.Mode(ctx, testModuleURL("basic-file/foo.txt"))
   239  	if err != nil {
   240  		t.Fatalf("err: %s", err)
   241  	}
   242  	if mode != ModeFile {
   243  		t.Fatal("expect ModeFile")
   244  	}
   245  }
   246  
   247  func TestFileGetter_Mode_dir(t *testing.T) {
   248  	g := new(FileGetter)
   249  	ctx := context.Background()
   250  
   251  	// Check the client mode when pointed at a directory.
   252  	mode, err := g.Mode(ctx, testModuleURL("basic"))
   253  	if err != nil {
   254  		t.Fatalf("err: %s", err)
   255  	}
   256  	if mode != ModeDir {
   257  		t.Fatal("expect ModeDir")
   258  	}
   259  }