<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# !/usr/bin/env python

# ---------Power measurement ------------------------------#
# This script will run the servo with the given benchmark and
# get the power usage using Powermetrics. Results will be put
# in sperate files with that name.
# Do not forget to run the script in servo/src/test/power folder
# --------------------------------------------------------#

import os
import time
import argparse

# ------------------------PowerCollector----------------------------#
# Collecting all the power data and put them into files

def PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer):
    print " Running  the power collector"
    os.mkdir(os.path.join(OutputDir+"power"), 0777)
    os.mkdir(os.path.join(OutputDir+"time"), 0777)
    os.mkdir(os.path.join(OutputDir+"Addtional"), 0777)
    SleepTime = 20
    GuardTime = 0.5
    powerTiming = 1
    ExperimentNum = 21
    for ExpNum in range(1, ExperimentNum):
        for layoutT in range(1, LayoutThreads+1):
            PowerFiles = OutputDir + 'power/' + 'power-Layout' + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            TimeFiles = OutputDir + 'time/' + "time-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            # ServoCmd ="(time ./servo -x -y " + str(layoutT) \
            #    +" "+ Renderer + " " + Benchmarks + " ) 2> " + TimeFiles
            ServoCmd = "(time ../../../build/servo -x -y " + \
                str(layoutT) + " " + Renderer + " " + \
                Benchmarks + " ) 2> " + TimeFiles
            Metrics = OutputDir + 'Addtional/' + "metrics-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + "-css.csv"
            cmd = '(sudo powermetrics -i ' + str(powerTiming) + \
                ' | grep \"energy\\|elapsed\\|servo\" > ' + \
                PowerFiles + '& ) 2> ' + Metrics
            time.sleep(SleepTime)
            os.system(cmd)
            time.sleep(GuardTime)
            os.system(ServoCmd)
            time.sleep(GuardTime)
            os.system('sudo pkill -9 powermetrics')
            time.sleep(SleepTime)

# -------------------PowerParser ---------------------------------#
# Parsing collected power by PowerCollector fucntion

def PowerParser(OutputDir, LayoutThreads):
    print "Running the PowerParser"
    ExperimentNum = 21
    ResultTable = OutputDir + "ResultTable.csv"
    ResultFile = open(ResultTable, "w")
    ResultFile.write("LayoutThreads, MeanPower, MaxPower , MinPower, MeanTime , MaxTime, "
                     "MinTime  \n")

    for layoutT in range(1, LayoutThreads+1):
        MaxTime = 0
        MinTime = 1000000
        MaxPower = 0
        MinPower = 1000000
        TotalPower = 0
        TotalTime = 0
        TimeGen = 0
        PowerGen = 0
        for ExpNum in range(1, ExperimentNum):
            Files = OutputDir + 'power/' + 'power-Layout' + str(layoutT) + \
                "-set" + str(ExpNum) + ".csv"
            NewFile = OutputDir + 'power/Servo-L' + str(layoutT) + \
                "set" + str(ExpNum) + ".csv"
            File = open(Files, 'r')
            PowerFile = open(NewFile, 'w')
            TimeFiles = OutputDir + 'time/' + "time-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            # ----Putting the power the power and its time into a table---- #

            for line in File:
                words = line.split()
                if words[0] == "***":
                    insertingWord = words[10][1:-2] + " "
                elif words[0] == "Intel":
                    insertingWord += words[7][:-1]
                    insertingWord += "\n"
                    PowerFile.write(insertingWord)
            File.close()
            PowerFile.close()

            # ---------------geting the total power of experiments-------- #

            TempFile = open(NewFile, 'r')
            Power = 0
            for line in TempFile:
                words2 = line.split()
                Power += float(words2[0]) * float(words2[1])
            TotalPower = float(Power / 1000.0)
            if TotalPower > MaxPower:
                MaxPower = TotalPower
            if TotalPower < MinPower:
                MinPower = TotalPower

            # -------------getting the total time of execution---------- #

            TempFile2 = open(TimeFiles, "r")
            for line in TempFile2:
                words3 = line.split()
                if line != "\n" and words3[0] == "real":
                    TotalTime = (float(words3[1][0]) * 60) + \
                        float(words3[1][2:-1])
            if TotalTime > MaxTime:
                MaxTime = TotalTime
            if TotalTime < MinTime:
                MinTime = TotalTime
            TimeGen = TimeGen + TotalTime
            PowerGen = PowerGen + TotalPower

        TotalPower = PowerGen / float(ExperimentNum-1)
        TotalTime = TimeGen / float(ExperimentNum-1)
        ResultFile.write(str(layoutT) + " , " + str(TotalPower) + " , " +
                         str(MaxPower) + " , " + str(MinPower) + " , " +
                         str(TotalTime) + " , " + str(MaxTime) + " , " +
                         str(MinTime) + "\n")
    ResultFile.close()
    Opener = ResultFile = open(ResultTable, "r")
    for line in Opener:
        print line

    print "Also you can find all the numbers for Power " \
        "and Performance in : ", ResultTable

# ----------------------------------------------------#
def main():
    LayoutThreads = 8  # Maximum number of threads considered for Layout
    Benchmarks = "../../test/html/perf-rainbow.html"
    OutputDir = "Experiments/"
    os.mkdir(os.path.join(OutputDir), 0777)
    Renderer = " "

    # Parsing the input of the script
    parser = argparse.ArgumentParser(description="Mesuring \
        power and performance of your servo runs")
    parser.add_argument("-b", "--benchmark", help="Gets the \
        benchmark, for example \"-B perf-rainbow.html\"")
    parser.add_argument("-c", "--CPU", help="Rendering with \
        CPU instead of  GPU, for example -C")
    parser.add_argument("-l", "--LayoutThreads", help="Specifyes \
        the maximum number of threads for layout, for example \" -L 5\"")
    parser.add_argument("-o", "--Output", help=" Specifyes \
        the output directory")

    args = parser.parse_args()
    if args.benchmark:
        Benchmarks = args.benchmark
    if args.CPU:
        Renderer = "-c"
    if args.LayoutThreads:
        LayoutThreads = int(args.LayoutThreads)
    if args.Output:
        OutputDir = args.Output

    PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer)
    PowerParser(OutputDir, LayoutThreads)

if __name__ == "__main__":
    main()
<commit_msg>Update PowerMeasure.py<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# !/usr/bin/env python

# ---------Power measurement ------------------------------#
# This script will run the servo with the given benchmark and
# get the power usage using Powermetrics. Results will be put
# in sperate files with that name.
# Do not forget to run the script in servo/src/test/power folder
# --------------------------------------------------------#

import os
import time
import argparse

# ------------------------PowerCollector----------------------------#
# Collecting all the power data and put them into files

def PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer):
    print " Running  the power collector"
    os.mkdir(os.path.join(OutputDir+"power"), 0777)
    os.mkdir(os.path.join(OutputDir+"time"), 0777)
    os.mkdir(os.path.join(OutputDir+"Addtional"), 0777)
    SleepTime = 20
    GuardTime = 0.5
    powerTiming = 1
    ExperimentNum = 21
    for ExpNum in range(1, ExperimentNum):
        for layoutT in range(1, LayoutThreads+1):
            PowerFiles = OutputDir + 'power/' + 'power-Layout' + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            TimeFiles = OutputDir + 'time/' + "time-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            # ServoCmd ="(time ./servo -x -y " + str(layoutT) \
            #    +" "+ Renderer + " " + Benchmarks + " ) 2> " + TimeFiles
            ServoCmd = "(time ../../../build/servo -x -y " + \
                str(layoutT) + " " + Renderer + " " + \
                Benchmarks + " ) 2> " + TimeFiles
            Metrics = OutputDir + 'Addtional/' + "metrics-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + "-css.csv"
            cmd = '(sudo powermetrics -i ' + str(powerTiming) + \
                ' | grep \"energy\\|elapsed\\|servo\" > ' + \
                PowerFiles + '& ) 2> ' + Metrics
            time.sleep(SleepTime)
            os.system(cmd)
            time.sleep(GuardTime)
            os.system(ServoCmd)
            time.sleep(GuardTime)
            os.system('sudo pkill -9 powermetrics')
            time.sleep(SleepTime)

# -------------------PowerParser ---------------------------------#
# Parsing collected power by PowerCollector fucntion

def PowerParser(OutputDir, LayoutThreads):
    print "Running the PowerParser"
    ExperimentNum = 21
    ResultTable = OutputDir + "ResultTable.csv"
    ResultFile = open(ResultTable, "w")
    ResultFile.write("LayoutThreads, MeanPower, MaxPower , MinPower, MeanTime , MaxTime, "
                     "MinTime  \n")

    for layoutT in range(1, LayoutThreads+1):
        MaxTime = 0
        MinTime = 1000000
        MaxPower = 0
        MinPower = 1000000
        TotalPower = 0
        TotalTime = 0
        TimeGen = 0
        PowerGen = 0
        for ExpNum in range(1, ExperimentNum):
            Files = OutputDir + 'power/' + 'power-Layout' + str(layoutT) + \
                "-set" + str(ExpNum) + ".csv"
            NewFile = OutputDir + 'power/Servo-L' + str(layoutT) + \
                "set" + str(ExpNum) + ".csv"
            File = open(Files, 'r')
            PowerFile = open(NewFile, 'w')
            TimeFiles = OutputDir + 'time/' + "time-Layout" + \
                str(layoutT) + "-set" + str(ExpNum) + ".csv"
            # ----Putting the power the power and its time into a table---- #

            for line in File:
                words = line.split()
                if words[0] == "***":
                    insertingWord = words[10][1:-2] + " "
                elif words[0] == "Intel":
                    insertingWord += words[7][:-1]
                    insertingWord += "\n"
                    PowerFile.write(insertingWord)
            File.close()
            PowerFile.close()

            # ---------------geting the total power of experiments-------- #

            TempFile = open(NewFile, 'r')
            Power = 0
            for line in TempFile:
                words2 = line.split()
                Power += float(words2[0]) * float(words2[1])
            TotalPower = float(Power / 1000.0)
            if TotalPower > MaxPower:
                MaxPower = TotalPower
            if TotalPower < MinPower:
                MinPower = TotalPower

            # -------------getting the total time of execution---------- #

            TempFile2 = open(TimeFiles, "r")
            for line in TempFile2:
                words3 = line.split()
                if line != "\n" and words3[0] == "real":
                    TotalTime = (float(words3[1][0]) * 60) + \
                        float(words3[1][2:-1])
            if TotalTime > MaxTime:
                MaxTime = TotalTime
            if TotalTime < MinTime:
                MinTime = TotalTime
            TimeGen = TimeGen + TotalTime
            PowerGen = PowerGen + TotalPower

        TotalPower = PowerGen / float(ExperimentNum-1)
        TotalTime = TimeGen / float(ExperimentNum-1)
        ResultFile.write(str(layoutT) + " , " + str(TotalPower) + " , " +
                         str(MaxPower) + " , " + str(MinPower) + " , " +
                         str(TotalTime) + " , " + str(MaxTime) + " , " +
                         str(MinTime) + "\n")
    ResultFile.close()
    Opener = ResultFile = open(ResultTable, "r")
    for line in Opener:
        print line

    print "Also you can find all the numbers for Power " \
        "and Performance in : ", ResultTable

