30ed937cd62e78bf7b9e1bc8c54941ab92e783d9
[it/test.git] / ric_robot_suite / robot / resources / ric / ric_utils.robot
1 #   Copyright (c) 2019 AT&T Intellectual Property.
2 #
3 #   Licensed under the Apache License, Version 2.0 (the "License");
4 #   you may not use this file except in compliance with the License.
5 #   You may obtain a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #   Unless required by applicable law or agreed to in writing, software
10 #   distributed under the License is distributed on an "AS IS" BASIS,
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #   See the License for the specific language governing permissions and
13 #   limitations under the License.
14
15 *** Settings ***
16 Documentation  A library of utility keywords that may be useful
17 ...            in various test suites.
18
19 Library        Collections
20
21 *** Keywords ***
22 #
23 # Kubernetes Utilities
24 Most Recent Availability Condition
25   # this makes the probably-unsafe assumption that the conditions are ordered
26   # temporally.
27   [Arguments]  @{Conditions}
28   ${status} =  Set Variable  'False'
29   :FOR  ${Condition}  IN  @{Conditions}
30   \  ${status} =  Set Variable If  '${Condition.type}' == 'Available'  ${Condition.status}  ${status}
31   [Return]  ${status}
32
33 Most Recent Container Logs
34   [Arguments]  ${deployment}  ${container}=${EMPTY}  ${regex}=${EMPTY}
35   ${pods} =            Retrieve Pods For Deployment  ${deployment}
36   ${logs} =            Create List
37   :FOR  ${pod}  IN  @{pods}
38   \  ${log} =   Retrieve Log For Pod     ${pod}             ${container}
39   \  Should Not Be Empty        ${log}   No log entries for ${pod}/${container}
40   \  ${line} =  Run Keyword If           "${regex}" != "${EMPTY}"
41   ...                                    Most Recent Match  ${log}  ${regex}
42   ...           ELSE
43   ...                                    Get From List      ${log}  -1
44   \  Append To List             ${logs}  ${line}
45   [Return]                      ${logs}
46
47 #
48 # Robot metatools
49 Run Keyword If Present
50   [Documentation]  Run the given keyword, ignoring errors and returning status, if it exists.
51   ...              Returns "PASS" if the keyword does not exist.
52   [Arguments]            ${kw}
53   ${exists}  ${u} =      Run Keyword And Ignore Error
54   ...                    Keyword Should Exist          ${kw}
55   ${status}  ${u} =      Run Keyword If                '${exists}' == 'PASS'
56   ...                    Run Keyword And Ignore Error  ${kw}
57   ...                    ELSE
58   ...                    Set Variable   PASS  unused
59   [Return]               ${status}
60
61 #
62 # Data manipulation
63 Toggle Flag
64     [Documentation]      Apply "not" to a boolean flag
65     [Arguments]          ${flag}
66     ${bFlag} =           Convert To Boolean  ${flag}
67     ${bFlag} =           Set Variable If     ${bFlag}  ${false}  ${true}
68     [Return]             ${bFlag}
69
70 Most Recent Match
71     [Arguments]    ${list}        ${regex}
72     ${matches} =   Get Matches    ${list}     regexp=${regex}
73     Should Not Be Empty           ${matches}  No log entries matching ${regex}
74     ${match} =     Get From List  ${matches}  -1
75     [Return]       ${match}
76   
77 Pluck
78      [Documentation]  Get the values of a specific key from a list of dictionaries
79      [Arguments]      ${k}      ${l}
80      @{names} =       Evaluate  filter(lambda v: v != None, [i.get('${k}', None) for i in ${l}])
81      [Return]         ${names}
82
83 Subtract From List
84      [Documentation]  Remove the elements of the second argument from the first
85      [Arguments]      ${x}  ${y}
86      ${diff} =        Run Keyword If  ${y}
87      ...              Evaluate  filter(lambda v: v not in ${y}, ${x})
88      ...              ELSE
89      ...              Set Variable    ${x}
90      [Return]         ${diff}