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

     1  package getter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	testing_helper "github.com/hashicorp/go-getter/v2/helper/testing"
    12  )
    13  
    14  func TestGet_badSchema(t *testing.T) {
    15  	ctx := context.Background()
    16  
    17  	dst := testing_helper.TempDir(t)
    18  	u := testModule("basic")
    19  	u = "nope::" + u
    20  
    21  	op, err := Get(ctx, dst, u)
    22  	if err == nil {
    23  		t.Fatal("should error")
    24  	}
    25  	if op != nil {
    26  		t.Fatal("op should be nil")
    27  	}
    28  }
    29  
    30  func TestGet_file(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	dst := testing_helper.TempDir(t)
    34  	u := testModule("basic")
    35  
    36  	op, err := Get(ctx, dst, u)
    37  	if err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
    41  		t.Fatalf("unexpected op: %s", diff)
    42  	}
    43  
    44  	mainPath := filepath.Join(dst, "main.tf")
    45  	if _, err := os.Stat(mainPath); err != nil {
    46  		t.Fatalf("err: %s", err)
    47  	}
    48  }
    49  
    50  // https://github.com/hashicorp/terraform/issues/11438
    51  func TestGet_fileDecompressorExt(t *testing.T) {
    52  	ctx := context.Background()
    53  
    54  	dst := testing_helper.TempDir(t)
    55  	u := testModule("basic-tgz")
    56  
    57  	op, err := Get(ctx, dst, u)
    58  	if err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
    62  		t.Fatalf("unexpected op: %s", diff)
    63  	}
    64  
    65  	mainPath := filepath.Join(dst, "main.tf")
    66  	if _, err := os.Stat(mainPath); err != nil {
    67  		t.Fatalf("err: %s", err)
    68  	}
    69  }
    70  
    71  // https://github.com/hashicorp/terraform/issues/8418
    72  func TestGet_filePercent2F(t *testing.T) {
    73  	ctx := context.Background()
    74  
    75  	dst := testing_helper.TempDir(t)
    76  	u := testModule("basic%2Ftest")
    77  
    78  	op, err := Get(ctx, dst, u)
    79  	if err != nil {
    80  		t.Fatalf("err: %s", err)
    81  	}
    82  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
    83  		t.Fatalf("unexpected op: %s", diff)
    84  	}
    85  
    86  	mainPath := filepath.Join(dst, "main.tf")
    87  	if _, err := os.Stat(mainPath); err != nil {
    88  		t.Fatalf("err: %s", err)
    89  	}
    90  }
    91  
    92  func TestGet_fileDetect(t *testing.T) {
    93  	ctx := context.Background()
    94  
    95  	dst := testing_helper.TempDir(t)
    96  	u := filepath.Join(".", "testdata", "basic")
    97  	pwd, err := os.Getwd()
    98  	if err != nil {
    99  		t.Fatalf("err: %s", err)
   100  	}
   101  
   102  	req := &Request{
   103  		Src:     u,
   104  		Dst:     dst,
   105  		Pwd:     pwd,
   106  		GetMode: ModeAny,
   107  	}
   108  	client := &Client{}
   109  
   110  	if err := client.configure(); err != nil {
   111  		t.Fatalf("configure: %s", err)
   112  	}
   113  
   114  	op, err := client.Get(ctx, req)
   115  	if err != nil {
   116  		t.Fatalf("get: %s", err)
   117  	}
   118  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   119  		t.Fatalf("unexpected op: %s", diff)
   120  	}
   121  
   122  	mainPath := filepath.Join(dst, "main.tf")
   123  	if _, err := os.Stat(mainPath); err != nil {
   124  		t.Fatalf("stat: %s", err)
   125  	}
   126  }
   127  
   128  func TestGet_fileForced(t *testing.T) {
   129  	ctx := context.Background()
   130  
   131  	dst := testing_helper.TempDir(t)
   132  	u := testModule("basic")
   133  	u = "file::" + u
   134  
   135  	op, err := Get(ctx, dst, u)
   136  	if err != nil {
   137  		t.Fatalf("err: %s", err)
   138  	}
   139  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   140  		t.Fatalf("unexpected op: %s", diff)
   141  	}
   142  
   143  	mainPath := filepath.Join(dst, "main.tf")
   144  	if _, err := os.Stat(mainPath); err != nil {
   145  		t.Fatalf("err: %s", err)
   146  	}
   147  }
   148  
   149  func TestGet_fileSubdir(t *testing.T) {
   150  	ctx := context.Background()
   151  
   152  	dst := testing_helper.TempDir(t)
   153  	u := testModule("basic//subdir")
   154  
   155  	op, err := Get(ctx, dst, u)
   156  	if err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   160  		t.Fatalf("unexpected op: %s", diff)
   161  	}
   162  
   163  	mainPath := filepath.Join(dst, "sub.tf")
   164  	if _, err := os.Stat(mainPath); err != nil {
   165  		t.Fatalf("err: %s", err)
   166  	}
   167  }
   168  
   169  func TestGet_archive(t *testing.T) {
   170  	ctx := context.Background()
   171  
   172  	dst := testing_helper.TempDir(t)
   173  	u := filepath.Join("./testdata", "archive.tar.gz")
   174  	u, _ = filepath.Abs(u)
   175  
   176  	op, err := Get(ctx, dst, u)
   177  	if err != nil {
   178  		t.Fatalf("err: %s", err)
   179  	}
   180  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   181  		t.Fatalf("unexpected op: %s", diff)
   182  	}
   183  
   184  	mainPath := filepath.Join(dst, "main.tf")
   185  	if _, err := os.Stat(mainPath); err != nil {
   186  		t.Fatalf("err: %s", err)
   187  	}
   188  }
   189  
   190  func TestGetAny_archive(t *testing.T) {
   191  	ctx := context.Background()
   192  
   193  	dst := testing_helper.TempDir(t)
   194  	u := filepath.Join("./testdata", "archive.tar.gz")
   195  	u, _ = filepath.Abs(u)
   196  
   197  	op, err := GetAny(ctx, dst, u)
   198  	if err != nil {
   199  		t.Fatalf("err: %s", err)
   200  	}
   201  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   202  		t.Fatalf("unexpected op: %s", diff)
   203  	}
   204  
   205  	mainPath := filepath.Join(dst, "main.tf")
   206  	if _, err := os.Stat(mainPath); err != nil {
   207  		t.Fatalf("err: %s", err)
   208  	}
   209  }
   210  
   211  func TestGet_archiveRooted(t *testing.T) {
   212  	ctx := context.Background()
   213  
   214  	dst := testing_helper.TempDir(t)
   215  	u := testModule("archive-rooted/archive.tar.gz")
   216  	op, err := Get(ctx, dst, u)
   217  	if err != nil {
   218  		t.Fatalf("err: %s", err)
   219  	}
   220  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   221  		t.Fatalf("unexpected op: %s", diff)
   222  	}
   223  
   224  	mainPath := filepath.Join(dst, "root", "hello.txt")
   225  	if _, err := os.Stat(mainPath); err != nil {
   226  		t.Fatalf("err: %s", err)
   227  	}
   228  }
   229  
   230  func TestGet_archiveSubdirWild(t *testing.T) {
   231  	ctx := context.Background()
   232  
   233  	dst := testing_helper.TempDir(t)
   234  	u := testModule("archive-rooted/archive.tar.gz")
   235  	u += "//*"
   236  	op, err := Get(ctx, dst, u)
   237  	if err != nil {
   238  		t.Fatalf("err: %s", err)
   239  	}
   240  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   241  		t.Fatalf("unexpected op: %s", diff)
   242  	}
   243  
   244  	mainPath := filepath.Join(dst, "hello.txt")
   245  	if _, err := os.Stat(mainPath); err != nil {
   246  		t.Fatalf("err: %s", err)
   247  	}
   248  }
   249  
   250  func TestGet_archiveSubdirWildMultiMatch(t *testing.T) {
   251  	ctx := context.Background()
   252  
   253  	dst := testing_helper.TempDir(t)
   254  	u := testModule("archive-rooted-multi/archive.tar.gz")
   255  	u += "//*"
   256  	op, err := Get(ctx, dst, u)
   257  	switch err {
   258  	case nil:
   259  		t.Fatal("should error")
   260  	default:
   261  		if !strings.Contains(err.Error(), "multiple") {
   262  			t.Fatalf("err: %s", err)
   263  		}
   264  		if op != nil {
   265  			t.Fatal("GetResult should be nil")
   266  		}
   267  	}
   268  }
   269  
   270  func TestGetAny_file(t *testing.T) {
   271  	ctx := context.Background()
   272  
   273  	dst := testing_helper.TempDir(t)
   274  	u := testModule("basic-file/foo.txt")
   275  
   276  	if _, err := GetAny(ctx, dst, u); err != nil {
   277  		t.Fatalf("err: %s", err)
   278  	}
   279  
   280  	mainPath := filepath.Join(dst, "foo.txt")
   281  	if _, err := os.Stat(mainPath); err != nil {
   282  		t.Fatalf("err: %s", err)
   283  	}
   284  }
   285  
   286  func TestGetAny_dir(t *testing.T) {
   287  	ctx := context.Background()
   288  
   289  	dst := testing_helper.TempDir(t)
   290  	u := filepath.Join("./testdata", "basic")
   291  	u, _ = filepath.Abs(u)
   292  
   293  	op, err := GetAny(ctx, dst, u)
   294  	if err != nil {
   295  		t.Fatalf("err: %s", err)
   296  	}
   297  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   298  		t.Fatalf("unexpected op: %s", diff)
   299  	}
   300  
   301  	check := []string{
   302  		"main.tf",
   303  		"foo/main.tf",
   304  	}
   305  
   306  	for _, name := range check {
   307  		mainPath := filepath.Join(dst, name)
   308  		if _, err := os.Stat(mainPath); err != nil {
   309  			t.Fatalf("err: %s", err)
   310  		}
   311  	}
   312  }
   313  
   314  func TestGetFile(t *testing.T) {
   315  	ctx := context.Background()
   316  
   317  	dst := testing_helper.TempTestFile(t)
   318  	defer os.RemoveAll(filepath.Dir(dst))
   319  	u := testModule("basic-file/foo.txt")
   320  
   321  	op, err := GetFile(ctx, dst, u)
   322  	if err != nil {
   323  		t.Fatalf("err: %s", err)
   324  	}
   325  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   326  		t.Fatalf("unexpected op: %s", diff)
   327  	}
   328  
   329  	// Verify the main file exists
   330  	testing_helper.AssertContents(t, dst, "Hello\n")
   331  }
   332  
   333  func TestGetFile_archive(t *testing.T) {
   334  	ctx := context.Background()
   335  
   336  	dst := testing_helper.TempTestFile(t)
   337  	defer os.RemoveAll(filepath.Dir(dst))
   338  	u := testModule("basic-file-archive/archive.tar.gz")
   339  
   340  	op, err := GetFile(ctx, dst, u)
   341  	if err != nil {
   342  		t.Fatalf("err: %s", err)
   343  	}
   344  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   345  		t.Fatalf("unexpected op: %s", diff)
   346  	}
   347  
   348  	// Verify the main file exists
   349  	testing_helper.AssertContents(t, dst, "Hello\n")
   350  }
   351  func TestGetFile_filename_path_traversal(t *testing.T) {
   352  	dst := testing_helper.TempDir(t)
   353  	u := testModule("basic-file/foo.txt")
   354  
   355  	u += "?filename=../../../../../../../../../../../../../tmp/bar.txt"
   356  
   357  	ctx := context.Background()
   358  	op, err := GetAny(ctx, dst, u)
   359  
   360  	if op != nil {
   361  		t.Fatalf("unexpected op: %v", op)
   362  	}
   363  
   364  	if err == nil {
   365  		t.Fatalf("expected error")
   366  	}
   367  
   368  	if !strings.Contains(err.Error(), "filename query parameter contain path traversal") {
   369  		t.Fatalf("unexpected err: %s", err)
   370  	}
   371  }
   372  
   373  func TestGetFile_archiveChecksum(t *testing.T) {
   374  	ctx := context.Background()
   375  
   376  	dst := testing_helper.TempTestFile(t)
   377  	defer os.RemoveAll(filepath.Dir(dst))
   378  	u := testModule(
   379  		"basic-file-archive/archive.tar.gz?checksum=md5:fbd90037dacc4b1ab40811d610dde2f0")
   380  
   381  	op, err := GetFile(ctx, dst, u)
   382  	if err != nil {
   383  		t.Fatalf("err: %s", err)
   384  	}
   385  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   386  		t.Fatalf("unexpected op: %s", diff)
   387  	}
   388  
   389  	// Verify the main file exists
   390  	testing_helper.AssertContents(t, dst, "Hello\n")
   391  }
   392  
   393  func TestGetFile_archiveNoUnarchive(t *testing.T) {
   394  	ctx := context.Background()
   395  
   396  	dst := testing_helper.TempTestFile(t)
   397  	defer os.RemoveAll(filepath.Dir(dst))
   398  	u := testModule("basic-file-archive/archive.tar.gz")
   399  	u += "?archive=false"
   400  
   401  	op, err := GetFile(ctx, dst, u)
   402  	if err != nil {
   403  		t.Fatalf("err: %s", err)
   404  	}
   405  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   406  		t.Fatalf("unexpected op: %s", diff)
   407  	}
   408  
   409  	// Verify the main file exists
   410  	actual := testMD5(t, dst)
   411  	expected := "fbd90037dacc4b1ab40811d610dde2f0"
   412  	if actual != expected {
   413  		t.Fatalf("bad: %s", actual)
   414  	}
   415  }
   416  
   417  func TestGetFile_checksum(t *testing.T) {
   418  	ctx := context.Background()
   419  
   420  	cases := []struct {
   421  		Append string
   422  		Err    bool
   423  	}{
   424  		{
   425  			"",
   426  			false,
   427  		},
   428  
   429  		// MD5
   430  		{
   431  			"?checksum=09f7e02f1290be211da707a266f153b3",
   432  			false,
   433  		},
   434  		{
   435  			"?checksum=md5:09f7e02f1290be211da707a266f153b3",
   436  			false,
   437  		},
   438  		{
   439  			"?checksum=md5:09f7e02f1290be211da707a266f153b4",
   440  			true,
   441  		},
   442  
   443  		// SHA1
   444  		{
   445  			"?checksum=1d229271928d3f9e2bb0375bd6ce5db6c6d348d9",
   446  			false,
   447  		},
   448  		{
   449  			"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d9",
   450  			false,
   451  		},
   452  		{
   453  			"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d0",
   454  			true,
   455  		},
   456  
   457  		// SHA256
   458  		{
   459  			"?checksum=66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
   460  			false,
   461  		},
   462  		{
   463  			"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
   464  			false,
   465  		},
   466  		{
   467  			"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f19",
   468  			true,
   469  		},
   470  
   471  		// SHA512
   472  		{
   473  			"?checksum=c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750cef",
   474  			false,
   475  		},
   476  		{
   477  			"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750cef",
   478  			false,
   479  		},
   480  		{
   481  			"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750ced",
   482  			true,
   483  		},
   484  	}
   485  
   486  	for _, tc := range cases {
   487  		u := testModule("basic-file/foo.txt") + tc.Append
   488  
   489  		func() {
   490  			dst := testing_helper.TempTestFile(t)
   491  			defer os.RemoveAll(filepath.Dir(dst))
   492  			op, err := GetFile(ctx, dst, u)
   493  			if (err != nil) != tc.Err {
   494  				t.Fatalf("append: %s\n\nerr: %s", tc.Append, err)
   495  			}
   496  			if err == nil {
   497  				if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   498  					t.Fatalf("unexpected dst: %s", diff)
   499  				}
   500  			}
   501  
   502  			// Verify the main file exists
   503  			testing_helper.AssertContents(t, dst, "Hello\n")
   504  		}()
   505  	}
   506  }
   507  
   508  func TestGetFile_checksum_from_file(t *testing.T) {
   509  
   510  	checksums := testModule("checksum-file")
   511  	httpChecksums := httpTestModule("checksum-file")
   512  	defer httpChecksums.Close()
   513  
   514  	cases := []struct {
   515  		Append       string
   516  		WantTransfer bool
   517  		WantErr      bool
   518  	}{
   519  
   520  		{
   521  			"",
   522  			true,
   523  			false,
   524  		},
   525  
   526  		// md5
   527  		{
   528  			"?checksum=file:" + checksums + "/md5-p.sum",
   529  			true,
   530  			false,
   531  		},
   532  		{
   533  			"?checksum=file:" + httpChecksums.URL + "/md5-bsd.sum",
   534  			true,
   535  			false,
   536  		},
   537  		{
   538  			"?checksum=file:" + checksums + "/md5-bsd-bad.sum",
   539  			false,
   540  			true,
   541  		},
   542  		{
   543  			"?checksum=file:" + httpChecksums.URL + "/md5-bsd-wrong.sum",
   544  			true,
   545  			true,
   546  		},
   547  
   548  		// sha1
   549  		{
   550  			"?checksum=file:" + checksums + "/sha1-p.sum",
   551  			true,
   552  			false,
   553  		},
   554  		{
   555  			"?checksum=file:" + httpChecksums.URL + "/sha1.sum",
   556  			true,
   557  			false,
   558  		},
   559  
   560  		// sha256
   561  		{
   562  			"?checksum=file:" + checksums + "/sha256-p.sum",
   563  			true,
   564  			false,
   565  		},
   566  
   567  		// sha512
   568  		{
   569  			"?checksum=file:" + httpChecksums.URL + "/sha512-p.sum",
   570  			true,
   571  			false,
   572  		},
   573  
   574  		// sha512
   575  		{
   576  			"?checksum=file:" + checksums + "/CHECKSUM_sha256_gpg",
   577  			true,
   578  			false,
   579  		},
   580  	}
   581  
   582  	for _, tc := range cases {
   583  		u := checksums + "/content.txt" + tc.Append
   584  		t.Run(tc.Append, func(t *testing.T) {
   585  			ctx := context.Background()
   586  
   587  			dst := testing_helper.TempTestFile(t)
   588  			defer os.RemoveAll(filepath.Dir(dst))
   589  			op, err := GetFile(ctx, dst, u)
   590  			if (err != nil) != tc.WantErr {
   591  				t.Fatalf("append: %s\n\nerr: %s", tc.Append, err)
   592  			}
   593  			if err == nil {
   594  				if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   595  					t.Fatalf("unexpected dst: %s", diff)
   596  				}
   597  			}
   598  
   599  			if tc.WantTransfer {
   600  				// Verify the main file exists
   601  				testing_helper.AssertContents(t, dst, "I am a file with some content\n")
   602  			}
   603  		})
   604  		return
   605  	}
   606  }
   607  
   608  func TestGetFile_checksumURL(t *testing.T) {
   609  	ctx := context.Background()
   610  
   611  	dst := testing_helper.TempTestFile(t)
   612  	defer os.RemoveAll(filepath.Dir(dst))
   613  	u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
   614  
   615  	getter := &MockGetter{Proxy: new(FileGetter)}
   616  	req := &Request{
   617  		Src:     u,
   618  		Dst:     dst,
   619  		GetMode: ModeFile,
   620  	}
   621  	client := &Client{
   622  		Getters: []Getter{getter},
   623  	}
   624  
   625  	op, err := client.Get(ctx, req)
   626  	if err != nil {
   627  		t.Fatalf("err: %s", err)
   628  	}
   629  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   630  		t.Fatalf("unexpected op: %s", diff)
   631  	}
   632  
   633  	if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
   634  		t.Fatalf("bad: %s", v)
   635  	}
   636  }
   637  
   638  func TestGetFile_filename(t *testing.T) {
   639  	ctx := context.Background()
   640  
   641  	dst := testing_helper.TempDir(t)
   642  	u := testModule("basic-file/foo.txt")
   643  
   644  	u += "?filename=bar.txt"
   645  	realDst := filepath.Join(dst, "bar.txt")
   646  
   647  	op, err := GetAny(ctx, dst, u)
   648  	if err != nil {
   649  		t.Fatalf("err: %s", err)
   650  	}
   651  	if diff := cmp.Diff(realDst, op.Dst); diff != "" {
   652  		t.Fatalf("unexpected op: %s", diff)
   653  	}
   654  
   655  	mainPath := realDst
   656  	if _, err := os.Stat(mainPath); err != nil {
   657  		t.Fatalf("err: %s", err)
   658  	}
   659  }
   660  
   661  func TestGetFile_checksumSkip(t *testing.T) {
   662  	ctx := context.Background()
   663  
   664  	dst := testing_helper.TempTestFile(t)
   665  	defer os.RemoveAll(filepath.Dir(dst))
   666  	u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
   667  
   668  	getter := &MockGetter{Proxy: new(FileGetter)}
   669  	req := &Request{
   670  		Src:     u,
   671  		Dst:     dst,
   672  		GetMode: ModeFile,
   673  	}
   674  	client := &Client{
   675  		Getters: []Getter{getter},
   676  	}
   677  
   678  	// get the file
   679  	op, err := client.Get(ctx, req)
   680  	if err != nil {
   681  		t.Fatalf("err: %s", err)
   682  	}
   683  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   684  		t.Fatalf("unexpected op: %s", diff)
   685  	}
   686  
   687  	if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
   688  		t.Fatalf("bad: %s", v)
   689  	}
   690  
   691  	// remove proxy file getter and reset GetFileCalled so that we can re-test.
   692  	getter.Proxy = nil
   693  	getter.GetFileCalled = false
   694  
   695  	op, err = client.Get(ctx, req)
   696  	if err != nil {
   697  		t.Fatalf("err: %s", err)
   698  	}
   699  	if diff := cmp.Diff(&GetResult{Dst: dst}, op); diff != "" {
   700  		t.Fatalf("unexpected op: %s", diff)
   701  	}
   702  
   703  	if getter.GetFileCalled {
   704  		t.Fatalf("get should not have been called")
   705  	}
   706  }
   707  
   708  func TestGetFile_inplace(t *testing.T) {
   709  	ctx := context.Background()
   710  
   711  	dst := testing_helper.TempTestFile(t)
   712  	defer os.RemoveAll(filepath.Dir(dst))
   713  	src := testModule("basic-file/foo.txt")
   714  
   715  	getter := &MockGetter{Proxy: new(FileGetter)}
   716  	req := &Request{
   717  		Src:     src + "?checksum=md5:09f7e02f1290be211da707a266f153b3",
   718  		Dst:     dst,
   719  		GetMode: ModeFile,
   720  		Inplace: true,
   721  	}
   722  	client := &Client{
   723  		Getters: []Getter{getter},
   724  	}
   725  
   726  	// get the file
   727  	op, err := client.Get(ctx, req)
   728  	if err != nil {
   729  		t.Fatalf("err: %s", err)
   730  	}
   731  	if diff := cmp.Diff(&GetResult{Dst: strings.ReplaceAll(src, "file://", "")}, op); diff != "" {
   732  		t.Fatalf("unexpected op: %s", diff)
   733  	}
   734  
   735  	if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
   736  		t.Fatalf("bad: %s", v)
   737  	}
   738  
   739  	// remove proxy file getter and reset GetFileCalled so that we can re-test.
   740  	getter.Proxy = nil
   741  	getter.GetFileCalled = false
   742  
   743  	op, err = client.Get(ctx, req)
   744  	if err != nil {
   745  		t.Fatalf("err: %s", err)
   746  	}
   747  	if diff := cmp.Diff(&GetResult{Dst: strings.ReplaceAll(src, "file://", "")}, op); diff != "" {
   748  		t.Fatalf("unexpected op: %s", diff)
   749  	}
   750  
   751  	if getter.GetFileCalled {
   752  		t.Fatalf("get should not have been called")
   753  	}
   754  }
   755  
   756  func TestGetFile_inplace_badChecksum(t *testing.T) {
   757  	ctx := context.Background()
   758  
   759  	dst := testing_helper.TempTestFile(t)
   760  	defer os.RemoveAll(filepath.Dir(dst))
   761  	src := testModule("basic-file/foo.txt")
   762  
   763  	getter := &MockGetter{Proxy: new(FileGetter)}
   764  	req := &Request{
   765  		Src:     src + "?checksum=md5:09f7e02f1290be211da707a266f153b4",
   766  		Dst:     dst,
   767  		GetMode: ModeFile,
   768  		Inplace: true,
   769  	}
   770  	client := &Client{
   771  		Getters: []Getter{getter},
   772  	}
   773  
   774  	// get the file
   775  	op, err := client.Get(ctx, req)
   776  	if err == nil {
   777  		t.Fatalf("err is nil")
   778  	}
   779  	if _, ok := err.(*ChecksumError); !ok {
   780  		t.Fatalf("err is not a checksum error: %v", err)
   781  	}
   782  	if op != nil {
   783  		t.Fatalf("op is not nil")
   784  	}
   785  }
   786  
   787  func TestGetForcedGetter(t *testing.T) {
   788  	type args struct {
   789  		src string
   790  	}
   791  	tests := []struct {
   792  		name  string
   793  		args  args
   794  		want  string
   795  		want1 string
   796  	}{
   797  		{"s3 AWSv1234",
   798  			args{src: "s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz?version=1234"},
   799  			"s3", "https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz?version=1234",
   800  		},
   801  		{"s3 localhost-1",
   802  			args{src: "s3::http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret&region=us-east-2&version=1"},
   803  			"s3", "http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret&region=us-east-2&version=1",
   804  		},
   805  		{"s3 localhost-2",
   806  			args{src: "s3::http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret&version=1"},
   807  			"s3", "http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret&version=1",
   808  		},
   809  		{"s3 localhost-3",
   810  			args{src: "s3::http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret"},
   811  			"s3", "http://127.0.0.1:9000/test-bucket/hello.txt?aws_access_key_id=TESTID&aws_access_key_secret=TestSecret",
   812  		},
   813  
   814  		{
   815  			"gcs test1",
   816  			args{"gcs::https://www.googleapis.com/storage/v1/go-getter-test/go-getter/foo/null.zip"},
   817  			"gcs", "https://www.googleapis.com/storage/v1/go-getter-test/go-getter/foo/null.zip",
   818  		},
   819  	}
   820  	for _, tt := range tests {
   821  		t.Run(tt.name, func(t *testing.T) {
   822  			got, got1 := getForcedGetter(tt.args.src)
   823  			if got != tt.want {
   824  				t.Errorf("getForcedGetter() got = %v, want %v", got, tt.want)
   825  			}
   826  			if got1 != tt.want1 {
   827  				t.Errorf("getForcedGetter() got1 = %v, want %v", got1, tt.want1)
   828  			}
   829  		})
   830  	}
   831  }