# ----------------------------------------------------#
def main():
    LayoutThreads = 8  # Maximum number of threads considered for Layout
    Benchmarks = "../../tests/html/perf-rainbow.html"
    OutputDir = "Experiments/"
    os.mkdir(os.path.join(OutputDir), 0777)
    Renderer = " "

    # Parsing the input of the script
    parser = argparse.ArgumentParser(description="Mesuring \
        power and performance of your servo runs")
    parser.add_argument("-b", "--benchmark", help="Gets the \
        benchmark, for example \"-B perf-rainbow.html\"")
    parser.add_argument("-c", "--CPU", help="Rendering with \
        CPU instead of  GPU, for example -C")
    parser.add_argument("-l", "--LayoutThreads", help="Specifyes \
        the maximum number of threads for layout, for example \" -L 5\"")
    parser.add_argument("-o", "--Output", help=" Specifyes \
        the output directory")

    args = parser.parse_args()
    if args.benchmark:
        Benchmarks = args.benchmark
    if args.CPU:
        Renderer = "-c"
    if args.LayoutThreads:
        LayoutThreads = int(args.LayoutThreads)
    if args.Output:
        OutputDir = args.Output

    PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer)
    PowerParser(OutputDir, LayoutThreads)

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# nlimitations under the License.

import os

def test_envvar():
    assert 'TRAMPOLINE_VERSION' in os.environ
    assert os.environ['TRAMPOLINE_VERSION'][0] == '2'
    assert 'RUNNING_IN_CI' in os.environ
    # This is set in .travis.yml or .kokoro/python/common.cfg
    assert os.environ.get('TEST_ENV') == 'test'
    if os.environ['RUNNING_IN_CI'] == 'true':
        ci_envvars_tested = False
        assert 'TRAMPOLINE_CI' in os.environ
        # Travis
        if 'TRAMPOLINE_CI' == 'travis':
            assert 'TRAVIS_BRANCH' in os.environ
            assert 'TRAVIS_BUILD_ID' in os.environ
            assert 'TRAVIS_BUILD_NUMBER' in os.environ
            assert 'TRAVIS_BUILD_WEB_URL' in os.environ
            assert 'TRAVIS_COMMIT' in os.environ
            assert 'TRAVIS_COMMIT_MESSAGE' in os.environ
            assert 'TRAVIS_COMMIT_RANGE' in os.environ
            assert 'TRAVIS_JOB_NAME' in os.environ
            assert 'TRAVIS_JOB_NUMBER' in os.environ
            assert 'TRAVIS_JOB_WEB_URL' in os.environ
            assert 'TRAVIS_REPO_SLUG' in os.environ
            assert 'TRAVIS_SECURE_ENV_VARS' in os.environ
            ci_envvars_tested = True
        if 'TRAMPOLINE_CI' == 'kokoro':
            assert 'KOKORO_BUILD_NUMBER' in os.environ
            assert 'KOKORO_BUILD_ID' in os.environ
            assert 'KOKORO_JOB_NAME' in os.environ
            assert 'KOKORO_GIT_COMMIT' in os.environ
            assert 'KOKORO_GITHUB_COMMIT' in os.environ
            assert 'KOKORO_GITHUB_COMMIT_URL' in os.environ
            if os.environ['KOKORO_JOB_NAME'].endswith('presubmit'):
                assert 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' in os.environ
                assert 'KOKORO_GITHUB_PULL_REQUEST_COMMIT' in os.environ
                assert 'KOKORO_GITHUB_PULL_REQUEST_URL' in os.environ
            ci_envvars_tested = True
        # Make sure we test the ci specific env vars.
        assert ci_envvars_tested
<commit_msg>fix tests again<commit_after>#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# nlimitations under the License.

import os

def test_envvar():
    assert 'TRAMPOLINE_VERSION' in os.environ
    assert os.environ['TRAMPOLINE_VERSION'][0] == '2'
    assert 'RUNNING_IN_CI' in os.environ
    # This is set in .travis.yml or .kokoro/python/common.cfg
    assert os.environ.get('TEST_ENV') == 'test'
    if os.environ['RUNNING_IN_CI'] == 'true':
        ci_envvars_tested = False
        assert 'TRAMPOLINE_CI' in os.environ
        # Travis
        if os.environ['TRAMPOLINE_CI'] == 'travis':
            assert 'TRAVIS_BRANCH' in os.environ
            assert 'TRAVIS_BUILD_ID' in os.environ
            assert 'TRAVIS_BUILD_NUMBER' in os.environ
            assert 'TRAVIS_BUILD_WEB_URL' in os.environ
            assert 'TRAVIS_COMMIT' in os.environ
            assert 'TRAVIS_COMMIT_MESSAGE' in os.environ
            assert 'TRAVIS_COMMIT_RANGE' in os.environ
            assert 'TRAVIS_JOB_NAME' in os.environ
            assert 'TRAVIS_JOB_NUMBER' in os.environ
            assert 'TRAVIS_JOB_WEB_URL' in os.environ
            assert 'TRAVIS_REPO_SLUG' in os.environ
            assert 'TRAVIS_SECURE_ENV_VARS' in os.environ
            ci_envvars_tested = True
        if os.environ['TRAMPOLINE_CI'] == 'kokoro':
            assert 'KOKORO_BUILD_NUMBER' in os.environ
            assert 'KOKORO_BUILD_ID' in os.environ
            assert 'KOKORO_JOB_NAME' in os.environ
            assert 'KOKORO_GIT_COMMIT' in os.environ
            assert 'KOKORO_GITHUB_COMMIT' in os.environ
            assert 'KOKORO_GITHUB_COMMIT_URL' in os.environ
            if os.environ['KOKORO_JOB_NAME'].endswith('presubmit'):
                assert 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' in os.environ
                assert 'KOKORO_GITHUB_PULL_REQUEST_COMMIT' in os.environ
                assert 'KOKORO_GITHUB_PULL_REQUEST_URL' in os.environ
            ci_envvars_tested = True
        # Make sure we test the ci specific env vars.
        assert ci_envvars_tested
<|endoftext|>


<commit_before>#!/usr/bin/env python3

# Copyright (C) 2017 Alexandre Abadie <alexandre.abadie@inria.fr>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import sys
import os
from testrunner import run

EXPECTED_HELP = (
    'Command              Description',
    '---------------------------------------',
    'start_test           starts a test',
    'end_test             ends a test',
    'echo                 prints the input command',
    'reboot               Reboot the node',
    'ps                   Prints information about running threads.',
    'app_metadata         Returns application metadata'
)

EXPECTED_PS = (
    '\tpid | state    Q | pri',
    '\t  1 | pending  Q |  15',
    '\t  2 | running  Q |   7'
)

# In native we are directly executing the binary (no terminal program). We must
# therefore use Ctrl-V (DLE or "data link escape") before Ctrl-C to send a
# literal ETX instead of SIGINT.
# When using a board it is also a problem because we are using a "user friendly"
# terminal that interprets Ctrl-C. So until we have rawterm we must also use
# ctrl-v in boards.

DLE = '\x16'

CONTROL_C = DLE+'\x03'
CONTROL_D = DLE+'\x04'

PROMPT = '> '

CMDS = (
    ('start_test', '[TEST_START]'),
    (CONTROL_C, PROMPT),
    ('\n', PROMPT),
    ('123456789012345678901234567890123456789012345678901234567890',
     'shell: command not found: '
     '123456789012345678901234567890123456789012345678901234567890'),
    ('unknown_command', 'shell: command not found: unknown_command'),
    ('hello-willy\b\b\b\borld', 'shell: command not found: hello-world'),
    ('\b\b\b\becho', '\"echo\"'),
    ('help', EXPECTED_HELP),
    ('echo a string', '\"echo\"\"a\"\"string\"'),
    ("""echo "t\e st" "\\"" '\\'' a\ b""", '"echo""te st"""""\'""a b"'),
    ('echo a\\', 'shell: incorrect quoting'),
    ('echo "', 'shell: incorrect quoting'),
    ("echo '", 'shell: incorrect quoting'),
    ('echo "\'" \'"\'', '"echo""\'""""'),
    ('ps', EXPECTED_PS),
    ('help', EXPECTED_HELP),
    ('reboot', 'test_shell.'),
    ('end_test', '[TEST_END]'),
)

BOARD = os.environ['BOARD']

def print_error(message):
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    print(FAIL + message + ENDC)

def check_cmd(child, cmd, expected):
    child.expect(PROMPT)
    child.sendline(cmd)
    for line in expected:
        child.expect_exact(line)

def check_startup(child):
    child.sendline(CONTROL_C)
    child.expect_exact(PROMPT)

def check_and_get_bufsize(child):
    child.sendline('bufsize')
    child.expect('([0-9]+)\r\n')
    bufsize = int(child.match.group(1))

    return bufsize

def check_line_exceeded(child, longline):

    if BOARD == 'nrf52dk':
        # There is an issue with nrf52dk when the Virtual COM port is connected
        # and sending more than 64 bytes over UART. If no terminal is connected
        # to the Virtual COM and interfacing directly to the nrf52832 UART pins
        # the issue is not present. See issue #10639 on GitHub.
        print_error('test case "check_line_exceeded" broken for nrf52dk. SKIP')
        return

    child.sendline(longline)
    child.expect('shell: maximum line length exceeded')

def check_line_canceling(child):
    child.expect(PROMPT)
    child.sendline('garbage1234' + CONTROL_C)
    garbage_expected = 'garbage1234\r\r\n'
    garbage_received = child.read(len(garbage_expected))

    assert garbage_expected == garbage_received

