Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test module #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions .applatix/be-controller-wf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

---
type: workflow
name: workflow
description: test-workflow

fixtures:
- influxsrv:
template: influxsrv

inputs:
parameters:
commit:
default: "%%session.commit%%"
repo:
default: "%%session.repo%%"

steps:
-
checkout:
template: checkout
-
build:
template: test
parameters:
code: "%%steps.checkout.code%%"
influxHost: "%%fixtures.influxsrv.ip%%"
24 changes: 24 additions & 0 deletions .applatix/checkout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# checkout.yaml
---
type: container
name: checkout
description: Checks out commit to /src and export it as an artifact

inputs:
parameters:
commit:
default: "%%session.commit%%"
repo:
default: "%%session.repo%%"

outputs:
artifacts:
code:
path: /src

container:
image: get.applatix.io/applatix/axscm:v2.0
resources:
mem_mib: 256
cpu_cores: 0.1
command: axscm clone %%repo%% /src --commit %%commit%%
9 changes: 9 additions & 0 deletions .applatix/fixtures.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# fixtures.yaml
# Test Environment
---
type: container
name: influxsrv
description: Influx DB instnce
container:
image: hyperpilot/influx:1.2.2
docker_options: -e ADMIN_USER=root -e INFLUXDB_INIT_PWD=root
22 changes: 22 additions & 0 deletions .applatix/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# policy.yaml
---
type: policy
name: be-ctrl-workflow-policy
description: Trigger workflow on pushes, pull requests and as a cron job
template: workflow
notifications:
-
when:
- on_success
- on_failure
whom:
- committer
- author
when:
-
event: on_push
target_branches:
- "unit_test"
labels:
milestone: unit_test
version: 1.0.1
21 changes: 21 additions & 0 deletions .applatix/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# test.yaml
---
type: container
name: test
description: Build-and-test-the-code

inputs:
artifacts:
- from: "%%code%%"
path: /src
parameters:
code:
influxHost:

container:
resources:
mem_mib: 256
cpu_cores: 1.0
image: python:2
command: sh -c 'cd /src && pip install -r requirements.txt && . ./run_tests.sh'
docker_options: -e "INFLUXDB_HOST=%%influxHost%%"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pyc
*.pyc
.venv/
.cache/
2 changes: 1 addition & 1 deletion blkioclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, block_dev, max_rd_iops, max_wr_iops):
self.keys = set()

# check if blockio is active
if not os.path.isdir('/sys/fs/cgroup/blkio/kubepods'):
if not os.path.isdir('/sys/fs/cgroup/blkio'):
raise Exception('Blkio not configured for K8S')


Expand Down
26 changes: 21 additions & 5 deletions blkiocontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
# hyperpilot imports
import settings as st
import blkioclass as blkioclass
import os

def GetBlkioPath(base_path, pod, container_id):
"""
construct blkio path
Parameters:
pod: Pod instance
container_id: desire container
"""
if pod.qosclass == 'guaranteed':
root = os.path.join(base_path, 'pod{}'.format(pod.uid), container_id)
else:
root = os.path.join(base_path, pod.qosclass.lower(), "pod{}".format(pod.uid), container_id)

return root

