Refactor e2e tests

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
pull/1721/head
Stefan Prodan 3 years ago
parent cec8b5336c
commit d1982e64b2
No known key found for this signature in database
GPG Key ID: 3299AEB0E4085BAF

@ -40,10 +40,10 @@ cleanup-kind:
rm $(TEST_KUBECONFIG)
test: $(EMBEDDED_MANIFESTS_TARGET) tidy fmt vet
go test ./... -coverprofile cover.out
go test ./... -coverprofile cover.out --tags=unit
e2e: $(EMBEDDED_MANIFESTS_TARGET) tidy fmt vet
TEST_KUBECONFIG=$(TEST_KUBECONFIG) go test ./cmd/flux/... -coverprofile cover.out --tags=e2e -parallel=1
TEST_KUBECONFIG=$(TEST_KUBECONFIG) go test ./cmd/flux/... -coverprofile e2e.cover.out --tags=e2e -v -failfast
test-with-kind: setup-envtest
make setup-kind

@ -177,7 +177,7 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error {
return err
}
utils.PrintTable(os.Stdout, header, rows)
utils.PrintTable(cmd.OutOrStderr(), header, rows)
if getAll {
fmt.Println()

@ -0,0 +1,57 @@
// +build e2e
package main
import "testing"
func TestHelmReleaseFromGit(t *testing.T) {
cases := []struct {
args string
goldenFile string
}{
{
"create source git thrfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0",
"testdata/helmrelease/create_source_git.golden",
},
{
"create helmrelease thrfg --source=GitRepository/thrfg --chart=./charts/podinfo",
"testdata/helmrelease/create_helmrelease_from_git.golden",
},
{
"get helmrelease thrfg",
"testdata/helmrelease/get_helmrelease_from_git.golden",
},
{
"reconcile helmrelease thrfg --with-source",
"testdata/helmrelease/reconcile_helmrelease_from_git.golden",
},
{
"suspend helmrelease thrfg",
"testdata/helmrelease/suspend_helmrelease_from_git.golden",
},
{
"resume helmrelease thrfg",
"testdata/helmrelease/resume_helmrelease_from_git.golden",
},
{
"delete helmrelease thrfg --silent",
"testdata/helmrelease/delete_helmrelease_from_git.golden",
},
}
namespace := "thrfg"
del, err := setupTestNamespace(namespace)
if err != nil {
t.Fatal(err)
}
defer del()
for _, tc := range cases {
cmd := cmdTestCase{
args: tc.args + " -n=" + namespace,
goldenFile: tc.goldenFile,
testClusterMode: ExistingClusterMode,
}
cmd.runTestCmd(t)
}
}

@ -1,54 +0,0 @@
// +build e2e
package main
import (
"testing"
"time"
)
func TestInstallNoArgs(t *testing.T) {
cmd := cmdTestCase{
args: "install",
wantError: false,
testClusterMode: ExistingClusterMode,
goldenFile: "testdata/install/install_no_args.golden",
}
cmd.runTestCmd(t)
testUninstallSilent(t)
time.Sleep(30 * time.Second)
}
func TestInstallExtraComponents(t *testing.T) {
cmd := cmdTestCase{
args: "install --components-extra=image-reflector-controller,image-automation-controller",
wantError: false,
testClusterMode: ExistingClusterMode,
goldenFile: "testdata/install/install_extra_components.golden",
}
cmd.runTestCmd(t)
testUninstallSilentForExtraComponents(t)
time.Sleep(30 * time.Second)
}
func testUninstallSilent(t *testing.T) {
cmd := cmdTestCase{
args: "uninstall -s",
wantError: false,
testClusterMode: ExistingClusterMode,
goldenFile: "testdata/uninstall/uninstall.golden",
}
cmd.runTestCmd(t)
}
func testUninstallSilentForExtraComponents(t *testing.T) {
cmd := cmdTestCase{
args: "uninstall -s",
wantError: false,
testClusterMode: ExistingClusterMode,
goldenFile: "testdata/uninstall/uninstall_extra_components.golden",
}
cmd.runTestCmd(t)
}

@ -0,0 +1,57 @@
// +build e2e
package main
import "testing"
func TestKustomizationFromGit(t *testing.T) {
cases := []struct {
args string
goldenFile string
}{
{
"create source git tkfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0",
"testdata/kustomization/create_source_git.golden",
},
{
"create kustomization tkfg --source=tkfg --path=./deploy/overlays/dev --prune=true --interval=5m --validation=client --health-check=Deployment/frontend.dev --health-check=Deployment/backend.dev --health-check-timeout=3m",
"testdata/kustomization/create_kustomization_from_git.golden",
},
{
"get kustomization tkfg",
"testdata/kustomization/get_kustomization_from_git.golden",
},
{
"reconcile kustomization tkfg --with-source",
"testdata/kustomization/reconcile_kustomization_from_git.golden",
},
{
"suspend kustomization tkfg",
"testdata/kustomization/suspend_kustomization_from_git.golden",
},
{
"resume kustomization tkfg",
"testdata/kustomization/resume_kustomization_from_git.golden",
},
{
"delete kustomization tkfg --silent",
"testdata/kustomization/delete_kustomization_from_git.golden",
},
}
namespace := "tkfg"
del, err := setupTestNamespace(namespace)
if err != nil {
t.Fatal(err)
}
defer del()
for _, tc := range cases {
cmd := cmdTestCase{
args: tc.args + " -n=" + namespace,
goldenFile: tc.goldenFile,
testClusterMode: ExistingClusterMode,
}
cmd.runTestCmd(t)
}
}

@ -0,0 +1,59 @@
// +build e2e
package main
import (
"context"
"fmt"
"os"
"testing"
"github.com/fluxcd/flux2/internal/utils"
)
func TestMain(m *testing.M) {
// Ensure tests print consistent timestamps regardless of timezone
os.Setenv("TZ", "UTC")
// Install Flux
km, err := NewTestEnvKubeManager(ExistingClusterMode)
if err != nil {
panic(fmt.Errorf("error creating kube manager: '%w'", err))
}
rootCtx.kubeManager = km
output, err := executeCommand("install --components-extra=image-reflector-controller,image-automation-controller")
if err != nil {
panic(fmt.Errorf("install falied: %s error:'%w'", output, err))
}
// Run tests
code := m.Run()
// Uninstall Flux
output, err = executeCommand("uninstall -s --keep-namespace")
if err != nil {
panic(fmt.Errorf("uninstall falied: %s error:'%w'", output, err))
}
// Delete namespace and wait for finalisation
kubectlArgs := []string{"delete", "namespace", "flux-system"}
_, err = utils.ExecKubectlCommand(context.TODO(), utils.ModeStderrOS, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...)
if err != nil {
panic(fmt.Errorf("delete namespace error:'%w'", err))
}
os.Exit(code)
}
func setupTestNamespace(namespace string) (func(), error) {
kubectlArgs := []string{"create", "namespace", namespace}
_, err := utils.ExecKubectlCommand(context.TODO(), utils.ModeStderrOS, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...)
if err != nil {
return nil, err
}
return func() {
kubectlArgs := []string{"delete", "namespace", namespace}
utils.ExecKubectlCommand(context.TODO(), utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...)
}, nil
}

@ -23,12 +23,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
)
func TestMain(m *testing.M) {
// Ensure tests print consistent timestamps regardless of timezone
os.Setenv("TZ", "UTC")
os.Exit(m.Run())
}
func readYamlObjects(objectFile string) ([]client.Object, error) {
obj, err := os.ReadFile(objectFile)
if err != nil {
@ -245,11 +239,3 @@ func executeCommand(cmd string) (string, error) {
return result, err
}
func TestVersion(t *testing.T) {
cmd := cmdTestCase{
args: "--version",
goldenValue: "flux version 0.0.0-dev.0\n",
}
cmd.runTestCmd(t)
}

@ -0,0 +1,14 @@
// +build unit
package main
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
// Ensure tests print consistent timestamps regardless of timezone
os.Setenv("TZ", "UTC")
os.Exit(m.Run())
}

@ -0,0 +1,6 @@
✚ generating HelmRelease
► applying HelmRelease
✔ HelmRelease created
◎ waiting for HelmRelease reconciliation
✔ HelmRelease thrfg is ready
✔ applied revision 6.0.0

@ -0,0 +1,6 @@
✚ generating GitRepository source
► applying GitRepository source
✔ GitRepository source created
◎ waiting for GitRepository source reconciliation
✔ GitRepository source reconciliation completed
✔ fetched revision: 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951

@ -0,0 +1,2 @@
► deleting helmreleases thrfg in thrfg namespace
✔ helmreleases deleted

@ -0,0 +1,2 @@
NAME READY MESSAGE REVISION SUSPENDED
thrfg True Release reconciliation succeeded 6.0.0 False

@ -0,0 +1,10 @@
► annotating GitRepository thrfg in thrfg namespace
✔ GitRepository annotated
◎ waiting for GitRepository reconciliation
✔ GitRepository reconciliation completed
✔ fetched revision 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951
► annotating HelmRelease thrfg in thrfg namespace
✔ HelmRelease annotated
◎ waiting for HelmRelease reconciliation
✔ HelmRelease reconciliation completed
✔ applied revision 6.0.0

@ -0,0 +1,5 @@
► resuming helmreleases thrfg in thrfg namespace
✔ helmreleases resumed
◎ waiting for HelmRelease reconciliation
✔ HelmRelease reconciliation completed
✔ applied revision 6.0.0

@ -0,0 +1,2 @@
► suspending helmreleases thrfg in thrfg namespace
✔ helmreleases suspended

@ -1,11 +0,0 @@
✚ generating manifests
✔ manifests build completed
► installing components in flux-system namespace
◎ verifying installation
✔ helm-controller: deployment ready
✔ image-automation-controller: deployment ready
✔ image-reflector-controller: deployment ready
✔ kustomize-controller: deployment ready
✔ notification-controller: deployment ready
✔ source-controller: deployment ready
✔ install finished

@ -1,9 +0,0 @@
✚ generating manifests
✔ manifests build completed
► installing components in flux-system namespace
◎ verifying installation
✔ helm-controller: deployment ready
✔ kustomize-controller: deployment ready
✔ notification-controller: deployment ready
✔ source-controller: deployment ready
✔ install finished

@ -0,0 +1,6 @@
✚ generating Kustomization
► applying Kustomization
✔ Kustomization created
◎ waiting for Kustomization reconciliation
✔ Kustomization tkfg is ready
✔ applied revision 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951

@ -0,0 +1,6 @@
✚ generating GitRepository source
► applying GitRepository source
✔ GitRepository source created
◎ waiting for GitRepository source reconciliation
✔ GitRepository source reconciliation completed
✔ fetched revision: 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951

@ -0,0 +1,2 @@
► deleting kustomizations tkfg in tkfg namespace
✔ kustomizations deleted

@ -0,0 +1,2 @@
NAME READY MESSAGE REVISION SUSPENDED
tkfg True Applied revision: 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951 False

@ -0,0 +1,10 @@
► annotating GitRepository tkfg in tkfg namespace
✔ GitRepository annotated
◎ waiting for GitRepository reconciliation
✔ GitRepository reconciliation completed
✔ fetched revision 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951
► annotating Kustomization tkfg in tkfg namespace
✔ Kustomization annotated
◎ waiting for Kustomization reconciliation
✔ Kustomization reconciliation completed
✔ applied revision 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951

@ -0,0 +1,5 @@
► resuming kustomizations tkfg in tkfg namespace
✔ kustomizations resumed
◎ waiting for Kustomization reconciliation
✔ Kustomization reconciliation completed
✔ applied revision 6.0.0/627d5c4bb67b77185f37e31d734b085019ff2951

@ -0,0 +1,2 @@
► suspending kustomizations tkfg in tkfg namespace
✔ kustomizations suspended

@ -1,31 +0,0 @@
► deleting components in flux-system namespace
✔ Deployment/flux-system/helm-controller deleted
✔ Deployment/flux-system/kustomize-controller deleted
✔ Deployment/flux-system/notification-controller deleted
✔ Deployment/flux-system/source-controller deleted
✔ Service/flux-system/notification-controller deleted
✔ Service/flux-system/source-controller deleted
✔ Service/flux-system/webhook-receiver deleted
✔ NetworkPolicy/flux-system/allow-egress deleted
✔ NetworkPolicy/flux-system/allow-scraping deleted
✔ NetworkPolicy/flux-system/allow-webhooks deleted
✔ ServiceAccount/flux-system/helm-controller deleted
✔ ServiceAccount/flux-system/kustomize-controller deleted
✔ ServiceAccount/flux-system/notification-controller deleted
✔ ServiceAccount/flux-system/source-controller deleted
✔ ClusterRole/crd-controller-flux-system deleted
✔ ClusterRoleBinding/cluster-reconciler-flux-system deleted
✔ ClusterRoleBinding/crd-controller-flux-system deleted
► deleting toolkit.fluxcd.io finalizers in all namespaces
► deleting toolkit.fluxcd.io custom resource definitions
✔ CustomResourceDefinition/alerts.notification.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/buckets.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/gitrepositories.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmcharts.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmreleases.helm.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmrepositories.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/kustomizations.kustomize.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/providers.notification.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/receivers.notification.toolkit.fluxcd.io deleted
✔ Namespace/flux-system deleted
✔ uninstall finished

@ -1,38 +0,0 @@
► deleting components in flux-system namespace
✔ Deployment/flux-system/helm-controller deleted
✔ Deployment/flux-system/image-automation-controller deleted
✔ Deployment/flux-system/image-reflector-controller deleted
✔ Deployment/flux-system/kustomize-controller deleted
✔ Deployment/flux-system/notification-controller deleted
✔ Deployment/flux-system/source-controller deleted
✔ Service/flux-system/notification-controller deleted
✔ Service/flux-system/source-controller deleted
✔ Service/flux-system/webhook-receiver deleted
✔ NetworkPolicy/flux-system/allow-egress deleted
✔ NetworkPolicy/flux-system/allow-scraping deleted
✔ NetworkPolicy/flux-system/allow-webhooks deleted
✔ ServiceAccount/flux-system/helm-controller deleted
✔ ServiceAccount/flux-system/image-automation-controller deleted
✔ ServiceAccount/flux-system/image-reflector-controller deleted
✔ ServiceAccount/flux-system/kustomize-controller deleted
✔ ServiceAccount/flux-system/notification-controller deleted
✔ ServiceAccount/flux-system/source-controller deleted
✔ ClusterRole/crd-controller-flux-system deleted
✔ ClusterRoleBinding/cluster-reconciler-flux-system deleted
✔ ClusterRoleBinding/crd-controller-flux-system deleted
► deleting toolkit.fluxcd.io finalizers in all namespaces
► deleting toolkit.fluxcd.io custom resource definitions
✔ CustomResourceDefinition/alerts.notification.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/buckets.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/gitrepositories.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmcharts.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmreleases.helm.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/helmrepositories.source.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/imagepolicies.image.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/imagerepositories.image.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/imageupdateautomations.image.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/kustomizations.kustomize.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/providers.notification.toolkit.fluxcd.io deleted
✔ CustomResourceDefinition/receivers.notification.toolkit.fluxcd.io deleted
✔ Namespace/flux-system deleted
✔ uninstall finished

@ -1,4 +1,4 @@
// +build !e2e
// +build unit
package main

@ -0,0 +1,15 @@
// +build unit
package main
import (
"testing"
)
func TestVersion(t *testing.T) {
cmd := cmdTestCase{
args: "--version",
goldenValue: "flux version 0.0.0-dev.0\n",
}
cmd.runTestCmd(t)
}
Loading…
Cancel
Save