def check_erase_long_line(child, longline):
    # FIXME: this only works on native, due to #10634 combined with socat
    # insisting in line-buffering the terminal.

    if BOARD == 'native':
        longline_erased = longline + "\b"*len(longline) + "echo"
        child.sendline(longline_erased)
        child.expect_exact('"echo"')

def testfunc(child):
    # avoid sending an extra empty line on native.
    if BOARD == 'native':
        child.crlf = '\n'

    check_startup(child)

    bufsize = check_and_get_bufsize(child)
    longline = "_"*bufsize + "verylong"

    check_line_exceeded(child, longline)

    check_line_canceling(child)

    check_erase_long_line(child, longline)

    # loop other defined commands and expected output
    for cmd, expected in CMDS:
        check_cmd(child, cmd, expected)

if __name__ == "__main__":
    sys.exit(run(testfunc))
<commit_msg>tests/shell: refactor test file and add several new test cases<commit_after>#!/usr/bin/env python3

# Copyright (C) 2017 Alexandre Abadie <alexandre.abadie@inria.fr>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import sys
import os
from testrunner import run

EXPECTED_HELP = (
    'Command              Description',
    '---------------------------------------',
    'start_test           starts a test',
    'end_test             ends a test',
    'echo                 prints the input command',
    'reboot               Reboot the node',
    'ps                   Prints information about running threads.',
    'app_metadata         Returns application metadata'
)

EXPECTED_PS = (
    '\tpid | state    Q | pri',
    '\t  1 | pending  Q |  15',
    '\t  2 | running  Q |   7'
)

# In native we are directly executing the binary (no terminal program). We must
# therefore use Ctrl-V (DLE or "data link escape") before Ctrl-C to send a
# literal ETX instead of SIGINT.
# When using a board it is also a problem because we are using a "user friendly"
# terminal that interprets Ctrl-C. So until we have rawterm we must also use
# ctrl-v in boards.

DLE = '\x16'

CONTROL_C = DLE+'\x03'
CONTROL_D = DLE+'\x04'

PROMPT = '> '

CMDS = (
    # test start
    ('start_test', '[TEST_START]'),
    (CONTROL_C, PROMPT),
    ('\n', PROMPT),

    # test simple word separation
    ('echo a string', '"echo""a""string"'),
    ('echo   multiple   spaces   between   argv', '"echo""multiple""spaces""between""argv"'),
    ('echo \t tabs\t\t processed \t\tlike\t \t\tspaces', '"echo""tabs""processed""like""spaces"'),

    # test long line
    ('123456789012345678901234567890123456789012345678901234567890',
     'shell: command not found: '
     '123456789012345678901234567890123456789012345678901234567890'),
    ('unknown_command', 'shell: command not found: unknown_command'),

    # test leading/trailing BLANK
    ('     echo leading spaces', '"echo""leading""spaces"'),
    ('\t\t\t\t\techo leading tabs', '"echo""leading""tabs"'),
    ('echo trailing spaces     ', '"echo""trailing""spaces"'),
    ('echo trailing tabs\t\t\t\t\t', '"echo""trailing""tabs"'),

    # test backspace
    ('hello-willy\b\b\b\borld', 'shell: command not found: hello-world'),
    ('\b\b\b\becho', '"echo"'),

    # test escaping
    ('echo \\\'', '"echo""\'"'),
    ('echo \\"', '"echo""""'),
    ('echo escaped\\ space', '"echo""escaped space"'),
    ('echo escape within \'\\s\\i\\n\\g\\l\\e\\q\\u\\o\\t\\e\'', '"echo""escape""within""singlequote"'),
    ('echo escape within "\\d\\o\\u\\b\\l\\e\\q\\u\\o\\t\\e"', '"echo""escape""within""doublequote"'),
    ("""echo "t\e st" "\\"" '\\'' a\ b""", '"echo""te st"""""\'""a b"'),

    # test correct quoting
    ('echo "hello"world', '"echo""helloworld"'),
    ('echo hel"lowo"rld', '"echo""helloworld"'),
    ('echo hello"world"', '"echo""helloworld"'),
    ('echo quoted space " "', '"echo""quoted""space"" "'),
    ('echo abc"def\'ghijk"lmn', '"echo""abcdef\'ghijklmn"'),
    ('echo abc\'def"ghijk\'lmn', '"echo""abcdef"ghijklmn"'),
    ('echo "\'" \'"\'', '"echo""\'""""'),

    # test incorrect quoting
    ('echo a\\', 'shell: incorrect quoting'),
    ('echo "', 'shell: incorrect quoting'),
    ('echo \'', 'shell: incorrect quoting'),
    ('echo abcdef"ghijklmn', 'shell: incorrect quoting'),
    ('echo abcdef\'ghijklmn', 'shell: incorrect quoting'),

    # test default commands
    ('ps', EXPECTED_PS),
    ('help', EXPECTED_HELP),

    # test end
    ('reboot', 'test_shell.'),
    ('end_test', '[TEST_END]'),
)

BOARD = os.environ['BOARD']

def print_error(message):
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    print(FAIL + message + ENDC)

def check_cmd(child, cmd, expected):
    child.expect(PROMPT)
    child.sendline(cmd)
    for line in expected:
        child.expect_exact(line)

def check_startup(child):
    child.sendline(CONTROL_C)
    child.expect_exact(PROMPT)

def check_and_get_bufsize(child):
    child.sendline('bufsize')
    child.expect('([0-9]+)\r\n')
    bufsize = int(child.match.group(1))

    return bufsize

def check_line_exceeded(child, longline):

    if BOARD == 'nrf52dk':
        # There is an issue with nrf52dk when the Virtual COM port is connected
        # and sending more than 64 bytes over UART. If no terminal is connected
        # to the Virtual COM and interfacing directly to the nrf52832 UART pins
        # the issue is not present. See issue #10639 on GitHub.
        print_error('test case "check_line_exceeded" broken for nrf52dk. SKIP')
        return

    child.sendline(longline)
    child.expect('shell: maximum line length exceeded')

def check_line_canceling(child):
    child.expect(PROMPT)
    child.sendline('garbage1234' + CONTROL_C)
    garbage_expected = 'garbage1234\r\r\n'
    garbage_received = child.read(len(garbage_expected))

    assert garbage_expected == garbage_received

def check_erase_long_line(child, longline):
    # FIXME: this only works on native, due to #10634 combined with socat
    # insisting in line-buffering the terminal.

    if BOARD == 'native':
        longline_erased = longline + "\b"*len(longline) + "echo"
        child.sendline(longline_erased)
        child.expect_exact('"echo"')

def testfunc(child):
    # avoid sending an extra empty line on native.
    if BOARD == 'native':
        child.crlf = '\n'

    check_startup(child)

    bufsize = check_and_get_bufsize(child)
    longline = "_"*bufsize + "verylong"

    check_line_exceeded(child, longline)

    check_line_canceling(child)

    check_erase_long_line(child, longline)

    # loop other defined commands and expected output
    for cmd, expected in CMDS:
        check_cmd(child, cmd, expected)

if __name__ == "__main__":
    sys.exit(run(testfunc))
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
    Unit tests for sync_app
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import hashlib
import unittest

CURDIR = os.path.abspath(os.curdir)
os.sys.path.append(CURDIR)

from sync_app.file_cache import FileListCache
from sync_app.file_list_local import FileInfoLocal, FileListLocal
#from sync_app.file_list_gdrive import FileInfoGdrive, FileListGdrive
from sync_app.gdrive_instance import GdriveInstance

TEST_FILE = 'tests/test_dir/hello_world.txt'
TEST_DIR = 'tests/test_dir'
TEST_GDR = 'Amazon-Gift-Card.pdf'

