Skip to content

Commit

Permalink
add e2e tests for custom packages (#278)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Jun 4, 2024
1 parent c13a570 commit ad84c13
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 61 deletions.
6 changes: 3 additions & 3 deletions pkg/controllers/custompackage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ func TestReconcileCustomPkg(t *testing.T) {
c := mgr.GetClient()
repo := v1alpha1.GitRepository{
ObjectMeta: metav1.ObjectMeta{
Name: localRepoName("my-app", "test/resources/customPackages/testDir/busybox"),
Name: localRepoName("my-app", "test/resources/customPackages/testDir/app1"),
Namespace: "test",
},
}
err = c.Get(context.Background(), client.ObjectKeyFromObject(&repo), &repo)
if err != nil {
t.Fatalf("getting my-app-busybox git repo %v", err)
t.Fatalf("getting my-app-app1 git repo %v", err)
}

p, _ := filepath.Abs("test/resources/customPackages/testDir/busybox")
p, _ := filepath.Abs("test/resources/customPackages/testDir/app1")
expectedRepo := v1alpha1.GitRepository{
Spec: v1alpha1.GitRepositorySpec{
Source: v1alpha1.GitRepositorySource{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
namespace: my-app
server: "https://kubernetes.default.svc"
source:
repoURL: cnoe://busybox
repoURL: cnoe://app1
targetRevision: HEAD
path: "."
project: default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
test1: "one"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: one-config
data:
test1: "one"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: two-config
data:
test1: "one"

This file was deleted.

48 changes: 48 additions & 0 deletions tests/e2e/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

func CleanUpDocker(t *testing.T) {
t.Log("cleaning up docker env")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
b, err := e2e.RunCommand(ctx, `docker ps -aqf name=localdev-control-plane`, 10*time.Second)
Expand All @@ -36,6 +37,7 @@ func CleanUpDocker(t *testing.T) {

b, err = e2e.RunCommand(ctx, "docker volume prune -f", 60*time.Second)
assert.Nil(t, err, fmt.Sprintf("error while pruning volumes: %s, %s", err, b))
t.Log("finished cleaning up docker env")
}

func Test_CreateDocker(t *testing.T) {
Expand All @@ -45,6 +47,7 @@ func Test_CreateDocker(t *testing.T) {
testCreate(t)
testCreatePath(t)
testCreatePort(t)
testCustomPkg(t)
}

// test idpbuilder create
Expand Down Expand Up @@ -109,3 +112,48 @@ func testCreatePort(t *testing.T) {
giteaBaseUrl := fmt.Sprintf("https://gitea.%s:%s", e2e.DefaultBaseDomain, port)
e2e.TestCoreEndpoints(ctx, t, argoBaseUrl, giteaBaseUrl)
}

func testCustomPkg(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Minute)
defer cancel()
defer CleanUpDocker(t)

cmdString := "create -p ../../../pkg/controllers/custompackage/test/resources/customPackages/testDir"

t.Log(fmt.Sprintf("running %s", cmdString))
cmd := exec.CommandContext(ctx, e2e.IdpbuilderBinaryLocation, strings.Split(cmdString, " ")...)
b, err := cmd.CombinedOutput()
assert.Nil(t, err, fmt.Sprintf("error while running create: %s, %s", err, b))

kubeClient, err := e2e.GetKubeClient()

assert.Nil(t, err, fmt.Sprintf("error while getting client: %s", err))
if err != nil {
assert.FailNow(t, "failed creating cluster")
}

e2e.TestArgoCDApps(ctx, t, kubeClient, e2e.CorePackages)

giteaBaseUrl := fmt.Sprintf("https://gitea.%s:%s", e2e.DefaultBaseDomain, e2e.DefaultPort)

expectedApps := map[string]string{
"my-app": "argocd",
"my-app2": "argocd",
}
e2e.TestArgoCDApps(ctx, t, kubeClient, expectedApps)
repos, err := e2e.GetGiteaRepos(ctx, giteaBaseUrl)
assert.Nil(t, err)
expectedRepoNames := map[string]struct{}{
"idpbuilder-localdev-my-app-app1": {},
"idpbuilder-localdev-my-app2-app2": {},
}

for i := range repos {
repo := repos[i]
_, ok := expectedRepoNames[repo.Name]
if ok {
delete(expectedRepoNames, repo.Name)
}
}
assert.Empty(t, expectedRepoNames)
}
42 changes: 36 additions & 6 deletions tests/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,60 @@ func SendAndParse(target any, httpClient *http.Client, req *http.Request) error

func TestGiteaEndpoints(ctx context.Context, t *testing.T, baseUrl string) {
t.Log("testing gitea endpoints")
repos, err := GetGiteaRepos(ctx, baseUrl)
assert.Nil(t, err)

assert.Equal(t, 3, len(repos))
expectedRepoNames := map[string]struct{}{
"idpbuilder-localdev-gitea": {},
"idpbuilder-localdev-nginx": {},
"idpbuilder-localdev-argocd": {},
}

for i := range repos {
_, ok := expectedRepoNames[repos[i].Name]
if ok {
delete(expectedRepoNames, repos[i].Name)
}
}
assert.Equal(t, 0, len(expectedRepoNames))
}

func GetGiteaRepos(ctx context.Context, baseUrl string) ([]gitea.Repository, error) {
auth, err := GetBasicAuth(ctx, "gitea-credential")
assert.Nil(t, err, fmt.Sprintf("getting gitea k8s secret %s", err))
if err != nil {
return nil, fmt.Errorf("getting gitea credentials %w", err)
}

token, err := GetGiteaSessionToken(ctx, auth, baseUrl)
assert.Nil(t, err, fmt.Sprintf("getting gitea token %s", err))
if err != nil {
return nil, fmt.Errorf("getting gitea token %w", err)
}

userEP := fmt.Sprintf("%s%s", baseUrl, fmt.Sprintf(GiteaUserEndpoint, auth.Username))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, userEP, nil)
assert.Nil(t, err, fmt.Sprintf("creating new request %s", err))
if err != nil {
return nil, fmt.Errorf("creating new request %w", err)
}

httpClient := GetHttpClient()
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))

user := gitea.User{}
err = SendAndParse(&user, httpClient, req)
assert.Nil(t, err, fmt.Sprintf("getting user info %s", err))
if err != nil {
return nil, fmt.Errorf("getting user info %w", err)
}

repos := GiteaSearchRepoResponse{}
repoEp := fmt.Sprintf("%s%s", baseUrl, GiteaRepoEndpoint)
repoReq, _ := http.NewRequestWithContext(ctx, http.MethodGet, repoEp, nil)
err = SendAndParse(&repos, httpClient, repoReq)
assert.Nil(t, err, fmt.Sprintf("getting user info %s", err))
if err != nil {
return nil, fmt.Errorf("getting gitea repositories %w", err)
}

assert.Equal(t, 3, len(repos.Data))
return repos.Data, nil
}

func GetGiteaSessionToken(ctx context.Context, auth BasicAuth, baseUrl string) (string, error) {
Expand Down

0 comments on commit ad84c13

Please sign in to comment.