Skip to content

Commit 5a7c5ee

Browse files
authored
Merge pull request #38 from nyu-devops/su24-updates
Updates for Summer 2024 Semester
2 parents 3a937a3 + bc86180 commit 5a7c5ee

19 files changed

+146
-82
lines changed

.devcontainer/devcontainer.json

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
"customizations": {
99
"vscode": {
1010
"settings": {
11+
"cSpell.words": [
12+
"Rofrano",
13+
"sqlalchemy",
14+
"psycopg",
15+
"pytest",
16+
"tekton",
17+
"creds",
18+
"virtualenvs"
19+
],
1120
"[python]": {
1221
"editor.defaultFormatter": "ms-python.black-formatter",
1322
"editor.formatOnSave": true
1423
},
24+
"git.mergeEditor": true,
1525
"markdown-preview-github-styles.colorTheme": "light",
1626
"makefile.extensionOutputFolder": "/tmp",
1727
"python.testing.unittestEnabled": false,
@@ -36,36 +46,32 @@
3646
"extensions": [
3747
"ms-python.python",
3848
"ms-python.vscode-pylance",
39-
"VisualStudioExptTeam.vscodeintellicode",
4049
"ms-python.pylint",
4150
"ms-python.flake8",
4251
"ms-python.black-formatter",
4352
"ms-vscode.makefile-tools",
44-
"cstrap.flask-snippets",
45-
"wholroyd.jinja",
46-
"ms-vscode.makefile-tools",
4753
"yzhang.markdown-all-in-one",
54+
"DavidAnson.vscode-markdownlint",
4855
"bierner.github-markdown-preview",
4956
"hnw.vscode-auto-open-markdown-preview",
50-
"davidanson.vscode-markdownlint",
5157
"bierner.markdown-preview-github-styles",
5258
"tamasfe.even-better-toml",
5359
"donjayamanne.githistory",
5460
"GitHub.vscode-pull-request-github",
61+
"github.vscode-github-actions",
5562
"hbenl.vscode-test-explorer",
5663
"LittleFoxTeam.vscode-python-test-adapter",
5764
"njpwerner.autodocstring",
5865
"wholroyd.jinja",
5966
"redhat.vscode-yaml",
60-
"rangav.vscode-thunder-client",
6167
"redhat.fabric8-analytics",
62-
"streetsidesoftware.code-spell-checker",
6368
"ms-azuretools.vscode-docker",
6469
"ms-kubernetes-tools.vscode-kubernetes-tools",
65-
"github.vscode-github-actions",
70+
"inercia.vscode-k3d",
71+
"rangav.vscode-thunder-client",
6672
"alexkrechik.cucumberautocomplete",
6773
"Zignd.html-css-class-completion",
68-
"streetsidesoftware.code-spell-checker",
74+
"streetsidesoftware.code-spell-checker",
6975
"bbenoist.vagrant"
7076
]
7177
}

.devcontainer/scripts/install-tools.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sudo sh -c 'echo "127.0.0.1 cluster-registry" >> /etc/hosts'
2424
echo "**********************************************************************"
2525
echo "Installing K9s..."
2626
echo "**********************************************************************"
27-
curl -L -o k9s.tar.gz "https://github.com/derailed/k9s/releases/download/v0.27.3/k9s_Linux_$ARCH.tar.gz"
27+
curl -L -o k9s.tar.gz "https://github.com/derailed/k9s/releases/download/v0.32.5/k9s_Linux_$ARCH.tar.gz"
2828
tar xvzf k9s.tar.gz
2929
sudo install -c -m 0755 k9s /usr/local/bin
3030
rm k9s.tar.gz
@@ -34,3 +34,9 @@ echo "Installing Skaffold..."
3434
echo "**********************************************************************"
3535
curl -Lo skaffold "https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-$ARCH"
3636
sudo install skaffold /usr/local/bin/
37+
38+
echo "**********************************************************************"
39+
echo "Installing DevSpace..."
40+
echo "**********************************************************************"
41+
curl -Lo devspace "https://github.com/loft-sh/devspace/releases/latest/download/devspace-linux-$ARCH"
42+
sudo install -c -m 0755 devspace /usr/local/bin

Makefile

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# These can be overidden with env vars.
2-
REGISTRY ?= cluster-registry:32000
2+
REGISTRY ?= cluster-registry:5000
33
NAMESPACE ?= nyu-devops
4-
IMAGE_NAME ?= lab-flask-bdd
5-
IMAGE_TAG ?= 1.0
4+
IMAGE_NAME ?= petshop
5+
IMAGE_TAG ?= 1.0.0
66
IMAGE ?= $(REGISTRY)/$(NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG)
77
PLATFORM ?= "linux/amd64,linux/arm64"
88
CLUSTER ?= nyu-devops
@@ -61,7 +61,7 @@ secret: ## Generate a secret hex key
6161
.PHONY: cluster
6262
cluster: ## Create a K3D Kubernetes cluster with load balancer and registry
6363
$(info Creating Kubernetes cluster with a registry and 1 node...)
64-
k3d cluster create nyu-devops --agents 1 --registry-create cluster-registry:0.0.0.0:32000 --port '8080:80@loadbalancer'
64+
k3d cluster create nyu-devops --agents 1 --registry-create cluster-registry:0.0.0.0:5000 --port '8080:80@loadbalancer'
6565

6666
.PHONY: cluster-rm
6767
cluster-rm: ## Remove a K3D Kubernetes cluster
@@ -71,15 +71,25 @@ cluster-rm: ## Remove a K3D Kubernetes cluster
7171
##@ Deploy
7272

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

78+
.PHONY: postgres
79+
postgres: ## Deploy the PostgreSQL service on local Kubernetes
80+
$(info Deploying PostgreSQL service to Kubernetes...)
81+
kubectl apply -f k8s/postgres
82+
7883
.PHONY: deploy
79-
deploy: ## Deploy the service on local Kubernetes
84+
deploy: postgres ## Deploy the service on local Kubernetes
8085
$(info Deploying service locally...)
8186
kubectl apply -f k8s/
8287

88+
.PHONY: undeploy
89+
undeploy: ## Delete the deployment on local Kubernetes
90+
$(info Removing service on Kubernetes...)
91+
kubectl delete -f k8s/
92+
8393
############################################################
8494
# COMMANDS FOR BUILDING THE IMAGE
8595
############################################################

README.md

+45-3
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,64 @@ Pytest is configured to automatically include the flags `--pspec --cov=service -
7878

7979
These tests require the service to be running because unlike the the TDD unit tests that test the code locally, these BDD integration tests are using Selenium to manipulate a web page on a running server.
8080

81-
Run the tests using `behave`
81+
#### Run using two shells
8282

8383
Start the server in a separate bash shell:
8484

8585
```sh
8686
honcho start
8787
```
8888

89-
Then start behave in your original bash shell:
89+
Then start `behave` in your original bash shell:
9090

9191
```sh
9292
behave
9393
```
9494

9595
You will see the results of the tests scroll down yur screen using the familiar red/green/refactor colors.
9696

97+
#### Run using Kubernetes
98+
99+
You can also use Kubernetes to host your application and test against it with BDD. The commands to do this are:
100+
101+
```bash
102+
make cluster
103+
make build
104+
make push
105+
make deploy
106+
```
107+
108+
What did these commands do?
109+
110+
| Command | What does it do? |
111+
|---------|------------------|
112+
| make cluster | Creates a local Kubernetes cluster using `k3d` |
113+
| make build | Builds the Docker image |
114+
| make push | Pushes the image to the local Docker registry |
115+
| make deploy | Deploys the application using the image that was just built and pushed |
116+
117+
Now you can just run `behave` against the application running in the local Kubernetes cluster
118+
119+
```bash
120+
behave
121+
```
122+
123+
### See what images are in the local registry
124+
125+
You can use the `curl` command to query what images you have pushed to your local Docker registry. This will return `JSON` so you might want to use the silent flag `-s` and pipe it through `jq` like this:
126+
127+
```bash
128+
curl -XGET http://localhost:5000/v2/_catalog -s | jq
129+
```
130+
131+
That will return all of the image names without the tags.
132+
133+
To get the tags use:
134+
135+
```bash
136+
curl -XGET http://localhost:5000/v2/<image-name>/tags/list -s | jq
137+
```
138+
97139
## What's featured in the project?
98140

99141
```text
@@ -108,7 +150,7 @@ You will see the results of the tests scroll down yur screen using the familiar
108150

109151
## License
110152

111-
Copyright (c) 2016, 2023, John J. Rofrano. All rights reserved.
153+
Copyright (c) 2016, 2024, John J. Rofrano. All rights reserved.
112154

113155
Licensed under the Apache License. See [LICENSE](LICENSE)
114156

features/environment.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def get_chrome():
3434
"""Creates a headless Chrome driver"""
3535
options = webdriver.ChromeOptions()
3636
options.add_argument("--no-sandbox")
37+
options.add_argument("--disable-dev-shm-usage")
3738
options.add_argument("--headless")
3839
return webdriver.Chrome(options=options)
3940

@@ -43,4 +44,4 @@ def get_firefox():
4344
options = webdriver.FirefoxOptions()
4445
options.add_argument("--headless")
4546
return webdriver.Firefox(options=options)
46-
47+

features/steps/pets_steps.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
######################################################################
2-
# Copyright 2016, 2023 John J. Rofrano. All Rights Reserved.
2+
# Copyright 2016, 2024 John J. Rofrano. All Rights Reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -23,24 +23,29 @@
2323
https://selenium-python.readthedocs.io/waits.html
2424
"""
2525
import requests
26-
from behave import given
26+
from compare3 import expect
27+
from behave import given # pylint: disable=no-name-in-module
2728

2829
# HTTP Return Codes
2930
HTTP_200_OK = 200
3031
HTTP_201_CREATED = 201
3132
HTTP_204_NO_CONTENT = 204
3233

34+
WAIT_TIMEOUT = 60
35+
36+
3337
@given('the following pets')
3438
def step_impl(context):
3539
""" Delete all Pets and load new ones """
3640

37-
# List all of the pets and delete them one by one
41+
# Get a list all of the pets
3842
rest_endpoint = f"{context.base_url}/pets"
39-
context.resp = requests.get(rest_endpoint)
40-
assert(context.resp.status_code == HTTP_200_OK)
43+
context.resp = requests.get(rest_endpoint, timeout=WAIT_TIMEOUT)
44+
expect(context.resp.status_code).equal_to(HTTP_200_OK)
45+
# and delete them one by one
4146
for pet in context.resp.json():
42-
context.resp = requests.delete(f"{rest_endpoint}/{pet['id']}")
43-
assert(context.resp.status_code == HTTP_204_NO_CONTENT)
47+
context.resp = requests.delete(f"{rest_endpoint}/{pet['id']}", timeout=WAIT_TIMEOUT)
48+
expect(context.resp.status_code).equal_to(HTTP_204_NO_CONTENT)
4449

4550
# load the database with new pets
4651
for row in context.table:
@@ -51,5 +56,5 @@ def step_impl(context):
5156
"gender": row['gender'],
5257
"birthday": row['birthday']
5358
}
54-
context.resp = requests.post(rest_endpoint, json=payload)
55-
assert(context.resp.status_code == HTTP_201_CREATED)
59+
context.resp = requests.post(rest_endpoint, json=payload, timeout=WAIT_TIMEOUT)
60+
expect(context.resp.status_code).equal_to(HTTP_201_CREATED)

features/steps/web_steps.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
######################################################################
2-
# Copyright 2016, 2023 John J. Rofrano. All Rights Reserved.
2+
# Copyright 2016, 2024 John J. Rofrano. All Rights Reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
https://selenium-python.readthedocs.io/waits.html
2626
"""
2727
import logging
28-
from behave import when, then
28+
from behave import when, then # pylint: disable=no-name-in-module
2929
from selenium.webdriver.common.by import By
3030
from selenium.webdriver.support.ui import Select, WebDriverWait
3131
from selenium.webdriver.support import expected_conditions

k3d-config.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
apiVersion: k3d.io/v1alpha3
22
kind: Simple
3-
name: devops
3+
name: nyu-devops
44
servers: 1
55
agents: 1
66
ports:
@@ -11,9 +11,9 @@ registries:
1111
create:
1212
name: cluster-registry
1313
host: "0.0.0.0"
14-
hostPort: "32000"
14+
hostPort: "5000"
1515
config: |
1616
mirrors:
17-
"cluster-registry:32000":
17+
"cluster-registry":
1818
endpoint:
19-
- http://cluster-registry:32000
19+
- http://cluster-registry:5000

k8s/deployment.yaml

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
name: lab-flask-bdd
4+
name: petshop
55
labels:
6-
app: lab-flask-bdd
6+
app: petshop
77
spec:
8-
replicas: 2
8+
replicas: 1
99
strategy:
1010
type: RollingUpdate
1111
rollingUpdate:
1212
maxSurge: 0%
1313
maxUnavailable: 50%
1414
selector:
1515
matchLabels:
16-
app: lab-flask-bdd
16+
app: petshop
1717
template:
1818
metadata:
1919
labels:
20-
app: lab-flask-bdd
20+
app: petshop
2121
spec:
22-
imagePullSecrets:
23-
- name: all-icr-io
2422
restartPolicy: Always
2523
containers:
26-
- name: lab-flask-bdd
27-
# image: cluster-registry:32000/lab-flask-bdd:1.0
28-
image: lab-flask-bdd
24+
- name: petshop
25+
image: cluster-registry:5000/nyu-devops/petshop:1.0.0
26+
# image: petshop
2927
imagePullPolicy: IfNotPresent
3028
ports:
3129
- containerPort: 8080

k8s/ingress.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: networking.k8s.io/v1
33
kind: Ingress
44
metadata:
5-
name: lab-flask-bdd
5+
name: petshop
66
annotations:
77
nginx.ingress.kubernetes.io/rewrite-target: /
88
spec:
@@ -13,6 +13,6 @@ spec:
1313
pathType: Prefix
1414
backend:
1515
service:
16-
name: lab-flask-bdd
16+
name: petshop
1717
port:
1818
number: 8080

k8s/pv.yaml renamed to k8s/postgres/pv.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ spec:
1111
persistentVolumeReclaimPolicy: Recycle
1212
hostPath:
1313
path: /data/pv0001
14-
storageClassName: "default"

k8s/postgres/pvc.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
apiVersion: v1
3+
kind: PersistentVolumeClaim
4+
metadata:
5+
name: postgres-pvc
6+
spec:
7+
accessModes:
8+
- ReadWriteOnce
9+
resources:
10+
requests:
11+
storage: 1Gi
File renamed without changes.

k8s/postgres/service.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: postgres
6+
labels:
7+
app: postgres
8+
spec:
9+
type: ClusterIP
10+
selector:
11+
app: postgres
12+
ports:
13+
- port: 5432
14+
targetPort: 5432

0 commit comments

Comments
 (0)