-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from riskfuel/feature/add-tests-and-better-exam…
…ples adding initial test & updating example spec
- Loading branch information
Showing
10 changed files
with
341 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: run test suite | ||
on: | ||
push: | ||
paths: | ||
- 'tests/**' | ||
- 'src/**' | ||
- '.github/workflows/run-tests.yml' | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Docker Buildx | ||
id: buildx | ||
uses: crazy-max/ghaction-docker-buildx@v3 | ||
with: | ||
buildx-version: latest | ||
|
||
- name: Cache Docker layers | ||
uses: actions/cache@v2 | ||
id: cache | ||
with: | ||
path: /tmp/.buildx-cache | ||
key: ${{ runner.os }}-buildx-${{ github.sha }} | ||
restore-keys: | | ||
${{ runner.os }}-buildx- | ||
- name: Build image | ||
run: | | ||
version=$(cat version) | ||
DOCKER_IMAGE=riskfuel/k8s-mig-operator:$version | ||
docker buildx build \ | ||
--cache-from "type=local,src=/tmp/.buildx-cache" \ | ||
--cache-to "type=local,dest=/tmp/.buildx-cache" \ | ||
--platform linux/amd64 \ | ||
--tag ${DOCKER_IMAGE} \ | ||
--output "type=docker" \ | ||
. | ||
- name: Run tests | ||
run: | | ||
version=$(cat version) | ||
DOCKER_IMAGE=riskfuel/k8s-mig-operator:$version | ||
docker tag ${DOCKER_IMAGE} mig-operator-ci-shell | ||
docker-compose run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '3.5' | ||
|
||
services: | ||
|
||
shell: &basic | ||
container_name: mig-operator-ci-shell | ||
image: mig-operator-ci-shell | ||
build: | ||
context: . | ||
working_dir: /home/app_user/app | ||
volumes: | ||
- .:/home/app_user/app | ||
entrypoint: bash | ||
stdin_open: true | ||
tty: true | ||
|
||
test: &test | ||
<<: *basic | ||
entrypoint: ["pytest", "-vv", "tests/"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
ansible==2.9.12 | ||
pyyaml==5.3.1 | ||
python-dotenv==0.14.0 | ||
pytest==6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
def load_context(): | ||
""" | ||
Add the src dir to sys.path so we can | ||
import code in the test folder as expected | ||
""" | ||
import sys, os | ||
current_path = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.insert(0, current_path + '/../src') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
from load_context import load_context | ||
load_context() | ||
|
||
from actions import get_required_actions | ||
from test_case_1 import \ | ||
desired_spec, \ | ||
current_state, \ | ||
expected_actions | ||
|
||
testdata = [ | ||
(current_state, desired_spec, expected_actions), | ||
] | ||
|
||
def get_actions(current_state, desired_spec, expected_actions): | ||
actions = [] | ||
for i in range(8): | ||
gpu_instance_profiles = current_state[f"gpu-{i}"]["gpu_instance_profiles"] | ||
compute_instance_profiles = current_state[f"gpu-{i}"]["compute_instance_profiles"] | ||
gpu_instances = current_state[f"gpu-{i}"]["gpu_instances"] | ||
comp_instances = current_state[f"gpu-{i}"]["comp_instances"] | ||
|
||
actions += get_required_actions( | ||
i, | ||
desired_spec[f"gpu-{i}"], | ||
gpu_instances, | ||
comp_instances, | ||
gpu_instance_profiles, | ||
compute_instance_profiles, | ||
True, | ||
False | ||
) | ||
|
||
return actions | ||
|
||
@pytest.mark.parametrize("current_state,desired_spec,expected_actions", testdata) | ||
def test_action_selection(current_state, desired_spec, expected_actions): | ||
""" | ||
Ensure correct actions are being triggered | ||
:param: current_state | ||
:param: desired_spec | ||
:param: expected_actions | ||
""" | ||
|
||
actions = get_actions(*testdata[0]) | ||
for i, desired_action in enumerate(expected_actions): | ||
assert desired_action == actions[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
TEST CASE 1 | ||
----------- | ||
Tests creating instances for the example spec defined | ||
in the README.md from a state with no instances. | ||
""" | ||
|
||
import os, yaml | ||
|
||
# DESIRED SPEC | ||
|
||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
with open(f"{dir_path}/migoperator.yml") as f: | ||
manifest = yaml.load(f, Loader=yaml.SafeLoader) | ||
|
||
desired_spec = {} | ||
for device in manifest["spec"]["nodes"]["nodehostname"]["devices"]: | ||
desired_spec[f"gpu-{device['gpu']}"] = { | ||
"migEnabled": device["migEnabled"], | ||
"gpuInstances": device["gpuInstances"] | ||
} | ||
|
||
# CURRENT STATE | ||
|
||
gpu_instance_profiles = {'1g.5gb': '19', '2g.10gb': '14', '3g.20gb': '9', '4g.20gb': '5', '7g.40gb': '0'} | ||
compute_instance_profiles = {'1c.7g.40gb': '0', '2c.7g.40gb': '1', '3c.7g.40gb': '2', '4c.7g.40gb': '3', '7g.40gb': '4'} | ||
|
||
current_state = {} | ||
for i, gpu in enumerate(desired_spec): | ||
current_state[gpu] = { | ||
"gpu_instance_profiles": gpu_instance_profiles, | ||
"compute_instance_profiles": compute_instance_profiles, | ||
"gpu_instances": [{'gpu': i, 'profile_id': '0', 'instance_id': '0', 'placement': '0:8'}], | ||
"comp_instances": [] | ||
} | ||
|
||
# EXPECTED ACTIONS | ||
|
||
expected_actions = [ | ||
{ | ||
"type": "CREATE_COMP_INSTANCE", | ||
"gpu": i, | ||
"gpu_instance_id": "0", | ||
"profile_id": "4" | ||
} | ||
for i in range(8) | ||
] |
Oops, something went wrong.