github.com/weiwenhao/getter@v1.30.1/get_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestGet_badSchema(t *testing.T) {
    11  	dst := tempDir(t)
    12  	u := testModule("basic")
    13  	u = strings.Replace(u, "file", "nope", -1)
    14  
    15  	if err := Get(dst, u); err == nil {
    16  		t.Fatal("should error")
    17  	}
    18  }
    19  
    20  func TestGet_file(t *testing.T) {
    21  	dst := tempDir(t)
    22  	u := testModule("basic")
    23  
    24  	if err := Get(dst, u); err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  
    28  	mainPath := filepath.Join(dst, "main.tf")
    29  	if _, err := os.Stat(mainPath); err != nil {
    30  		t.Fatalf("err: %s", err)
    31  	}
    32  }
    33  
    34  // https://github.com/hashicorp/terraform/issues/11438
    35  func TestGet_fileDecompressorExt(t *testing.T) {
    36  	dst := tempDir(t)
    37  	u := testModule("basic-tgz")
    38  
    39  	if err := Get(dst, u); err != nil {
    40  		t.Fatalf("err: %s", err)
    41  	}
    42  
    43  	mainPath := filepath.Join(dst, "main.tf")
    44  	if _, err := os.Stat(mainPath); err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  }
    48  
    49  // https://github.com/hashicorp/terraform/issues/8418
    50  func TestGet_filePercent2F(t *testing.T) {
    51  	dst := tempDir(t)
    52  	u := testModule("basic%2Ftest")
    53  
    54  	if err := Get(dst, u); err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  
    58  	mainPath := filepath.Join(dst, "main.tf")
    59  	if _, err := os.Stat(mainPath); err != nil {
    60  		t.Fatalf("err: %s", err)
    61  	}
    62  }
    63  
    64  func TestGet_fileDetect(t *testing.T) {
    65  	dst := tempDir(t)
    66  	u := filepath.Join(".", "testdata", "basic")
    67  	pwd, err := os.Getwd()
    68  	if err != nil {
    69  		t.Fatalf("err: %s", err)
    70  	}
    71  
    72  	client := &Client{
    73  		Src: u,
    74  		Dst: dst,
    75  		Pwd: pwd,
    76  		Dir: true,
    77  	}
    78  
    79  	if err := client.Configure(); err != nil {
    80  		t.Fatalf("configure: %s", err)
    81  	}
    82  
    83  	if err := client.Get(); err != nil {
    84  		t.Fatalf("get: %s", err)
    85  	}
    86  
    87  	mainPath := filepath.Join(dst, "main.tf")
    88  	if _, err := os.Stat(mainPath); err != nil {
    89  		t.Fatalf("stat: %s", err)
    90  	}
    91  }
    92  
    93  func TestGet_fileForced(t *testing.T) {
    94  	dst := tempDir(t)
    95  	u := testModule("basic")
    96  	u = "file::" + u
    97  
    98  	if err := Get(dst, u); err != nil {
    99  		t.Fatalf("err: %s", err)
   100  	}
   101  
   102  	mainPath := filepath.Join(dst, "main.tf")
   103  	if _, err := os.Stat(mainPath); err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  }
   107  
   108  func TestGet_fileSubdir(t *testing.T) {
   109  	dst := tempDir(t)
   110  	u := testModule("basic//subdir")
   111  
   112  	if err := Get(dst, u); err != nil {
   113  		t.Fatalf("err: %s", err)
   114  	}
   115  
   116  	mainPath := filepath.Join(dst, "sub.tf")
   117  	if _, err := os.Stat(mainPath); err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  }
   121  
   122  func TestGet_archive(t *testing.T) {
   123  	dst := tempDir(t)
   124  	u := filepath.Join("./testdata", "archive.tar.gz")
   125  	u, _ = filepath.Abs(u)
   126  
   127  	if err := Get(dst, u); err != nil {
   128  		t.Fatalf("err: %s", err)
   129  	}
   130  
   131  	mainPath := filepath.Join(dst, "main.tf")
   132  	if _, err := os.Stat(mainPath); err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  }
   136  
   137  func TestGetAny_archive(t *testing.T) {
   138  	dst := tempDir(t)
   139  	u := filepath.Join("./testdata", "archive.tar.gz")
   140  	u, _ = filepath.Abs(u)
   141  
   142  	if err := GetAny(dst, u); err != nil {
   143  		t.Fatalf("err: %s", err)
   144  	}
   145  
   146  	mainPath := filepath.Join(dst, "main.tf")
   147  	if _, err := os.Stat(mainPath); err != nil {
   148  		t.Fatalf("err: %s", err)
   149  	}
   150  }
   151  
   152  func TestGet_archiveRooted(t *testing.T) {
   153  	dst := tempDir(t)
   154  	u := testModule("archive-rooted/archive.tar.gz")
   155  	if err := Get(dst, u); err != nil {
   156  		t.Fatalf("err: %s", err)
   157  	}
   158  
   159  	mainPath := filepath.Join(dst, "root", "hello.txt")
   160  	if _, err := os.Stat(mainPath); err != nil {
   161  		t.Fatalf("err: %s", err)
   162  	}
   163  }
   164  
   165  func TestGet_archiveSubdirWild(t *testing.T) {
   166  	dst := tempDir(t)
   167  	u := testModule("archive-rooted/archive.tar.gz")
   168  	u += "//*"
   169  	if err := Get(dst, u); err != nil {
   170  		t.Fatalf("err: %s", err)
   171  	}
   172  
   173  	mainPath := filepath.Join(dst, "hello.txt")
   174  	if _, err := os.Stat(mainPath); err != nil {
   175  		t.Fatalf("err: %s", err)
   176  	}
   177  }
   178  
   179  func TestGet_archiveSubdirWildMultiMatch(t *testing.T) {
   180  	dst := tempDir(t)
   181  	u := testModule("archive-rooted-multi/archive.tar.gz")
   182  	u += "//*"
   183  	if err := Get(dst, u); err == nil {
   184  		t.Fatal("should error")
   185  	} else if !strings.Contains(err.Error(), "multiple") {
   186  		t.Fatalf("err: %s", err)
   187  	}
   188  }
   189  
   190  func TestGetAny_file(t *testing.T) {
   191  	dst := tempDir(t)
   192  	u := testModule("basic-file/foo.txt")
   193  
   194  	if err := GetAny(dst, u); err != nil {
   195  		t.Fatalf("err: %s", err)
   196  	}
   197  
   198  	mainPath := filepath.Join(dst, "foo.txt")
   199  	if _, err := os.Stat(mainPath); err != nil {
   200  		t.Fatalf("err: %s", err)
   201  	}
   202  }
   203  
   204  func TestGetAny_dir(t *testing.T) {
   205  	dst := tempDir(t)
   206  	u := filepath.Join("./testdata", "basic")
   207  	u, _ = filepath.Abs(u)
   208  
   209  	if err := GetAny(dst, u); err != nil {
   210  		t.Fatalf("err: %s", err)
   211  	}
   212  
   213  	check := []string{
   214  		"main.tf",
   215  		"foo/main.tf",
   216  	}
   217  
   218  	for _, name := range check {
   219  		mainPath := filepath.Join(dst, name)
   220  		if _, err := os.Stat(mainPath); err != nil {
   221  			t.Fatalf("err: %s", err)
   222  		}
   223  	}
   224  }
   225  
   226  func TestGetFile(t *testing.T) {
   227  	dst := tempTestFile(t)
   228  	defer os.RemoveAll(filepath.Dir(dst))
   229  	u := testModule("basic-file/foo.txt")
   230  
   231  	if err := GetFile(dst, u); err != nil {
   232  		t.Fatalf("err: %s", err)
   233  	}
   234  
   235  	// Verify the main file exists
   236  	assertContents(t, dst, "Hello\n")
   237  }
   238  
   239  func TestGetFile_archive(t *testing.T) {
   240  	dst := tempTestFile(t)
   241  	defer os.RemoveAll(filepath.Dir(dst))
   242  	u := testModule("basic-file-archive/archive.tar.gz")
   243  
   244  	if err := GetFile(dst, u); err != nil {
   245  		t.Fatalf("err: %s", err)
   246  	}
   247  
   248  	// Verify the main file exists
   249  	assertContents(t, dst, "Hello\n")
   250  }
   251  
   252  func TestGetFile_archiveChecksum(t *testing.T) {
   253  	dst := tempTestFile(t)
   254  	defer os.RemoveAll(filepath.Dir(dst))
   255  	u := testModule(
   256  		"basic-file-archive/archive.tar.gz?checksum=md5:fbd90037dacc4b1ab40811d610dde2f0")
   257  
   258  	if err := GetFile(dst, u); err != nil {
   259  		t.Fatalf("err: %s", err)
   260  	}
   261  
   262  	// Verify the main file exists
   263  	assertContents(t, dst, "Hello\n")
   264  }
   265  
   266  func TestGetFile_archiveNoUnarchive(t *testing.T) {
   267  	dst := tempTestFile(t)
   268  	defer os.RemoveAll(filepath.Dir(dst))
   269  	u := testModule("basic-file-archive/archive.tar.gz")
   270  	u += "?archive=false"
   271  
   272  	if err := GetFile(dst, u); err != nil {
   273  		t.Fatalf("err: %s", err)
   274  	}
   275  
   276  	// Verify the main file exists
   277  	actual := testMD5(t, dst)
   278  	expected := "fbd90037dacc4b1ab40811d610dde2f0"
   279  	if actual != expected {
   280  		t.Fatalf("bad: %s", actual)
   281  	}
   282  }
   283  
   284  func TestGetFile_checksum(t *testing.T) {
   285  	cases := []struct {
   286  		Append string
   287  		Err    bool
   288  	}{
   289  		{
   290  			"",
   291  			false,
   292  		},
   293  
   294  		// MD5
   295  		{
   296  			"?checksum=09f7e02f1290be211da707a266f153b3",
   297  			false,
   298  		},
   299  		{
   300  			"?checksum=md5:09f7e02f1290be211da707a266f153b3",
   301  			false,
   302  		},
   303  		{
   304  			"?checksum=md5:09f7e02f1290be211da707a266f153b4",
   305  			true,
   306  		},
   307  
   308  		// SHA1
   309  		{
   310  			"?checksum=1d229271928d3f9e2bb0375bd6ce5db6c6d348d9",
   311  			false,
   312  		},
   313  		{
   314  			"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d9",
   315  			false,
   316  		},
   317  		{
   318  			"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d0",
   319  			true,
   320  		},
   321  
   322  		// SHA256
   323  		{
   324  			"?checksum=66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
   325  			false,
   326  		},
   327  		{
   328  			"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
   329  			false,
   330  		},
   331  		{
   332  			"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f19",
   333  			true,
   334  		},
   335  
   336  		// SHA512
   337  		{
   338  			"?checksum=c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750cef",
   339  			false,
   340  		},
   341  		{
   342  			"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750cef",
   343  			false,
   344  		},
   345  		{
   346  			"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750ced",
   347  			true,
   348  		},
   349  	}
   350  
   351  	for _, tc := range cases {
   352  		u := testModule("basic-file/foo.txt") + tc.Append
   353  
   354  		func() {
   355  			dst := tempTestFile(t)
   356  			defer os.RemoveAll(filepath.Dir(dst))
   357  			if err := GetFile(dst, u); (err != nil) != tc.Err {
   358  				t.Fatalf("append: %s\n\nerr: %s", tc.Append, err)
   359  			}
   360  
   361  			// Verify the main file exists
   362  			assertContents(t, dst, "Hello\n")
   363  		}()
   364  	}
   365  }
   366  
   367  func TestGetFile_checksum_from_file(t *testing.T) {
   368  	checksums := testModule("checksum-file")
   369  	httpChecksums := httpTestModule("checksum-file")
   370  	defer httpChecksums.Close()
   371  
   372  	cases := []struct {
   373  		Append       string
   374  		WantTransfer bool
   375  		WantErr      bool
   376  	}{
   377  		{
   378  			"",
   379  			true,
   380  			false,
   381  		},
   382  
   383  		// md5
   384  		{
   385  			"?checksum=file:" + checksums + "/md5-p.sum",
   386  			true,
   387  			false,
   388  		},
   389  		{
   390  			"?checksum=file:" + httpChecksums.URL + "/md5-bsd.sum",
   391  			true,
   392  			false,
   393  		},
   394  		{
   395  			"?checksum=file:" + checksums + "/md5-bsd-bad.sum",
   396  			false,
   397  			true,
   398  		},
   399  		{
   400  			"?checksum=file:" + httpChecksums.URL + "/md5-bsd-wrong.sum",
   401  			true,
   402  			true,
   403  		},
   404  
   405  		// sha1
   406  		{
   407  			"?checksum=file:" + checksums + "/sha1-p.sum",
   408  			true,
   409  			false,
   410  		},
   411  		{
   412  			"?checksum=file:" + httpChecksums.URL + "/sha1.sum",
   413  			true,
   414  			false,
   415  		},
   416  
   417  		// sha256
   418  		{
   419  			"?checksum=file:" + checksums + "/sha256-p.sum",
   420  			true,
   421  			false,
   422  		},
   423  
   424  		// sha512
   425  		{
   426  			"?checksum=file:" + httpChecksums.URL + "/sha512-p.sum",
   427  			true,
   428  			false,
   429  		},
   430  		{
   431  			// checksum file does not have EOL, ends line with EOF
   432  			"?checksum=file:" + httpChecksums.URL + "/sha512-p-EOF.sum",
   433  			true,
   434  			false,
   435  		},
   436  	}
   437  
   438  	for _, tc := range cases {
   439  		u := checksums + "/content.txt" + tc.Append
   440  		t.Run(tc.Append, func(t *testing.T) {
   441  			dst := tempTestFile(t)
   442  			defer os.RemoveAll(filepath.Dir(dst))
   443  			if err := GetFile(dst, u); (err != nil) != tc.WantErr {
   444  				t.Fatalf("append: %s\n\nerr: %s", tc.Append, err)
   445  			}
   446  
   447  			if tc.WantTransfer {
   448  				// Verify the main file exists
   449  				assertContents(t, dst, "I am a file with some content\n")
   450  			}
   451  		})
   452  	}
   453  }
   454  
   455  func TestGetFile_checksumURL(t *testing.T) {
   456  	dst := tempTestFile(t)
   457  	defer os.RemoveAll(filepath.Dir(dst))
   458  	u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
   459  
   460  	getter := &MockGetter{Proxy: new(FileGetter)}
   461  	client := &Client{
   462  		Src: u,
   463  		Dst: dst,
   464  		Dir: false,
   465  		Getters: map[string]Getter{
   466  			"file": getter,
   467  		},
   468  	}
   469  
   470  	if err := client.Get(); err != nil {
   471  		t.Fatalf("err: %s", err)
   472  	}
   473  
   474  	if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
   475  		t.Fatalf("bad: %s", v)
   476  	}
   477  }
   478  
   479  func TestGetFile_filename(t *testing.T) {
   480  	dst := tempDir(t)
   481  	u := testModule("basic-file/foo.txt")
   482  
   483  	u += "?filename=bar.txt"
   484  
   485  	if err := GetAny(dst, u); err != nil {
   486  		t.Fatalf("err: %s", err)
   487  	}
   488  
   489  	mainPath := filepath.Join(dst, "bar.txt")
   490  	if _, err := os.Stat(mainPath); err != nil {
   491  		t.Fatalf("err: %s", err)
   492  	}
   493  }
   494  
   495  func TestGetFile_checksumSkip(t *testing.T) {
   496  	dst := tempTestFile(t)
   497  	defer os.RemoveAll(filepath.Dir(dst))
   498  	u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
   499  
   500  	getter := &MockGetter{Proxy: new(FileGetter)}
   501  	client := &Client{
   502  		Src: u,
   503  		Dst: dst,
   504  		Dir: false,
   505  		Getters: map[string]Getter{
   506  			"file": getter,
   507  		},
   508  	}
   509  
   510  	// get the file
   511  	if err := client.Get(); err != nil {
   512  		t.Fatalf("err: %s", err)
   513  	}
   514  
   515  	if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
   516  		t.Fatalf("bad: %s", v)
   517  	}
   518  
   519  	// remove proxy file getter and reset GetFileCalled so that we can re-test.
   520  	getter.Proxy = nil
   521  	getter.GetFileCalled = false
   522  
   523  	if err := client.Get(); err != nil {
   524  		t.Fatalf("err: %s", err)
   525  	}
   526  
   527  	if getter.GetFileCalled {
   528  		t.Fatalf("get should not have been called")
   529  	}
   530  }