3 minute read

Redis - Wikipedia

Redis is an in-memory data structure store which can be used as a database cache and supports different data types, most notably a simple key, value pair (aka dict), lists, etc.

It could be used for any relevant purpose within your test automation framework, for instance, one of the use cases that I found helpful was that we could use it as a cache of temporary auth information for test user accounts, which could be retrieved by tests running in parallel and avoid calling the actual auth APIs every time, Thus cutting a bit of external dependencies and making your tests faster.

Redis provides a server and a CLI interface if you want to play with the data and a bunch of client libraries for different programming languages to use them in your solution

In this post, we would see how to set up redis on an existing Ubuntu VM and how to interact with it for a very simple use case via redis-cli and then see what a simple kotlin file could look like to have an abstraction over this.

Installation and setup of redis server on an Ubuntu VM

Pre-requisites:

  • Ensure you have ubuntu already setup on your machine or have a VM created.
  • SSH into the VM or open terminal app
  • Execute sudo suto elevate your permissions to root
  • Ensure you have the GCC compiler already installed, if not, follow the below steps to set it up.
sudo apt update
sudo apt install build-essential
gcc --version

Install Redis via sudo apt

sudo apt update
sudo apt install redis-server

Expose the redis server to outside the VM

By default, redis would start running on localhost (127.0.0.1) , however, if you want to use it for your use cases, you would need to update the IP which it binds to in redis.conf file

Open the file in your comfortable editor of choice

sudo vim /etc/redis/redis.conf

Inside redis.conf file, update

bind 127.0.0.1

to

bind 0.0.0.0

Restart redis service

sudo systemctl restart redis.service

Once done, check if the redis service is running

sudo systemctl status redis

You should see an output like below:

redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since ... ;machines_local_timezone...;; 4min 15s ago
......

Note: I have intentionally removed some part of this output as it held sensitive information, However, you should see details around your VM/machines hostname and CPU/memory stats for your current redis instance.

Cool, 🥳 with this we have already set up a redis server on Ubuntu.

🤾🏻‍♂️ Play with redis-cli

To open redis-cli just type below

redis-cli

We can create a dummy key and set its value via the set command and get its value.

127.0.0.1:6379> set "test-key" "test-value"
OK
127.0.0.1:6379> get "test-key"
"test-value"

And that’s it.

If you want to connect to this from your local machine or runner, enter below command (provided you have redis-cli already setup)

redis-cli -h host_name_or_ip -p 6379

How do we interact with this in our automation code?

Redis supports multiple clients to interact with the redis server. In below example, we have chosen to use <a href="https://redis.io/clients#java">Jedis</a>, however based on your needs you could go with any other client as well.

Add below line in your build.gradle file

compile group: 'redis.clients', name: 'jedis', version: '2.9.0'

In below class, we create a class RedisHandler which accepts a host and port (where your redis server is running) and then has simple get(key) and put(key, value) methods to work with redis (quite similar to how we used the cli above)

Simple Kotlin file with an abstraction over jedis (JVM support library for redis)

And that’s a wrap 🥳. Hopefully you would find creative use cases to use redis augment your automated tests. If you found this useful, do share with a friend or colleague. Until next time. Happy testing and automating!

Further read on Redis, its data types and cli utility:

Comments