Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing Helm params (#9565) #9566

Merged
merged 5 commits into from Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions reposerver/repository/repository_test.go
Expand Up @@ -1152,6 +1152,7 @@ func TestListApps(t *testing.T) {
"kustomization_yml": "Kustomize",
"my-chart": "Helm",
"my-chart-2": "Helm",
"values-files": "Helm",
}
assert.Equal(t, expectedApps, res.Apps)
}
Expand Down Expand Up @@ -2019,3 +2020,32 @@ func runGit(t *testing.T, workDir string, args ...string) string {
require.NoError(t, err, stringOut)
return stringOut
}

func Test_findHelmValueFilesInPath(t *testing.T) {
t.Run("does not exist", func(t *testing.T) {
files, err := findHelmValueFilesInPath("/obviously/does/not/exist")
assert.Error(t, err)
assert.Empty(t, files)
})
t.Run("values files", func(t *testing.T) {
files, err := findHelmValueFilesInPath("./testdata/values-files")
assert.NoError(t, err)
assert.Len(t, files, 4)
})
}

func Test_populateHelmAppDetails(t *testing.T) {
res := apiclient.RepoAppDetailsResponse{}
q := apiclient.RepoServerAppDetailsQuery{
Repo: &argoappv1.Repository{},
Source: &argoappv1.ApplicationSource{
Helm: &argoappv1.ApplicationSourceHelm{ValueFiles: []string{"exclude.yaml", "has-the-word-values.yaml"}},
},
}
appPath, err := filepath.Abs("./testdata/values-files/")
require.NoError(t, err)
err = populateHelmAppDetails(&res, appPath, appPath, &q)
require.NoError(t, err)
assert.Len(t, res.Helm.Parameters, 3)
assert.Len(t, res.Helm.ValueFiles, 4)
}
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/values-files/Chart.yaml
@@ -0,0 +1,2 @@
name: my-chart
version: 1.1.0
Empty file.
1 change: 1 addition & 0 deletions reposerver/repository/testdata/values-files/exclude.yaml
@@ -0,0 +1 @@
exclude: yaml
@@ -0,0 +1,4 @@
has:
the:
word:
values: yaml
1 change: 1 addition & 0 deletions reposerver/repository/testdata/values-files/values.yaml
@@ -0,0 +1 @@
values: yaml
Empty file.
6 changes: 2 additions & 4 deletions util/helm/helm.go
Expand Up @@ -6,7 +6,6 @@ import (
"net/url"
"os"
"os/exec"
"path"
"strings"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -143,11 +142,10 @@ func (h *helm) GetParameters(valuesFiles []pathutil.ResolvedFilePath) (map[strin
if err == nil && (parsedURL.Scheme == "http" || parsedURL.Scheme == "https") {
fileValues, err = config.ReadRemoteFile(file)
} else {
filePath := path.Join(h.cmd.WorkDir, file)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
if _, err := os.Stat(file); os.IsNotExist(err) {
continue
}
fileValues, err = ioutil.ReadFile(filePath)
fileValues, err = ioutil.ReadFile(file)
}
if err != nil {
return nil, fmt.Errorf("failed to read value file %s: %s", file, err)
Expand Down
43 changes: 33 additions & 10 deletions util/helm/helm_test.go
Expand Up @@ -3,8 +3,11 @@ package helm
import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/argoproj/argo-cd/v2/util/io/path"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
Expand Down Expand Up @@ -53,11 +56,16 @@ func TestHelmTemplateParams(t *testing.T) {
}

func TestHelmTemplateValues(t *testing.T) {
h, err := NewHelmApp("./testdata/redis", []HelmRepository{}, false, "", "", false)
repoRoot := "./testdata/redis"
repoRootAbs, err := filepath.Abs(repoRoot)
require.NoError(t, err)
h, err := NewHelmApp(repoRootAbs, []HelmRepository{}, false, "", "", false)
assert.NoError(t, err)
valuesPath, _, err := path.ResolveFilePath(repoRootAbs, repoRootAbs, "values-production.yaml", nil)
require.NoError(t, err)
opts := TemplateOpts{
Name: "test",
Values: []path.ResolvedFilePath{"values-production.yaml"},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests were passing relative paths. That worked when we were incorrectly using filepath.Join. Since the code now expects (and should always receive) absolute paths, we have to calculate absolute paths in the tests.

Values: []path.ResolvedFilePath{valuesPath},
}
objs, err := template(h, &opts)
assert.Nil(t, err)
Expand All @@ -74,33 +82,48 @@ func TestHelmTemplateValues(t *testing.T) {
}

func TestHelmGetParams(t *testing.T) {
h, err := NewHelmApp("./testdata/redis", nil, false, "", "", false)
repoRoot := "./testdata/redis"
repoRootAbs, err := filepath.Abs(repoRoot)
require.NoError(t, err)
h, err := NewHelmApp(repoRootAbs, nil, false, "", "", false)
assert.NoError(t, err)
params, err := h.GetParameters(nil)
assert.Nil(t, err)

slaveCountParam := params["cluster.slaveCount"]
assert.Equal(t, slaveCountParam, "1")
assert.Equal(t, "1", slaveCountParam)
}

func TestHelmGetParamsValueFiles(t *testing.T) {
h, err := NewHelmApp("./testdata/redis", nil, false, "", "", false)
repoRoot := "./testdata/redis"
repoRootAbs, err := filepath.Abs(repoRoot)
require.NoError(t, err)
h, err := NewHelmApp(repoRootAbs, nil, false, "", "", false)
assert.NoError(t, err)
params, err := h.GetParameters([]path.ResolvedFilePath{"values-production.yaml"})
valuesPath, _, err := path.ResolveFilePath(repoRootAbs, repoRootAbs, "values-production.yaml", nil)
require.NoError(t, err)
params, err := h.GetParameters([]path.ResolvedFilePath{valuesPath})
assert.Nil(t, err)

slaveCountParam := params["cluster.slaveCount"]
assert.Equal(t, slaveCountParam, "3")
assert.Equal(t, "3", slaveCountParam)
}

func TestHelmGetParamsValueFilesThatExist(t *testing.T) {
h, err := NewHelmApp("./testdata/redis", nil, false, "", "", false)
repoRoot := "./testdata/redis"
repoRootAbs, err := filepath.Abs(repoRoot)
require.NoError(t, err)
h, err := NewHelmApp(repoRootAbs, nil, false, "", "", false)
assert.NoError(t, err)
params, err := h.GetParameters([]path.ResolvedFilePath{"values-missing.yaml", "values-production.yaml"})
valuesMissingPath, _, err := path.ResolveFilePath(repoRootAbs, repoRootAbs, "values-missing.yaml", nil)
require.NoError(t, err)
valuesProductionPath, _, err := path.ResolveFilePath(repoRootAbs, repoRootAbs, "values-production.yaml", nil)
require.NoError(t, err)
params, err := h.GetParameters([]path.ResolvedFilePath{valuesMissingPath, valuesProductionPath})
assert.Nil(t, err)

slaveCountParam := params["cluster.slaveCount"]
assert.Equal(t, slaveCountParam, "3")
assert.Equal(t, "3", slaveCountParam)
}

func TestHelmDependencyBuild(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions util/io/path/resolved.go
Expand Up @@ -11,6 +11,7 @@ import (
)

// ResolvedFilePath represents a resolved file path and intended to prevent unintentional use of not verified file path.
// It is always either a URL or an absolute path.
type ResolvedFilePath string

// resolveSymbolicLinkRecursive resolves the symlink path recursively to its
Expand Down