def BlkioControll():
""" Blkio controller
Expand Down Expand Up @@ -59,12 +74,13 @@ def BlkioControll():
active_be_ids = set()
st.active.lock.acquire_read()
for _, pod in st.active.pods.items():
if pod.qosclass == 'guaranteed':
root = 'kubepods/' + 'pod' + pod.uid + '/'
else:
root = 'kubepods/' + pod.qosclass.lower() + '/pod' + pod.uid + '/'
# if pod.qosclass == 'guaranteed':
# root = netst['blkio_path'] + 'pod' + pod.uid + '/'
# else:
# root = netst['blkio_path'] + pod.qosclass.lower() + '/pod' + pod.uid + '/'
for cont in pod.container_ids:
key = root + cont
# key = root + cont
key = getBlkioPath(netst['blkio_path'], pod, cont)
active_ids.add(key)
if pod.wclass == 'BE':
active_be_ids.add(key)
Expand Down
14 changes: 11 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"mode" : "k8s",
"ctlloc" : "in",
"mode": "k8s",
"ctlloc": "in",
"influx_db": {
"host": "influxsrv.hyperpilot",
"port": 8086,
"user": "root",
"password": "root",
"db": "be_controller"
},
"quota_controller": {
"period": 5,
"slack_threshold_disable": -0.5,
Expand Down Expand Up @@ -33,7 +40,8 @@
"max_rd_iops": 1500,
"hp_iops": 1000,
"disabled": false,
"write_metrics": true
"write_metrics": true,
"blkio_path": "kubepods/"
},
"write_metrics": true
}
2 changes: 1 addition & 1 deletion controller.daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ spec:
name: lib
imagePullSecrets:
- name: myregistrykey
terminationGracePeriodSeconds: 10
terminationGracePeriodSeconds: 10
7 changes: 5 additions & 2 deletions maincontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ def __init__():

# parse arguments
st.params = ParseArgs()
st.stats_writer = store.InfluxWriter()
influx_conn = st.params['influx_db']
st.stats_writer = store.InfluxWriter(influx_conn['host'], influx_conn['port'],
influx_conn['user'], influx_conn['password'],
influx_conn['be_controller'])

if st.get_param("write_metrics", None, False) is True:
# flatten the setting params
Expand Down Expand Up @@ -532,4 +535,4 @@ def __init__():
cycle += 1
time.sleep(period)

__init__()
__init__()
17 changes: 17 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Requirements automatically generated by pigar.
# https://github.com/Damnever/pigar

# maincontrol.py: 28
docker == 2.1.0

# store.py: 1,2
influxdb == 4.0.0

# maincontrol.py: 29,30
kubernetes == 2.0.0

# maincontrol.py: 27
pycurl == 7.43.0

# urllib
urllib3>=1.19.1,!=1.21
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python -m unittest netclass_tests
python -m unittest discover
3 changes: 2 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from kubernetes import watch
import rwlock
import store
import os

class Container(object):
""" A class for tracking active containers
Expand Down Expand Up @@ -80,7 +81,7 @@ def add_pod(self, k8s_object, key):
pod.namespace = k8s_object.metadata.namespace
pod.uid = k8s_object.metadata.uid
pod.ipaddress = k8s_object.status.pod_ip
pod.qosclass = k8s_object.status.qos_class.lower()
pod.qosclass = k8s_object.status.qos_class.lower() if k8s_object.status.qos_class != None else None
pod.wclass = ExtractWClass(k8s_object)
if pod.wclass == 'BE' and pod.qosclass != 'besteffort':
print "K8SWatch:WARNING: Pod %s is not BestEffort in K8S" %(key)
Expand Down
21 changes: 14 additions & 7 deletions store.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from influxdb import InfluxDBClient
from influxdb.client import InfluxDBClientError
import json

class InfluxWriter(object):
def __init__(self):
self.client = InfluxDBClient(
"influxsrv.hyperpilot", 8086, "root", "root", "be_controller")
try:
self.client.create_database("be_controller")
except InfluxDBClientError:
pass #Ignore

def __init__(self, host=None, port=None, user=None, password=None, db=None):
if any(x is None for x in [host, port, user, password, db]):
self.client = None
else:
self.client = InfluxDBClient(
host, port, user, password, db)
try:
self.client.create_database(db)
except InfluxDBClientError:
pass #Ignore

def write(self, time, hostname, controller, data):
if self.client is None:
raise Exception("store:ERROR: influxdb client not confgure properly")
try:
self.client.write_points([{
"time": time,
Expand Down
Empty file added tests/__init__.py
Empty file.