Add ability to use JAVA_HOME to find java
[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 ${JDKVERSION}     None
21 ${JAVA_7_HOME_CENTOS}    /usr/lib/jvm/java-1.7.0
22 ${JAVA_7_HOME_UBUNTU}    /usr/lib/jvm/java-7-openjdk-amd64
23 ${JAVA_8_HOME_CENTOS}    /usr/lib/jvm/java-1.8.0
24 ${JAVA_8_HOME_UBUNTU}    /usr/lib/jvm/java-8-openjdk-amd64
25 ${NEXUS_FALLBACK_URL}    ${NEXUSURL_PREFIX}/content/repositories/opendaylight.snapshot
26
27 *** Keywords ***
28 Initialize_Artifact_Deployment_And_Usage
29     [Arguments]    ${tools_system_connect}=True
30     [Documentation]    Places search utility to ODL system, which will be needed for version detection.
31     ...    By default also initialize a SSH connection to Tools system,
32     ...    as following Keywords assume a working connection towards target system.
33     # Connect to the ODL machine.
34     ${odl}=    SSHKeywords.Open_Connection_To_ODL_System
35     # Deploy the search tool.
36     SSHLibrary.Put_File    ${CURDIR}/../../tools/deployment/search.sh
37     SSHLibrary.Close_Connection
38     # Optionally connect to the Tools System machine.
39     BuiltIn.Return_From_Keyword_If    not (${tools_system_connect})    # the argument may be a convoluted Python expression
40     SSHKeywords.Open_Connection_To_Tools_System
41
42 NexusKeywords__Get_Items_To_Look_At
43     [Arguments]    ${component}
44     [Documentation]    Get a list of items that might contain the version number that we are looking for.
45     BuiltIn.Return_From_Keyword_If    '${component}' == 'bgpcep'    pcep-impl
46     [Return]    ${component}-impl
47
48 NexusKeywords__Detect_Version_To_Pull
49     [Arguments]    ${component}
50     [Documentation]    Determine the exact Nexus directory to be used as a source for a particular test tool
51     ...    Figure out what version of the tool needs to be pulled out of the
52     ...    Nexus by looking at the version directory of the subsystem from
53     ...    which the tool is being pulled. This code is REALLY UGLY but there
54     ...    is no way around it until the bug
55     ...    https://bugs.opendaylight.org/show_bug.cgi?id=5206 gets fixed.
56     ...    I also don't want to depend on maven-metadata-local.xml and other
57     ...    bits and pieces of ODL distribution which are not required for ODL
58     ...    to function properly.
59     ${itemlist}=    NexusKeywords__Get_Items_To_Look_At    ${component}
60     ${current_ssh_connection}=    SSHLibrary.Get Connection
61     SSHKeywords.Open_Connection_To_ODL_System
62     ${version}    ${result}=    SSHLibrary.Execute_Command    sh search.sh ${WORKSPACE}/${BUNDLEFOLDER}/system ${itemlist}    return_rc=True
63     SSHLibrary.Close_Connection
64     Restore Current SSH Connection From Index    ${current_ssh_connection.index}
65     BuiltIn.Log    ${version}
66     BuiltIn.Run_Keyword_If    ${result}!=0    BuiltIn.Fail    Component "${component}" not found, cannot locate test tool
67     ${version}    ${location}=    String.Split_String    ${version}    max_split=1
68     [Return]    ${version}    ${location}
69
70 Deploy_Artifact
71     [Arguments]    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}=-executable.jar    ${fallback_url}=${NEXUS_FALLBACK_URL}
72     [Documentation]    Deploy the specified artifact from Nexus to the cwd of the machine to which the active SSHLibrary connection points.
73     ...    Must have ${BUNDLE_URL} variable set to the URL from which the
74     ...    tested ODL distribution was downloaded and this place must be
75     ...    inside a repository created by a standard distribution
76     ...    construction job. If this is detected to ne be the case, fallback URL is used.
77     ${urlbase}=    String.Fetch_From_Left    ${BUNDLE_URL}    /org/opendaylight
78     # If the BUNDLE_URL points somewhere else (perhaps *patch-test* job in Jenkins),
79     # ${urlbase} is the whole ${BUNDLE_URL}, in which case we use the ${fallback_url}
80     ${urlbase}=    BuiltIn.Set_Variable_If    '${urlbase}' != '${BUNDLE_URL}'    ${urlbase}    ${fallback_url}
81     ${version}    ${location}=    NexusKeywords__Detect_Version_To_Pull    ${component}
82     # TODO: Use RequestsLibrary and String instead of curl and bash utilities?
83     ${url}=    BuiltIn.Set_Variable    ${urlbase}/${location}/${artifact}/${version}
84     ${namepart}=    SSHLibrary.Execute_Command    curl -L ${url}/maven-metadata.xml | grep value | head -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1
85     BuiltIn.Log    ${namepart}
86     ${length}=    BuiltIn.Get_Length    ${namepart}
87     ${namepart}=    BuiltIn.Set_Variable_If    ${length} == 0    ${version}    ${namepart}
88     ${filename}=    BuiltIn.Set_Variable    ${name_prefix}${namepart}${name_suffix}
89     BuiltIn.Log    ${filename}
90     ${url}=    BuiltIn.Set_Variable    ${url}/${filename}
91     ${response}    ${result}=    SSHLibrary.Execute_Command    wget -q -N ${url} 2>&1    return_rc=True
92     BuiltIn.Log    ${response}
93     BuiltIn.Run_Keyword_If    ${result} != 0    BuiltIn.Fail    Artifact "${artifact}" in component "${component}" could not be downloaded from ${url}
94     [Return]    ${filename}
95
96 Deploy_Test_Tool
97     [Arguments]    ${component}    ${artifact}    ${suffix}=executable
98     [Documentation]    Deploy a test tool.
99     ...    The test tools have naming convention of the form
100     ...    "<repository_url>/some/dir/somewhere/<tool-name>/<tool-name>-<version-tag>-${suffix}.jar"
101     ...    where "<tool-name>" is the name of the tool and "<version-tag>" is
102     ...    the version tag that is digged out of the maven metadata. This
103     ...    keyword calculates ${name_prefix} and ${name_suffix} for
104     ...    "Deploy_Artifact" and then calls "Deploy_Artifact" to do the real
105     ...    work of deploying the artifact.
106     ${name_prefix}=    BuiltIn.Set_Variable    ${artifact}-
107     ${name_suffix}=    BuiltIn.Set_Variable    -${suffix}.jar
108     ${filename}=    Deploy_Artifact    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}
109     [Return]    ${filename}
110
111 Compose_Dilemma_Filepath
112     [Arguments]    ${default_path}    ${specific_path}
113     [Documentation]    Query active SSH connection, return specific path if it exists else default path.
114     ${out}    ${rc}=    SSHLibrary.Execute_Command    ls -lA ${specific_path} 2>&1    return_rc=True
115     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${specific_path}
116     BuiltIn.Return_From_Keyword    ${default_path}
117
118 Compose_Base_Java_Command
119     [Arguments]    ${openjdk}=${JDKVERSION}
120     [Documentation]    Return string suitable for launching Java programs over SSHLibrary, depending on JRE version needed.
121     ...    This requires that the SSH connection on which the command is going to be used is active as it is needed for querying files.
122     ...    Commands composed for one SSH connection shall not be reused on other SSH connections as the two connections may have different Java setups.
123     ...    Not directly related to Nexus, but versioned Java tools may need this.
124     # Check whether the user set the override and return it if yes.
125     BuiltIn.Run_Keyword_And_Return_If    """${openjdk}""" == "openjdk8"    Compose_Dilemma_Filepath    ${JAVA_8_HOME_CENTOS}/bin/java    ${JAVA_8_HOME_UBUNTU}/bin/java
126     BuiltIn.Run_Keyword_And_Return_If    """${openjdk}""" == "openjdk7"    Compose_Dilemma_Filepath    ${JAVA_7_HOME_CENTOS}/bin/java    ${JAVA_7_HOME_UBUNTU}/bin/java
127     # Attempt to call plain "java" command directly. If it works, return it.
128     ${out}    ${rc}=    SSHLibrary.Execute_Command    java -version 2>&1    return_rc=True
129     BuiltIn.Return_From_Keyword_If    ${rc} == 0    java
130     # Query the virtual machine for the JAVA_HOME environment variable and
131     # use it to assemble a (hopefully) working command. If that worked out,
132     # return the result.
133     ${java}=    SSHLibrary.Execute_Command    echo \$JAVA_HOME/bin/java 2>&1
134     ${out}    ${rc}=    SSHLibrary.Execute_Command    ${java} -version 2>&1    return_rc=True
135     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${java}
136     # There are bizzare test environment setups where the (correct) JAVA_HOME
137     # is set in the VM where Robot is running but not in the VM where the
138     # tools are supposed to run (usually because these two are really one
139     # and the same system and idiosyncracies of BASH prevent easy injection
140     # of the JAVA_HOME environment variable into a place where connections
141     # made by SSHLibrary would pick it up). So try to use that value to
142     # create a java command and check that it works.
143     ${JAVA_HOME}=    OperatingSystem.Get_Environment_Variable    JAVA_HOME    ${EMPTY}
144     ${java}=    BuiltIn.Set_Variable_If    """${JAVA_HOME}"""!=""    ${JAVA_HOME}/bin/java    false
145     ${out}    ${rc}=    SSHLibrary.Execute_Command    ${java} -version 2>&1    return_rc=True
146     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${java}
147     # Nothing works, most likely java is not installed at all on the target
148     # machine or it is hopelesly lost. Bail out with a helpful message
149     # telling the user how to make it accessible for the script.
150     BuiltIn.Fail    Unable to find Java; specify \${JDKVERSION}, put it to your PATH or set JAVA_HOME environment variable.
151
152 Compose_Full_Java_Command
153     [Arguments]    ${options}    ${openjdk}=${JDKVERSION}
154     [Documentation]    Return full Bash command to run Java with given options.
155     ...    This requires that the SSH connection on which the command is going to be used is active as it is needed for querying files.
156     ...    The options may include JVM options, application command line arguments, Bash redirects and other constructs.
157     ${base_command}=    Compose_Base_Java_Command    openjdk=${openjdk}
158     ${full_command}=    BuiltIn.Set_Variable    ${base_command} ${options}
159     BuiltIn.Log    ${full_command}
160     [Return]    ${full_command}