Skip to content

Commit

Permalink
Merge branch 'main' into AN/refactor_rollout_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-nour-fdc authored Jul 25, 2024
2 parents 4e5c627 + f868d29 commit b7168f0
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18 deletions.
23 changes: 11 additions & 12 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ integration-test-deps:
SAVE ARTIFACT /usr/bin/argocd

integration-test:
# We pick ubuntu here because it seems to have the least amount of issues.
# With alpine:3.18, we get occasional issues with gpg that says there's a process running already, even though there shouldn't be.
# Ubuntu:22.04 seems to solve this issue.
FROM ubuntu:22.04
FROM golang:1.21-bookworm
RUN apt update && apt install --auto-remove -y curl gpg gpg-agent gettext bash git golang netcat-openbsd docker.io
ARG GO_TEST_ARGS
# K3S environment variables
Expand All @@ -122,20 +119,22 @@ integration-test:
COPY +integration-test-deps/* /usr/bin/
COPY tests/integration-tests/cluster-setup/docker-compose-k3s.yml .

COPY go.mod go.sum .
RUN go mod download

RUN --no-cache echo GPG gen starting...
RUN --no-cache gpg --keyring trustedkeys-kuberpult.gpg --no-default-keyring --batch --passphrase '' --quick-gen-key kuberpult-kind@example.com
RUN --no-cache echo GPG export starting...
RUN --no-cache gpg --keyring trustedkeys-kuberpult.gpg --armor --export kuberpult-kind@example.com > /kp/kuberpult-keyring.gpg
# Note that multiple commands here are writing to "." which is slightly dangerous, because
# if there are files with the same name, old ones will be overridden.
COPY charts/kuberpult .
COPY database/migrations migrations
COPY database/migrations database/migrations
COPY infrastructure/scripts/create-testdata/testdata_template/environments environments

COPY infrastructure/scripts/create-testdata/create-release.sh .
COPY tests/integration-tests integration-tests
COPY go.mod go.sum .
COPY pkg/conversion pkg/conversion
COPY tests/integration-tests tests/integration-tests
COPY ./pkg+artifacts/pkg pkg

ARG --required kuberpult_version
ENV VERSION=$kuberpult_version
Expand All @@ -146,9 +145,9 @@ integration-test:
set -e; \
echo Waiting for K3s cluster to be ready; \
sleep 10 && kubectl wait --for=condition=Ready nodes --all --timeout=300s && sleep 3; \
./integration-tests/cluster-setup/setup-cluster-ssh.sh& \
./integration-tests/cluster-setup/setup-postgres.sh& \
./integration-tests/cluster-setup/argocd-kuberpult.sh && \
cd integration-tests && go test $GO_TEST_ARGS ./... || ./cluster-setup/get-logs.sh; \
./tests/integration-tests/cluster-setup/setup-cluster-ssh.sh& \
./tests/integration-tests/cluster-setup/setup-postgres.sh& \
./tests/integration-tests/cluster-setup/argocd-kuberpult.sh && \
cd tests/integration-tests && go test $GO_TEST_ARGS ./... || ./cluster-setup/get-logs.sh; \
echo ============ SUCCESS ============
END
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
ALTER TABLE IF EXISTS events
RENAME TO commit_events;
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'commit_events') THEN
ALTER TABLE IF EXISTS events RENAME TO commit_events;
END IF;
END $$;
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
DO $$ BEGIN IF NOT EXISTS (
SELECT 'fk_commit_events_transformer_id'
FROM information_schema.table_constraints
WHERE table_name = 'commit_events'
AND constraint_name = 'fk_commit_events_transformer_id'
) THEN
ALTER TABLE commit_events
ADD COLUMN IF NOT EXISTS transformerEslId INTEGER
CONSTRAINT fk_commit_events_transformer_id
REFERENCES event_sourcing_light(eslId) default 0;
ADD COLUMN IF NOT EXISTS transformerEslId INTEGER CONSTRAINT fk_commit_events_transformer_id REFERENCES event_sourcing_light(eslId) default 0;
end if;
END $$;
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
ALTER TABLE deployments ADD COLUMN IF NOT EXISTS transformerEslId INTEGER;
DO $$ BEGIN IF NOT EXISTS (
SELECT 'fk_deployments_transformer_id'
FROM information_schema.table_constraints
WHERE table_name = 'deployments'
AND constraint_name = 'fk_deployments_transformer_id'
) THEN
ALTER TABLE deployments
ADD COLUMN IF NOT EXISTS transformerEslId INTEGER CONSTRAINT fk_deployments_transformer_id REFERENCES event_sourcing_light(eslId) default 0;
end if;
END $$;
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
DO $$ BEGIN IF NOT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = 'app_locks'
) THEN
ALTER TABLE IF EXISTS application_locks
RENAME TO app_locks;
END IF;
END $$;
1 change: 1 addition & 0 deletions tests/integration-tests/cluster-setup/argocd-kuberpult.sh
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ $(sed -e "s/^/ /" </kp/kuberpult-keyring.gpg)
VALUES

# Get helm dependency charts and unzip them
cp -r database/migrations migrations
(rm -rf charts && helm dep update && cd charts && for filename in *.tgz; do tar -xf "$filename" && rm -f "$filename"; done;)
helm template -s templates/migrations.yaml ./ -f values.yaml --set git.url=test --set ingress.domainName=kuberpult.example.com > templates/generated-migrations.yaml
rm -rf migrations
Expand Down
20 changes: 20 additions & 0 deletions tests/integration-tests/cluster-setup/setup-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
set -eu
set -o pipefail

function print() {
/bin/echo "$0:" "$@"
}

function waitForDeployment() {
ns="$1"
label="$2"
Expand All @@ -18,6 +22,21 @@ function waitForDeployment() {
done
}

function portForwardAndWait() {
ns="$1"
deployment="$2"
portHere="$3"
portThere="$4"
ports="$portHere:$portThere"
print "portForwardAndWait for $ns/$deployment $ports"
kubectl -n "$ns" port-forward "$deployment" "$ports" &
print "portForwardAndWait: waiting until the port forward works..."
until nc -vz localhost "$portHere"
do
sleep 1s
done
}

kubectl apply -f - <<EOF
---
apiVersion: v1
Expand Down Expand Up @@ -68,4 +87,5 @@ spec:
EOF

waitForDeployment default "app=postgres"
portForwardAndWait "default" deploy/postgres "5432" "5432"
echo "done setting up postgres"
82 changes: 82 additions & 0 deletions tests/integration-tests/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright freiheit.com*/

