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

     1  package repos
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    13  	"github.com/argoproj/argo-cd/v3/util/errors"
    14  )
    15  
    16  func CertPath(t *testing.T) string {
    17  	t.Helper()
    18  	return mustToAbsPath(t, "../fixture/certs/argocd-test-client.crt")
    19  }
    20  
    21  func CertKeyPath(t *testing.T) string {
    22  	t.Helper()
    23  	return mustToAbsPath(t, "../fixture/certs/argocd-test-client.key")
    24  }
    25  
    26  func mustToAbsPath(t *testing.T, relativePath string) string {
    27  	t.Helper()
    28  	res, err := filepath.Abs(relativePath)
    29  	require.NoError(t, err)
    30  	return res
    31  }
    32  
    33  // sets the current repo as the default SSH test repo
    34  func AddSSHRepo(t *testing.T, insecure bool, credentials bool, repoURLType fixture.RepoURLType) {
    35  	t.Helper()
    36  	keyPath, err := filepath.Abs("../fixture/testrepos/id_rsa")
    37  	require.NoError(t, err)
    38  	args := []string{"repo", "add", fixture.RepoURL(repoURLType)}
    39  	if credentials {
    40  		args = append(args, "--ssh-private-key-path", keyPath)
    41  	}
    42  	if insecure {
    43  		args = append(args, "--insecure-ignore-host-key")
    44  	}
    45  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    46  }
    47  
    48  // sets the current repo as the default HTTPS test repo
    49  func AddHTTPSRepo(t *testing.T, insecure bool, credentials bool, project string, repoURLType fixture.RepoURLType) {
    50  	t.Helper()
    51  	// This construct is somewhat necessary to satisfy the compiler
    52  	args := []string{"repo", "add", fixture.RepoURL(repoURLType)}
    53  	if credentials {
    54  		args = append(args, "--username", fixture.GitUsername, "--password", fixture.GitPassword)
    55  	}
    56  	if insecure {
    57  		args = append(args, "--insecure-skip-server-verification")
    58  	}
    59  	if project != "" {
    60  		args = append(args, "--project", project)
    61  	}
    62  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    63  }
    64  
    65  // sets a HTTPS repo using TLS client certificate authentication
    66  func AddHTTPSRepoClientCert(t *testing.T, insecure bool) {
    67  	t.Helper()
    68  	args := []string{
    69  		"repo",
    70  		"add",
    71  		fixture.RepoURL(fixture.RepoURLTypeHTTPSClientCert),
    72  		"--username", fixture.GitUsername,
    73  		"--password", fixture.GitPassword,
    74  		"--tls-client-cert-path", CertPath(t),
    75  		"--tls-client-cert-key-path", CertKeyPath(t),
    76  	}
    77  	if insecure {
    78  		args = append(args, "--insecure-skip-server-verification")
    79  	}
    80  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    81  }
    82  
    83  func AddHelmRepo(t *testing.T, name string) {
    84  	t.Helper()
    85  	args := []string{
    86  		"repo",
    87  		"add",
    88  		fixture.RepoURL(fixture.RepoURLTypeHelm),
    89  		"--username", fixture.GitUsername,
    90  		"--password", fixture.GitPassword,
    91  		"--tls-client-cert-path", CertPath(t),
    92  		"--tls-client-cert-key-path", CertKeyPath(t),
    93  		"--type", "helm",
    94  		"--name", name,
    95  	}
    96  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    97  }
    98  
    99  func AddOCIRepo(t *testing.T, name, imagePath string) {
   100  	t.Helper()
   101  	args := []string{
   102  		"repo",
   103  		"add",
   104  		fmt.Sprintf("%s/%s", fixture.OCIHostURL, imagePath),
   105  		"--type", "oci",
   106  		"--name", name,
   107  		"--insecure-oci-force-http",
   108  	}
   109  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   110  }
   111  
   112  func AddAuthenticatedOCIRepo(t *testing.T, name, imagePath string) {
   113  	t.Helper()
   114  	args := []string{
   115  		"repo",
   116  		"add",
   117  		fmt.Sprintf("%s/%s", fixture.AuthenticatedOCIHostURL, imagePath),
   118  		"--username", fixture.GitUsername,
   119  		"--password", fixture.GitPassword,
   120  		"--type", "oci",
   121  		"--name", name,
   122  		"--insecure-oci-force-http",
   123  	}
   124  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   125  }
   126  
   127  func AddHelmOCIRepo(t *testing.T, name string) {
   128  	t.Helper()
   129  	args := []string{
   130  		"repo",
   131  		"add",
   132  		fixture.HelmOCIRegistryURL,
   133  		"--type", "helm",
   134  		"--name", name,
   135  		"--enable-oci",
   136  	}
   137  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   138  }
   139  
   140  // AddHTTPSRepoCredentialsUserPass adds E2E username/password credentials for HTTPS repos to context
   141  func AddHTTPSCredentialsUserPass(t *testing.T) {
   142  	t.Helper()
   143  	var repoURLType fixture.RepoURLType = fixture.RepoURLTypeHTTPS
   144  	args := []string{"repocreds", "add", fixture.RepoURL(repoURLType), "--username", fixture.GitUsername, "--password", fixture.GitPassword}
   145  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   146  }
   147  
   148  // AddHTTPSRepoCredentialsTLSClientCert adds E2E  for HTTPS repos to context
   149  func AddHTTPSCredentialsTLSClientCert(t *testing.T) {
   150  	t.Helper()
   151  	certPath, err := filepath.Abs("../fixture/certs/argocd-test-client.crt")
   152  	require.NoError(t, err)
   153  	keyPath, err := filepath.Abs("../fixture/certs/argocd-test-client.key")
   154  	require.NoError(t, err)
   155  	args := []string{
   156  		"repocreds",
   157  		"add",
   158  		fixture.RepoBaseURL(fixture.RepoURLTypeHTTPSClientCert),
   159  		"--username", fixture.GitUsername,
   160  		"--password", fixture.GitPassword,
   161  		"--tls-client-cert-path", certPath,
   162  		"--tls-client-cert-key-path", keyPath,
   163  	}
   164  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   165  }
   166  
   167  // AddHelmHTTPSCredentialsTLSClientCert adds credentials for Helm repos to context
   168  func AddHelmHTTPSCredentialsTLSClientCert(t *testing.T) {
   169  	t.Helper()
   170  	certPath, err := filepath.Abs("../fixture/certs/argocd-test-client.crt")
   171  	require.NoError(t, err)
   172  	keyPath, err := filepath.Abs("../fixture/certs/argocd-test-client.key")
   173  	require.NoError(t, err)
   174  	args := []string{
   175  		"repocreds",
   176  		"add",
   177  		fixture.RepoURL(fixture.RepoURLTypeHelmParent),
   178  		"--username", fixture.GitUsername,
   179  		"--password", fixture.GitPassword,
   180  		"--tls-client-cert-path", certPath,
   181  		"--tls-client-cert-key-path", keyPath,
   182  		"--type", "helm",
   183  	}
   184  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   185  }
   186  
   187  // AddHelmoOCICredentialsWithoutUserPass adds credentials for Helm OIC repo to context
   188  func AddHelmoOCICredentialsWithoutUserPass(t *testing.T) {
   189  	t.Helper()
   190  	args := []string{
   191  		"repocreds", "add", fixture.RepoURL(fixture.RepoURLTypeHelmOCI),
   192  		"--enable-oci", "--type", "helm",
   193  	}
   194  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   195  }
   196  
   197  // AddSSHRepoCredentials adds E2E fixture credentials for SSH repos to context
   198  func AddSSHCredentials(t *testing.T) {
   199  	t.Helper()
   200  	keyPath, err := filepath.Abs("../fixture/testrepos/id_rsa")
   201  	require.NoError(t, err)
   202  	var repoURLType fixture.RepoURLType = fixture.RepoURLTypeSSH
   203  	args := []string{"repocreds", "add", fixture.RepoBaseURL(repoURLType), "--ssh-private-key-path", keyPath}
   204  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
   205  }
   206  
   207  // PushChartToOCIRegistry adds a helm chart to helm OCI registry
   208  func PushChartToOCIRegistry(t *testing.T, chartPathName, chartName, chartVersion string) {
   209  	t.Helper()
   210  	// create empty temp directory to extract chart from the registry
   211  	tempDest, err1 := os.MkdirTemp("", "helm")
   212  	require.NoError(t, err1)
   213  	defer func() { _ = os.RemoveAll(tempDest) }()
   214  
   215  	chartAbsPath, err2 := filepath.Abs("./" + chartPathName)
   216  	require.NoError(t, err2)
   217  
   218  	t.Setenv("HELM_EXPERIMENTAL_OCI", "1")
   219  	errors.NewHandler(t).FailOnErr(fixture.Run("", "helm", "dependency", "build", chartAbsPath))
   220  	errors.NewHandler(t).FailOnErr(fixture.Run("", "helm", "package", chartAbsPath, "--destination", tempDest))
   221  	_ = os.RemoveAll(fmt.Sprintf("%s/%s", chartAbsPath, "charts"))
   222  	errors.NewHandler(t).FailOnErr(fixture.Run(
   223  		"",
   224  		"helm",
   225  		"push",
   226  		fmt.Sprintf("%s/%s-%s.tgz", tempDest, chartName, chartVersion),
   227  		"oci://"+fixture.HelmOCIRegistryURL,
   228  	))
   229  }
   230  
   231  // PushChartToAuthenticatedOCIRegistry adds a helm chart to helm OCI registry
   232  func PushChartToAuthenticatedOCIRegistry(t *testing.T, chartPathName, chartName, chartVersion string) {
   233  	t.Helper()
   234  	// create empty temp directory to extract chart from the registry
   235  	tempDest, err1 := os.MkdirTemp("", "helm")
   236  	require.NoError(t, err1)
   237  	defer func() { _ = os.RemoveAll(tempDest) }()
   238  
   239  	chartAbsPath, err2 := filepath.Abs("./" + chartPathName)
   240  	require.NoError(t, err2)
   241  
   242  	t.Setenv("HELM_EXPERIMENTAL_OCI", "1")
   243  	errors.NewHandler(t).FailOnErr(fixture.Run("", "helm", "dependency", "build", chartAbsPath))
   244  	errors.NewHandler(t).FailOnErr(fixture.Run("", "helm", "package", chartAbsPath, "--destination", tempDest))
   245  	_ = os.RemoveAll(fmt.Sprintf("%s/%s", chartAbsPath, "charts"))
   246  
   247  	errors.NewHandler(t).FailOnErr(fixture.Run(
   248  		"",
   249  		"helm",
   250  		"registry",
   251  		"login",
   252  		"--username", fixture.GitUsername,
   253  		"--password", fixture.GitPassword,
   254  		"localhost:5001",
   255  	))
   256  
   257  	errors.NewHandler(t).FailOnErr(fixture.Run(
   258  		"",
   259  		"helm",
   260  		"push",
   261  		fmt.Sprintf("%s/%s-%s.tgz", tempDest, chartName, chartVersion),
   262  		"oci://"+fixture.HelmAuthenticatedOCIRegistryURL,
   263  	))
   264  
   265  	errors.NewHandler(t).FailOnErr(fixture.Run(
   266  		"",
   267  		"helm",
   268  		"registry",
   269  		"logout",
   270  		"localhost:5001",
   271  	))
   272  }
   273  
   274  // PushImageToOCIRegistry adds a helm chart to helm OCI registry
   275  func PushImageToOCIRegistry(t *testing.T, pathName, tag string) {
   276  	t.Helper()
   277  	imagePath := "./" + pathName
   278  
   279  	errors.NewHandler(t).FailOnErr(fixture.Run(
   280  		imagePath,
   281  		"oras",
   282  		"push",
   283  		fmt.Sprintf("%s:%s", fmt.Sprintf("%s/%s", strings.TrimPrefix(fixture.OCIHostURL, "oci://"), filepath.Base(pathName)), tag),
   284  		".",
   285  	))
   286  }
   287  
   288  // PushImageToAuthenticatedOCIRegistry adds a helm chart to helm OCI registry
   289  func PushImageToAuthenticatedOCIRegistry(t *testing.T, pathName, tag string) {
   290  	t.Helper()
   291  	imagePath := "./" + pathName
   292  
   293  	errors.NewHandler(t).FailOnErr(fixture.Run(
   294  		imagePath,
   295  		"oras",
   296  		"push",
   297  		fmt.Sprintf("%s:%s", fmt.Sprintf("%s/%s", strings.TrimPrefix(fixture.AuthenticatedOCIHostURL, "oci://"), filepath.Base(pathName)), tag),
   298  		".",
   299  	))
   300  }