1# Copyright 2015 LinkedIn Corp. 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19 20import os 21import zopkio.remote_host_helper as remote_host_helper 22import zopkio.runtime as runtime 23from zopkio.ztests import ZTest, ZTestSuite 24import shutil 25from time import sleep 26__test__ = False # don't this as a test 27 28TEST_DIRECTORY = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 29 30 31class SampleTest0(ZTest): 32 phase = 0 33 def test(self): 34 """ 35 Yay! Docstring! 36 """ 37 assert 1 == 2 38 39class SampleTest1(ZTest): 40 phase = 0 41 42 def __init__(self, sampletestsuite): 43 self.sampletestsuite = sampletestsuite 44 45 def test(self): 46 self.sampletestsuite.sample_code_in.write("test1\n") 47 self.sampletestsuite.sample_code_in.flush() 48 sleep(5) 49 with open("/tmp/trivial_timed_output", "r") as f: 50 lines = f.readlines() 51 test_str = "".join(lines) 52 assert "test1" in test_str 53 54class SampleTest2(ZTest): 55 phase = 1 56 57 def __init__(self, sampletestsuite): 58 self.sampletestsuite = sampletestsuite 59 60 61 def test(self): 62 """ 63 Another docstring 64 """ 65 for i in xrange(50, 150): 66 self.sampletestsuite.sample_code_in.write("{0}\n".format(i)) 67 self.sampletestsuite.sample_code_in.flush() 68 sleep(.05) 69 70 71 72 def validate(self): 73 assert 1 == 1 74 75 76class SampleTestSuite(ZTestSuite): 77 config_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ztestsuite_configs") 78 79 def __init__(self, deployer = None): 80 self.ssh = remote_host_helper.sshclient() 81 self.ssh.load_system_host_keys() 82 self.sample_code_in = None 83 self.test0 = SampleTest0() 84 self.test1 = SampleTest1(self) 85 self.test2 = SampleTest2(self) 86 self._deployer = deployer 87 88 def setup_suite(self): 89 if self._deployer is not None: 90 runtime.set_deployer("ztestsuite.unittest.deployer", self._deployer ) 91 if os.path.isdir("/tmp/ztestsute"): 92 shutil.rmtree("/tmp/ztestsuite") 93 if not os.path.isdir(runtime.get_active_config("LOGS_DIRECTORY")): 94 os.makedirs(runtime.get_active_config("LOGS_DIRECTORY")) 95 if not os.path.isdir(runtime.get_active_config("OUTPUT_DIRECTORY")): 96 os.makedirs(runtime.get_active_config("OUTPUT_DIRECTORY")) 97 sample_code = os.path.join(TEST_DIRECTORY, "samples","trivial_program_with_timing") 98 self.ssh.connect("localhost") 99 self.sample_code_in, stdout, stderr = self.ssh.exec_command("python {0}".format(sample_code)) 100 if self._deployer is not None: 101 self._deployer.start("ztestsuite.unittest") 102 103 def teardown_suite(self): 104 if self.sample_code_in is not None: 105 self.sample_code_in.write("quit\n") 106 self.sample_code_in.flush() 107 if self.ssh is not None: 108 with self.ssh.open_sftp() as ftp: 109 input_csv = os.path.join(runtime.get_active_config("LOGS_DIRECTORY"), "output.csv") 110 input_log = os.path.join(runtime.get_active_config("LOGS_DIRECTORY"), "output.log") 111 ftp.get("/tmp/trivial_timed_output.csv", input_csv) 112 ftp.get("/tmp/trivial_timed_output", input_log) 113 ftp.remove("/tmp/trivial_timed_output.csv") 114 ftp.remove("/tmp/trivial_timed_output") 115 self.ssh.close() 116 if self._deployer is not None: 117 self._deployer.stop("ztestsuite.unittest") 118 119 def naarad_config(self): 120 return os.path.join(TEST_DIRECTORY, "samples/naarad_config.cfg") 121 122 123 def process_logs(self, servicename): 124 pass 125