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

test: argo agent unit tests #2536

Merged
merged 68 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6dbbb7d
works on my machine
lucasrod16 May 20, 2024
9599ad4
Merge branch 'main' into agent-unit-tests
AustinAbro321 May 20, 2024
2249473
Merge branch 'main' into agent-unit-tests
lucasrod16 May 20, 2024
13bb0da
move sendAdmissionRequest() utils_test.go
lucasrod16 May 20, 2024
db0f7f3
Merge branch 'agent-unit-tests' of https://github.com/defenseunicorns…
lucasrod16 May 20, 2024
04eb292
load state from cluster
lucasrod16 May 20, 2024
131f666
Merge branch 'main' into agent-unit-tests
lucasrod16 May 20, 2024
c287ca2
adding clusterrole to agent
AustinAbro321 May 20, 2024
bcad43e
WIP flux tests
AustinAbro321 May 20, 2024
ddb08eb
WIP flux
lucasrod16 May 20, 2024
0070938
tests passing
AustinAbro321 May 20, 2024
f213837
flux working
lucasrod16 May 20, 2024
48304d4
use upstream type for flux test and delete comments
lucasrod16 May 20, 2024
5f25efc
beter err handling
lucasrod16 May 20, 2024
628ab54
moar stuff
lucasrod16 May 20, 2024
e253431
ctx
lucasrod16 May 20, 2024
a477285
add test cases for flux webhook
lucasrod16 May 20, 2024
3de6435
add helpers for test setup
lucasrod16 May 20, 2024
5218d10
argo working
lucasrod16 May 21, 2024
edec371
copy argo app types from upstream
lucasrod16 May 21, 2024
3ae4ec4
use upstream types for argo mutations
lucasrod16 May 21, 2024
32f88b0
happy path argo test
AustinAbro321 May 21, 2024
1985894
happy path argo test
AustinAbro321 May 21, 2024
83b0bfc
separating PR
AustinAbro321 May 21, 2024
0940c58
pod pod pod
AustinAbro321 May 21, 2024
093bd36
refactor
AustinAbro321 May 21, 2024
73eb950
fix source
AustinAbro321 May 21, 2024
18554a9
Merge branch 'agent-unit-tests' into pod-agent-unit-tests
AustinAbro321 May 21, 2024
70644ad
test
AustinAbro321 May 21, 2024
554ecf5
messaging
AustinAbro321 May 21, 2024
c86b62f
uno reverse
AustinAbro321 May 21, 2024
0202eff
extrememlely very much wip wip wip
AustinAbro321 May 21, 2024
053d5da
unit tests passing
AustinAbro321 May 21, 2024
6847125
refactor tests
AustinAbro321 May 21, 2024
6644e6d
merge
AustinAbro321 May 21, 2024
1aa5b07
changing error we return
AustinAbro321 May 22, 2024
63452c3
simplify
AustinAbro321 May 22, 2024
ad32444
cleaning up test
AustinAbro321 May 22, 2024
e6bb0cc
Merge branch 'main' into flux-agent-unit-tests
AustinAbro321 May 22, 2024
d68265d
adding admission response
AustinAbro321 May 22, 2024
637096f
Merge branch 'flux-agent-unit-tests' of github.com:defenseunicorns/za…
AustinAbro321 May 22, 2024
8244894
admission metadata is required? :/
AustinAbro321 May 22, 2024
3f27d95
wip
AustinAbro321 May 22, 2024
7c0658e
merge
AustinAbro321 May 22, 2024
4e703df
Merge branch 'main' into flux-agent-unit-tests
AustinAbro321 May 22, 2024
804afce
argo changes
AustinAbro321 May 22, 2024
a9271a1
Merge branch 'main' into agent-unit-tests
AustinAbro321 May 22, 2024
34e515f
adding test
AustinAbro321 May 22, 2024
4a4b25e
Merge branch 'main' into flux-agent-unit-tests
AustinAbro321 May 22, 2024
12d8528
go mod
AustinAbro321 May 22, 2024
4c27af5
Merge branch 'main' into flux-agent-unit-tests
AustinAbro321 May 22, 2024
5ebfe9b
Merge branch 'main' into flux-agent-unit-tests
Noxsios May 22, 2024
f35d515
comment
AustinAbro321 May 22, 2024
b8a3214
Merge branch 'flux-agent-unit-tests' of github.com:defenseunicorns/za…
AustinAbro321 May 22, 2024
084ded8
WIP
AustinAbro321 May 22, 2024
2d5e0ae
Merge branch 'flux-agent-unit-tests' into agent-unit-tests
AustinAbro321 May 22, 2024
bf23d5b
testing
AustinAbro321 May 22, 2024
ffc740f
merge
AustinAbro321 May 22, 2024
532af60
simplify
AustinAbro321 May 22, 2024
ea90366
simplify
AustinAbro321 May 22, 2024
69e21ed
proxy
AustinAbro321 May 22, 2024
de7621f
Merge branch 'main' into agent-unit-tests
lucasrod16 May 23, 2024
e1fe865
fix test case name and test file names
lucasrod16 May 23, 2024
1f2525a
Merge branch 'main' into agent-unit-tests
lucasrod16 May 23, 2024
83fc9a6
Merge branch 'main' into agent-unit-tests
lucasrod16 May 23, 2024
7e1f166
Merge branch 'main' into agent-unit-tests
schristoff-du May 23, 2024
3a5ce84
Merge branch 'main' into agent-unit-tests
lucasrod16 May 23, 2024
006637d
Merge branch 'main' into agent-unit-tests
lucasrod16 May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/zarf-agent/manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ spec:
- name: tls-certs
mountPath: /etc/certs
readOnly: true
- name: zarf-state
mountPath: /etc/zarf-state
readOnly: true
# Required for OpenShift to mount k9s vendored directories
- name: config
mountPath: /.config
Expand All @@ -55,9 +52,6 @@ spec:
- name: tls-certs
secret:
secretName: agent-hook-tls
- name: zarf-state
secret:
secretName: zarf-state
# Required for OpenShift to mount k9s vendored directories
- name: config
emptyDir: {}
Expand Down
95 changes: 95 additions & 0 deletions src/internal/agent/hooks/argo_application_test.go
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package hooks

