Skip to content

Commit

Permalink
build dependencies before test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
eliobischof committed Oct 4, 2024
1 parent 7c1c8e8 commit 21563b8
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/stretchr/testify/suite"
"github.com/zitadel/zitadel-charts/charts/zitadel/acceptance"
)

func TestPostgresInsecure(t *testing.T) {
t.Parallel()
example := "1-postgres-insecure"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
Expand All @@ -35,15 +34,15 @@ func TestPostgresSecure(t *testing.T) {
example := "2-postgres-secure"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
cfg.ExternalSecure,
func(cfg *acceptance.ConfigurationTest) {
func(cfg *ConfigurationTest) {
k8s.KubectlApply(t, cfg.KubeOptions, filepath.Join(workDir, "certs-job.yaml"))
k8s.WaitUntilJobSucceed(t, cfg.KubeOptions, "create-certs", 120, 3*time.Second)
},
Expand All @@ -57,10 +56,10 @@ func TestCockroachInsecure(t *testing.T) {
example := "3-cockroach-insecure"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Cockroach.WithValues(filepath.Join(workDir, "cockroach-values.yaml")),
Cockroach.WithValues(filepath.Join(workDir, "cockroach-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
Expand All @@ -76,16 +75,16 @@ func TestCockroachSecure(t *testing.T) {
example := "4-cockroach-secure"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Cockroach.WithValues(filepath.Join(workDir, "cockroach-values.yaml")),
Cockroach.WithValues(filepath.Join(workDir, "cockroach-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
cfg.ExternalSecure,
nil,
func(cfg *acceptance.ConfigurationTest) {
func(cfg *ConfigurationTest) {
k8s.KubectlApply(t, cfg.KubeOptions, filepath.Join(workDir, "zitadel-cert-job.yaml"))
k8s.WaitUntilJobSucceed(t, cfg.KubeOptions, "create-zitadel-cert", 120, 3*time.Second)
},
Expand All @@ -98,16 +97,16 @@ func TestReferencedSecrets(t *testing.T) {
example := "5-referenced-secrets"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
cfg.ExternalSecure,
nil,
func(cfg *acceptance.ConfigurationTest) {
func(cfg *ConfigurationTest) {
k8s.KubectlApply(t, cfg.KubeOptions, filepath.Join(workDir, "zitadel-secrets.yaml"))
k8s.KubectlApply(t, cfg.KubeOptions, filepath.Join(workDir, "zitadel-masterkey.yaml"))
},
Expand All @@ -121,10 +120,10 @@ func TestMachineUser(t *testing.T) {
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
saUsername := cfg.FirstInstance.Org.Machine.Machine.Username
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
Expand All @@ -140,10 +139,10 @@ func TestSelfSigned(t *testing.T) {
example := "7-self-signed"
workDir, valuesFile, values := readConfig(t, example)
cfg := values.Zitadel.ConfigmapConfig
suite.Run(t, acceptance.Configure(
suite.Run(t, Configure(
t,
newNamespaceIdentifier(example),
acceptance.Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
Postgres.WithValues(filepath.Join(workDir, "postgres-values.yaml")),
[]string{valuesFile},
cfg.ExternalDomain,
cfg.ExternalPort,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

func (s *ConfigurationTest) AfterTest(_, _ string) {
if s.afterZITADELFunc == nil || s.T().Failed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/zitadel/oidc/pkg/oidc"
"github.com/zitadel/zitadel-charts/charts/zitadel/acceptance"
mgmt_api "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management"
"net/http"
"net/url"
Expand All @@ -15,8 +14,8 @@ import (
"time"
)

func testAuthenticatedAPI(secretName, secretKey string) func(test *acceptance.ConfigurationTest) {
return func(cfg *acceptance.ConfigurationTest) {
func testAuthenticatedAPI(secretName, secretKey string) func(test *ConfigurationTest) {
return func(cfg *ConfigurationTest) {
t := cfg.T()
apiBaseURL := cfg.APIBaseURL()
secret := k8s.GetSecret(t, cfg.KubeOptions, secretName)
Expand All @@ -32,15 +31,15 @@ func testAuthenticatedAPI(secretName, secretKey string) func(test *acceptance.Co
if err != nil {
t.Fatal(err)
}
closeTunnel := acceptance.ServiceTunnel(cfg)
closeTunnel := ServiceTunnel(cfg)
defer closeTunnel()
var token string
acceptance.Await(cfg.Ctx, t, nil, 60, func(ctx context.Context) error {
Await(cfg.Ctx, t, nil, 60, func(ctx context.Context) error {
var tokenErr error
token, tokenErr = getToken(ctx, t, jwt, apiBaseURL)
return tokenErr
})
acceptance.Await(cfg.Ctx, t, nil, 60, func(ctx context.Context) error {
Await(cfg.Ctx, t, nil, 60, func(ctx context.Context) error {
if httpErr := callAuthenticatedHTTPEndpoint(ctx, token, apiBaseURL); httpErr != nil {
return httpErr
}
Expand All @@ -55,7 +54,7 @@ func getToken(ctx context.Context, t *testing.T, jwt, apiBaseURL string) (string
form.Add("scope", fmt.Sprintf("%s %s %s urn:zitadel:iam:org:project:id:zitadel:aud", oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail))
form.Add("assertion", jwt)
//nolint:bodyclose
resp, tokenBody, err := acceptance.HttpPost(ctx, fmt.Sprintf("%s/oauth/v2/token", apiBaseURL), func(req *http.Request) {
resp, tokenBody, err := HttpPost(ctx, fmt.Sprintf("%s/oauth/v2/token", apiBaseURL), func(req *http.Request) {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}, strings.NewReader(form.Encode()))
if err != nil {
Expand All @@ -77,7 +76,7 @@ func callAuthenticatedHTTPEndpoint(ctx context.Context, token, apiBaseURL string
checkCtx, checkCancel := context.WithTimeout(ctx, 5*time.Second)
defer checkCancel()
//nolint:bodyclose
resp, _, err := acceptance.HttpGet(checkCtx, fmt.Sprintf("%s/management/v1/languages", apiBaseURL), func(req *http.Request) {
resp, _, err := HttpGet(checkCtx, fmt.Sprintf("%s/management/v1/languages", apiBaseURL), func(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
})
if err != nil {
Expand All @@ -89,9 +88,9 @@ func callAuthenticatedHTTPEndpoint(ctx context.Context, token, apiBaseURL string
return nil
}

func callAuthenticatedGRPCEndpoint(cfg *acceptance.ConfigurationTest, key []byte) error {
func callAuthenticatedGRPCEndpoint(cfg *ConfigurationTest, key []byte) error {
t := cfg.T()
conn, err := acceptance.OpenGRPCConnection(cfg, key)
conn, err := OpenGRPCConnection(cfg, key)
if err != nil {
return fmt.Errorf("couldn't open gRPC connection: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import "github.com/gruntwork-io/terratest/modules/helm"

Expand All @@ -15,7 +15,7 @@ func (s *ConfigurationTest) BeforeTest(_, _ string) {
if s.dbChart.valuesFile != "" {
options.ValuesFiles = []string{s.dbChart.valuesFile}
}
helm.Install(s.T(), options, s.dbChart.name+"/"+s.dbChart.name, s.dbRelease)
helm.Install(s.T(), options, s.dbChart.Name+"/"+s.dbChart.Name, s.dbRelease)
if s.afterDBFunc != nil {
s.afterDBFunc(s)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package acceptance
package acceptance_test

import (
"context"
"fmt"
"path/filepath"
"testing"

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -17,40 +15,40 @@ type hookFunc func(*ConfigurationTest)

type ConfigurationTest struct {
suite.Suite
Ctx context.Context
log *logger.Logger
KubeOptions *k8s.KubectlOptions
KubeClient *kubernetes.Clientset
Scheme, Domain string
Port uint16
zitadelValues []string
dbChart *databaseChart
zitadelChartPath, zitadelRelease, dbRelease string
beforeFunc, afterDBFunc, afterZITADELFunc hookFunc
Ctx context.Context
log *logger.Logger
KubeOptions *k8s.KubectlOptions
KubeClient *kubernetes.Clientset
Scheme, Domain string
Port uint16
zitadelValues []string
dbChart *databaseChart
zitadelRelease, dbRelease string
beforeFunc, afterDBFunc, afterZITADELFunc hookFunc
}

func (c *ConfigurationTest) APIBaseURL() string {
return fmt.Sprintf(`%s://%s:%d`, c.Scheme, c.Domain, c.Port)
}

type databaseChart struct {
valuesFile, repoUrl, name, version string
valuesFile, RepoUrl, Name, version string
testValues map[string]string
}

var (
Cockroach = databaseChart{
repoUrl: "https://charts.cockroachdb.com/",
name: "cockroachdb",
RepoUrl: "https://charts.cockroachdb.com/",
Name: "cockroachdb",
version: "13.0.1",
testValues: map[string]string{
"statefulset.replicas": "1",
"conf.single-node": "true",
},
}
Postgres = databaseChart{
repoUrl: "https://charts.bitnami.com/bitnami",
name: "postgresql",
RepoUrl: "https://charts.bitnami.com/bitnami",
Name: "postgresql",
version: "12.10.0",
}
)
Expand All @@ -70,8 +68,6 @@ func Configure(
externalSecure bool,
before, afterDB, afterZITADEL hookFunc,
) *ConfigurationTest {
chartPath, err := filepath.Abs("..")
require.NoError(t, err)
kubeOptions := k8s.NewKubectlOptions("", "", namespace)
clientset, err := k8s.GetKubernetesClientFromOptionsE(t, kubeOptions)
if err != nil {
Expand All @@ -87,7 +83,6 @@ func Configure(
KubeOptions: kubeOptions,
KubeClient: clientset,
zitadelValues: zitadelValues,
zitadelChartPath: chartPath,
zitadelRelease: "zitadel-test",
dbChart: dbChart,
dbRelease: "db",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"fmt"
Expand Down
51 changes: 51 additions & 0 deletions charts/zitadel/acceptance_test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package acceptance_test

import (
"github.com/gruntwork-io/terratest/modules/helm"
terratesting "github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
"log"
"os"
"path/filepath"
"testing"
)

var ChartPath string

func TestMain(m *testing.M) {
t := &mockT{*log.New(os.Stderr, "", 0)}
var err error
ChartPath, err = filepath.Abs("..")
require.NoError(t, err)
helm.AddRepo(t, &helm.Options{}, Postgres.Name, Postgres.RepoUrl)
helm.AddRepo(t, &helm.Options{}, Cockroach.Name, Cockroach.RepoUrl)
_, err = helm.RunHelmCommandAndGetOutputE(t, &helm.Options{}, "dependencies", "build", ChartPath)
require.NoError(t, err)
m.Run()
}

var _ terratesting.TestingT = &mockT{}

type mockT struct {
log.Logger
}

func (m *mockT) Fail() {
m.Logger.Fatal("Fail() called")
}

func (m *mockT) FailNow() {
m.Logger.Fatal("FailNow() called")
}

func (m *mockT) Error(args ...interface{}) {
m.Logger.Fatalf("FailNow(%v) called", args)
}

func (m *mockT) Errorf(format string, args ...interface{}) {
m.Logger.Fatalf("FailNow(%s, %v) called", format, args)
}

func (m *mockT) Name() string {
return "TestMain"
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acceptance
package acceptance_test

import (
"testing"
Expand All @@ -18,8 +18,7 @@ func (s *ConfigurationTest) TestZITADELInstallation() {
"replicaCount": "1",
"pdb.enabled": "true",
},
BuildDependencies: true,
}, s.zitadelChartPath, s.zitadelRelease)
}, ChartPath, s.zitadelRelease)
k8s.WaitUntilJobSucceed(s.T(), s.KubeOptions, "zitadel-test-init", 900, time.Second)
k8s.WaitUntilJobSucceed(s.T(), s.KubeOptions, "zitadel-test-setup", 900, time.Second)
pods := listPods(s.T(), 5, s.KubeOptions)
Expand Down
Loading

0 comments on commit 21563b8

Please sign in to comment.