Skip to content

Commit

Permalink
test: dded coverage for all the new code
Browse files Browse the repository at this point in the history
Signed-off-by: David Vidal Villamide <david@askharilabs.com>
  • Loading branch information
askhari committed Oct 29, 2023
1 parent 44c452e commit 02b8500
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,13 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
getHelmParam(appSource.Helm.Parameters, app.Annotations[fmt.Sprintf(common.HelmParamImageTagAnnotation, c.ImageName)]).Value,
)

mergedParams, err := conflate.FromData(originalData, []byte(helmValues))
var mergedParams *conflate.Conflate
mergedParams, err = conflate.FromData(originalData, []byte(helmValues))
if err != nil {
return nil, err
}

override, err = mergedParams.MarshalYAML()
if err != nil {
return nil, err
}
}
} else {
var params helmOverride
Expand Down
275 changes: 275 additions & 0 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,108 @@ helm:
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Empty originalData error with valid Helm source", func(t *testing.T) {
expected := `
helm:
parameters:
- name: foo
value: bar
forcestring: true
- name: bar
value: foo
forcestring: true
`
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "foo",
Value: "bar",
ForceString: true,
},
{
Name: "bar",
Value: "foo",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
},
}

originalData := []byte(``)
yaml, err := marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Invalid unmarshal originalData error with valid Helm source", func(t *testing.T) {
expected := `
helm:
parameters:
- name: foo
value: bar
forcestring: true
- name: bar
value: foo
forcestring: true
`
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "foo",
Value: "bar",
ForceString: true,
},
{
Name: "bar",
Value: "foo",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
},
}

originalData := []byte(`random content`)
yaml, err := marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Empty Helm source", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Expand All @@ -1216,6 +1318,111 @@ helm:
assert.Empty(t, yaml)
})

t.Run("Valid Helm source with Helm values file", func(t *testing.T) {
expected := `
image.name: nginx
image.tag: v1.0.0
replicas: 1
`
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "image.name",
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "image.tag",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.name",
Value: "nginx",
ForceString: true,
},
{
Name: "image.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`
image.name: nginx
image.tag: v0.0.0
replicas: 1
`)
yaml, err := marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Invalid parameters merge for Helm source with Helm values file", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "image.name",
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "image.tag",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.name",
Value: "nginx",
ForceString: true,
},
{
Name: "image.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`random content`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
})

t.Run("Unknown source", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Expand Down Expand Up @@ -1523,6 +1730,74 @@ func Test_GetWriteBackConfig(t *testing.T) {
assert.Equal(t, wbc.Target, "helm/app/values.yaml")
})

t.Run("Plain write back target without kustomize or helm types", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "target/folder/app-parameters.yaml",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Path: "config/foo",
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
},
}

argoClient := argomock.ArgoCD{}
argoClient.On("UpdateSpec", mock.Anything, mock.Anything).Return(nil, nil)

kubeClient := kube.KubernetesClient{
Clientset: fake.NewFakeKubeClient(),
}

wbc, err := getWriteBackConfig(&app, &kubeClient, &argoClient)
require.NoError(t, err)
require.NotNil(t, wbc)
assert.Equal(t, wbc.Method, WriteBackGit)
assert.Equal(t, wbc.Target, "target/folder/app-parameters.yaml")
})

t.Run("Unknown credentials", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git:error:argocd-image-updater/git-creds",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Path: "config/foo",
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
},
}

argoClient := argomock.ArgoCD{}
argoClient.On("UpdateSpec", mock.Anything, mock.Anything).Return(nil, nil)

kubeClient := kube.KubernetesClient{
Clientset: fake.NewFakeKubeClient(),
}

_, err := getWriteBackConfig(&app, &kubeClient, &argoClient)
assert.Error(t, err)
})

t.Run("Default write-back config - argocd", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Expand Down

0 comments on commit 02b8500

Please sign in to comment.