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