1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
|
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package git
import (
"fmt"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewRepoSpecFromUrl_Permute(t *testing.T) {
// Generate all many permutations of host, RepoPath, pathName, and ref.
// Not all combinations make sense, but the parsing is very permissive and
// we probably stil don't want to break backwards compatibility for things
// that are unintentionally supported.
var schemeAuthority = []struct{ raw, normalized string }{
{"gitHub.com/", "https://github.com/"},
{"github.com:", "https://github.com/"},
{"http://github.com/", "https://github.com/"},
{"https://github.com/", "https://github.com/"},
{"hTTps://github.com/", "https://github.com/"},
{"https://git-codecommit.us-east-2.amazonaws.com/", "https://git-codecommit.us-east-2.amazonaws.com/"},
{"https://fabrikops2.visualstudio.com/", "https://fabrikops2.visualstudio.com/"},
{"ssh://git.example.com:7999/", "ssh://git.example.com:7999/"},
{"git::https://gitlab.com/", "https://gitlab.com/"},
{"git::http://git.example.com/", "http://git.example.com/"},
{"git::https://git.example.com/", "https://git.example.com/"},
{"git@github.com:", "git@github.com:"},
{"git@github.com/", "git@github.com:"},
{"git::git@github.com:", "git@github.com:"},
{"org-12345@github.com:", "org-12345@github.com:"},
{"org-12345@github.com/", "org-12345@github.com:"},
}
var repoPaths = []string{"someOrg/someRepo", "kubernetes/website"}
var pathNames = []string{"README.md", "foo/krusty.txt", ""}
var refArgs = []string{"group/version", "someBranch", "master", "v0.1.0", ""}
makeURL := func(hostFmt, repoPath, path, ref string) string {
if len(path) > 0 {
repoPath = filepath.Join(repoPath, path)
}
url := hostFmt + repoPath
if ref != "" {
url += refQuery + ref
}
return url
}
var i int
for _, v := range schemeAuthority {
hostRaw := v.raw
hostSpec := v.normalized
for _, repoPath := range repoPaths {
for _, pathName := range pathNames {
for _, hrefArg := range refArgs {
t.Run(fmt.Sprintf("t%d", i), func(t *testing.T) {
uri := makeURL(hostRaw, repoPath, pathName, hrefArg)
rs, err := NewRepoSpecFromURL(uri)
require.NoErrorf(t, err, "unexpected error creating RepoSpec for uri %s", uri)
assert.Equal(t, hostSpec, rs.Host, "unexpected host for uri %s", uri)
assert.Equal(t, repoPath, rs.RepoPath, "unexpected RepoPath for uri %s", uri)
assert.Equal(t, pathName, rs.KustRootPath, "unexpected KustRootPath for uri %s", uri)
assert.Equal(t, hrefArg, rs.Ref, "unexpected ref for uri %s", uri)
})
i++
}
}
}
}
}
func TestNewRepoSpecFromUrlErrors(t *testing.T) {
badData := map[string]struct {
url, error string
}{
"absolute_path": {
"/tmp",
"uri looks like abs path",
},
"relative path": {
"../../tmp",
"failed to parse scheme",
},
"local path that looks somewhat like a github url": {
"src/github.com/org/repo/path",
"failed to parse scheme",
},
"no_slashes": {
"iauhsdiuashduas",
"failed to parse scheme",
},
"bad_scheme": {
"htxxxtp://github.com/",
"failed to parse scheme",
},
"no_org_repo": {
"ssh://git.example.com",
"failed to parse repo path segment",
},
"hashicorp_git_only": {
"git::___",
"failed to parse scheme",
},
"query_after_host": {
"https://host?ref=group/version/minor_version",
"failed to parse repo path segment",
},
"path_exits_repo": {
"https://github.com/org/repo.git//path/../../exits/repo",
"url path exits repo",
},
"bad github separator": {
"github.com!org/repo.git//path",
"failed to parse scheme",
},
"mysterious gh: prefix previously supported is no longer handled": {
"gh:org/repo",
"failed to parse scheme",
},
"invalid Github url missing orgrepo": {
"https://github.com/thisisa404.yaml",
"failed to parse repo path segment",
},
"file protocol with excessive slashes": { // max valid is three: two for the scheme and one for the root
"file:////tmp//path/to/whatever",
"failed to parse repo path segment",
},
"unsupported protocol after username (invalid)": {
"git@scp://github.com/org/repo.git//path",
"failed to parse repo path segment",
},
"supported protocol after username (invalid)": {
"git@ssh://github.com/org/repo.git//path",
"failed to parse repo path segment",
},
}
for name, testCase := range badData {
t.Run(name, func(t *testing.T) {
_, err := NewRepoSpecFromURL(testCase.url)
require.Error(t, err)
require.Contains(t, err.Error(), testCase.error)
})
}
}
func TestNewRepoSpecFromUrl_Smoke(t *testing.T) {
// A set of end to end parsing tests that smoke out obvious issues
// No tests for submodules and timeout as the expectations are hard-coded
// to the defaults for compactness.
testcases := []struct {
name string
input string
repoSpec RepoSpec
cloneSpec string
absPath string
}{
{
name: "https aws code commit url",
input: "https://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo/somedir",
cloneSpec: "https://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "https://git-codecommit.us-east-2.amazonaws.com/",
RepoPath: "someorg/somerepo",
KustRootPath: "somedir",
},
},
{
name: "https aws code commit url with params",
input: "https://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo/somedir?ref=testbranch",
cloneSpec: "https://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "https://git-codecommit.us-east-2.amazonaws.com/",
RepoPath: "someorg/somerepo",
KustRootPath: "somedir",
Ref: "testbranch",
},
},
{
name: "legacy azure https url with params",
input: "https://fabrikops2.visualstudio.com/someorg/somerepo?ref=master",
cloneSpec: "https://fabrikops2.visualstudio.com/someorg/somerepo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://fabrikops2.visualstudio.com/",
RepoPath: "someorg/somerepo",
Ref: "master",
},
},
{
name: "http github url without git suffix",
input: "http://github.com/someorg/somerepo/somedir",
cloneSpec: "https://github.com/someorg/somerepo",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "https://github.com/",
RepoPath: "someorg/somerepo",
KustRootPath: "somedir",
},
},
{
name: "scp github url without git suffix",
input: "git@github.com:someorg/somerepo/somedir",
cloneSpec: "git@github.com:someorg/somerepo",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "git@github.com:",
RepoPath: "someorg/somerepo",
KustRootPath: "somedir",
},
},
{
name: "http github url with git suffix",
input: "http://github.com/someorg/somerepo.git/somedir",
cloneSpec: "https://github.com/someorg/somerepo.git",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "https://github.com/",
RepoPath: "someorg/somerepo.git",
KustRootPath: "somedir",
},
},
{
name: "scp github url with git suffix",
input: "git@github.com:someorg/somerepo.git/somedir",
cloneSpec: "git@github.com:someorg/somerepo.git",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "git@github.com:",
RepoPath: "someorg/somerepo.git",
KustRootPath: "somedir",
},
},
{
name: "non-github_scp",
input: "git@gitlab2.sqtools.ru:infra/kubernetes/thanos-base.git?ref=v0.1.0",
cloneSpec: "git@gitlab2.sqtools.ru:infra/kubernetes/thanos-base.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "git@gitlab2.sqtools.ru:",
RepoPath: "infra/kubernetes/thanos-base.git",
Ref: "v0.1.0",
},
},
{
name: "non-github_scp with path delimiter",
input: "git@bitbucket.org:company/project.git//path?ref=branch",
cloneSpec: "git@bitbucket.org:company/project.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "git@bitbucket.org:",
RepoPath: "company/project.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "non-github_scp incorrectly using slash (invalid but currently passed through to git)",
input: "git@bitbucket.org/company/project.git//path?ref=branch",
cloneSpec: "git@bitbucket.org/company/project.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "git@bitbucket.org/",
RepoPath: "company/project.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "non-github_git-user_ssh",
input: "ssh://git@bitbucket.org/company/project.git//path?ref=branch",
cloneSpec: "ssh://git@bitbucket.org/company/project.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "ssh://git@bitbucket.org/",
RepoPath: "company/project.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "_git host delimiter in non-github url",
input: "https://itfs.mycompany.com/collection/project/_git/somerepos",
cloneSpec: "https://itfs.mycompany.com/collection/project/_git/somerepos",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://itfs.mycompany.com/",
RepoPath: "collection/project/_git/somerepos",
},
},
{
name: "_git host delimiter in non-github url with params",
input: "https://itfs.mycompany.com/collection/project/_git/somerepos?version=v1.0.0",
cloneSpec: "https://itfs.mycompany.com/collection/project/_git/somerepos",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://itfs.mycompany.com/",
RepoPath: "collection/project/_git/somerepos",
Ref: "v1.0.0",
},
},
{
name: "_git host delimiter in non-github url with kust root path and params",
input: "https://itfs.mycompany.com/collection/project/_git/somerepos/somedir?version=v1.0.0",
cloneSpec: "https://itfs.mycompany.com/collection/project/_git/somerepos",
absPath: notCloned.Join("somedir"),
repoSpec: RepoSpec{
Host: "https://itfs.mycompany.com/",
RepoPath: "collection/project/_git/somerepos",
KustRootPath: "somedir",
Ref: "v1.0.0",
},
},
{
name: "_git host delimiter in non-github url with no kust root path",
input: "git::https://itfs.mycompany.com/collection/project/_git/somerepos",
cloneSpec: "https://itfs.mycompany.com/collection/project/_git/somerepos",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://itfs.mycompany.com/",
RepoPath: "collection/project/_git/somerepos",
},
},
{
name: "https bitbucket url with git suffix",
input: "https://bitbucket.example.com/scm/project/repository.git",
cloneSpec: "https://bitbucket.example.com/scm/project/repository.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://bitbucket.example.com/",
RepoPath: "scm/project/repository.git",
},
},
{
name: "ssh aws code commit url",
input: "ssh://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo/somepath",
cloneSpec: "ssh://git-codecommit.us-east-2.amazonaws.com/someorg/somerepo",
absPath: notCloned.Join("somepath"),
repoSpec: RepoSpec{
Host: "ssh://git-codecommit.us-east-2.amazonaws.com/",
RepoPath: "someorg/somerepo",
KustRootPath: "somepath",
},
},
{
name: "scp Github with slash fixed to colon",
input: "git@github.com/someorg/somerepo/somepath",
cloneSpec: "git@github.com:someorg/somerepo",
absPath: notCloned.Join("somepath"),
repoSpec: RepoSpec{
Host: "git@github.com:",
RepoPath: "someorg/somerepo",
KustRootPath: "somepath",
},
},
{
name: "https Github with double slash path delimiter and params",
input: "https://github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?ref=v1.0.6",
cloneSpec: "https://github.com/kubernetes-sigs/kustomize",
absPath: notCloned.Join("/examples/multibases/dev"),
repoSpec: RepoSpec{
Host: "https://github.com/",
RepoPath: "kubernetes-sigs/kustomize",
KustRootPath: "examples/multibases/dev/",
Ref: "v1.0.6",
},
},
{
name: "file protocol with git-suffixed repo path and params",
input: "file://a/b/c/someRepo.git/somepath?ref=someBranch",
cloneSpec: "file://a/b/c/someRepo.git",
absPath: notCloned.Join("somepath"),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "a/b/c/someRepo.git",
KustRootPath: "somepath",
Ref: "someBranch",
},
},
{
name: "file protocol with two slashes, with kust root path and params",
input: "file://a/b/c/someRepo//somepath?ref=someBranch",
cloneSpec: "file://a/b/c/someRepo",
absPath: notCloned.Join("somepath"),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "a/b/c/someRepo",
KustRootPath: "somepath",
Ref: "someBranch",
},
},
{
name: "file protocol with two slashes, with ref and no kust root path",
input: "file://a/b/c/someRepo?ref=someBranch",
cloneSpec: "file://a/b/c/someRepo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "a/b/c/someRepo",
Ref: "someBranch",
},
},
{
name: "file protocol with three slashes, with ref and no kust root path",
input: "file:///a/b/c/someRepo?ref=someBranch",
cloneSpec: "file:///a/b/c/someRepo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "/a/b/c/someRepo",
Ref: "someBranch",
},
},
{
name: "ssh Github with double-slashed path delimiter and params",
input: "ssh://git@github.com/kubernetes-sigs/kustomize//examples/multibases/dev?ref=v1.0.6",
cloneSpec: "git@github.com:kubernetes-sigs/kustomize",
absPath: notCloned.Join("examples/multibases/dev"),
repoSpec: RepoSpec{
Host: "git@github.com:",
RepoPath: "kubernetes-sigs/kustomize",
KustRootPath: "examples/multibases/dev",
Ref: "v1.0.6",
},
},
{
name: "file protocol with three slashes, no kust root path or params",
input: "file:///a/b/c/someRepo",
cloneSpec: "file:///a/b/c/someRepo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "/a/b/c/someRepo",
},
},
{
name: "file protocol with three slashes, no repo or kust root path or params",
input: "file:///",
cloneSpec: "file:///",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "/",
},
},
{
name: "arbitrary https host with double-slash path delimiter",
input: "https://example.org/path/to/repo//examples/multibases/dev",
cloneSpec: "https://example.org/path/to/repo",
absPath: notCloned.Join("/examples/multibases/dev"),
repoSpec: RepoSpec{
Host: "https://example.org/",
RepoPath: "path/to/repo",
KustRootPath: "examples/multibases/dev",
},
},
{
name: "arbitrary https host with .git repo suffix",
input: "https://example.org/path/to/repo.git/examples/multibases/dev",
cloneSpec: "https://example.org/path/to/repo.git",
absPath: notCloned.Join("/examples/multibases/dev"),
repoSpec: RepoSpec{
Host: "https://example.org/",
RepoPath: "path/to/repo.git",
KustRootPath: "examples/multibases/dev",
},
},
{
name: "arbitrary ssh host with double-slash path delimiter",
input: "ssh://alice@example.com/path/to/repo//examples/multibases/dev",
cloneSpec: "ssh://alice@example.com/path/to/repo",
absPath: notCloned.Join("/examples/multibases/dev"),
repoSpec: RepoSpec{
Host: "ssh://alice@example.com/",
RepoPath: "path/to/repo",
KustRootPath: "examples/multibases/dev",
},
},
{
name: "query_slash",
input: "https://authority/org/repo?ref=group/version",
cloneSpec: "https://authority/org/repo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://authority/",
RepoPath: "org/repo",
Ref: "group/version",
},
},
{
name: "query_git_delimiter",
input: "https://authority/org/repo/?ref=includes_git/for_some_reason",
cloneSpec: "https://authority/org/repo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://authority/",
RepoPath: "org/repo",
Ref: "includes_git/for_some_reason",
},
},
{
name: "query_git_suffix",
input: "https://authority/org/repo/?ref=includes.git/for_some_reason",
cloneSpec: "https://authority/org/repo",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://authority/",
RepoPath: "org/repo",
Ref: "includes.git/for_some_reason",
},
},
{
name: "non_parsable_path",
input: "https://authority/org/repo/%-invalid-uri-so-not-parsable-by-net/url.Parse",
cloneSpec: "https://authority/org/repo",
absPath: notCloned.Join("%-invalid-uri-so-not-parsable-by-net/url.Parse"),
repoSpec: RepoSpec{
Host: "https://authority/",
RepoPath: "org/repo",
KustRootPath: "%-invalid-uri-so-not-parsable-by-net/url.Parse",
},
},
{
name: "non-git username with non-github host",
input: "ssh://myusername@bitbucket.org/ourteamname/ourrepositoryname.git//path?ref=branch",
cloneSpec: "ssh://myusername@bitbucket.org/ourteamname/ourrepositoryname.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "ssh://myusername@bitbucket.org/",
RepoPath: "ourteamname/ourrepositoryname.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "username-like filepath with file protocol",
input: "file://git@home/path/to/repository.git//path?ref=branch",
cloneSpec: "file://git@home/path/to/repository.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "file://",
RepoPath: "git@home/path/to/repository.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "username with http protocol (invalid but currently passed through to git)",
input: "http://git@home.com/path/to/repository.git//path?ref=branch",
cloneSpec: "http://git@home.com/path/to/repository.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "http://git@home.com/",
RepoPath: "path/to/repository.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "username with https protocol (invalid but currently passed through to git)",
input: "https://git@home.com/path/to/repository.git//path?ref=branch",
cloneSpec: "https://git@home.com/path/to/repository.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "https://git@home.com/",
RepoPath: "path/to/repository.git",
KustRootPath: "path",
Ref: "branch",
},
},
{
name: "complex github ssh url from docs",
input: "ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git",
cloneSpec: "ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "ssh://git@ssh.github.com:443/",
RepoPath: "YOUR-USERNAME/YOUR-REPOSITORY.git",
KustRootPath: "",
},
},
{
name: "colon behind slash not scp delimiter",
input: "git@gitlab.com/user:name/YOUR-REPOSITORY.git/path",
cloneSpec: "git@gitlab.com/user:name/YOUR-REPOSITORY.git",
absPath: notCloned.Join("path"),
repoSpec: RepoSpec{
Host: "git@gitlab.com/",
RepoPath: "user:name/YOUR-REPOSITORY.git",
KustRootPath: "path",
},
},
{
name: "gitlab URLs with explicit git suffix",
input: "git@gitlab.com:gitlab-tests/sample-project.git",
cloneSpec: "git@gitlab.com:gitlab-tests/sample-project.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "git@gitlab.com:",
RepoPath: "gitlab-tests/sample-project.git",
},
},
{
name: "gitlab URLs without explicit git suffix",
input: "git@gitlab.com:gitlab-tests/sample-project",
cloneSpec: "git@gitlab.com:gitlab-tests/sample-project",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "git@gitlab.com:",
RepoPath: "gitlab-tests/sample-project",
},
},
{
name: "azure host with _git and // path separator",
input: "https://username@dev.azure.com/org/project/_git/repo//path/to/kustomization/root",
cloneSpec: "https://username@dev.azure.com/org/project/_git/repo",
absPath: notCloned.Join("path/to/kustomization/root"),
repoSpec: RepoSpec{
Host: "https://username@dev.azure.com/",
RepoPath: "org/project/_git/repo",
KustRootPath: "path/to/kustomization/root",
},
},
{
name: "legacy format azure host with _git",
input: "https://org.visualstudio.com/project/_git/repo/path/to/kustomization/root",
cloneSpec: "https://org.visualstudio.com/project/_git/repo",
absPath: notCloned.Join("path/to/kustomization/root"),
repoSpec: RepoSpec{
Host: "https://org.visualstudio.com/",
RepoPath: "project/_git/repo",
KustRootPath: "path/to/kustomization/root",
},
},
{
name: "ssh on github with custom username for custom ssh certificate authority",
input: "ssh://org-12345@github.com/kubernetes-sigs/kustomize",
cloneSpec: "org-12345@github.com:kubernetes-sigs/kustomize",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "org-12345@github.com:",
RepoPath: "kubernetes-sigs/kustomize",
},
},
{
name: "scp on github with custom username for custom ssh certificate authority",
input: "org-12345@github.com/kubernetes-sigs/kustomize",
cloneSpec: "org-12345@github.com:kubernetes-sigs/kustomize",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "org-12345@github.com:",
RepoPath: "kubernetes-sigs/kustomize",
},
},
{
name: "scp format gist url",
input: "git@gist.github.com:bc7947cb727d7f9217e7862d961a1ffd.git",
cloneSpec: "git@gist.github.com:bc7947cb727d7f9217e7862d961a1ffd.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "git@gist.github.com:",
RepoPath: "bc7947cb727d7f9217e7862d961a1ffd.git",
},
},
{
name: "https gist url",
input: "https://gist.github.com/bc7947cb727d7f9217e7862d961a1ffd.git",
cloneSpec: "https://gist.github.com/bc7947cb727d7f9217e7862d961a1ffd.git",
absPath: notCloned.String(),
repoSpec: RepoSpec{
Host: "https://gist.github.com/",
RepoPath: "bc7947cb727d7f9217e7862d961a1ffd.git",
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
rs, err := NewRepoSpecFromURL(tc.input)
require.NoError(t, err)
assert.Equal(t, tc.cloneSpec, rs.CloneSpec(), "cloneSpec mismatch")
assert.Equal(t, tc.absPath, rs.AbsPath(), "absPath mismatch")
// some values have defaults. Clear them here so test cases remain compact.
// This means submodules and timeout cannot be tested here. That's fine since
// they are tested in TestParseQuery.
rs.raw = ""
rs.Dir = ""
rs.Submodules = false
rs.Timeout = 0
assert.Equal(t, &tc.repoSpec, rs) //nolint:gosec
})
}
}
func TestNewRepoSpecFromURL_DefaultQueryParams(t *testing.T) {
repoSpec, err := NewRepoSpecFromURL("https://github.com/org/repo")
require.NoError(t, err)
require.Equal(t, defaultSubmodules, repoSpec.Submodules)
require.Equal(t, defaultTimeout, repoSpec.Timeout)
}
func TestParseQuery(t *testing.T) {
testcases := []struct {
name string
input string
ref string
submodules bool
timeout time.Duration
}{
{
name: "empty",
input: "",
ref: "",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "ref",
input: "ref=v1.0.0",
ref: "v1.0.0",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "ref_slash",
input: "ref=kustomize/v4.5.7",
ref: "kustomize/v4.5.7",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "version",
input: "version=master",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "ref_and_version",
// A ref value takes precedence over a version value.
input: "version=master&ref=v1.0.0",
ref: "v1.0.0",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "empty_submodules",
// Empty submodules value uses default.
input: "version=master&submodules=",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "bad_submodules",
// Malformed submodules value uses default.
input: "version=master&submodules=maybe",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "submodules_true",
input: "version=master&submodules=true",
ref: "master",
submodules: true,
timeout: defaultTimeout,
},
{
name: "submodules_false",
input: "version=master&submodules=false",
ref: "master",
submodules: false,
timeout: defaultTimeout,
},
{
name: "empty_timeout",
// Empty timeout value uses default.
input: "version=master&timeout=",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "bad_timeout",
// Malformed timeout value uses default.
input: "version=master&timeout=jiffy",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "zero_timeout",
// Zero timeout value uses default.
input: "version=master&timeout=0",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "zero_unit_timeout",
input: "version=master&timeout=0s",
ref: "master",
submodules: defaultSubmodules,
timeout: defaultTimeout,
},
{
name: "timeout",
input: "version=master&timeout=61",
ref: "master",
submodules: defaultSubmodules,
timeout: 61 * time.Second,
},
{
name: "timeout_unit",
input: "version=master&timeout=1m1s",
ref: "master",
submodules: defaultSubmodules,
timeout: 61 * time.Second,
},
{
name: "all",
input: "version=master&submodules=false&timeout=1m1s",
ref: "master",
submodules: false,
timeout: 61 * time.Second,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
ref, timeout, submodules := parseQuery(tc.input)
assert.Equal(t, tc.ref, ref, "ref mismatch")
assert.Equal(t, tc.timeout, timeout, "timeout mismatch")
assert.Equal(t, tc.submodules, submodules, "submodules mismatch")
})
}
}
|