Writing Regression Tests for Apache AGE High Availability (AGEHA) or pgpool-II- A Quick Guide
Tags: apacheage, postgres, pgpoool, loadbalancing
Published: 2023-05-12 20:06:08

## Introduction

One of the key aspects of maintaining robust software is the consistent application of testing, and regression tests play a significant role in this process. They help ensure new changes don't disrupt existing functionality or introduce new bugs. In this blog post, we will guide you on writing regression tests for the AGEHA version of Pgpool-II (version 4.4.2), which is designed to boost the speed and provide high availability for Apache AGE queries.

Before proceeding, if you are unfamiliar with how to run regression tests in Pgpool-II, please refer to our previous blog post: **[Running Regression Test for AGE-AGEHA (AGE High Availability) and in Pgpool-II version 4.4.2](https://dev.to/kamleshmmb45/running-regression-test-for-age-ageha-age-high-availability-and-in-pgpool-ii-version-442-am0).**

## Understanding the Regression Test Script

First surface through the code avaliable [here](https://github.com/apache/age/pull/891/files).
**
An overview how these regression tests are working.**

To write your own regression tests, it's crucial to understand how the existing test script works. Let's take a look at the structure of a regression test script for Pgpool-II (AGEHA):

The script begins with setting up the test environment, which includes sourcing necessary libraries and setting up directories. Then, the test environment for Pgpool-II is created with the pgpool_setup command. After the setup, the script starts the Pgpool-II servers and sets up the necessary environment variables.

Next, the script creates a new graph database in each PostgreSQL backend and initiates some data. After that, it executes various Cypher queries to interact with the database.

The script then checks the pgpool logs to ensure the queries have been executed on the correct backend nodes, depending on the type of the query (read or write) and the weights of the backend nodes. If the queries haven't been executed as expected, the script exits with a failure message. Otherwise, it continues to the next set of queries.

Finally, the script shuts down all servers and exits with a success message.



## Example: Writing a Regression Test for Simple Load Balancing

In this example, we will explain the process of writing a regression test for simple load balancing using Cypher queries. We will follow the pattern shown in the provided code snippet.

The test is designed to check whether the load balancing works as expected when executing Cypher queries. Here's a breakdown of the test steps:

1. **Execute Cypher queries to create vertices:** The following two queries are executed to create two vertices with different properties in the graph.

Follwoing code is taken from [here](https://github.com/apache/age/pull/891/files).
```
$PSQL template1 <<EOF
SELECT * FROM cypher('test_graph', \$\$ CREATE (:vertex1 {i: 123}) \$\$) as (v agtype);
SELECT * FROM cypher('test_graph', \$\$ CREATE (:vertex2 {i: 124}) \$\$) as (v agtype);
EOF
```

2. **Check if simple load balancing worked:** After executing the queries, the script checks the pgpool logs to ensure that the queries were executed on the correct backend node (in this case, DB node id: 1). This is done using the fgrep command, which searches for the expected log message in the log/pgpool.log file. If the message is found, the test proceeds; otherwise, the script exits with a failure message.

```
fgrep "SELECT * FROM cypher('test_graph', \$\$ MATCH (v) RETURN v \$\$) as (v agtype);" log/pgpool.log |grep "DB node id: 1">/dev/null 2>&1
if [ $? != 0 ];then
# expected result not found
	echo fail: select is sent to zero-weight node.
	./shutdownall
	exit 1
fi
echo ok: simple load balance works.
```

This example demonstrates a simple regression test for load balancing. You can follow this pattern to create your own tests by modifying the Cypher queries and the expected log messages.


## About the .sh File

The .sh file is a shell script file that contains a series of commands to be executed by the shell interpreter. In the context of writing regression tests, the .sh file contains the test script, which sets up the test environment, executes the Cypher queries, checks the results, and cleans up the test environment after completion.


## Writing Your Own Regression Test

Now that you understand the structure of an existing regression test, let's explore how you can write your own. Here are some steps to get started:

1. **Set Up the Test Environment:** At the beginning of your script, you should set up the test environment similar to the one in the existing test script.

2. **Create Your Test Data:** Depending on the specific feature or functionality you're testing, you may need to create your own test data. This could involve creating a new graph database and populating it with data, or modifying existing data.

3. **Write Your Test Queries:** The core part of your regression test will be the Cypher queries you use to interact with the database. Try to cover as many edge cases as possible to ensure the feature or functionality is robust.

4. **Check the Results:** After executing your test queries, you should check whether the queries were executed as expected. You can do this by checking the pgpool logs, as in the existing test script, or by querying the database and comparing the results to the expected output.

5. **Clean Up:** Finally, clean up the test environment by shutting down all servers and removing any temporary files or directories.

Remember, the key to writing a good regression test is to cover as many edge cases as possible and to check the results thoroughly to ensure the system behaves as expected. Happy testing!


Further references:
Apache-age AGEHA(Pgpool-II) repo: https://github.com/apache/age/tree/AGEHA
Apache-age-repo: https://github.com/apache/age/
Apache-age documentation: https://age.apache.org/age-manual/master/intro/overview.html