diff --git a/Earthfile b/Earthfile index 2fba0d5e79..4400b52c62 100644 --- a/Earthfile +++ b/Earthfile @@ -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 @@ -122,6 +119,9 @@ 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... @@ -129,13 +129,12 @@ integration-test: # 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 @@ -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 diff --git a/database/migrations/postgres/1720012750850077_rename_commit_events.up.sql b/database/migrations/postgres/1720012750850077_rename_commit_events.up.sql index f2b47efcec..86203cd663 100644 --- a/database/migrations/postgres/1720012750850077_rename_commit_events.up.sql +++ b/database/migrations/postgres/1720012750850077_rename_commit_events.up.sql @@ -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 $$; diff --git a/database/migrations/postgres/1720014158596691_fk_commit_events.up.sql b/database/migrations/postgres/1720014158596691_fk_commit_events.up.sql index 1f34c58b18..26b5ac88a2 100644 --- a/database/migrations/postgres/1720014158596691_fk_commit_events.up.sql +++ b/database/migrations/postgres/1720014158596691_fk_commit_events.up.sql @@ -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; \ No newline at end of file +ADD COLUMN IF NOT EXISTS transformerEslId INTEGER CONSTRAINT fk_commit_events_transformer_id REFERENCES event_sourcing_light(eslId) default 0; +end if; +END $$; \ No newline at end of file diff --git a/database/migrations/postgres/1720428454051313_fk_deployments.up.sql b/database/migrations/postgres/1720428454051313_fk_deployments.up.sql index cd4fbbfa97..dd4c226bd2 100644 --- a/database/migrations/postgres/1720428454051313_fk_deployments.up.sql +++ b/database/migrations/postgres/1720428454051313_fk_deployments.up.sql @@ -1 +1,10 @@ -ALTER TABLE deployments ADD COLUMN IF NOT EXISTS transformerEslId INTEGER; \ No newline at end of file +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 $$; \ No newline at end of file diff --git a/database/migrations/postgres/1721378995777505_rename_application_locks.up.sql b/database/migrations/postgres/1721378995777505_rename_application_locks.up.sql index b1c2b58afb..b8eac29d2e 100644 --- a/database/migrations/postgres/1721378995777505_rename_application_locks.up.sql +++ b/database/migrations/postgres/1721378995777505_rename_application_locks.up.sql @@ -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 $$; \ No newline at end of file diff --git a/tests/integration-tests/cluster-setup/argocd-kuberpult.sh b/tests/integration-tests/cluster-setup/argocd-kuberpult.sh index 2afd5e0d65..34b45e8d63 100755 --- a/tests/integration-tests/cluster-setup/argocd-kuberpult.sh +++ b/tests/integration-tests/cluster-setup/argocd-kuberpult.sh @@ -203,6 +203,7 @@ $(sed -e "s/^/ /" templates/generated-migrations.yaml rm -rf migrations diff --git a/tests/integration-tests/cluster-setup/setup-postgres.sh b/tests/integration-tests/cluster-setup/setup-postgres.sh index 22f967a527..de02aa7a7c 100755 --- a/tests/integration-tests/cluster-setup/setup-postgres.sh +++ b/tests/integration-tests/cluster-setup/setup-postgres.sh @@ -3,6 +3,10 @@ set -eu set -o pipefail +function print() { + /bin/echo "$0:" "$@" +} + function waitForDeployment() { ns="$1" label="$2" @@ -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 - <. + +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) + } + }) + } + +}