github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/transform/git_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package transform provides helper functions to transform URLs to airgap equivalents
     5  package transform
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var gitURLs = []string{
    14  	// Normal git repos and references for pushing/pulling
    15  	"https://repo1.dso.mil/platform-one/big-bang/apps/security-tools/twistlock.git",
    16  	"https://github.com/Racer159/jackal.git",
    17  	"https://ghcr.io/stefanprodan/podinfo_fasd-123.git",
    18  	"git://k3d-cluster.localhost/Racer159/jackal-agent",
    19  	"http://localhost:5000/some-cool-repo",
    20  	"ssh://ghcr.io/stefanprodan/podinfo@6.0.0",
    21  	"https://stefanprodan/podinfo.git@adf0fasd10.1.223124123123-asdf",
    22  	"https://repo1.dso.mil/platform-one/big-bang/apps/security-tools/twistlock.git@0.0.9-bb.0",
    23  	"file:///srv/git/stefanprodan/podinfo@adf0fasd10.1.223124123123-asdf",
    24  	"https://me0515@dev.azure.com/me0515/jackal-public-test/_git/jackal-public-test",
    25  	"https://me0515@dev.azure.com/me0515/jackal-public-test/_git/jackal-public-test@524980951ff16e19dc25232e9aea8fd693989ba6",
    26  	"https://github.com/Racer159/jackal.helm.git",
    27  	"https://github.com/Racer159/jackal.git@refs/tags/v0.16.0",
    28  	"https://github.com/DoD-Platform-One/big-bang.git@refs/heads/release-1.54.x",
    29  	"https://github.com/prometheus-community/helm-charts.git@kube-prometheus-stack-47.3.0",
    30  	"https://github.com/prometheus-community/",
    31  	"https://github.com/",
    32  
    33  	// Smart Git Protocol URLs for proxying (https://www.git-scm.com/docs/http-protocol)
    34  	"https://github.com/Racer159/jackal.helm.git/info/refs",
    35  	"https://github.com/Racer159/jackal.helm.git/info/refs?service=git-upload-pack",
    36  	"https://github.com/Racer159/jackal.helm.git/info/refs?service=git-receive-pack",
    37  	"https://github.com/Racer159/jackal.helm.git/git-upload-pack",
    38  	"https://github.com/Racer159/jackal.helm.git/git-receive-pack",
    39  }
    40  
    41  var badGitURLs = []string{
    42  	"i am not a url at all",
    43  	"C:\\Users\\jackal",
    44  }
    45  
    46  func TestMutateGitURLsInText(t *testing.T) {
    47  	dummyLogger := func(_ string, _ ...any) {}
    48  	originalText := `
    49  	# Here we handle invalid URLs (see below comment)
    50  	# We transform https://*/*.git URLs
    51  	https://github.com/Racer159/jackal.git
    52  	# Even URLs with things on either side
    53  	stuff https://github.com/Racer159/jackal.git andthings
    54  	# Including ssh://*/*.git URLs
    55  	ssh://git@github.com/Racer159/jackal.git
    56  	# Or non .git URLs
    57  	https://www.defenseunicorns.com/
    58  	`
    59  
    60  	expectedText := `
    61  	# Here we handle invalid URLs (see below comment)
    62  	# We transform https://*/*.git URLs
    63  	https://gitlab.com/repo-owner/jackal-1211668992.git
    64  	# Even URLs with things on either side
    65  	stuff https://gitlab.com/repo-owner/jackal-1211668992.git andthings
    66  	# Including ssh://*/*.git URLs
    67  	https://gitlab.com/repo-owner/jackal-2566185087.git
    68  	# Or non .git URLs
    69  	https://www.defenseunicorns.com/
    70  	`
    71  
    72  	resultingText := MutateGitURLsInText(dummyLogger, "https://gitlab.com", originalText, "repo-owner")
    73  	require.Equal(t, expectedText, resultingText)
    74  }
    75  
    76  func TestGitURLSplitRef(t *testing.T) {
    77  	var expectedResult = [][]string{
    78  		// Normal git repos and references for pushing/pulling
    79  		{"https://repo1.dso.mil/platform-one/big-bang/apps/security-tools/twistlock.git", ""},
    80  		{"https://github.com/Racer159/jackal.git", ""},
    81  		{"https://ghcr.io/stefanprodan/podinfo_fasd-123.git", ""},
    82  		{"git://k3d-cluster.localhost/Racer159/jackal-agent", ""},
    83  		{"http://localhost:5000/some-cool-repo", ""},
    84  		{"ssh://ghcr.io/stefanprodan/podinfo", "6.0.0"},
    85  		{"https://stefanprodan/podinfo.git", "adf0fasd10.1.223124123123-asdf"},
    86  		{"https://repo1.dso.mil/platform-one/big-bang/apps/security-tools/twistlock.git", "0.0.9-bb.0"},
    87  		{"file:///srv/git/stefanprodan/podinfo", "adf0fasd10.1.223124123123-asdf"},
    88  		{"https://me0515@dev.azure.com/me0515/jackal-public-test/_git/jackal-public-test", ""},
    89  		{"https://me0515@dev.azure.com/me0515/jackal-public-test/_git/jackal-public-test", "524980951ff16e19dc25232e9aea8fd693989ba6"},
    90  		{"https://github.com/Racer159/jackal.helm.git", ""},
    91  		{"https://github.com/Racer159/jackal.git", "refs/tags/v0.16.0"},
    92  		{"https://github.com/DoD-Platform-One/big-bang.git", "refs/heads/release-1.54.x"},
    93  		{"https://github.com/prometheus-community/helm-charts.git", "kube-prometheus-stack-47.3.0"},
    94  		{"https://github.com/prometheus-community", ""},
    95  		{"https://github.com/", ""},
    96  
    97  		// Smart Git Protocol URLs for proxying (https://www.git-scm.com/docs/http-protocol)
    98  		{"https://github.com/Racer159/jackal.helm.git", ""},
    99  		{"https://github.com/Racer159/jackal.helm.git", ""},
   100  		{"https://github.com/Racer159/jackal.helm.git", ""},
   101  		{"https://github.com/Racer159/jackal.helm.git", ""},
   102  		{"https://github.com/Racer159/jackal.helm.git", ""},
   103  	}
   104  
   105  	for idx, url := range gitURLs {
   106  		gitURLNoRef, refPlain, err := GitURLSplitRef(url)
   107  		require.NoError(t, err)
   108  		require.Equal(t, expectedResult[idx][0], gitURLNoRef)
   109  		require.Equal(t, expectedResult[idx][1], refPlain)
   110  	}
   111  
   112  	for _, url := range badGitURLs {
   113  		_, _, err := GitURLSplitRef(url)
   114  		require.Error(t, err)
   115  	}
   116  }
   117  
   118  func TestGitURLtoFolderName(t *testing.T) {
   119  	var expectedResult = []string{
   120  		// Normal git repos and references for pushing/pulling
   121  		"twistlock-1590638614",
   122  		"jackal-3863619701",
   123  		"podinfo_fasd-123-1478387306",
   124  		"jackal-agent-802453811",
   125  		"some-cool-repo-1916670310",
   126  		"podinfo-1350532569",
   127  		"podinfo-1853010387",
   128  		"twistlock-1920149257",
   129  		"podinfo-122075437",
   130  		"jackal-public-test-612413317",
   131  		"jackal-public-test-634307705",
   132  		"jackal.helm-2570741950",
   133  		"jackal-2175050463",
   134  		"big-bang-2705706079",
   135  		"helm-charts-1319967699",
   136  		"prometheus-community-3453166319",
   137  		"-1276058275",
   138  
   139  		// Smart Git Protocol URLs for proxying (https://www.git-scm.com/docs/http-protocol)
   140  		"jackal.helm-2570741950",
   141  		"jackal.helm-2570741950",
   142  		"jackal.helm-2570741950",
   143  		"jackal.helm-2570741950",
   144  		"jackal.helm-2570741950",
   145  	}
   146  
   147  	for idx, url := range gitURLs {
   148  		repoFolder, err := GitURLtoFolderName(url)
   149  		require.NoError(t, err)
   150  		require.Equal(t, expectedResult[idx], repoFolder)
   151  	}
   152  
   153  	for _, url := range badGitURLs {
   154  		_, err := GitURLtoFolderName(url)
   155  		require.Error(t, err)
   156  	}
   157  }
   158  
   159  func TestGitURLtoRepoName(t *testing.T) {
   160  	var expectedResult = []string{
   161  		// Normal git repos and references for pushing/pulling
   162  		"twistlock-97328248",
   163  		"jackal-1211668992",
   164  		"podinfo_fasd-123-84577122",
   165  		"jackal-agent-3633494462",
   166  		"some-cool-repo-926913879",
   167  		"podinfo-2985051089",
   168  		"podinfo-2197246515",
   169  		"twistlock-97328248",
   170  		"podinfo-1175499642",
   171  		"jackal-public-test-2170732467",
   172  		"jackal-public-test-2170732467",
   173  		"jackal.helm-842267124",
   174  		"jackal-1211668992",
   175  		"big-bang-2366614037",
   176  		"helm-charts-3648076006",
   177  		"prometheus-community-2749132599",
   178  		"-98306241",
   179  
   180  		// Smart Git Protocol URLs for proxying (https://www.git-scm.com/docs/http-protocol)
   181  		"jackal.helm-842267124",
   182  		"jackal.helm-842267124",
   183  		"jackal.helm-842267124",
   184  		"jackal.helm-842267124",
   185  		"jackal.helm-842267124",
   186  	}
   187  
   188  	for idx, url := range gitURLs {
   189  		repoName, err := GitURLtoRepoName(url)
   190  		require.NoError(t, err)
   191  		require.Equal(t, expectedResult[idx], repoName)
   192  	}
   193  
   194  	for _, url := range badGitURLs {
   195  		_, err := GitURLtoRepoName(url)
   196  		require.Error(t, err)
   197  	}
   198  }
   199  
   200  func TestGitURL(t *testing.T) {
   201  	var expectedResult = []string{
   202  		// Normal git repos and references for pushing/pulling
   203  		"https://gitlab.com/repo-owner/twistlock-97328248.git",
   204  		"https://gitlab.com/repo-owner/jackal-1211668992.git",
   205  		"https://gitlab.com/repo-owner/podinfo_fasd-123-84577122.git",
   206  		"https://gitlab.com/repo-owner/jackal-agent-3633494462",
   207  		"https://gitlab.com/repo-owner/some-cool-repo-926913879",
   208  		"https://gitlab.com/repo-owner/podinfo-2985051089",
   209  		"https://gitlab.com/repo-owner/podinfo-2197246515.git",
   210  		"https://gitlab.com/repo-owner/twistlock-97328248.git",
   211  		"https://gitlab.com/repo-owner/podinfo-1175499642",
   212  		"https://gitlab.com/repo-owner/jackal-public-test-2170732467",
   213  		"https://gitlab.com/repo-owner/jackal-public-test-2170732467",
   214  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git",
   215  		"https://gitlab.com/repo-owner/jackal-1211668992.git",
   216  		"https://gitlab.com/repo-owner/big-bang-2366614037.git",
   217  		"https://gitlab.com/repo-owner/helm-charts-3648076006.git",
   218  		"https://gitlab.com/repo-owner/prometheus-community-2749132599",
   219  		"https://gitlab.com/repo-owner/-98306241",
   220  
   221  		// Smart Git Protocol URLs for proxying (https://www.git-scm.com/docs/http-protocol)
   222  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git/info/refs",
   223  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git/info/refs?service=git-upload-pack",
   224  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git/info/refs?service=git-receive-pack",
   225  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git/git-upload-pack",
   226  		"https://gitlab.com/repo-owner/jackal.helm-842267124.git/git-receive-pack",
   227  	}
   228  
   229  	for idx, url := range gitURLs {
   230  		repoURL, err := GitURL("https://gitlab.com", url, "repo-owner")
   231  		require.NoError(t, err)
   232  		require.Equal(t, expectedResult[idx], repoURL.String())
   233  	}
   234  
   235  	for _, url := range badGitURLs {
   236  		_, err := GitURL("https://gitlab.com", url, "repo-owner")
   237  		require.Error(t, err)
   238  	}
   239  }