diff --git a/pkg/apis/application/v1alpha1/types_test.go b/pkg/apis/application/v1alpha1/types_test.go index f82da945a6920..334264bad100d 100644 --- a/pkg/apis/application/v1alpha1/types_test.go +++ b/pkg/apis/application/v1alpha1/types_test.go @@ -2,10 +2,14 @@ package v1alpha1 import ( fmt "fmt" + "io/ioutil" + "os" + "path" "reflect" "testing" "time" + argocdcommon "github.com/argoproj/argo-cd/v2/common" "k8s.io/utils/pointer" "github.com/argoproj/gitops-engine/pkg/sync/common" @@ -2596,3 +2600,45 @@ func TestEnvsubst(t *testing.T) { assert.Equal(t, "bar", env.Envsubst("$foo")) assert.Equal(t, "$foo", env.Envsubst("$$foo")) } + +func TestGetCAPath(t *testing.T) { + + temppath, err := ioutil.TempDir("", "argocd-cert-test") + if err != nil { + panic(err) + } + cert, err := ioutil.ReadFile("../../../../test/fixture/certs/argocd-test-server.crt") + if err != nil { + panic(err) + } + err = ioutil.WriteFile(path.Join(temppath, "foo.example.com"), cert, 0666) + if err != nil { + panic(err) + } + defer os.RemoveAll(temppath) + os.Setenv(argocdcommon.EnvVarTLSDataPath, temppath) + validcert := []string{ + "https://foo.example.com", + "oci://foo.example.com", + "foo.example.com", + } + invalidpath := []string{ + "https://bar.example.com", + "oci://bar.example.com", + "bar.example.com", + "ssh://foo.example.com", + "/some/invalid/thing", + "../another/invalid/thing", + "./also/invalid", + "$invalid/as/well", + } + + for _, str := range validcert { + path := getCAPath(str) + assert.NotEmpty(t, path) + } + for _, str := range invalidpath { + path := getCAPath(str) + assert.Empty(t, path) + } +}