github.com/argoproj/argo-cd/v2@v2.10.9/test/e2e/fixture/repos/repos.go (about)

     1  package repos
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
     9  	"github.com/argoproj/argo-cd/v2/util/errors"
    10  )
    11  
    12  var (
    13  	CertPath    = mustToAbsPath("../fixture/certs/argocd-test-client.crt")
    14  	CertKeyPath = mustToAbsPath("../fixture/certs/argocd-test-client.key")
    15  )
    16  
    17  func mustToAbsPath(relativePath string) string {
    18  	res, err := filepath.Abs(relativePath)
    19  	errors.CheckError(err)
    20  	return res
    21  }
    22  
    23  // sets the current repo as the default SSH test repo
    24  func AddSSHRepo(insecure bool, credentials bool, repoURLType fixture.RepoURLType) {
    25  	keyPath, err := filepath.Abs("../fixture/testrepos/id_rsa")
    26  	errors.CheckError(err)
    27  	args := []string{"repo", "add", fixture.RepoURL(repoURLType)}
    28  	if credentials {
    29  		args = append(args, "--ssh-private-key-path", keyPath)
    30  	}
    31  	if insecure {
    32  		args = append(args, "--insecure-ignore-host-key")
    33  	}
    34  	errors.FailOnErr(fixture.RunCli(args...))
    35  }
    36  
    37  // sets the current repo as the default HTTPS test repo
    38  func AddHTTPSRepo(insecure bool, credentials bool, repoURLType fixture.RepoURLType) {
    39  	// This construct is somewhat necessary to satisfy the compiler
    40  	args := []string{"repo", "add", fixture.RepoURL(repoURLType)}
    41  	if credentials {
    42  		args = append(args, "--username", fixture.GitUsername, "--password", fixture.GitPassword)
    43  	}
    44  	if insecure {
    45  		args = append(args, "--insecure-skip-server-verification")
    46  	}
    47  	errors.FailOnErr(fixture.RunCli(args...))
    48  }
    49  
    50  // sets a HTTPS repo using TLS client certificate authentication
    51  func AddHTTPSRepoClientCert(insecure bool) {
    52  	args := []string{
    53  		"repo",
    54  		"add",
    55  		fixture.RepoURL(fixture.RepoURLTypeHTTPSClientCert),
    56  		"--username", fixture.GitUsername,
    57  		"--password", fixture.GitPassword,
    58  		"--tls-client-cert-path", CertPath,
    59  		"--tls-client-cert-key-path", CertKeyPath,
    60  	}
    61  	if insecure {
    62  		args = append(args, "--insecure-skip-server-verification")
    63  	}
    64  	errors.FailOnErr(fixture.RunCli(args...))
    65  }
    66  
    67  func AddHelmRepo(name string) {
    68  	args := []string{
    69  		"repo",
    70  		"add",
    71  		fixture.RepoURL(fixture.RepoURLTypeHelm),
    72  		"--username", fixture.GitUsername,
    73  		"--password", fixture.GitPassword,
    74  		"--tls-client-cert-path", CertPath,
    75  		"--tls-client-cert-key-path", CertKeyPath,
    76  		"--type", "helm",
    77  		"--name", name,
    78  	}
    79  	errors.FailOnErr(fixture.RunCli(args...))
    80  }
    81  
    82  func AddHelmOCIRepo(name string) {
    83  	args := []string{
    84  		"repo",
    85  		"add",
    86  		fixture.HelmOCIRegistryURL,
    87  		"--type", "helm",
    88  		"--name", name,
    89  		"--enable-oci",
    90  	}
    91  	errors.FailOnErr(fixture.RunCli(args...))
    92  }
    93  
    94  // AddHTTPSRepoCredentialsUserPass adds E2E username/password credentials for HTTPS repos to context
    95  func AddHTTPSCredentialsUserPass() {
    96  	var repoURLType fixture.RepoURLType = fixture.RepoURLTypeHTTPS
    97  	args := []string{"repocreds", "add", fixture.RepoURL(repoURLType), "--username", fixture.GitUsername, "--password", fixture.GitPassword}
    98  	errors.FailOnErr(fixture.RunCli(args...))
    99  }
   100  
   101  // AddHTTPSRepoCredentialsTLSClientCert adds E2E  for HTTPS repos to context
   102  func AddHTTPSCredentialsTLSClientCert() {
   103  	certPath, err := filepath.Abs("../fixture/certs/argocd-test-client.crt")
   104  	errors.CheckError(err)
   105  	keyPath, err := filepath.Abs("../fixture/certs/argocd-test-client.key")
   106  	errors.CheckError(err)
   107  	args := []string{
   108  		"repocreds",
   109  		"add",
   110  		fixture.RepoBaseURL(fixture.RepoURLTypeHTTPSClientCert),
   111  		"--username", fixture.GitUsername,
   112  		"--password", fixture.GitPassword,
   113  		"--tls-client-cert-path", certPath,
   114  		"--tls-client-cert-key-path", keyPath,
   115  	}
   116  	errors.FailOnErr(fixture.RunCli(args...))
   117  }
   118  
   119  // AddHelmHTTPSCredentialsTLSClientCert adds credentials for Helm repos to context
   120  func AddHelmHTTPSCredentialsTLSClientCert() {
   121  	certPath, err := filepath.Abs("../fixture/certs/argocd-test-client.crt")
   122  	errors.CheckError(err)
   123  	keyPath, err := filepath.Abs("../fixture/certs/argocd-test-client.key")
   124  	errors.CheckError(err)
   125  	args := []string{
   126  		"repocreds",
   127  		"add",
   128  		fixture.RepoURL(fixture.RepoURLTypeHelmParent),
   129  		"--username", fixture.GitUsername,
   130  		"--password", fixture.GitPassword,
   131  		"--tls-client-cert-path", certPath,
   132  		"--tls-client-cert-key-path", keyPath,
   133  		"--type", "helm",
   134  	}
   135  	errors.FailOnErr(fixture.RunCli(args...))
   136  }
   137  
   138  // AddHelmoOCICredentialsWithoutUserPass adds credentials for Helm OIC repo to context
   139  func AddHelmoOCICredentialsWithoutUserPass() {
   140  	args := []string{"repocreds", "add", fixture.RepoURL(fixture.RepoURLTypeHelmOCI),
   141  		"--enable-oci", "--type", "helm"}
   142  	errors.FailOnErr(fixture.RunCli(args...))
   143  }
   144  
   145  // AddSSHRepoCredentials adds E2E fixture credentials for SSH repos to context
   146  func AddSSHCredentials() {
   147  	keyPath, err := filepath.Abs("../fixture/testrepos/id_rsa")
   148  	errors.CheckError(err)
   149  	var repoURLType fixture.RepoURLType = fixture.RepoURLTypeSSH
   150  	args := []string{"repocreds", "add", fixture.RepoBaseURL(repoURLType), "--ssh-private-key-path", keyPath}
   151  	errors.FailOnErr(fixture.RunCli(args...))
   152  }
   153  
   154  // PushChartToOCIRegistry adds a helm chart to helm OCI registry
   155  func PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) {
   156  	// create empty temp directory to extract chart from the registry
   157  	tempDest, err1 := os.MkdirTemp("", "helm")
   158  	errors.CheckError(err1)
   159  	defer func() { _ = os.RemoveAll(tempDest) }()
   160  
   161  	chartAbsPath, err2 := filepath.Abs(fmt.Sprintf("./testdata/%s", chartPathName))
   162  	errors.CheckError(err2)
   163  
   164  	_ = os.Setenv("HELM_EXPERIMENTAL_OCI", "1")
   165  	errors.FailOnErr(fixture.Run("", "helm", "dependency", "build", chartAbsPath))
   166  	errors.FailOnErr(fixture.Run("", "helm", "package", chartAbsPath, "--destination", tempDest))
   167  	_ = os.RemoveAll(fmt.Sprintf("%s/%s", chartAbsPath, "charts"))
   168  	errors.FailOnErr(fixture.Run(
   169  		"",
   170  		"helm",
   171  		"push",
   172  		fmt.Sprintf("%s/%s-%s.tgz", tempDest, chartName, chartVersion),
   173  		fmt.Sprintf("oci://%s", fixture.HelmOCIRegistryURL),
   174  	))
   175  
   176  }