github.com/argoproj/argo-cd/v3@v3.2.1/test/e2e/repo_management_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	repositorypkg "github.com/argoproj/argo-cd/v3/pkg/apiclient/repository"
    10  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    12  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/app"
    13  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/repos"
    14  	"github.com/argoproj/argo-cd/v3/util/errors"
    15  	utilio "github.com/argoproj/argo-cd/v3/util/io"
    16  )
    17  
    18  func TestAddRemovePublicRepo(t *testing.T) {
    19  	app.Given(t).And(func() {
    20  		repoURL := fixture.RepoURL(fixture.RepoURLTypeFile)
    21  		_, err := fixture.RunCli("repo", "add", repoURL)
    22  		require.NoError(t, err)
    23  
    24  		conn, repoClient, err := fixture.ArgoCDClientset.NewRepoClient()
    25  		require.NoError(t, err)
    26  		defer utilio.Close(conn)
    27  
    28  		repo, err := repoClient.ListRepositories(t.Context(), &repositorypkg.RepoQuery{})
    29  
    30  		require.NoError(t, err)
    31  		exists := false
    32  		for i := range repo.Items {
    33  			if repo.Items[i].Repo == repoURL {
    34  				exists = true
    35  				break
    36  			}
    37  		}
    38  		assert.True(t, exists)
    39  
    40  		_, err = fixture.RunCli("repo", "rm", repoURL)
    41  		require.NoError(t, err)
    42  
    43  		repo, err = repoClient.ListRepositories(t.Context(), &repositorypkg.RepoQuery{})
    44  		require.NoError(t, err)
    45  		exists = false
    46  		for i := range repo.Items {
    47  			if repo.Items[i].Repo == repoURL {
    48  				exists = true
    49  				break
    50  			}
    51  		}
    52  		assert.False(t, exists)
    53  	})
    54  }
    55  
    56  func TestGetRepoWithInheritedCreds(t *testing.T) {
    57  	app.Given(t).And(func() {
    58  		// create repo credentials
    59  		errors.NewHandler(t).FailOnErr(fixture.RunCli("repocreds", "add", fixture.RepoURL(fixture.RepoURLTypeHTTPSOrg), "--github-app-id", fixture.GithubAppID, "--github-app-installation-id", fixture.GithubAppInstallationID, "--github-app-private-key-path", repos.CertKeyPath(t)))
    60  
    61  		repoURL := fixture.RepoURL(fixture.RepoURLTypeHTTPS)
    62  
    63  		// Hack: First we need to create repo with valid credentials
    64  		errors.NewHandler(t).FailOnErr(fixture.RunCli("repo", "add", repoURL, "--username", fixture.GitUsername, "--password", fixture.GitPassword, "--insecure-skip-server-verification"))
    65  
    66  		// Then, we remove username/password so that the repo inherits the credentials from our repocreds
    67  		conn, repoClient, err := fixture.ArgoCDClientset.NewRepoClient()
    68  		require.NoError(t, err)
    69  		defer utilio.Close(conn)
    70  
    71  		_, err = repoClient.UpdateRepository(t.Context(), &repositorypkg.RepoUpdateRequest{
    72  			Repo: &v1alpha1.Repository{
    73  				Repo: repoURL,
    74  			},
    75  		})
    76  		require.NoError(t, err)
    77  
    78  		// CLI output should indicate that repo has inherited credentials
    79  		out, err := fixture.RunCli("repo", "get", repoURL)
    80  		require.NoError(t, err)
    81  		assert.Contains(t, out, "inherited")
    82  
    83  		_, err = fixture.RunCli("repo", "rm", repoURL)
    84  		require.NoError(t, err)
    85  	})
    86  }
    87  
    88  func TestUpsertExistingRepo(t *testing.T) {
    89  	app.Given(t).And(func() {
    90  		repoURL := fixture.RepoURL(fixture.RepoURLTypeFile)
    91  		_, err := fixture.RunCli("repo", "add", repoURL)
    92  		require.NoError(t, err)
    93  
    94  		_, err = fixture.RunCli("repo", "add", repoURL, "--username", fixture.GitUsername, "--password", fixture.GitPassword)
    95  		require.Error(t, err)
    96  
    97  		_, err = fixture.RunCli("repo", "add", repoURL, "--upsert", "--username", fixture.GitUsername, "--password", fixture.GitPassword)
    98  		require.NoError(t, err)
    99  	})
   100  }
   101  
   102  func TestAddRemoveHelmRepo(t *testing.T) {
   103  	app.Given(t).CustomCACertAdded().And(func() {
   104  		_, err := fixture.RunCli("repo", "add", fixture.RepoURL(fixture.RepoURLTypeHelm),
   105  			"--name", "testrepo",
   106  			"--type", "helm",
   107  			"--username", fixture.GitUsername,
   108  			"--password", fixture.GitPassword,
   109  			"--tls-client-cert-path", repos.CertPath(t),
   110  			"--tls-client-cert-key-path", repos.CertKeyPath(t))
   111  		require.NoError(t, err)
   112  
   113  		conn, repoClient, err := fixture.ArgoCDClientset.NewRepoClient()
   114  		require.NoError(t, err)
   115  		defer utilio.Close(conn)
   116  
   117  		repo, err := repoClient.ListRepositories(t.Context(), &repositorypkg.RepoQuery{})
   118  
   119  		require.NoError(t, err)
   120  		exists := false
   121  		for i := range repo.Items {
   122  			if repo.Items[i].Repo == fixture.RepoURL(fixture.RepoURLTypeHelm) {
   123  				exists = true
   124  				break
   125  			}
   126  		}
   127  		assert.True(t, exists)
   128  
   129  		_, err = fixture.RunCli("repo", "rm", fixture.RepoURL(fixture.RepoURLTypeHelm))
   130  		require.NoError(t, err)
   131  
   132  		repo, err = repoClient.ListRepositories(t.Context(), &repositorypkg.RepoQuery{})
   133  		require.NoError(t, err)
   134  		exists = false
   135  		for i := range repo.Items {
   136  			if repo.Items[i].Repo == fixture.RepoURL(fixture.RepoURLTypeHelm) {
   137  				exists = true
   138  				break
   139  			}
   140  		}
   141  		assert.False(t, exists)
   142  	})
   143  }
   144  
   145  func TestAddHelmRepoInsecureSkipVerify(t *testing.T) {
   146  	app.Given(t).And(func() {
   147  		_, err := fixture.RunCli("repo", "add", fixture.RepoURL(fixture.RepoURLTypeHelm),
   148  			"--name", "testrepo",
   149  			"--type", "helm",
   150  			"--username", fixture.GitUsername,
   151  			"--password", fixture.GitPassword,
   152  			"--insecure-skip-server-verification",
   153  			"--tls-client-cert-path", repos.CertPath(t),
   154  			"--tls-client-cert-key-path", repos.CertKeyPath(t))
   155  
   156  		require.NoError(t, err)
   157  
   158  		conn, repoClient, err := fixture.ArgoCDClientset.NewRepoClient()
   159  		require.NoError(t, err)
   160  
   161  		defer utilio.Close(conn)
   162  
   163  		repo, err := repoClient.ListRepositories(t.Context(), &repositorypkg.RepoQuery{})
   164  
   165  		require.NoError(t, err)
   166  
   167  		exists := false
   168  		for i := range repo.Items {
   169  			if repo.Items[i].Repo == fixture.RepoURL(fixture.RepoURLTypeHelm) {
   170  				exists = true
   171  				break
   172  			}
   173  		}
   174  		assert.True(t, exists)
   175  	})
   176  }
   177  
   178  func TestFailOnPrivateRepoCreationWithPasswordAndBearerToken(t *testing.T) {
   179  	app.Given(t).And(func() {
   180  		repoURL := fixture.RepoURL(fixture.RepoURLTypeFile)
   181  		_, err := fixture.RunCli("repo", "add", repoURL, "--password", fixture.GitPassword, "--bearer-token", fixture.GitBearerToken)
   182  		require.ErrorContains(t, err, "only --bearer-token or --password is allowed, not both")
   183  	})
   184  }
   185  
   186  func TestFailOnCreatePrivateNonHTTPSRepoWithBearerToken(t *testing.T) {
   187  	app.Given(t).And(func() {
   188  		repoURL := fixture.RepoURL(fixture.RepoURLTypeFile)
   189  		_, err := fixture.RunCli("repo", "add", repoURL, "--bearer-token", fixture.GitBearerToken)
   190  		require.ErrorContains(t, err, "--bearer-token is only supported for HTTPS repositories")
   191  	})
   192  }
   193  
   194  func TestFailOnCreatePrivateNonGitRepoWithBearerToken(t *testing.T) {
   195  	app.Given(t).And(func() {
   196  		repoURL := fixture.RepoURL(fixture.RepoURLTypeHelm)
   197  		_, err := fixture.RunCli("repo", "add", repoURL, "--bearer-token", fixture.GitBearerToken,
   198  			"--insecure-skip-server-verification",
   199  			"--tls-client-cert-path", repos.CertPath(t),
   200  			"--tls-client-cert-key-path", repos.CertKeyPath(t),
   201  			"--name", "testrepo",
   202  			"--type", "helm")
   203  		require.ErrorContains(t, err, "--bearer-token is only supported for Git repositories")
   204  	})
   205  }