Skip to content

PTEUDO-1096 perform reconcile tests on PR builds #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pipeline {
sudo apt-get update

if ! which psql > /dev/null; then


timeout 300 bash -c -- 'while sudo fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1
do
echo "Waiting to get lock /var/lib/dpkg/lock-frontend..."
sleep 5
done'
sudo apt-get install -y postgresql-client-14
fi

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test: manifests generate fmt vet envtest ## Run tests.
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
test-e2e: NS?=$(shell cat .id)
test-e2e:
NAMESPACE=$(NS) go test ./test/e2e/ -v -ginkgo.v
NAMESPACE=$(NS) ENV=box-3 go test ./test/e2e/ -v -ginkgo.v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of creating a file like the ".id" one to put the server instead of hardcoding it here?


.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter
Expand Down
2 changes: 1 addition & 1 deletion Makefile.infoblox
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ push-images: docker-push-db-controller docker-push-dbproxy docker-push-dsnexec
${HELM_SETFLAGS}
# Consider removing this, since we dont actually no
# helm upgrade is applied in the cluster
@touch $@
#@touch $@

deploy: package-chart-db-controller .deploy-$(GIT_COMMIT)

Expand Down
3 changes: 1 addition & 2 deletions cmd/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ passwordConfig:
minPasswordLength: 15
passwordRotationPeriod: 60
sample-connection:
masterUsername: root
masterUsername: postgres
username: postgres
host: localhost
port: 5432
sslMode: disable
passwordSecretRef: postgres-postgresql
passwordSecretKey: postgresql-password
# host omitted, allocates database dynamically
dynamic-connection:
masterUsername: root
Expand Down
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ func main() {
}

dbClaimConfig := &databaseclaim.DatabaseClaimConfig{
Viper: ctlConfig,

Viper: ctlConfig,
Namespace: os.Getenv("SERVICE_NAMESPACE"),
Class: class,
DbIdentifierPrefix: dbIdentifierPrefix,
// Log: ctrl.Log.WithName("controllers").WithName("DatabaseClaim").V(controllers.InfoLevel),
MasterAuth: rdsauth.NewMasterAuth(),
MetricsEnabled: true,
MetricsDepYamlPath: metricsDepYamlPath,
MetricsConfigYamlPath: metricsConfigYamlPath,
}
Expand Down
10 changes: 7 additions & 3 deletions internal/controller/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ func (r *DatabaseClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return r.reconciler.Reconcile(ctx, req)
}

