Skip to content

Commit

Permalink
Have better hygiene of e2e leftovers
Browse files Browse the repository at this point in the history
Signed-off-by: jose.vazquez <jose.vazquez@mongodb.com>
  • Loading branch information
josvazg committed May 10, 2024
1 parent fbbe7e0 commit 5a47361
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/actions/gen-install-scripts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
description: "Version of the Operator"
required: true
ENV:
description: "Kustomize patch name (enviroment configuration patch)"
description: "Kustomize patch name (environment configuration patch)"
required: true
runs:
using: 'docker'
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,5 @@ gen-sdlc-checklist: envsubst docker-sbom ## Generate the SDLC checklist
.PHONY: clear-e2e-leftovers
clear-e2e-leftovers: ## Clear the e2e test leftovers quickly
git restore bundle* config deploy
cd helm-charts && git restore .
git submodule update helm-charts
4 changes: 2 additions & 2 deletions test/e2e/helm_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ var _ = Describe("HELM charts", Ordered, func() {
Expect(err).Should(BeNil())
namespaceDeploymentJSON, err := json.MarshalIndent(namespaceDeployment, "", " ")
Expect(err).Should(BeNil())
utils.SaveToFile("namespace-deployment.json", namespaceDeploymentJSON)
utils.SaveToFile("../../output/namespace-deployment.json", namespaceDeploymentJSON)

pod, err := k8s.GetAllDeploymentPods("mongodb-atlas-operator", data.Resources.Namespace)
Expect(err).Should(BeNil())
podJSON, err := json.MarshalIndent(pod, "", " ")
Expect(err).Should(BeNil())
utils.SaveToFile("namespace-pod.json", podJSON)
utils.SaveToFile("../../output/namespace-pod.json", podJSON)

bytes, err := k8s.GetPodLogsByDeployment("mongodb-atlas-operator", config.DefaultOperatorNS, corev1.PodLogOptions{})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion test/helper/e2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
DefaultNamespacedOperatorConfig = "../../deploy/namespaced/namespaced-config.yaml"

// Default names/path for tests coordinates
DataGenFolder = "data/gen" // for generated configs
DataGenFolder = "../helper/e2e/data/gen" // for generated configs
DefaultOperatorNS = "mongodb-atlas-system"
DefaultOperatorName = "mongodb-atlas-operator"
DefaultOperatorGlobalKey = "mongodb-atlas-operator-api-key"
Expand Down Expand Up @@ -42,4 +42,7 @@ const (

// GCP
FileNameSAGCP = "../../output/gcp_service_account.json"

// X509 auth test PEM key
PEMCertFileName = "../../output/x509cert.pem"
)
17 changes: 9 additions & 8 deletions test/helper/e2e/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/pem"
"fmt"
"os"
"path/filepath"

. "github.com/onsi/gomega"
"github.com/sethvargo/go-password/password"
Expand All @@ -14,16 +15,17 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
k8scfg "sigs.k8s.io/controller-runtime/pkg/client/config"

akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/connectionsecret"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e/config"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e/utils"
)

func CreateNewClient() (client.Client, error) {
cfg, err := config.GetConfig()
cfg, err := k8scfg.GetConfig()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,26 +254,25 @@ func CreateCertificateX509(ctx context.Context, k8sClient client.Client, name, n
return fmt.Errorf("error generating x509 cert: %w", err)
}

certFileName := "x509cert.pem"
certFile, err := os.Create(certFileName)
certFile, err := os.Create(filepath.Clean(config.PEMCertFileName))
if err != nil {
return fmt.Errorf("failed to create file %s: %w", certFileName, err)
return fmt.Errorf("failed to create file %s: %w", config.PEMCertFileName, err)
}

err = pem.Encode(certFile, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
if err != nil {
return fmt.Errorf("failed to write data to %s: %w", certFileName, err)
return fmt.Errorf("failed to write data to %s: %w", config.PEMCertFileName, err)
}
err = certFile.Close()
if err != nil {
return fmt.Errorf("cant close file: %w", err)
}

var rawCert []byte
rawCert, err = os.ReadFile(certFileName)
rawCert, err = os.ReadFile(filepath.Clean(config.PEMCertFileName))
if err != nil {
return fmt.Errorf("failed to read cert file: %w", err)
}
Expand All @@ -282,7 +283,7 @@ func CreateCertificateX509(ctx context.Context, k8sClient client.Client, name, n
Namespace: ns,
},
Data: map[string][]byte{
certFileName: rawCert,
config.PEMCertFileName: rawCert,
},
}
certificateSecret.Labels = map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions test/helper/e2e/k8s/pod_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
k8scfg "sigs.k8s.io/controller-runtime/pkg/client/config"
)

func GetPodLogsByDeployment(deploymentName, deploymentNS string, options corev1.PodLogOptions) ([]byte, error) {
Expand All @@ -24,7 +24,7 @@ func GetPodLogsByDeployment(deploymentName, deploymentNS string, options corev1.
}

func GetPodLogs(options corev1.PodLogOptions, ns string, podName string) ([]byte, error) {
cfg, err := config.GetConfig()
cfg, err := k8scfg.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get config: %w", err)
}
Expand All @@ -50,7 +50,7 @@ func GetPodLogs(options corev1.PodLogOptions, ns string, podName string) ([]byte
}

func GetAllDeploymentPods(deploymentName, deploymentNS string) ([]corev1.Pod, error) {
cfg, err := config.GetConfig()
cfg, err := k8scfg.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get config: %w", err)
}
Expand All @@ -75,7 +75,7 @@ func GetAllDeploymentPods(deploymentName, deploymentNS string) ([]corev1.Pod, er
}

func GetDeployment(deploymentName, deploymentNS string) (*appsv1.Deployment, error) {
cfg, err := config.GetConfig()
cfg, err := k8scfg.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get config: %w", err)
}
Expand Down

0 comments on commit 5a47361

Please sign in to comment.