1# Copyright 2014 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 shutil
23from time import sleep
24__test__ = False  # don't this as a test
25
26TEST_DIRECTORY = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
27LOGS_DIRECTORY = "/tmp/test_with_naarad/collected_logs/"
28OUTPUT_DIRECTORY = "/tmp/test_with_naarad/results/"
29__ssh__ = None
30__sample_code_in__ = None
31
32test = {
33  "deployment_code": os.path.abspath(__file__),
34  "test_code": [os.path.abspath(__file__)],
35  "dynamic_configuration_code": os.path.abspath(__file__),
36  "configs_directory": os.path.join(os.path.dirname(os.path.abspath(__file__)), "sample_configs")
37}
38
39
40def setup_suite():
41  if os.path.isdir("/tmp/test_with_naarad"):
42    shutil.rmtree("/tmp/test_with_naarad")
43  if not os.path.isdir(LOGS_DIRECTORY):
44    os.makedirs(LOGS_DIRECTORY)
45  if not os.path.isdir(OUTPUT_DIRECTORY):
46    os.makedirs(OUTPUT_DIRECTORY)
47  global __ssh__
48  __ssh__ = remote_host_helper.sshclient()
49  __ssh__.load_system_host_keys()
50  __ssh__.connect("localhost")
51  sample_code = os.path.join(TEST_DIRECTORY, "samples/trivial_program_with_timing")
52  global __sample_code_in__
53  __sample_code_in__, stdout, stderr = __ssh__.exec_command("python {0}".format(sample_code))
54
55
56def teardown_suite():
57  if __sample_code_in__ is not None:
58    __sample_code_in__.write("quit\n")
59    __sample_code_in__.flush()
60  if __ssh__ is not None:
61    ftp = __ssh__.open_sftp()
62    input_csv = os.path.join(LOGS_DIRECTORY, "output.csv")
63    input_log = os.path.join(LOGS_DIRECTORY, "output.log")
64    ftp.get("/tmp/trivial_timed_output.csv", input_csv)
65    ftp.get("/tmp/trivial_timed_output", input_log)
66    ftp.remove("/tmp/trivial_timed_output.csv")
67    ftp.remove("/tmp/trivial_timed_output")
68    ftp.close()
69    __ssh__.close()
70
71
72def test0():
73  """
74  Yay! Docstring!
75  """
76  assert 1 == 2
77
78
79def test1():
80  __sample_code_in__.write("test1\n")
81  __sample_code_in__.flush()
82  sleep(5)
83  with open("/tmp/trivial_timed_output", "r") as f:
84    lines = f.readlines()
85    test_str = "".join(lines)
86    assert "test1" in test_str
87
88
89def test2():
90  """
91  Another docstring
92  """
93  for i in xrange(50, 150):
94    __sample_code_in__.write("{0}\n".format(i))
95    __sample_code_in__.flush()
96    sleep(.05)
97
98
99def test3():
100    assert 1 == 1
101
102
103def validate2():
104  assert 1 == 1
105
106
107def naarad_config():
108  return os.path.join(TEST_DIRECTORY, "samples/naarad_config.cfg")
109
110
111class MockConfig(object):
112  """
113  Mock config class currently only has a name so that I can use it in execute run and an empty config map
114  """
115  def __init__(self, name):
116    self.name = name
117    self.mapping = {}
118