import (
"context"
"encoding/json"
"net/http"
"testing"

"github.com/defenseunicorns/zarf/src/internal/agent/http/admission"
"github.com/defenseunicorns/zarf/src/internal/agent/operations"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func createArgoAppAdmissionRequest(t *testing.T, op v1.Operation, argoApp *Application) *v1.AdmissionRequest {
t.Helper()
raw, err := json.Marshal(argoApp)
require.NoError(t, err)
return &v1.AdmissionRequest{
Operation: op,
Object: runtime.RawExtension{
Raw: raw,
},
}
}

func TestArgoAppWebhook(t *testing.T) {
t.Parallel()

ctx := context.Background()
state := &types.ZarfState{GitServer: types.GitServerInfo{
Address: "https://git-server.com",
PushUsername: "a-push-user",
}}
c := createTestClientWithZarfState(ctx, t, state)
handler := admission.NewHandler().Serve(NewApplicationMutationHook(ctx, c))

tests := []admissionTest{
{
name: "should be mutated",
admissionReq: createArgoAppAdmissionRequest(t, v1.Create, &Application{
Spec: ApplicationSpec{
Source: &ApplicationSource{RepoURL: "https://diff-git-server.com/peanuts"},
Sources: []ApplicationSource{
{
RepoURL: "https://diff-git-server.com/cashews",
},
{
RepoURL: "https://diff-git-server.com/almonds",
},
},
},
}),
patch: []operations.PatchOperation{
operations.ReplacePatchOperation(
"/spec/source/repoURL",
"https://git-server.com/a-push-user/peanuts-3883081014",
),
operations.ReplacePatchOperation(
"/spec/sources/0/repoURL",
"https://git-server.com/a-push-user/cashews-580170494",
),
operations.ReplacePatchOperation(
"/spec/sources/1/repoURL",
"https://git-server.com/a-push-user/almonds-640159520",
),
},
code: http.StatusOK,
},
{
name: "should be mutated",
admissionReq: createArgoAppAdmissionRequest(t, v1.Create, &Application{
Spec: ApplicationSpec{
Source: &ApplicationSource{RepoURL: "https://bad-url"},
},
}),
code: http.StatusInternalServerError,
errContains: AgentErrTransformGitURL,
},
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rr := sendAdmissionRequest(t, tt.admissionReq, handler)
verifyAdmission(t, rr, tt)
})
}
}
119 changes: 119 additions & 0 deletions src/internal/agent/hooks/argo_repo_test.go
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package hooks

import (
"context"
b64 "encoding/base64"
"encoding/json"
"net/http"
"testing"

"github.com/defenseunicorns/zarf/src/internal/agent/http/admission"
"github.com/defenseunicorns/zarf/src/internal/agent/operations"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func createArgoRepoAdmissionRequest(t *testing.T, op v1.Operation, argoRepo *corev1.Secret) *v1.AdmissionRequest {
t.Helper()
raw, err := json.Marshal(argoRepo)
require.NoError(t, err)
return &v1.AdmissionRequest{
Operation: op,
Object: runtime.RawExtension{
Raw: raw,
},
}
}

func TestArgoRepoWebhook(t *testing.T) {
t.Parallel()

ctx := context.Background()
state := &types.ZarfState{GitServer: types.GitServerInfo{
Address: "https://git-server.com",
PushUsername: "a-push-user",
PullPassword: "a-pull-password",
PullUsername: "a-pull-user",
}}
c := createTestClientWithZarfState(ctx, t, state)
handler := admission.NewHandler().Serve(NewRepositorySecretMutationHook(ctx, c))

tests := []admissionTest{
{
name: "should be mutated",
admissionReq: createArgoRepoAdmissionRequest(t, v1.Create, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "repository",
},
Name: "argo-repo-secret",
Namespace: "argo",
},
Data: map[string][]byte{
"url": []byte("https://diff-git-server.com/podinfo"),
},
}),
patch: []operations.PatchOperation{
operations.ReplacePatchOperation(
"/data/url",
b64.StdEncoding.EncodeToString([]byte("https://git-server.com/a-push-user/podinfo-1868163476")),
),
operations.ReplacePatchOperation(
"/data/username",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullUsername)),
),
operations.ReplacePatchOperation(
"/data/password",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullPassword)),
),
},
code: http.StatusOK,
},
{
name: "matching hostname on update should stay the same, but secret should be added",
admissionReq: createArgoRepoAdmissionRequest(t, v1.Update, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "repository",
},
Name: "argo-repo-secret",
Namespace: "argo",
},
Data: map[string][]byte{
"url": []byte("https://git-server.com/podinfo"),
},
}),
patch: []operations.PatchOperation{
operations.ReplacePatchOperation(
"/data/url",
b64.StdEncoding.EncodeToString([]byte("https://git-server.com/podinfo")),
),
operations.ReplacePatchOperation(
"/data/username",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullUsername)),
),
operations.ReplacePatchOperation(
"/data/password",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullPassword)),
),
},
code: http.StatusOK,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rr := sendAdmissionRequest(t, tt.admissionReq, handler)
verifyAdmission(t, rr, tt)
})
}
}
Loading
Loading