Skip to content

Commit

Permalink
feat: Cert Manager tests (#2073)
Browse files Browse the repository at this point in the history
* feat: Tests for install and upgrade of cert-manager

* fix: Update .gitignore

* fix: Remove extra toolchain (issue with lint)
  • Loading branch information
lukeogg authored Apr 10, 2024
1 parent 76719c5 commit dfc3313
Show file tree
Hide file tree
Showing 11 changed files with 716 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
# for updating image licenses
bloodhound
images.txt

# apptest working directory
apptests/.work/
48 changes: 48 additions & 0 deletions apptests/appscenarios/appscenarios.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,51 @@ func absolutePathTo(application string) (string, error) {
var scenariosList = List{
"reloader": reloader{},
}

// getTestDataDir gets the directory path for test data
func getTestDataDir() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}

_, err = os.Stat(filepath.Join(wd, "../testdata"))
if err != nil {
return "", fmt.Errorf("testdata directory not found: %w", err)
}

return filepath.Abs(filepath.Join(wd, "../testdata"))
}

func getkAppsUpgradePath(application string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}

// Check that the app repo has been cloned
_, err = os.Stat(filepath.Join(wd, "../", upgradeKAppsRepoPath))
if err != nil {
return "", fmt.Errorf("kommander-applications upgrade directory not found: %w", err)
}

// Get the absolute path to the application directory
dir, err := filepath.Abs(filepath.Join(wd, "../", upgradeKAppsRepoPath, "services", application))
if err != nil {
return "", err
}

// filepath.Glob returns a sorted slice of matching paths
matches, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return "", err
}

if len(matches) == 0 {
return "", fmt.Errorf(
"no application directory found for %s in the given path:%s",
application, dir)
}

return matches[0], nil
}
170 changes: 170 additions & 0 deletions apptests/appscenarios/certmanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package appscenarios

import (
"context"
"github.com/mesosphere/kommander-applications/apptests/environment"
"path/filepath"
)

type certManager struct{}

func (r certManager) Name() string {
return "cert-manager"
}

var _ AppScenario = (*reloader)(nil)

func (r certManager) Install(ctx context.Context, env *environment.Env) error {
appPath, err := absolutePathTo(r.Name())
if err != nil {
return err
}

err = r.install(ctx, env, appPath)
if err != nil {
return err
}

return err
}

func (r certManager) InstallPreviousVersion(ctx context.Context, env *environment.Env) error {
appPath, err := getkAppsUpgradePath(r.Name())
if err != nil {
return err
}

err = r.install(ctx, env, appPath)
if err != nil {
return err
}

return nil
}

func (r certManager) Upgrade(ctx context.Context, env *environment.Env) error {
appPath, err := absolutePathTo(r.Name())
if err != nil {
return err
}

err = r.install(ctx, env, appPath)
if err != nil {
return err
}

return err
}

func (r certManager) install(ctx context.Context, env *environment.Env, appPath string) error {
// apply defaults config maps first
defaultKustomization := filepath.Join(appPath, "/defaults")
err := env.ApplyKustomizations(ctx, defaultKustomization, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

// apply the yaml for the namespace
namespacePath := filepath.Join(appPath, "/cert-manager-namespace")
err = env.ApplyYAML(ctx, namespacePath, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

// create the priority class and resource quota
priorityClassResourceQuotaPath := filepath.Join(appPath, "/priorityclass-resource-quota")
err = env.ApplyYAML(ctx, priorityClassResourceQuotaPath, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

// apply the kustomization for the release
releasePath := filepath.Join(appPath, "/release")
err = env.ApplyKustomizations(ctx, releasePath, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

return err
}

func (r certManager) UpgradeRootCA(ctx context.Context, env *environment.Env) error {
appPath, err := absolutePathTo(r.Name())
if err != nil {
return err
}

err = r.installRootCA(ctx, env, appPath)
if err != nil {
return err
}

return nil
}

func (r certManager) InstallPreviousVersionRootCA(ctx context.Context, env *environment.Env) error {
appPath, err := getkAppsUpgradePath(r.Name())
if err != nil {
return err
}

err = r.installRootCA(ctx, env, appPath)
if err != nil {
return err
}

return nil
}

func (r certManager) InstallRootCA(ctx context.Context, env *environment.Env) error {
appPath, err := absolutePathTo(r.Name())
if err != nil {
return err
}

err = r.installRootCA(ctx, env, appPath)
if err != nil {
return err
}

return nil
}

func (r certManager) installRootCA(ctx context.Context, env *environment.Env, appPath string) error {
// apply the yaml for the namespace
rootCAPath := filepath.Join(appPath, "/root-ca")
err := env.ApplyYAML(ctx, rootCAPath, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

return err
}

func (r certManager) InstallTestCertificate(ctx context.Context, env *environment.Env) error {
testDataPath, err := getTestDataDir()
if err != nil {
return err
}

// apply the yaml for the namespace
certificatePath := filepath.Join(testDataPath, "/cert-manager")
err = env.ApplyYAML(ctx, certificatePath, map[string]string{
"releaseNamespace": kommanderNamespace,
})
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit dfc3313

Please sign in to comment.