Skip to content

Commit

Permalink
Fix unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
KashunCheng committed Jul 28, 2023
1 parent 89c1168 commit f8a82f2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 70 deletions.
130 changes: 66 additions & 64 deletions acto/post_diff_test.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
import argparse
import glob
import json
import multiprocessing
import os
import logging
import pickle
import random
import time

from acto.config import actoConfig
import acto.config as acto_config
acto_config.load_config(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'config_post_diff_test.yaml'))
__test__ = False

from acto.lib.monkey_patch_loader import load_monkey_patch
from acto.lib.operator_config import OperatorConfig
from acto.post_process import PostDiffTest
if __name__ == '__main__':

# for debugging, set random seed to 0
random.seed(0)
multiprocessing.set_start_method('fork')
from acto.config import actoConfig
import acto.config as acto_config

parser = argparse.ArgumentParser(
description='Automatic, Continuous Testing for k8s/openshift Operators')
parser.add_argument('--workdir',
dest='workdir_path',
type=str,
help='Working directory')
parser.add_argument('--config', '-c', dest='config', help='Operator port config path')
parser.add_argument('--num-workers',
dest='num_workers',
type=int,
default=1,
help='Number of concurrent workers to run Acto with')
parser.add_argument('--checkonly', action='store_true')
acto_config.load_config(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'config_post_diff_test.yaml'))

args = parser.parse_args()
from acto.lib.monkey_patch_loader import load_monkey_patch
from acto.lib.operator_config import OperatorConfig
from acto.post_process import PostDiffTest

with open(args.config, 'r') as config_file:
config = OperatorConfig(**json.load(config_file))
load_monkey_patch(config)
# for debugging, set random seed to 0
random.seed(0)

os.makedirs(args.workdir_path, exist_ok=True)
# Setting up log infra
logging.basicConfig(
filename=os.path.join(args.workdir_path, 'test.log'),
level=logging.DEBUG,
filemode='w',
format='%(asctime)s %(levelname)-7s, %(name)s, %(filename)-9s:%(lineno)d, %(message)s')
logging.getLogger("kubernetes").setLevel(logging.ERROR)
logging.getLogger("sh").setLevel(logging.ERROR)
if actoConfig.parallel.executor == 'ray':
import ansible_runner
import ray
parser = argparse.ArgumentParser(
description='Automatic, Continuous Testing for k8s/openshift Operators')
parser.add_argument('--workdir',
dest='workdir_path',
type=str,
help='Working directory')
parser.add_argument('--config', '-c', dest='config', help='Operator port config path')
parser.add_argument('--num-workers',
dest='num_workers',
type=int,
default=1,
help='Number of concurrent workers to run Acto with')
parser.add_argument('--checkonly', action='store_true')

ansible_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'scripts', 'ansible')
ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'acto_ray.yaml'))
head_result = ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'ray_head.yaml'))
ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'ray_worker.yaml'))
if head_result.stats['changed'] != {}:
time.sleep(5)
ray.init(address='auto')
args = parser.parse_args()

with open(args.config, 'r') as config_file:
config = OperatorConfig(**json.load(config_file))
load_monkey_patch(config)

def main():
post_diff_test_dir = os.path.join(args.workdir_path, 'post_diff_test')
trials = {}
trial_paths = glob.glob(os.path.join(args.workdir_path, '**', 'trial.pkl'))
common_prefix = trial_paths[0][trial_paths[0].find('trial-'):] if len(trial_paths) == 1 else os.path.commonpath(
trial_paths) + os.path.sep
for trial_path in trial_paths:
trials[trial_path[len(common_prefix):][:-len('/trial.pkl')]] = pickle.load(open(trial_path, 'rb'))
p = PostDiffTest(trials=trials, config=config, num_workers=args.num_workers)
if not args.checkonly:
p.post_process(post_diff_test_dir)
p.check(post_diff_test_dir)
p.teardown()
os.makedirs(args.workdir_path, exist_ok=True)
# Setting up log infra
logging.basicConfig(
filename=os.path.join(args.workdir_path, 'test.log'),
level=logging.DEBUG,
filemode='w',
format='%(asctime)s %(levelname)-7s, %(name)s, %(filename)-9s:%(lineno)d, %(message)s')
logging.getLogger("kubernetes").setLevel(logging.ERROR)
logging.getLogger("sh").setLevel(logging.ERROR)
if actoConfig.parallel.executor == 'ray':
import ansible_runner
import ray

ansible_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'scripts', 'ansible')
ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'acto_ray.yaml'))
head_result = ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'ray_head.yaml'))
ansible_runner.run(inventory=actoConfig.parallel.ansible_inventory,
playbook=os.path.join(ansible_dir, 'ray_worker.yaml'))
if head_result.stats['changed'] != {}:
time.sleep(5)
ray.init(address='auto')


def main():
post_diff_test_dir = os.path.join(args.workdir_path, 'post_diff_test')
trials = {}
trial_paths = glob.glob(os.path.join(args.workdir_path, '**', 'trial.pkl'))
common_prefix = trial_paths[0][trial_paths[0].find('trial-'):] if len(trial_paths) == 1 else os.path.commonpath(
trial_paths) + os.path.sep
for trial_path in trial_paths:
trials[trial_path[len(common_prefix):][:-len('/trial.pkl')]] = pickle.load(open(trial_path, 'rb'))
p = PostDiffTest(trials=trials, config=config, num_workers=args.num_workers)
if not args.checkonly:
p.post_process(post_diff_test_dir)
p.check(post_diff_test_dir)
p.teardown()


if __name__ == '__main__':
main()
logging.info('Acto finished')
2 changes: 2 additions & 0 deletions acto/post_process/post_diff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

from .post_process import PostProcessor

__test__ = False


def digest_input(system_input: dict):
return hashlib.md5(json.dumps(system_input, sort_keys=True).encode("utf-8")).hexdigest()
Expand Down
14 changes: 8 additions & 6 deletions acto/runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, engine_class: Type[Engine], engine_version: str, num_nodes: i
preload_images_store = lambda image_hash: f'/tmp/acto_image_{image_hash}.tar'
preload_images_store = preload_images_store(hash_preload_images(preload_images))
self.cluster_name = None
self.cluster = None
self.preload_images = preload_images
self.preload_images_store = preload_images_store
self.kubernetes_engine_class = engine_class
Expand Down Expand Up @@ -92,9 +93,10 @@ def prefetch_image(self):
list(self.preload_images), stdout=subprocess.DEVNULL)

def teardown_cluster(self):
self.cluster.delete_cluster(self.cluster_name, self.kubectl_client.kubeconfig)
self.cluster_name = None
try:
os.remove(self.kubectl_client.kubeconfig)
except OSError:
pass
if self.cluster:
self.cluster.delete_cluster(self.cluster_name, self.kubectl_client.kubeconfig)
self.cluster_name = None
try:
os.remove(self.kubectl_client.kubeconfig)
except OSError:
pass

0 comments on commit f8a82f2

Please sign in to comment.