// SetupWithManager sets up the controller with the Manager.
func (r *DatabaseClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {

func (r *DatabaseClaimReconciler) Setup() {
r.reconciler = &databaseclaim.DatabaseClaimReconciler{
Client: r.Client,
Config: r.Config,
}
}

// SetupWithManager sets up the controller with the Manager.
func (r *DatabaseClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {

r.Setup()

return ctrl.NewControllerManagedBy(mgr).
For(&persistancev1.DatabaseClaim{}).
Expand Down
164 changes: 163 additions & 1 deletion internal/controller/databaseclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@ limitations under the License.
package controller

import (
"context"
"net/url"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
//. "github.com/onsi/gomega"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

persistancev1 "github.com/infobloxopen/db-controller/api/v1"
"github.com/infobloxopen/db-controller/pkg/config"
"github.com/infobloxopen/db-controller/pkg/databaseclaim"
)

var _ = Describe("DatabaseClaim Controller", func() {
Expand Down Expand Up @@ -78,3 +95,148 @@ var _ = Describe("DatabaseClaim Controller", func() {
})

})

var _ = Describe("db-controller", func() {

// Define utility constants for object names and testing timeouts/durations and intervals.

Context("When updating DB Claim Status", func() {

const resourceName = "test-dbclaim"
const secretName = "postgres-postgresql"

ctx := context.Background()
typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
typeNamespacedSecretName := types.NamespacedName{
Name: secretName,
Namespace: "default", // TODO(user):Modify as needed
}
claim := &persistancev1.DatabaseClaim{}

BeforeEach(func() {
parsedDSN, err := url.Parse(testDSN)
Expect(err).NotTo(HaveOccurred())
password, ok := parsedDSN.User.Password()
Expect(ok).To(BeTrue())

By("creating the custom resource for the Kind DatabaseClaim")
err = k8sClient.Get(ctx, typeNamespacedName, claim)
if err != nil && errors.IsNotFound(err) {
resource := &persistancev1.DatabaseClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: "persistance.atlas.infoblox.com/v1",
Kind: "DatabaseClaim",
},
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
Spec: persistancev1.DatabaseClaimSpec{
Class: ptr.To(""),
AppID: "sample-app",
DatabaseName: "sample_app",
InstanceLabel: "sample-connection",
SecretName: secretName,
Username: parsedDSN.User.Username(),
EnableSuperUser: ptr.To(false),
EnableReplicationRole: ptr.To(false),
UseExistingSource: ptr.To(false),
Type: "postgres",

Port: parsedDSN.Port(),
Host: parsedDSN.Hostname(),
},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}

secret := &corev1.Secret{}
err = k8sClient.Get(ctx, typeNamespacedSecretName, secret)
if err != nil && errors.IsNotFound(err) {
resource := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: "default",
},
StringData: map[string]string{
"password": password,
},
Type: "Opaque",
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())

}

})

AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &persistancev1.DatabaseClaim{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance DatabaseClaim")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())

secret := &corev1.Secret{}
err = k8sClient.Get(ctx, typeNamespacedSecretName, secret)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the database secret")

Expect(k8sClient.Delete(ctx, secret)).To(Succeed())

})

It("Should update DB Claim status", func() {

By("Reconciling the created resource")

configPath, err := filepath.Abs(filepath.Join("..", "..", "cmd", "config", "config.yaml"))
Expect(err).NotTo(HaveOccurred())

controllerReconciler := &DatabaseClaimReconciler{
Config: &databaseclaim.DatabaseClaimConfig{
Viper: config.NewConfig(logger, configPath),
Namespace: "default",
},
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
controllerReconciler.Setup()

// FIXME: make these actual properties on the reconciler struct
controllerReconciler.Config.Viper.Set("defaultMasterusername", "postgres")
controllerReconciler.Config.Viper.Set("defaultSslMode", "require")

_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())

Eventually(func() bool {
resource := &persistancev1.DatabaseClaim{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
if err != nil {
return false
}
return resource.Status.Error == ""

}, 10*time.Second, 100*time.Millisecond).Should(BeTrue())

})
})
})

func TestDB(t *testing.T) {
db, _, cleanup := RunDB()
defer cleanup()
defer db.Close()
_, err := db.Exec("CREATE TABLE test (id SERIAL PRIMARY KEY, name TEXT)")
if err != nil {
panic(err)
}
}
33 changes: 27 additions & 6 deletions internal/controller/dbroleclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func TestReconcileDbRoleClaim_CopyExistingSecret(t *testing.T) {
Name: resourceName,
Namespace: "default",
}
typeNamespacedClaimName := types.NamespacedName{
Name: "testdbclaim",
Namespace: "default",
}
typeNamespacedSecretName := types.NamespacedName{
Name: "master-secret",
Namespace: "default",
}

dbroleclaim := &persistancev1.DbRoleClaim{}
viperObj := viper.New()
viperObj.Set("passwordconfig::passwordRotationPeriod", 60)
Expand All @@ -77,7 +86,11 @@ func TestReconcileDbRoleClaim_CopyExistingSecret(t *testing.T) {
Status: persistancev1.DbRoleClaimStatus{},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}

dbclaim := persistancev1.DatabaseClaim{}
err = k8sClient.Get(ctx, typeNamespacedClaimName, &dbclaim)
if err != nil && errors.IsNotFound(err) {
dbClaim := &persistancev1.DatabaseClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "testdbclaim",
Expand All @@ -91,14 +104,22 @@ func TestReconcileDbRoleClaim_CopyExistingSecret(t *testing.T) {
Status: persistancev1.DatabaseClaimStatus{},
}
Expect(k8sClient.Create(ctx, dbClaim)).To(Succeed())
}

secret := corev1.Secret{}
err = k8sClient.Get(ctx, typeNamespacedSecretName, &secret)
if err != nil && errors.IsNotFound(err) {

sec := &corev1.Secret{}
sec.Data = map[string][]byte{
"password": []byte("masterpassword"),
"username": []byte("user_a"),
sec := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "master-secret",
Namespace: "default",
},
Data: map[string][]byte{
"password": []byte("masterpassword"),
"username": []byte("user_a"),
},
}
sec.Name = "master-secret"
sec.Namespace = "default"
Expect(k8sClient.Create(ctx, sec)).To(Succeed())
}
})
Expand Down
25 changes: 25 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ limitations under the License.
package controller

import (
"database/sql"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -42,17 +46,26 @@ import (
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var namespace string
var logger logr.Logger

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Controller Suite")

logger = funcr.New(func(prefix, args string) {
t.Log(prefix, args)
}, funcr.Options{
Verbosity: 1,
})
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
namespace = "default"
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
Expand Down Expand Up @@ -81,9 +94,21 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

now := time.Now()
testdb, testDSN, cleanupTestDB = RunDB()
logger.Info("postgres_setup_took", "duration", time.Since(now))

})

// Stand up postgres in a container
var (
testdb *sql.DB
testDSN string
cleanupTestDB func()
)

var _ = AfterSuite(func() {
cleanupTestDB()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading
Loading