Add ability to compose java command according to version
[integration/test.git] / csit / libraries / NexusKeywords.robot
1 *** Settings ***
2 Documentation     Nexus repository access keywords.
3 ...
4 ...               Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...               This program and the accompanying materials are made available under the
7 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...               This library encapsulates a bunch of somewhat complex and commonly used
12 ...               netconf operations into reusable keywords to make writing netconf
13 ...               test suites easier.
14 Library           OperatingSystem
15 Library           SSHLibrary
16 Library           String
17 Resource          SSHKeywords.robot
18
19 *** Variables ***
20 ${NEXUS_FALLBACK_URL}    ${NEXUSURL_PREFIX}/content/repositories/opendaylight.snapshot
21
22 *** Keywords ***
23 Initialize_Artifact_Deployment_And_Usage
24     [Arguments]    ${tools_system_connect}=True
25     [Documentation]    Places search utility to ODL system, which will be needed for version detection.
26     ...    By default also initialize a SSH connection to Tools system,
27     ...    as following Keywords assume a working connection towards target system.
28     # Connect to the ODL machine.
29     ${odl}=    SSHKeywords.Open_Connection_To_ODL_System
30     # Deploy the search tool.
31     SSHLibrary.Put_File    ${CURDIR}/../../tools/deployment/search.sh
32     SSHLibrary.Close_Connection
33     # Optionally connect to the Tools System machine.
34     BuiltIn.Return_From_Keyword_If    not (${tools_system_connect})    # the argument may be a convoluted Python expression
35     SSHKeywords.Open_Connection_To_Tools_System
36
37 NexusKeywords__Get_Items_To_Look_At
38     [Arguments]    ${component}
39     [Documentation]    Get a list of items that might contain the version number that we are looking for.
40     BuiltIn.Return_From_Keyword_If    '${component}' == 'bgpcep'    pcep-impl
41     [Return]    ${component}-impl
42
43 NexusKeywords__Detect_Version_To_Pull
44     [Arguments]    ${component}
45     [Documentation]    Determine the exact Nexus directory to be used as a source for a particular test tool
46     ...    Figure out what version of the tool needs to be pulled out of the
47     ...    Nexus by looking at the version directory of the subsystem from
48     ...    which the tool is being pulled. This code is REALLY UGLY but there
49     ...    is no way around it until the bug
50     ...    https://bugs.opendaylight.org/show_bug.cgi?id=5206 gets fixed.
51     ...    I also don't want to depend on maven-metadata-local.xml and other
52     ...    bits and pieces of ODL distribution which are not required for ODL
53     ...    to function properly.
54     ${itemlist}=    NexusKeywords__Get_Items_To_Look_At    ${component}
55     ${current_ssh_connection}=    SSHLibrary.Get Connection
56     SSHKeywords.Open_Connection_To_ODL_System
57     ${version}    ${result}=    SSHLibrary.Execute_Command    sh search.sh ${WORKSPACE}/${BUNDLEFOLDER}/system ${itemlist}    return_rc=True
58     SSHLibrary.Close_Connection
59     Restore Current SSH Connection From Index    ${current_ssh_connection.index}
60     BuiltIn.Log    ${version}
61     BuiltIn.Run_Keyword_If    ${result}!=0    BuiltIn.Fail    Component "${component}" not found, cannot locate test tool
62     ${version}    ${location}=    String.Split_String    ${version}    max_split=1
63     [Return]    ${version}    ${location}
64
65 Deploy_Artifact
66     [Arguments]    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}=-executable.jar    ${fallback_url}=${NEXUS_FALLBACK_URL}
67     [Documentation]    Deploy the specified artifact from Nexus to the cwd of the machine to which the active SSHLibrary connection points.
68     ...    Must have ${BUNDLE_URL} variable set to the URL from which the
69     ...    tested ODL distribution was downloaded and this place must be
70     ...    inside a repository created by a standard distribution
71     ...    construction job. If this is detected to ne be the case, fallback URL is used.
72     ${urlbase}=    String.Fetch_From_Left    ${BUNDLE_URL}    /org/opendaylight
73     # If the BUNDLE_URL points somewhere else (perhaps *patch-test* job in Jenkins),
74     # ${urlbase} is the whole ${BUNDLE_URL}, in which case we use the ${fallback_url}
75     ${urlbase}=    BuiltIn.Set_Variable_If    '${urlbase}' != '${BUNDLE_URL}'    ${urlbase}    ${fallback_url}
76     ${version}    ${location}=    NexusKeywords__Detect_Version_To_Pull    ${component}
77     # TODO: Use RequestsLibrary and String instead of curl and bash utilities?
78     ${url}=    BuiltIn.Set_Variable    ${urlbase}/${location}/${artifact}/${version}
79     ${namepart}=    SSHLibrary.Execute_Command    curl ${url}/maven-metadata.xml | grep value | head -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1
80     BuiltIn.Log    ${namepart}
81     ${length}=    BuiltIn.Get_Length    ${namepart}
82     ${namepart}=    BuiltIn.Set_Variable_If    ${length} == 0    ${version}    ${namepart}
83     ${filename}=    BuiltIn.Set_Variable    ${name_prefix}${namepart}${name_suffix}
84     BuiltIn.Log    ${filename}
85     ${url}=    BuiltIn.Set_Variable    ${url}/${filename}
86     ${response}    ${result}=    SSHLibrary.Execute_Command    wget -q -N ${url} 2>&1    return_rc=True
87     BuiltIn.Log    ${response}
88     BuiltIn.Run_Keyword_If    ${result} != 0    BuiltIn.Fail    Artifact "${artifact}" in component "${component}" could not be downloaded from ${url}
89     [Return]    ${filename}
90
91 Deploy_Test_Tool
92     [Arguments]    ${component}    ${artifact}    ${suffix}=executable
93     [Documentation]    Deploy a test tool.
94     ...    The test tools have naming convention of the form
95     ...    "<repository_url>/some/dir/somewhere/<tool-name>/<tool-name>-<version-tag>-${suffix}.jar"
96     ...    where "<tool-name>" is the name of the tool and "<version-tag>" is
97     ...    the version tag that is digged out of the maven metadata. This
98     ...    keyword calculates ${name_prefix} and ${name_suffix} for
99     ...    "Deploy_Artifact" and then calls "Deploy_Artifact" to do the real
100     ...    work of deploying the artifact.
101     ${name_prefix}=    BuiltIn.Set_Variable    ${artifact}-
102     ${name_suffix}=    BuiltIn.Set_Variable    -${suffix}.jar
103     ${filename}=    Deploy_Artifact    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}
104     [Return]    ${filename}
105
106 Compose_Base_Java_Command
107     [Arguments]    ${openjdk}=${JDKVERSION}
108     [Documentation]    Return string suitable for launching Java programs over SSHLibrary, depending on JRE version needed.
109     ...    Not directly related to nexus, but different versioned Java artifacts may need this.
110     BuiltIn.Return_From_Keyword_If    """${openjdk}""" == "openjdk8"    /usr/lib/jvm/java-1.8.0/bin/java
111     BuiltIn.Return_From_Keyword_If    """${openjdk}""" == "openjdk7"    /usr/lib/jvm/java-1.7.0/bin/java
112     BuiltIn.Return_From_Keyword    java
113
114 Compose_Full_Java_Command
115     [Arguments]    ${options}    ${openjdk}=${JDKVERSION}
116     [Documentation]    Return full Bash command to run Java with given options.
117     ...    The options may include JVM options, application command line arguments, Bash redirects and other constructs.
118     ${base_command} =    Compose_Base_Java_Command    openjdk=${openjdk}
119     ${full_command} =    BuiltIn.Set_Variable    ${base_command} ${options}
120     BuiltIn.Log    ${full_command}
121     [Return]    ${full_command}