class TestSyncApp(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        if os.path.exists('.tmp_file_list_cache.pkl.gz'):
            os.remove('.tmp_file_list_cache.pkl.gz')
        pass

    def test_file_info_local(self):
        finfo = FileInfoLocal(fn=TEST_FILE)
        output = ('%s' % finfo).replace(CURDIR, '')
#        print('finfo', output)
        m = hashlib.md5()
        m.update(output)
        self.assertEqual(m.hexdigest(), 'bb0681fe01c42429d0a7b0994f0d9265')

    def test_file_list_local(self):
        flist = FileListLocal()
        flist.fill_file_list_local(directory=TEST_DIR)
        output = []
        for fl in flist.filelist:
            output.append(('%s' % fl).replace(CURDIR, ''))
        output = sorted(output)
#        print('file_list', '\n'.join(output))
        m = hashlib.md5()
        for out in sorted(output):
            m.update(out)
        self.assertEqual(m.hexdigest(), '30eceb5a3d7b9c761651f8b1df5e53b5')

    def test_file_list_cache(self):
        flist = FileListLocal()
        flist.fill_file_list_local(directory=TEST_DIR)
        fcache = FileListCache(pickle_file='.tmp_file_list_cache.pkl.gz')
        fcache.write_cache_file_list(flist.filelist)
        del flist
        flist = fcache.get_cache_file_list()
        output = []
        for fl in flist.filelist:
            output.append(('%s' % fl).replace(CURDIR, ''))
        output = sorted(output)
#        print('file_cache', '\n'.join(output))
        m = hashlib.md5()
        for out in sorted(output):
            m.update(out)
        self.assertEqual(m.hexdigest(), '30eceb5a3d7b9c761651f8b1df5e53b5')

    def test_gdrive_list_files(self):
        self.test_title = None
        def get_title(item):
            self.test_title = item['title']
        self.gdrive = GdriveInstance(number_to_process=10)
        self.gdrive.list_files(get_title, searchstr=TEST_GDR)
        m = hashlib.md5()
        m.update(self.test_title)
        self.assertEqual(m.hexdigest(), 'ee3ff087897ce88747e7c2b2fc0a59df')

    def test_gdrive_upload_search_delete(self):
        self.test_title = None
        self.test_id = None
        def get_id(item):
            self.test_title = item['title']
            self.test_id= item['id']
        self.gdrive = GdriveInstance(number_to_process=10)
        self.gdrive.upload(TEST_FILE)

        self.gdrive.list_files(get_id,
                               searchstr=os.path.basename(TEST_FILE))

        self.gdrive.delete_file(self.test_id)
        print(self.test_id, self.test_title)

    def test_gdrive_search_directory(self):
        self.test_dirs = {}
        self.test_ids = {}
        def get_id(item):
            self.test_dirs[item['title']] = item['id']
            self.test_ids[item['id']] = item['title']
        self.gdrive = GdriveInstance()
        self.gdrive.get_folders(get_id)
        parents = self.gdrive.get_parents([self.test_dirs['papers']])
        for parent in parents:
#            print('parent', parent)
            if parent['id'] in self.test_ids:
                print('parent', self.test_ids[parent['id']])

    def test_gdrive_list_directories(self):
        self.number_directories = 0
        def get_id(item):
            self.number_directories += 1
        self.gdrive = GdriveInstance()
        self.gdrive.get_folders(get_id)
        print('number', self.number_directories)

if __name__ == '__main__':
    unittest.main()
<commit_msg>test gdrive create directory<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
    Unit tests for sync_app
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import hashlib
import unittest

CURDIR = os.path.abspath(os.curdir)
os.sys.path.append(CURDIR)

from sync_app.file_cache import FileListCache
from sync_app.file_list_local import FileInfoLocal, FileListLocal
#from sync_app.file_list_gdrive import FileInfoGdrive
from sync_app.file_list_gdrive import FileListGdrive
from sync_app.gdrive_instance import GdriveInstance

TEST_FILE = 'tests/test_dir/hello_world.txt'
TEST_DIR = 'tests/test_dir'
TEST_GDR = 'Amazon-Gift-Card.pdf'

class TestSyncApp(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        if os.path.exists('.tmp_file_list_cache.pkl.gz'):
            os.remove('.tmp_file_list_cache.pkl.gz')
        pass

    def test_file_info_local(self):
        finfo = FileInfoLocal(fn=TEST_FILE)
        output = ('%s' % finfo).replace(CURDIR, '')
#        print('finfo', output)
        m = hashlib.md5()
        m.update(output)
        self.assertEqual(m.hexdigest(), 'bb0681fe01c42429d0a7b0994f0d9265')

    def test_file_list_local(self):
        flist = FileListLocal()
        flist.fill_file_list_local(directory=TEST_DIR)
        output = []
        for fl in flist.filelist:
            output.append(('%s' % fl).replace(CURDIR, ''))
        output = sorted(output)
#        print('file_list', '\n'.join(output))
        m = hashlib.md5()
        for out in sorted(output):
            m.update(out)
        self.assertEqual(m.hexdigest(), '30eceb5a3d7b9c761651f8b1df5e53b5')

    def test_file_list_cache(self):
        flist = FileListLocal()
        flist.fill_file_list_local(directory=TEST_DIR)
        fcache = FileListCache(pickle_file='.tmp_file_list_cache.pkl.gz')
        fcache.write_cache_file_list(flist.filelist)
        del flist
        flist = fcache.get_cache_file_list()
        output = []
        for fl in flist.filelist:
            output.append(('%s' % fl).replace(CURDIR, ''))
        output = sorted(output)
#        print('file_cache', '\n'.join(output))
        m = hashlib.md5()
        for out in sorted(output):
            m.update(out)
        self.assertEqual(m.hexdigest(), '30eceb5a3d7b9c761651f8b1df5e53b5')

    def test_gdrive_list_files(self):
        self.test_title = None
        def get_title(item):
            self.test_title = item['title']
        gdrive = GdriveInstance(number_to_process=10)
        gdrive.list_files(get_title, searchstr=TEST_GDR)
        m = hashlib.md5()
        m.update(self.test_title)
        self.assertEqual(m.hexdigest(), 'ee3ff087897ce88747e7c2b2fc0a59df')

    def test_gdrive_upload_search_delete(self):
        self.test_title = None
        self.test_id = None
        def get_id(item):
            self.test_title = item['title']
            self.test_id= item['id']
        gdrive = GdriveInstance(number_to_process=10)
        gdrive.upload(TEST_FILE)

        gdrive.list_files(get_id,
                               searchstr=os.path.basename(TEST_FILE))

        gdrive.delete_file(self.test_id)
        print(self.test_id, self.test_title)

    def test_gdrive_search_directory(self):
        test_dirs = {}
        test_ids = {}
        def get_id(item):
            test_dirs[item['title']] = item['id']
            test_ids[item['id']] = item['title']
        gdrive = GdriveInstance()
        gdrive.get_folders(get_id)
        parents = gdrive.get_parents([test_dirs['share']])
        for parent in parents:
#            print('parent', parent)
            if parent['id'] in test_ids:
                print('parent', test_ids[parent['id']])

    def test_gdrive_list_directories(self):
        gdrive = GdriveInstance()
        flist_gdrive = FileListGdrive()
        gdrive.get_folders(flist_gdrive.append_dir)
        flist_gdrive.fix_export_path()
        expath_ = flist_gdrive.directory_name_dict['share'].exportpath
        self.assertEqual(expath_,
                         '/home/ddboline/gDrive/ATLAS/code/' +
                         'ISF_Calo_Validation/17.2.4.10')

    def test_gdrive_create_directory(self):
        gdrive = GdriveInstance(number_to_process=10)
        body_obj = {'title': 'tests',
                    'mimeType': 'application/vnd.google-apps.folder'}
        request = gdrive.service.files().insert(body=body_obj)
        response = request.execute()
        print(response)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Test Apis."""
import urllib

import pytest
from .factories import TestApiBase
from .utils import *

@pytest.mark.usefixtures('db')
class TestApiServer(TestApiBase):
    """api role testing"""
    uri_prefix = '/api/server'

    server_id = {}

    server_data = {
        'name': u'开发机01',
        'host': u'127.0.0.1',
    }

    # should be equal to server_data_2.name
    server_name_2 = u'test02'

    server_data_2 = {
        'name': u'test02',
        'host': u'192.168.0.1',
    }

    server_data_remove = {
        'name': u'this server will be deleted soon',
        'host': u'11.22.33.44',
    }

    def test_create(self, user, testapp, client, db):
        """create successful."""
        # 1.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data)

        response_success(resp)
        compare_req_resp(self.server_data, resp)

        self.server_data['id'] = resp_json(resp)['data']['id']

        # f.write(str(self.server_data))
        # f.write(str(resp_json(resp)['data']['id']))

        # 2.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data_2)

        response_success(resp)
        compare_req_resp(self.server_data_2, resp)

        self.server_data_2['id'] = resp_json(resp)['data']['id']

    def test_one(self, user, testapp, client, db):
        """item successful."""
        # Goes to homepage

        resp = client.get('%s/%d' % (self.uri_prefix, self.server_data['id']))

        response_success(resp)
        compare_req_resp(self.server_data, resp)

    def test_get_list_page_size(self, user, testapp, client):
        """test list should create 2 users at least, due to test pagination, searching."""

        query = {
            'page': 1,
            'size': 1,
        }
        response = {
            'count': 2,
        }
        resp = client.get('%s/?%s' % (self.uri_prefix, urllib.urlencode(query)))
        response_success(resp)
        resp_dict = resp_json(resp)

        compare_in(self.server_data_2, resp_dict['data']['list'].pop())
        compare_req_resp(response, resp)

    def test_get_list_query(self, user, testapp, client):
        """test list should create 2 users at least, due to test pagination, searching."""
        query = {
            'page': 1,
            'size': 1,
            'kw': self.server_name_2
        }
        response = {
            'count': 1,
        }
        resp = client.get('%s/?%s' % (self.uri_prefix, urllib.urlencode(query)))
        response_success(resp)
        resp_dict = resp_json(resp)

        compare_in(self.server_data_2, resp_dict['data']['list'].pop())
        compare_req_resp(response, resp)

    def test_get_update(self, user, testapp, client):
        """Login successful."""
        # 1.update
        server_data_2 = self.server_data_2
        server_data_2['name'] = 'Tester_edit'
        resp = client.put('%s/%d' % (self.uri_prefix, self.server_data_2['id']), data=server_data_2)

        response_success(resp)
        compare_req_resp(server_data_2, resp)

        # 3.get it
        resp = client.get('%s/%d' % (self.uri_prefix, self.server_data_2['id']))
        response_success(resp)
        compare_req_resp(server_data_2, resp)

    def test_get_remove(self, user, testapp, client):
        """Login successful."""
        # 1.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data_remove)
        server_id = resp_json(resp)['data']['id']
        response_success(resp)

        # 2.delete
        resp = client.delete('%s/%d' % (self.uri_prefix, server_id))
        response_success(resp)

        # 3.get it
        resp = client.get('%s/%d' % (self.uri_prefix, server_id))
        response_error(resp)
<commit_msg>walle 2.0 alpha -  server unittest<commit_after># -*- coding: utf-8 -*-
"""Test Apis."""
import urllib

import pytest
from .factories import TestApiBase
from .utils import *

@pytest.mark.usefixtures('db')
class TestApiServer(TestApiBase):
    """api role testing"""
    uri_prefix = '/api/server'

    server_id = {}

    server_data = {
        'name': u'开发机01',
        'host': u'127.0.0.1',
        'user': u'work',
        'port': u'22',
    }

    # should be equal to server_data_2.name
    server_name_2 = u'test02'

    server_data_2 = {
        'name': u'test02',
        'host': u'192.168.0.1',
        'user': u'work',
        'port': u'22',
    }

    server_data_remove = {
        'name': u'this server will be deleted soon',
        'host': u'11.22.33.44',
        'user': u'work',
        'port': u'22',
    }

    def test_create(self, user, testapp, client, db):
        """create successful."""
        # 1.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data)

        response_success(resp)
        compare_req_resp(self.server_data, resp)

        self.server_data['id'] = resp_json(resp)['data']['id']

        # f.write(str(self.server_data))
        # f.write(str(resp_json(resp)['data']['id']))

        # 2.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data_2)

        response_success(resp)
        compare_req_resp(self.server_data_2, resp)

        self.server_data_2['id'] = resp_json(resp)['data']['id']

    def test_one(self, user, testapp, client, db):
        """item successful."""
        # Goes to homepage

        resp = client.get('%s/%d' % (self.uri_prefix, self.server_data['id']))

        response_success(resp)
        compare_req_resp(self.server_data, resp)

    def test_get_list_page_size(self, user, testapp, client):
        """test list should create 2 users at least, due to test pagination, searching."""

        query = {
            'page': 1,
            'size': 1,
        }
        response = {
            'count': 2,
        }
        resp = client.get('%s/?%s' % (self.uri_prefix, urllib.urlencode(query)))
        response_success(resp)
        resp_dict = resp_json(resp)

        compare_in(self.server_data_2, resp_dict['data']['list'].pop())
        compare_req_resp(response, resp)

    def test_get_list_query(self, user, testapp, client):
        """test list should create 2 users at least, due to test pagination, searching."""
        query = {
            'page': 1,
            'size': 1,
            'kw': self.server_name_2
        }
        response = {
            'count': 1,
        }
        resp = client.get('%s/?%s' % (self.uri_prefix, urllib.urlencode(query)))
        response_success(resp)
        resp_dict = resp_json(resp)

        compare_in(self.server_data_2, resp_dict['data']['list'].pop())
        compare_req_resp(response, resp)

    def test_get_update(self, user, testapp, client):
        """Login successful."""
        # 1.update
        server_data_2 = self.server_data_2
        server_data_2['name'] = 'Tester_edit'
        resp = client.put('%s/%d' % (self.uri_prefix, self.server_data_2['id']), data=server_data_2)

        response_success(resp)
        compare_req_resp(server_data_2, resp)

        # 3.get it
        resp = client.get('%s/%d' % (self.uri_prefix, self.server_data_2['id']))
        response_success(resp)
        compare_req_resp(server_data_2, resp)

    def test_get_remove(self, user, testapp, client):
        """Login successful."""
        # 1.create another role
        resp = client.post('%s/' % (self.uri_prefix), data=self.server_data_remove)
        server_id = resp_json(resp)['data']['id']
        response_success(resp)

        # 2.delete
        resp = client.delete('%s/%d' % (self.uri_prefix, server_id))
        response_success(resp)

        # 3.get it
        resp = client.get('%s/%d' % (self.uri_prefix, server_id))
        response_error(resp)
<|endoftext|>


<commit_before>import kindred
import kindred.Dependencies
import os
import shutil
import pytest
import pytest_socket

def test_corenlpFail():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.initializeCoreNLP()
	assert excinfo.value.args == ("Could not find the Stanford CoreNLP files. Use kindred.downloadCoreNLP() first",)

def test_parseFailWithNoCoreNLP():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	text = 'You need to turn in your homework by next week'
	corpus = kindred.Corpus(text)

	parser = kindred.Parser()

	with pytest.raises(RuntimeError) as excinfo:
		parser.parse(corpus)
	assert excinfo.value.args == ("Cannot access local CoreNLP at http://localhost:9000 and cannot find CoreNLP files to launch subprocess. Please download using kindred.downloadCoreNLP() if subprocess should be used",)

def test_corenlpDownloadFail():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()
	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.downloadCoreNLP()
	pytest_socket.enable_socket()
	assert excinfo.value.args == ("A test tried to use socket.socket.",)

def test_corenlpDownloadFail_corruptExistingFile():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	# Create a corrupt file that will fail a SHA256 test
	corenlpDownloadPath = os.path.join(kindred.Dependencies.downloadDirectory,'stanford-corenlp-full-2016-10-31.zip')
	os.mkdir(kindred.Dependencies.downloadDirectory)
	with open(corenlpDownloadPath,'w') as f:
		f.write("\n".join(map(str,range(100))))

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()
	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.downloadCoreNLP()
	pytest_socket.enable_socket()
	assert excinfo.value.args == ("A test tried to use socket.socket.",)

def test_corenlpDownload():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.downloadCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True
	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()
	assert kindred.Dependencies.testCoreNLPConnection() == True

def test_corenlpDownloadTwice():
	kindred.Dependencies.downloadCoreNLP()

	kindred.Dependencies.downloadCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True

def test_corenlpKill():
	if not kindred.Dependencies.testCoreNLPConnection():
		kindred.Dependencies.initializeCoreNLP()

	kindred.Dependencies.killCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == True

def test_initializeTwice():
	kindred.Dependencies.initializeCoreNLP()

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True

def test_corenlpInitializeFail():
	if kindred.Dependencies.testCoreNLPConnection():
		kindred.Dependencies.killCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()

	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.initializeCoreNLP()

	pytest_socket.enable_socket()

	assert excinfo.value.args == ("Unable to connect to launched CoreNLP subprocess",)

	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == True

if __name__ == '__main__':
	test_corenlpFail()
<commit_msg>Added test to check Parser with uninit CoreNLP<commit_after>import kindred
import kindred.Dependencies
import os
import shutil
import pytest
import pytest_socket

def test_corenlpFail():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.initializeCoreNLP()
	assert excinfo.value.args == ("Could not find the Stanford CoreNLP files. Use kindred.downloadCoreNLP() first",)

def test_parseFailWithNoCoreNLP():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	text = 'You need to turn in your homework by next week'
	corpus = kindred.Corpus(text)

	parser = kindred.Parser()

	with pytest.raises(RuntimeError) as excinfo:
		parser.parse(corpus)
	assert excinfo.value.args == ("Cannot access local CoreNLP at http://localhost:9000 and cannot find CoreNLP files to launch subprocess. Please download using kindred.downloadCoreNLP() if subprocess should be used",)

def test_corenlpDownloadFail():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()
	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.downloadCoreNLP()
	pytest_socket.enable_socket()
	assert excinfo.value.args == ("A test tried to use socket.socket.",)

def test_corenlpDownloadFail_corruptExistingFile():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	# Create a corrupt file that will fail a SHA256 test
	corenlpDownloadPath = os.path.join(kindred.Dependencies.downloadDirectory,'stanford-corenlp-full-2016-10-31.zip')
	os.mkdir(kindred.Dependencies.downloadDirectory)
	with open(corenlpDownloadPath,'w') as f:
		f.write("\n".join(map(str,range(100))))

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()
	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.downloadCoreNLP()
	pytest_socket.enable_socket()
	assert excinfo.value.args == ("A test tried to use socket.socket.",)

def test_corenlpDownload():
	if os.path.isdir(kindred.Dependencies.downloadDirectory):
		shutil.rmtree(kindred.Dependencies.downloadDirectory)

	assert kindred.Dependencies.checkCoreNLPDownload() == False
	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.downloadCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True
	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()
	assert kindred.Dependencies.testCoreNLPConnection() == True

def test_corenlpDownloadTwice():
	kindred.Dependencies.downloadCoreNLP()

	kindred.Dependencies.downloadCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True

def test_corenlpKill():
	if not kindred.Dependencies.checkCoreNLPDownload():
		kindred.Dependencies.downloadCoreNLP()
	if not kindred.Dependencies.testCoreNLPConnection():
		kindred.Dependencies.initializeCoreNLP()

	kindred.Dependencies.killCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == True

def test_initializeTwice():
	if not kindred.Dependencies.checkCoreNLPDownload():
		kindred.Dependencies.downloadCoreNLP()

	kindred.Dependencies.initializeCoreNLP()

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.checkCoreNLPDownload() == True

def test_parseSucceedWithUninitializeCoreNLP():
	if not kindred.Dependencies.checkCoreNLPDownload():
		kindred.Dependencies.downloadCoreNLP()
	if kindred.Dependencies.testCoreNLPConnection():
		kindred.Dependencies.killCoreNLP()

	text = 'You need to turn in your homework by next week'
	corpus = kindred.Corpus(text)

	parser = kindred.Parser()

	parser.parse(corpus)

def test_corenlpInitializeFail():
	if not kindred.Dependencies.checkCoreNLPDownload():
		kindred.Dependencies.downloadCoreNLP()
	if kindred.Dependencies.testCoreNLPConnection():
		kindred.Dependencies.killCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == False

	pytest_socket.disable_socket()

	with pytest.raises(RuntimeError) as excinfo:
		kindred.Dependencies.initializeCoreNLP()

	pytest_socket.enable_socket()

	assert excinfo.value.args == ("Unable to connect to launched CoreNLP subprocess",)

	assert kindred.Dependencies.testCoreNLPConnection() == False

	kindred.Dependencies.initializeCoreNLP()

	assert kindred.Dependencies.testCoreNLPConnection() == True

if __name__ == '__main__':
	test_corenlpFail()
<|endoftext|>


<commit_before>"""Tests for build_package script."""

from mock import (
    Mock,
    patch
)
import os
from textwrap import dedent
import unittest

from build_package import (
    build_binary,
    BUILD_DEB_TEMPLATE,
    build_in_lxc,
    CREATE_LXC_TEMPLATE,
    DEFAULT_SPB,
    get_args,
    main,
    move_debs,
    parse_dsc,
    setup_local,
    setup_lxc,
    SourceFile,
    teardown_lxc,
)
from utils import (
    autopatch,
    temp_dir,
)

DSC_CONTENT = dedent("""\
    Format: 3.0 (quilt)
    Source: juju-core
    Binary: juju-core, juju, juju-local, juju-local-kvm
    Architecture: any all
    Version: 1.24-beta1-0ubuntu1~14.04.1~juju1
    Maintainer: Curtis Hovey <curtis.hovey@canonical.com>
    Homepage: http://launchpad.net/juju-core
    Standards-Version: 3.9.5
    Build-Depends: debhelper (>= 7.0.50~), golang-go, lsb-release, python
    Package-List:
     juju deb devel extra
     juju-core deb devel extra
     juju-local deb devel extra
     juju-local-kvm deb devel extra
    Checksums-Sha1:
     1234 9876 juju-core_1.24-beta1.orig.tar.gz
     2345 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Checksums-Sha256:
     3456 9876 juju-core_1.24-beta1.orig.tar.gz
     4567 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Files:
     5678 9876 juju-core_1.24-beta1.orig.tar.gz
     6789 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Testsuite: autopkgtest
    """)

def make_source_files(workspace, dsc_name):
    dsc_path = os.path.join(workspace, dsc_name)
    dsc_file = SourceFile(None, None, 'my.dsc', dsc_path)
    orig_file = SourceFile(
        '3456', '9876', 'juju-core_1.24-beta1.orig.tar.gz',
        '%s/juju-core_1.24-beta1.orig.tar.gz' % workspace)
    deb_file = SourceFile(
        '4567', '4321',
        'juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz',
        '%s/juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz' %
        workspace)
    source_files = [dsc_file, orig_file, deb_file]
    for sf in source_files:
        with open(sf.path, 'w') as f:
            if '.dsc' in sf.name:
                f.write(DSC_CONTENT)
            else:
                f.write(sf.name)
    return source_files

class BuildPackageTestCase(unittest.TestCase):

    def test_get_args_source(self):
        args = get_args(
            ['prog', 'source', 'my.tar.gz', '~/workspace', 'trusty',
             '123', '456'])
        self.assertEqual('source', args.command)
        self.assertEqual('my.tar.gz', args.tar_file)
        self.assertEqual('~/workspace', args.location)
        self.assertEqual('trusty', args.series)
        self.assertEqual(['123', '456'], args.bugs)
        self.assertFalse(args.verbose)

    def test_main_source(self):
        with patch('build_package.build_source', autospec=True,
                   return_value=0) as bs_mock:
            code = main([
                'prog', 'source', '--debemail', 'email', '--debfullname', 'me',
                'my.tar.gz', '~/workspace', 'trusty', '123', '456'])
        self.assertEqual(0, code)
        bs_mock.assert_called_with(
            'my.tar.gz', '~/workspace', 'trusty', ['123', '456'],
            debemail='email', debfullname='me', gpgcmd='/usr/bin/gpg',
            branch=DEFAULT_SPB, upatch=0, verbose=False)

    def test_get_args_binary(self):
        args = get_args(
            ['prog', 'binary', 'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual('binary', args.command)
        self.assertEqual('my.dsc', args.dsc)
        self.assertEqual('~/workspace', args.location)
        self.assertIs(None, args.ppa)
        self.assertFalse(args.verbose)

    def test_get_args_binary_with_ppa(self):
        args = get_args(
            ['prog', 'binary', '--ppa', 'ppa:juju/experimental',
             'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual('ppa:juju/experimental', args.ppa)

    def test_main_binary(self):
        with patch('build_package.build_binary', autospec=True,
                   return_value=0) as bb_mock:
            code = main(
                ['prog', 'binary', 'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual(0, code)
        bb_mock.assert_called_with(
            'my.dsc', '~/workspace', 'trusty', 'i386', ppa=None, verbose=False)

    @autopatch('build_package.move_debs', return_value=True)
    @autopatch('build_package.teardown_lxc', return_value=True)
    @autopatch('build_package.build_in_lxc')
    @autopatch('build_package.setup_lxc', return_value='trusty-i386')
    @autopatch('build_package.setup_local', return_value='build_dir')
    @autopatch('build_package.parse_dsc', return_value=['orig', 'debian'])
    def test_build_binary(self,
                          pd_mock, sl_mock, l_mock, bl_mock, tl_mock, md_mock):
        code = build_binary(
            'my.dsc', '~/workspace', 'trusty', 'i386', verbose=False)
        self.assertEqual(0, code)
        pd_mock.assert_called_with('my.dsc', verbose=False)
        sl_mock.assert_called_with(
            '~/workspace', 'trusty', 'i386', ['orig', 'debian'], verbose=False)
        l_mock.assert_called_with('trusty', 'i386', 'build_dir', verbose=False)
        bl_mock.assert_called_with(
            'trusty-i386', 'build_dir', ppa=None, verbose=False)
        tl_mock.assert_called_with('trusty-i386', verbose=False)
        md_mock.assert_called_with(
            'build_dir', '~/workspace', verbose=False)

    @patch('build_package.build_in_lxc', side_effect=Exception)
    @patch('build_package.setup_lxc', return_value='trusty-i386')
    @patch('build_package.setup_local')
    @patch('build_package.parse_dsc')
    def test_build_binary_teardown_lxc(self, p_mock, s_mock, l_mock, b_mock):
        with patch('build_package.teardown_lxc', autospec=True,
                   return_value=True) as tl_mock:
            with self.assertRaises(Exception):
                build_binary(
                    'my.dsc', '~/workspace', 'trusty', 'i386',
                    verbose=False)
        tl_mock.assert_called_with('trusty-i386', verbose=False)

    def test_parse_dsc(self):
        with temp_dir() as workspace:
            expected_files = make_source_files(workspace, 'my.dsc')
            dsc_path = os.path.join(workspace, 'my.dsc')
            source_files = parse_dsc(dsc_path, verbose=False)
        self.assertEqual(expected_files, source_files)

    def test_setup_local(self):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            self.assertEqual(
                os.path.join(workspace, 'juju-build-trusty-i386'),
                build_dir)
            self.assertTrue(os.path.isdir(build_dir))

    def test_setup_lxc(self):
        with patch('subprocess.check_output') as co_mock:
            container = setup_lxc(
                'trusty', 'i386', '/build-dir', verbose=False)
        self.assertEqual('trusty-i386', container)
        lxc_script = CREATE_LXC_TEMPLATE.format(
            container='trusty-i386', series='trusty', arch='i386',
            build_dir='/build-dir')
        co_mock.assert_called_with([lxc_script], shell=True)

    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            with patch('subprocess.call', return_value=0) as p_mock:
                code = build_in_lxc('trusty-i386', build_dir,
                                    ppa=None, verbose=False)
        self.assertEqual(0, code)
        oc_mock.assert_any_call(build_dir, 0o777)
        cc_mock.assert_any_call(
            ['sudo', 'lxc-start', '-d', '-n', 'trusty-i386'])
        build_script = BUILD_DEB_TEMPLATE.format(
            container='trusty-i386', ppa='# No PPA added.')
        p_mock.assert_called_with([build_script], shell=True)
        cc_mock.assert_any_call(['sudo', 'lxc-stop', '-n', 'trusty-i386'])
        user = os.environ.get('USER', 'jenkins')
        cc_mock.assert_any_call(['sudo', 'chown', '-R', user, build_dir])
        oc_mock.assert_any_call(build_dir, 0o775)

    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc_with_ppa(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            proc = Mock(returncode=0)
            with patch('subprocess.Popen', return_value=proc) as p_mock:
                build_in_lxc('trusty-i386', build_dir,
                             ppa='ppa:juju/golang', verbose=False)
        build_script = BUILD_DEB_TEMPLATE.format(
            container='trusty-i386',
            ppa='deb http://ppa.launchpad.net/juju/golang/ubuntu trusty main')
        p_mock.assert_called_with([build_script], shell=True)

    @patch.dict(os.environ, {'USER': 'bingo'})
    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc_stop_lxc(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            with patch('subprocess.Popen', side_effect=Exception):
                with self.assertRaises(Exception):
                    build_in_lxc('trusty-i386', build_dir, verbose=False)
        cc_mock.assert_any_call(['sudo', 'lxc-stop', '-n', 'trusty-i386'])
        cc_mock.assert_any_call(['sudo', 'chown', '-R', 'bingo', build_dir])
        oc_mock.assert_any_call(build_dir, 0o775)

    def test_teardown_lxc(self):
        with patch('subprocess.check_call') as cc_mock:
            teardown_lxc('trusty-i386', verbose=False)
        cc_mock.assert_called_with(
            ['sudo', 'lxc-destroy', '-n', 'trusty-i386'])

    def test_move_debs(self):
        with temp_dir() as workspace:
            build_dir = os.path.join(workspace, 'juju-build-trusty-i386')
            os.makedirs(build_dir)
            deb_file = os.path.join(build_dir, 'my.deb')
            with open(deb_file, 'w') as f:
                f.write('deb')
            found = move_debs(build_dir, workspace)
            self.assertTrue(found)
            self.assertFalse(os.path.isfile(deb_file))
            self.assertTrue(os.path.isfile(os.path.join(workspace, 'my.deb')))
<commit_msg>Added more assertions of arguments.<commit_after>"""Tests for build_package script."""

from mock import (
    Mock,
    patch
)
import os
from textwrap import dedent
import unittest

from build_package import (
    build_binary,
    BUILD_DEB_TEMPLATE,
    build_in_lxc,
    CREATE_LXC_TEMPLATE,
    DEFAULT_SPB,
    get_args,
    main,
    move_debs,
    parse_dsc,
    setup_local,
    setup_lxc,
    SourceFile,
    teardown_lxc,
)
from utils import (
    autopatch,
    temp_dir,
)

DSC_CONTENT = dedent("""\
    Format: 3.0 (quilt)
    Source: juju-core
    Binary: juju-core, juju, juju-local, juju-local-kvm
    Architecture: any all
    Version: 1.24-beta1-0ubuntu1~14.04.1~juju1
    Maintainer: Curtis Hovey <curtis.hovey@canonical.com>
    Homepage: http://launchpad.net/juju-core
    Standards-Version: 3.9.5
    Build-Depends: debhelper (>= 7.0.50~), golang-go, lsb-release, python
    Package-List:
     juju deb devel extra
     juju-core deb devel extra
     juju-local deb devel extra
     juju-local-kvm deb devel extra
    Checksums-Sha1:
     1234 9876 juju-core_1.24-beta1.orig.tar.gz
     2345 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Checksums-Sha256:
     3456 9876 juju-core_1.24-beta1.orig.tar.gz
     4567 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Files:
     5678 9876 juju-core_1.24-beta1.orig.tar.gz
     6789 4321 juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz
    Testsuite: autopkgtest
    """)

def make_source_files(workspace, dsc_name):
    dsc_path = os.path.join(workspace, dsc_name)
    dsc_file = SourceFile(None, None, 'my.dsc', dsc_path)
    orig_file = SourceFile(
        '3456', '9876', 'juju-core_1.24-beta1.orig.tar.gz',
        '%s/juju-core_1.24-beta1.orig.tar.gz' % workspace)
    deb_file = SourceFile(
        '4567', '4321',
        'juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz',
        '%s/juju-core_1.24-beta1-0ubuntu1~14.04.1~juju1.debian.tar.gz' %
        workspace)
    source_files = [dsc_file, orig_file, deb_file]
    for sf in source_files:
        with open(sf.path, 'w') as f:
            if '.dsc' in sf.name:
                f.write(DSC_CONTENT)
            else:
                f.write(sf.name)
    return source_files

class BuildPackageTestCase(unittest.TestCase):

    def test_get_args_source(self):
        shell_env = {'DEBEMAIL': 'me@email', 'DEBFULLNAME': 'me'}
        with patch.dict('os.environ', shell_env):
            args = get_args(
                ['prog', 'source', 'my.tar.gz', '~/workspace', 'trusty',
                 '123', '456'])
        self.assertEqual('source', args.command)
        self.assertEqual('my.tar.gz', args.tar_file)
        self.assertEqual('~/workspace', args.location)
        self.assertEqual('trusty', args.series)
        self.assertEqual(['123', '456'], args.bugs)
        self.assertEqual('me@email', args.debemail)
        self.assertEqual('me', args.debfullname)
        self.assertEqual('/usr/bin/gpg', args.gpgcmd)
        self.assertEqual(DEFAULT_SPB, args.branch)
        self.assertEqual(0, args.upatch)
        self.assertFalse(args.verbose)

    def test_main_source(self):
        with patch('build_package.build_source', autospec=True,
                   return_value=0) as bs_mock:
            code = main([
                'prog', 'source',
                '--debemail', 'me@email', '--debfullname', 'me',
                'my.tar.gz', '~/workspace', 'trusty', '123', '456'])
        self.assertEqual(0, code)
        bs_mock.assert_called_with(
            'my.tar.gz', '~/workspace', 'trusty', ['123', '456'],
            debemail='me@email', debfullname='me', gpgcmd='/usr/bin/gpg',
            branch=DEFAULT_SPB, upatch=0, verbose=False)

    def test_get_args_binary(self):
        args = get_args(
            ['prog', 'binary', 'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual('binary', args.command)
        self.assertEqual('my.dsc', args.dsc)
        self.assertEqual('~/workspace', args.location)
        self.assertIs(None, args.ppa)
        self.assertFalse(args.verbose)

    def test_get_args_binary_with_ppa(self):
        args = get_args(
            ['prog', 'binary', '--ppa', 'ppa:juju/experimental',
             'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual('ppa:juju/experimental', args.ppa)

    def test_main_binary(self):
        with patch('build_package.build_binary', autospec=True,
                   return_value=0) as bb_mock:
            code = main(
                ['prog', 'binary', 'my.dsc', '~/workspace', 'trusty', 'i386'])
        self.assertEqual(0, code)
        bb_mock.assert_called_with(
            'my.dsc', '~/workspace', 'trusty', 'i386', ppa=None, verbose=False)

    @autopatch('build_package.move_debs', return_value=True)
    @autopatch('build_package.teardown_lxc', return_value=True)
    @autopatch('build_package.build_in_lxc')
    @autopatch('build_package.setup_lxc', return_value='trusty-i386')
    @autopatch('build_package.setup_local', return_value='build_dir')
    @autopatch('build_package.parse_dsc', return_value=['orig', 'debian'])
    def test_build_binary(self,
                          pd_mock, sl_mock, l_mock, bl_mock, tl_mock, md_mock):
        code = build_binary(
            'my.dsc', '~/workspace', 'trusty', 'i386', verbose=False)
        self.assertEqual(0, code)
        pd_mock.assert_called_with('my.dsc', verbose=False)
        sl_mock.assert_called_with(
            '~/workspace', 'trusty', 'i386', ['orig', 'debian'], verbose=False)
        l_mock.assert_called_with('trusty', 'i386', 'build_dir', verbose=False)
        bl_mock.assert_called_with(
            'trusty-i386', 'build_dir', ppa=None, verbose=False)
        tl_mock.assert_called_with('trusty-i386', verbose=False)
        md_mock.assert_called_with(
            'build_dir', '~/workspace', verbose=False)

    @patch('build_package.build_in_lxc', side_effect=Exception)
    @patch('build_package.setup_lxc', return_value='trusty-i386')
    @patch('build_package.setup_local')
    @patch('build_package.parse_dsc')
    def test_build_binary_teardown_lxc(self, p_mock, s_mock, l_mock, b_mock):
        with patch('build_package.teardown_lxc', autospec=True,
                   return_value=True) as tl_mock:
            with self.assertRaises(Exception):
                build_binary(
                    'my.dsc', '~/workspace', 'trusty', 'i386',
                    verbose=False)
        tl_mock.assert_called_with('trusty-i386', verbose=False)

    def test_parse_dsc(self):
        with temp_dir() as workspace:
            expected_files = make_source_files(workspace, 'my.dsc')
            dsc_path = os.path.join(workspace, 'my.dsc')
            source_files = parse_dsc(dsc_path, verbose=False)
        self.assertEqual(expected_files, source_files)

    def test_setup_local(self):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            self.assertEqual(
                os.path.join(workspace, 'juju-build-trusty-i386'),
                build_dir)
            self.assertTrue(os.path.isdir(build_dir))

    def test_setup_lxc(self):
        with patch('subprocess.check_output') as co_mock:
            container = setup_lxc(
                'trusty', 'i386', '/build-dir', verbose=False)
        self.assertEqual('trusty-i386', container)
        lxc_script = CREATE_LXC_TEMPLATE.format(
            container='trusty-i386', series='trusty', arch='i386',
            build_dir='/build-dir')
        co_mock.assert_called_with([lxc_script], shell=True)

    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            with patch('subprocess.call', return_value=0) as p_mock:
                code = build_in_lxc('trusty-i386', build_dir,
                                    ppa=None, verbose=False)
        self.assertEqual(0, code)
        oc_mock.assert_any_call(build_dir, 0o777)
        cc_mock.assert_any_call(
            ['sudo', 'lxc-start', '-d', '-n', 'trusty-i386'])
        build_script = BUILD_DEB_TEMPLATE.format(
            container='trusty-i386', ppa='# No PPA added.')
        p_mock.assert_called_with([build_script], shell=True)
        cc_mock.assert_any_call(['sudo', 'lxc-stop', '-n', 'trusty-i386'])
        user = os.environ.get('USER', 'jenkins')
        cc_mock.assert_any_call(['sudo', 'chown', '-R', user, build_dir])
        oc_mock.assert_any_call(build_dir, 0o775)

    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc_with_ppa(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            proc = Mock(returncode=0)
            with patch('subprocess.Popen', return_value=proc) as p_mock:
                build_in_lxc('trusty-i386', build_dir,
                             ppa='ppa:juju/golang', verbose=False)
        build_script = BUILD_DEB_TEMPLATE.format(
            container='trusty-i386',
            ppa='deb http://ppa.launchpad.net/juju/golang/ubuntu trusty main')
        p_mock.assert_called_with([build_script], shell=True)

    @patch.dict(os.environ, {'USER': 'bingo'})
    @autopatch('subprocess.check_call')
    @autopatch('os.chmod')
    def test_build_in_lxc_stop_lxc(self, oc_mock, cc_mock):
        with temp_dir() as workspace:
            source_files = make_source_files(workspace, 'my.dsc')
            build_dir = setup_local(
                workspace, 'trusty', 'i386', source_files, verbose=False)
            with patch('subprocess.Popen', side_effect=Exception):
                with self.assertRaises(Exception):
                    build_in_lxc('trusty-i386', build_dir, verbose=False)
        cc_mock.assert_any_call(['sudo', 'lxc-stop', '-n', 'trusty-i386'])
        cc_mock.assert_any_call(['sudo', 'chown', '-R', 'bingo', build_dir])
        oc_mock.assert_any_call(build_dir, 0o775)

    def test_teardown_lxc(self):
        with patch('subprocess.check_call') as cc_mock:
            teardown_lxc('trusty-i386', verbose=False)
        cc_mock.assert_called_with(
            ['sudo', 'lxc-destroy', '-n', 'trusty-i386'])

    def test_move_debs(self):
        with temp_dir() as workspace:
            build_dir = os.path.join(workspace, 'juju-build-trusty-i386')
            os.makedirs(build_dir)
            deb_file = os.path.join(build_dir, 'my.deb')
            with open(deb_file, 'w') as f:
                f.write('deb')
            found = move_debs(build_dir, workspace)
            self.assertTrue(found)
            self.assertFalse(os.path.isfile(deb_file))
            self.assertTrue(os.path.isfile(os.path.join(workspace, 'my.deb')))
<|endoftext|>


<commit_before>from __future__ import absolute_import

import consulate
import pytest
import six

import proxenos.errors
import proxenos.mappers.consul

def test_mapper_construction(consul_mapper, consul_host, consul_port):
    consulate.Consul.assert_called_once_with(
        host=consul_host, port=consul_port)

def test_mapper_update(consul_mapper_synced, cluster):
    assert consul_mapper_synced.cluster == cluster

def test_mapper_connection_error():
    with pytest.raises(proxenos.errors.ServiceDiscoveryConnectionError):
        mapper = proxenos.mappers.consul.ConsulClusterMapper(
            host='127.0.0.1', port=65537)

@pytest.mark.parametrize('key,expected_host', (
    ('secret', '192.168.100.104:8000',),
    (six.text_type(u'touché'), '192.168.100.109:8000'),
    ('moist towelette', '192.168.100.106:8000'),
))
def test_mapper_select(consul_mapper_synced, key, expected_host):
    assert (str(consul_mapper_synced.select(key).socket_address) ==
            expected_host)
<commit_msg>lol python 2<commit_after># coding: utf-8
from __future__ import absolute_import

import consulate
import pytest
import six

import proxenos.errors
import proxenos.mappers.consul

def test_mapper_construction(consul_mapper, consul_host, consul_port):
    consulate.Consul.assert_called_once_with(
        host=consul_host, port=consul_port)

def test_mapper_update(consul_mapper_synced, cluster):
    assert consul_mapper_synced.cluster == cluster

def test_mapper_connection_error():
    with pytest.raises(proxenos.errors.ServiceDiscoveryConnectionError):
        mapper = proxenos.mappers.consul.ConsulClusterMapper(
            host='127.0.0.1', port=65537)

@pytest.mark.parametrize('key,expected_host', (
    ('secret', '192.168.100.104:8000',),
    (six.text_type(u'touché'), '192.168.100.109:8000'),
    ('moist towelette', '192.168.100.106:8000'),
))
def test_mapper_select(consul_mapper_synced, key, expected_host):
    assert (str(consul_mapper_synced.select(key).socket_address) ==
            expected_host)
<|endoftext|>


<commit_before>"""Tests for DockerSpawner class"""

import json

import docker
import pytest
from jupyterhub.tests.test_api import add_user, api_request
from jupyterhub.tests.mocking import public_url
from jupyterhub.tests.utils import async_requests
from jupyterhub.utils import url_path_join

from dockerspawner import DockerSpawner

pytestmark = pytest.mark.usefixtures("dockerspawner")

@pytest.mark.gen_test
def test_start_stop(app):
    name = "has@"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    token = user.new_api_token()
    # start the server
    r = yield api_request(app, "users", name, "server", method="post")
    while r.status_code == 202:
        # request again
        r = yield api_request(app, "users", name, "server", method="post")
    assert r.status_code == 201, r.text
    url = url_path_join(public_url(app, user), "api/status")
    r = yield async_requests.get(url, headers={"Authorization": "token %s" % token})
    assert r.url == url
    r.raise_for_status()
    print(r.text)
    assert "kernels" in r.json()

@pytest.mark.gen_test
@pytest.mark.parametrize("image", ["0.8", "0.9", "nomatch"])
def test_image_whitelist(app, image):
    name = "checker"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    user.spawner.remove_containers = True
    user.spawner.image_whitelist = {
        "0.9": "jupyterhub/singleuser:0.9",
        "0.8": "jupyterhub/singleuser:0.8",
    }
    token = user.new_api_token()
    # start the server
    r = yield api_request(
        app, "users", name, "server", method="post", data=json.dumps({"image": image})
    )
    if image not in user.spawner.image_whitelist:
        with pytest.raises(Exception):
            r.raise_for_status()
        return
    while r.status_code == 202:
        # request again
        r = yield api_request(app, "users", name, "server", method="post")
    assert r.status_code == 201, r.text
    url = url_path_join(public_url(app, user), "api/status")
    r = yield async_requests.get(url, headers={"Authorization": "token %s" % token})
    r.raise_for_status()
    assert r.headers['x-jupyterhub-version'].startswith(image)
    r = yield api_request(
        app, "users", name, "server", method="delete",
    )
    r.raise_for_status()

@pytest.mark.gen_test
def test_image_pull_policy(app):
    name = "gumby"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    spawner = user.spawners[""]
    spawner.image = "jupyterhub/doesntexist:nosuchtag"
    with pytest.raises(docker.errors.NotFound):
        spawner.image_pull_policy = "never"
        yield spawner.pull_image(spawner.image)

    repo = "busybox"
    tag = "1.29.1"  # a version that's definitely not latest
    # ensure image isn't present
    try:
        yield spawner.docker("remove_image", "{}:{}".format(repo, tag))
    except docker.errors.ImageNotFound:
        pass

    spawner.pull_policy = "ifnotpresent"
    image = "{}:{}".format(repo, tag)
    # should trigger a pull
    yield spawner.pull_image(image)
    # verify that the image exists now
    old_image_info = yield spawner.docker("inspect_image", image)
    print(old_image_info)

    # now tag busybox:latest as our current version
    # which is not latest!
    yield spawner.docker("tag", image, repo)

    image = repo  # implicit :latest
    spawner.pull_policy = "ifnotpresent"
    # check with ifnotpresent shouldn't pull
    yield spawner.pull_image(image)
    image_info = yield spawner.docker("inspect_image", repo)
    assert image_info["Id"] == old_image_info["Id"]

    # run again with Always,
    # should trigger a pull even though the image is present
    spawner.pull_policy = "always"
    spawner.pull_image(image)
    image_info = yield spawner.docker("inspect_image", repo)
    assert image_info["Id"] != old_image_info["Id"]

    # run again with never, make sure it's still happy
    spawner.pull_policy = "never"
    spawner.pull_image(image)
<commit_msg>wait more patiently<commit_after>"""Tests for DockerSpawner class"""

import json

import docker
import pytest
from jupyterhub.tests.test_api import add_user, api_request
from jupyterhub.tests.mocking import public_url
from jupyterhub.tests.utils import async_requests
from jupyterhub.utils import url_path_join
from tornado import gen

from dockerspawner import DockerSpawner

pytestmark = pytest.mark.usefixtures("dockerspawner")

@pytest.mark.gen_test
def test_start_stop(app):
    name = "has@"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    token = user.new_api_token()
    # start the server
    r = yield api_request(app, "users", name, "server", method="post")
    while r.status_code == 202:
        # request again
        r = yield api_request(app, "users", name, "server", method="post")
        yield gen.sleep(0.1)
    assert r.status_code == 201, r.text
    url = url_path_join(public_url(app, user), "api/status")
    r = yield async_requests.get(url, headers={"Authorization": "token %s" % token})
    assert r.url == url
    r.raise_for_status()
    print(r.text)
    assert "kernels" in r.json()

@pytest.mark.gen_test
@pytest.mark.parametrize("image", ["0.8", "0.9", "nomatch"])
def test_image_whitelist(app, image):
    name = "checker"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    user.spawner.remove_containers = True
    user.spawner.image_whitelist = {
        "0.9": "jupyterhub/singleuser:0.9",
        "0.8": "jupyterhub/singleuser:0.8",
    }
    token = user.new_api_token()
    # start the server
    r = yield api_request(
        app, "users", name, "server", method="post", data=json.dumps({"image": image})
    )
    if image not in user.spawner.image_whitelist:
        with pytest.raises(Exception):
            r.raise_for_status()
        return
    while r.status_code == 202:
        # request again
        r = yield api_request(app, "users", name, "server", method="post")
        yield gen.sleep(0.1)
    assert r.status_code == 201, r.text
    url = url_path_join(public_url(app, user), "api/status")
    r = yield async_requests.get(url, headers={"Authorization": "token %s" % token})
    r.raise_for_status()
    assert r.headers['x-jupyterhub-version'].startswith(image)
    r = yield api_request(
        app, "users", name, "server", method="delete",
    )
    r.raise_for_status()

@pytest.mark.gen_test
def test_image_pull_policy(app):
    name = "gumby"
    add_user(app.db, app, name=name)
    user = app.users[name]
    assert isinstance(user.spawner, DockerSpawner)
    spawner = user.spawners[""]
    spawner.image = "jupyterhub/doesntexist:nosuchtag"
    with pytest.raises(docker.errors.NotFound):
        spawner.image_pull_policy = "never"
        yield spawner.pull_image(spawner.image)

    repo = "busybox"
    tag = "1.29.1"  # a version that's definitely not latest
    # ensure image isn't present
    try:
        yield spawner.docker("remove_image", "{}:{}".format(repo, tag))
    except docker.errors.ImageNotFound:
        pass

    spawner.pull_policy = "ifnotpresent"
    image = "{}:{}".format(repo, tag)
    # should trigger a pull
    yield spawner.pull_image(image)
    # verify that the image exists now
    old_image_info = yield spawner.docker("inspect_image", image)
    print(old_image_info)

    # now tag busybox:latest as our current version
    # which is not latest!
    yield spawner.docker("tag", image, repo)

    image = repo  # implicit :latest
    spawner.pull_policy = "ifnotpresent"
    # check with ifnotpresent shouldn't pull
    yield spawner.pull_image(image)
    image_info = yield spawner.docker("inspect_image", repo)
    assert image_info["Id"] == old_image_info["Id"]

    # run again with Always,
    # should trigger a pull even though the image is present
    spawner.pull_policy = "always"
    spawner.pull_image(image)
    image_info = yield spawner.docker("inspect_image", repo)
    assert image_info["Id"] != old_image_info["Id"]

    # run again with never, make sure it's still happy
    spawner.pull_policy = "never"
    spawner.pull_image(image)
<|endoftext|>


<commit_before>from elm_doc import elm_codeshift

def test_elm_codeshift_strips_ports_from_single_line_declaration():
    source = 'port cmd : String -> Cmd ()'
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = '''cmd : String -> Cmd ()
cmd a0 = Cmd.none
'''
    assert actual == expected

def test_elm_codeshift_strips_ports_from_multiline_declaration():
    source = '''
-- all arguments on the next line
port cmd :
  String -> Cmd ()

{-| last argument on the next line
-}
port cmd : String
  -> Cmd ()

-- suddenly, an unrelated function
func : ()

{- first and last
   argument on the next line
-}
port sub :
  Sub ()

port sub1 : Sub ()

{-| with interspersed comments -}
port cmd : String
    -- Inline comment
  -> Foo
  {- Single line comment -}
  -> Foo
  {-| Single line doc comment -}
  -> Foo
  {-| Multi line
comment -}
  -> Cmd ()
'''
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = '''
-- all arguments on the next line
cmd :
  String -> Cmd ()
cmd a0 = Cmd.none

{-| last argument on the next line
-}
cmd : String
  -> Cmd ()
cmd a0 = Cmd.none

-- suddenly, an unrelated function
func : ()

{- first and last
   argument on the next line
-}
sub :
  Sub ()
sub = Sub.none

sub1 : Sub ()
sub1 = Sub.none

{-| with interspersed comments -}
cmd : String
    -- Inline comment
  -> Foo
  {- Single line comment -}
  -> Foo
  {-| Single line doc comment -}
  -> Foo
  {-| Multi line
comment -}
  -> Cmd ()
cmd a0 a1 a2 a3 = Cmd.none
'''
    assert actual == expected
<commit_msg>Add tests for codeshifting port module declaration<commit_after>from elm_doc import elm_codeshift

def test_elm_codeshift_strips_ports_from_single_line_port_module_declaration():
    source = 'port module Main exposing (main)'
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = 'module Main exposing (main)'
    assert actual == expected

def test_elm_codeshift_strips_ports_from_multiline_port_module_declaration():
    source = '''{-| doc comment before module -}
port module Main exposing
  {-| multiline
comment
-}
  ( main,
  -- inline comment
    init,
  )
'''
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = '''{-| doc comment before module -}
module Main exposing
  {-| multiline
comment
-}
  ( main,
  -- inline comment
    init,
  )
'''
    assert actual == expected

def test_elm_codeshift_strips_ports_from_single_line_port_function_declaration():
    source = 'port cmd : String -> Cmd ()'
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = '''cmd : String -> Cmd ()
cmd a0 = Cmd.none
'''
    assert actual == expected

def test_elm_codeshift_strips_ports_from_multiline_port_function_declaration():
    source = '''
-- all arguments on the next line
port cmd :
  String -> Cmd ()

{-| last argument on the next line
-}
port cmd : String
  -> Cmd ()

-- suddenly, an unrelated function
func : ()

{- first and last
   argument on the next line
-}
port sub :
  Sub ()

port sub1 : Sub ()

{-| with interspersed comments -}
port cmd : String
    -- Inline comment
  -> Foo
  {- Single line comment -}
  -> Foo
  {-| Single line doc comment -}
  -> Foo
  {-| Multi line
comment -}
  -> Cmd ()
'''
    actual = elm_codeshift.strip_ports_from_string(source)
    expected = '''
-- all arguments on the next line
cmd :
  String -> Cmd ()
cmd a0 = Cmd.none

{-| last argument on the next line
-}
cmd : String
  -> Cmd ()
cmd a0 = Cmd.none

-- suddenly, an unrelated function
func : ()

{- first and last
   argument on the next line
-}
sub :
  Sub ()
sub = Sub.none

sub1 : Sub ()
sub1 = Sub.none

{-| with interspersed comments -}
cmd : String
    -- Inline comment
  -> Foo
  {- Single line comment -}
  -> Foo
  {-| Single line doc comment -}
  -> Foo
  {-| Multi line
comment -}
  -> Cmd ()
cmd a0 a1 a2 a3 = Cmd.none
'''
    assert actual == expected
<|endoftext|>