Streamlining MongoDB Testing with CTO.ai

This blog post explores the process of testing a MongoDB NoSQL database using CTO.ai, a robust Continuous Integration/Continuous Deployment (CI/CD) platform. We'll explore each step, from setting up your environment to analyzing test results and providing comprehensive tutorials for developers and CTO.ai users.

Prerequisites

  • A GitHub repository with your MongoDB-based application.
  • An account on CTO.ai.
  • Basic understanding of YAML and MongoDB.

Set up the EC2 Workflow Infrastructure

The AWS EKS EC2 ASG Auto scaling Workflow is open source on GitHub, and you can install it by cloning the repository.

git clone git@github.com:workflows-sh/aws-eks-ec2-asg-cdk.git

cd aws-eks-ec2-asg-cdk



Create Secret for your Workflow from Settings

Create your secrets in your CTO.ai dashboard by selecting Settings and Secrets. Secrets are encrypted environment variables that CTO.ai utilizes within your workflow to build and run your application and deployments.

You will create four secrets:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_ACCOUNT_NUMBER
  • GITHUB_TOKEN
  • To create your AWS SECRET KEY AND ACCESS KEY. Log into your AWS Account, select the Identity and Access Management (IAM) dashboard, create a new user, copy the Access Key ID and Secret access key, and paste it into your secret dashboard on CTO.ai.
  • Your AWS ACCOUNT NUMBER can be obtained from your User ID on the top right corner of your AWS Console.

Generate GitHub Token

Generate your GITHUB_TOKEN from Github by going to Settings → Developer settings → Personal access tokens → Generate new token on your Github profile.

  • Back in your CTO.ai Secrets dashboard, create your secret key/value pairs.

Back in your AWS EKS EC2 ASG workflow, build and set up your infrastructure using the ops run -b . command. This will provision your AWS-EKS EC2 ASG stacks using Cloud Formation.

View EC2 instances on AWS

Back in your AWS console, you can see your EC2 instances running. The AWS EC2 Workflow creates four instances for you:

  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg
  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg
  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg

You can use any of the EC2 instance(s) to deploy your MongoDB NoSQL database.

Connect to Your EC2 Instance

Use SSH to connect to your instance. For example:

ssh -i /path/to/your-key.pem ec2-user@your-ec2-instance-public-ip

Install MongoDB

Update Your Package Database (if using Ubuntu or Debian):

sudo apt-get update

Install MongoDB (For Ubuntu/Debian)

sudo apt-get install -y mongodb

For Amazon Linux/CentOS:

sudo yum install -y mongodb-org

Start MongoDB and Enable it to Start on Boot

sudo systemctl start mongod

sudo systemctl enable mongod

Configure MongoDB

  • Edit the MongoDB config file (/etc/mongod.conf) to set up bind IP and other configurations.

  • Restart MongoDB to apply the changes:
sudo systemctl restart mongod

Test MongoDB Installation

Verify that MongoDB is running correctly

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

Configure Your CTO.ai Pipeline

The heart of CTO.ai integration lies in the ops.yaml file. This file contains instructions for the CTO.ai platform to set up your environment, run tests, and report results. A typical configuration might look like this:

version: "1"
pipelines:
- name: mongodb-test-pipeline-ec2:0.1.0
description: Build and test MongoDB with Node.js application on EC2
env:
static:
- DEBIAN_FRONTEND=noninteractive
- AWS_REGION=us-west-1
- NODE_VERSION=14.x  # Specify your Node.js version
secrets:
- GITHUB_TOKEN
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_ACCOUNT_NUMBER
- EC2_SSH_PRIVATE_KEY  # SSH key for EC2 access
- EC2_INSTANCE_IP      # Public IP of the EC2 instance
events:
- "github:<your-github-repo-path>:pull_request.merged"
- "github:<your-github-repo-path>:pull_request.opened"
- "github:<your-github-repo-path>:pull_request.synchronize"
jobs:
- name: mongodb-nodejs-test-job-ec2
description: MongoDB with Node.js test job on EC2
packages:
- git
- sshpass
steps:
- sshpass -p $EC2_INSTANCE_PASSWORD ssh -o StrictHostKeyChecking=no ec2-user@$EC2_INSTANCE_IP "sudo yum update -y"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "nvm install $NODE_VERSION"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "sudo yum install -y mongodb-org"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "sudo systemctl start mongod && sudo systemctl enable mongod"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "git clone https://oauth2:$GITHUB_TOKEN@github.com/$ORG/$REPO"
- sshpass -p $EC2_INSTANCE_PASSWORD ssh ec2-user@$EC2_INSTANCE_IP "cd $REPO && npm install && npm test"
services:
- name: mongodb-nodejs-service-ec2:0.1.0
description: MongoDB with Node.js service on EC2
run: node /ops/index.js
port: ['8080:8080']
sdk: off
domain: ""
env:
static:
- PORT=8080
events:
- "github:<your-github-repo-path>:pull_request.merged"
- "github:<your-github-repo-path>:pull_request.opened"
- "github:<your-github-repo-path>:pull_request.synchronize"
trigger:
- build
- publish
- start

Crafting MongoDB Tests

Testing a MongoDB database involves checking connection stability, data manipulation accuracy, and response to various queries. In a Node.js environment, we configured above using the ops.yaml file a test file can look like:

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
describe('MongoDB Operations', () => {
it('should perform a sample operation', async () => {
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
try {
await client.connect();
// Your test logic here
} catch (e) {
assert.fail("MongoDB operation failed");
} finally {
await client.close();
}
});
});

Pushing Code and Triggering Tests

With your tests in place, commit and push these changes to your repository. This action will trigger the CTO.ai pipeline, where the defined steps in your ops.yml  file will execute. CTO.ai orchestrates the entire process, from setting up the workflow and deploying your MongoDB database. You can also trigger the pipeline when you open a pull request.


Conclusion

Testing MongoDB with CTO.ai makes the process of ensuring database reliability and performance in your applications better and more reliable. By following this guide, you can set up an automated testing pipeline that aligns with modern deployment workflow practices, enhancing your software development lifecycle.

Ready to unlock the power of CTO.ai for your team? Schedule your consultation now with one of our experts today!