From eb1d8e55d341b109a503759945f89ca530c6aafa Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Mon, 11 Nov 2024 13:23:22 +0300
Subject: [PATCH v3 2/3] Add NetBSD and OpenBSD tasks to the Postgres CI

NetBSD and OpenBSD Postgres CI images are generated [1] but their tasks
are not added to the upstream Postgres yet. This patch adds them.

Note: These tasks will be triggered manually to save CI credits but a
related line is commented out for now to trigger CFBot.

Author: Nazir Bilal Yavuz <byavuz81@gmail.com>

[1] https://github.com/anarazel/pg-vm-images
---
 .cirrus.tasks.yml   | 90 +++++++++++++++++++++++++++++++++++++++++++++
 .cirrus.yml         | 10 +++++
 src/tools/ci/README |  2 +-
 3 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml
index 18e944ca89d..43cca1aeb58 100644
--- a/.cirrus.tasks.yml
+++ b/.cirrus.tasks.yml
@@ -213,6 +213,96 @@ task:
     cores_script: src/tools/ci/cores_backtrace.sh freebsd /tmp/cores
 
 
+task:
+  depends_on: SanityCheck
+  # trigger_type: manual
+
+  env:
+    # Below are experimentally derived to be a decent choice.
+    CPUS: 4
+    BUILD_JOBS: 8
+    TEST_JOBS: 8
+
+    # Default working directory is /tmp but its total size (1.2 GB) is not
+    # enough, so different working and cache directory are set.
+    CIRRUS_WORKING_DIR: /home/postgres/postgres
+    CCACHE_DIR: /home/postgres/cache
+
+    PATH: /usr/sbin:$PATH
+
+  matrix:
+    - name: NetBSD - Meson
+      only_if: $CIRRUS_CHANGE_MESSAGE !=~ '.*\nci-os-only:.*' || $CIRRUS_CHANGE_MESSAGE =~ '.*\nci-os-only:[^\n]*netbsd.*'
+      env:
+        IMAGE_FAMILY: pg-ci-netbsd-postgres
+        PKGCONFIG_PATH: '/usr/lib/pkgconfig:/usr/pkg/lib/pkgconfig'
+        # initdb fails with: 'invalid locale settings' error on NetBSD.
+        # Force 'LANG' and 'LC_*' variables to be 'C'.
+        LANG: "C"
+        LC_ALL: "C"
+        # -Duuid is not set for the NetBSD, see the comment at the configure
+        # script for more information.
+      setup_additional_packages_script: |
+        #pkgin -y install ...
+      <<: *netbsd_task_template
+
+    - name: OpenBSD - Meson
+      only_if: $CIRRUS_CHANGE_MESSAGE !=~ '.*\nci-os-only:.*' || $CIRRUS_CHANGE_MESSAGE =~ '.*\nci-os-only:[^\n]*openbsd.*'
+      env:
+        IMAGE_FAMILY: pg-ci-openbsd-postgres
+        PKGCONFIG_PATH: '/usr/lib/pkgconfig:/usr/local/lib/pkgconfig'
+        UUID: -Duuid=e2fs
+        TCL: -Dtcl_version=tcl86
+      setup_additional_packages_script: |
+        #pkg_add -I ...
+      <<: *openbsd_task_template
+
+  sysinfo_script: |
+    locale
+    id
+    uname -a
+    ulimit -a -H && ulimit -a -S
+    env
+
+  ccache_cache:
+    folder: $CCACHE_DIR
+
+  create_user_script: |
+    useradd postgres
+    chown -R postgres:users /home/postgres
+    mkdir -p ${CCACHE_DIR}
+    chown -R postgres:users ${CCACHE_DIR}
+
+  # -Duuid=bsd is not set since 'bsd' uuid option
+  # is not working on NetBSD & OpenBSD. See
+  # https://www.postgresql.org/message-id/17358-89806e7420797025@postgresql.org
+  # And other uuid options are not available on NetBSD.
+  configure_script: |
+    su postgres <<-EOF
+      meson setup \
+        --buildtype=debugoptimized \
+        --pkg-config-path ${PKGCONFIG_PATH} \
+        -Dcassert=true -Dinjection_points=true \
+        -Dssl=openssl ${UUID} ${TCL} \
+        -DPG_TEST_EXTRA="$PG_TEST_EXTRA" \
+        build
+    EOF
+
+  build_script: su postgres -c 'ninja -C build -j${BUILD_JOBS}'
+  upload_caches: ccache
+
+  test_world_script: |
+    su postgres <<-EOF
+      ulimit -c unlimited
+      # Otherwise tests will fail on OpenBSD, due to the lack of enough processes.
+      ulimit -p 256
+      meson test $MTEST_ARGS --num-processes ${TEST_JOBS}
+    EOF
+
+  on_failure:
+    <<: *on_failure_meson
+
+
 # configure feature flags, shared between the task running the linux tests and
 # the CompilerWarnings task
 LINUX_CONFIGURE_FEATURES: &LINUX_CONFIGURE_FEATURES >-
diff --git a/.cirrus.yml b/.cirrus.yml
index a83129ae46d..33c6e481d74 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -52,6 +52,16 @@ default_freebsd_task_template: &freebsd_task_template
     PLATFORM: freebsd
   <<: *cirrus_community_vm_template
 
+default_netbsd_task_template: &netbsd_task_template
+  env:
+    PLATFORM: netbsd
+  <<: *cirrus_community_vm_template
+
+default_openbsd_task_template: &openbsd_task_template
+  env:
+    PLATFORM: openbsd
+  <<: *cirrus_community_vm_template
+
 
 default_windows_task_template: &windows_task_template
   env:
diff --git a/src/tools/ci/README b/src/tools/ci/README
index 30ddd200c96..dd703272d3f 100644
--- a/src/tools/ci/README
+++ b/src/tools/ci/README
@@ -61,7 +61,7 @@ Controlling CI via commit messages
 The behavior of CI can be controlled by special content in commit
 messages. Currently the following controls are available:
 
-- ci-os-only: {(freebsd|linux|macos|windows|mingw)}
+- ci-os-only: {(freebsd|netbsd|openbsd|linux|macos|windows|mingw)}
 
   Only runs CI on operating systems specified. This can be useful when
   addressing portability issues affecting only a subset of platforms.
-- 
2.47.1

