Skip to content

Commit

Permalink
Merge pull request #48 from CSCI-GA-2820-SP24-001/config-bdd
Browse files Browse the repository at this point in the history
Config bdd
  • Loading branch information
Prasanna721 authored Apr 10, 2024
2 parents e276db0 + 5be9dce commit 8a59e1c
Show file tree
Hide file tree
Showing 21 changed files with 1,339 additions and 225 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/bdd-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: BDD Tests
on:
push:
branches:
- master
paths-ignore:
- "README.md"
- ".vscode/**"
- "**.md"
pull_request:
branches:
- master
paths-ignore:
- "README.md"
- ".vscode/**"
- "**.md"

jobs:
build:
runs-on: ubuntu-latest
container: rofrano/pipeline-selenium:latest

services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: pgs3cr3t
POSTGRES_DB: testdb
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
run: |
python -m pip install -U pip poetry
poetry config virtualenvs.create false
poetry install
- name: Run the service locally
run: |
echo "\n*** STARTING APPLICATION ***\n"
gunicorn --log-level=info --bind=0.0.0.0:8080 wsgi:app &
echo "Waiting for service to stabilize..."
sleep 5
echo "Checking service /health..."
curl -i http://localhost:8080/health
echo "\n*** SERVER IS RUNNING ***"
env:
DATABASE_URI: "postgresql+psycopg://postgres:pgs3cr3t@postgres:5432/testdb"

- name: Run Integration Tests
run: behave
File renamed without changes.
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# These can be overidden with env vars.
REGISTRY ?= cluster-registry:32000
NAMESPACE ?= nyu-devops
IMAGE_NAME ?= promotions
IMAGE_TAG ?= 1.0
IMAGE ?= $(REGISTRY)/$(IMAGE_NAME):$(IMAGE_TAG)
CLUSTER ?= nyu-devops

.SILENT:
Expand Down Expand Up @@ -58,7 +63,29 @@ cluster-rm: ## Remove a K3D Kubernetes cluster
$(info Removing Kubernetes cluster...)
k3d cluster delete

.PHONY: push
image-push: ## Push to a Docker image registry
$(info Logging into IBM Cloud cluster $(CLUSTER)...)
docker push $(IMAGE)

.PHONY: deploy
depoy: ## Deploy the service on local Kubernetes
$(info Deploying service locally...)
kubectl apply -f k8s/

############################################################
# COMMANDS FOR BUILDING THE IMAGE
############################################################

##@ Image Build

.PHONY: kube-local
kube-local: ## Deploy kubernetes to local
$(info Building $(IMAGE) for $(PLATFORM)...)
docker build -t $(IMAGE_NAME):$(IMAGE_TAG) .
kubectl config set-context --current --namespace $(NAMESPACE)
docker tag $(IMAGE_NAME):$(IMAGE_TAG) $(IMAGE)
docker push $(IMAGE)
kubectl create namespace $(NAMESPACE)
kubectl config set-context --current --namespace $(NAMESPACE)
kubectl apply -f k8s/
46 changes: 46 additions & 0 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Environment for Behave Testing
"""
from os import getenv
from selenium import webdriver

WAIT_SECONDS = int(getenv('WAIT_SECONDS', '60'))
BASE_URL = getenv('BASE_URL', 'http://localhost:8080')
DRIVER = getenv('DRIVER', 'chrome').lower()


def before_all(context):
""" Executed once before all tests """
context.base_url = BASE_URL
context.wait_seconds = WAIT_SECONDS
# Select either Chrome or Firefox
if 'firefox' in DRIVER:
context.driver = get_firefox()
else:
context.driver = get_chrome()
context.driver.implicitly_wait(context.wait_seconds)
context.config.setup_logging()


def after_all(context):
""" Executed after all tests """
context.driver.quit()

######################################################################
# Utility functions to create web drivers
######################################################################

def get_chrome():
"""Creates a headless Chrome driver"""
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
return webdriver.Chrome(options=options)


def get_firefox():
"""Creates a headless Firefox driver"""
options = webdriver.FirefoxOptions()
options.add_argument("--headless")
return webdriver.Firefox(options=options)

15 changes: 15 additions & 0 deletions features/promotions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: The promotion store service back-end
As a promotion Store Owner
I need a RESTful catalog service
So that I can keep track of all my promotions

Background:
Given the following promotions
| cust_promo_code | type | value | quantity | start_date | end_date | active | product_id | dev_created_at |
| JULY4 | SAVING | 50 | 10 | 2024-03-05 | 2024-03-10 | True | 33422 | 2024-03-05 |
| JUN2 | BOGO | 20 | 5 | 2024-03-05 | 2024-03-10 | True | 2928383 | 2024-03-05 |

Scenario: The server is running
When I visit the "Home Page"
Then I should see "promotion Demo RESTful Service" in the title
And I should not see "404 Not Found"
60 changes: 60 additions & 0 deletions features/steps/promotions_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
######################################################################
# Copyright 2016, 2023 John J. Rofrano. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
######################################################################

"""
promotion Steps
Steps file for promotion.feature
For information on Waiting until elements are present in the HTML see:
https://selenium-python.readthedocs.io/waits.html
"""
import requests
from behave import given

# HTTP Return Codes
HTTP_200_OK = 200
HTTP_201_CREATED = 201
HTTP_204_NO_CONTENT = 204


@given("the following promotions")
def step_impl(context):
"""Delete all promotions and load new ones"""

# List all of the promotions and delete them one by one
rest_endpoint = f"{context.base_url}/promotions"
context.resp = requests.get(rest_endpoint)
assert context.resp.status_code == HTTP_200_OK
for promotion in context.resp.json():
context.resp = requests.delete(f"{rest_endpoint}/{promotion['id']}")
assert context.resp.status_code == HTTP_204_NO_CONTENT

# load the database with new promotions
for row in context.table:
payload = {
"cust_promo_code": row["cust_promo_code"],
"type": row["type"],
"value": row["value"],
"quantity": row["quantity"],
"start_date": row["start_date"],
"end_date": row["end_date"],
"active": row["active"] in ["True", "true", "1"],
"product_id": row["product_id"],
"dev_created_at": row["dev_created_at"]
}
context.resp = requests.post(rest_endpoint, json=payload)
assert context.resp.status_code == HTTP_201_CREATED
Loading

0 comments on commit 8a59e1c

Please sign in to comment.