package integration_tests

import (
"testing"

"github.com/freiheit-com/kuberpult/pkg/db"
)

func deleteSchemaMigrationsTable(cfg db.DBConfig) error {
db, err := db.GetDBConnection(cfg)
if err != nil {
return err
}
_, err = db.Exec("DROP TABLE schema_migrations")
if err != nil {
return err
}
return nil
}
func TestMigrations(t *testing.T) {
dbConfig := db.DBConfig{
DbHost: "localhost",
DbPort: "5432",
DbUser: "postgres",
DbPassword: "mypassword",
DbName: "kuberpult",
DriverName: "postgres",
MigrationsPath: "../../database/migrations/postgres",
}
dbHandler, err := db.Connect(dbConfig)
if err != nil {
t.Fatalf("Error establishing DB connection: %v", err)
}
pErr := dbHandler.DB.Ping()
if pErr != nil {
t.Fatalf("Error pinging database: %v", err)
}
if err := deleteSchemaMigrationsTable(dbConfig); err != nil {
t.Fatalf("Failed to delete schema migrations table: %v", err)
}
testCases := []struct {
name string
}{
{
name: "Running migrations multiple times",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Run migrations for the first time
if err := db.RunDBMigrations(dbConfig); err != nil {
t.Errorf("Error running migrations: %v", err)
}
// Delete schema migrations
if err := deleteSchemaMigrationsTable(dbConfig); err != nil {
t.Fatalf("Failed to delete schema migrations table: %v", err)
}
// Run migrations again
if err := db.RunDBMigrations(dbConfig); err != nil {
t.Errorf("Error running migrations: %v", err)
}
})
}

}

0 comments on commit b7168f0

Please sign in to comment.