ce699a20222ea12edc696004f0c90a4b8551cc2f
[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   END   
32   [Return]  ${status}
33
34 Most Recent Container Logs
35   [Arguments]  ${deployment}  ${container}=${EMPTY}  ${regex}=${EMPTY}
36   ${pods} =            Retrieve Pods For Deployment  ${deployment}
37   ${logs} =            Create List
38   FOR  ${pod}  IN  @{pods}
39      ${log} =   Retrieve Log For Pod     ${pod}             ${container}
40      Should Not Be Empty        ${log}   No log entries for ${pod}/${container}
41      ${line} =  Run Keyword If           "${regex}" != "${EMPTY}"
42      ...                                 Most Recent Match  ${log}  ${regex}
43      ...        ELSE
44      ...                                 Get From List      ${log}  -1
45      Append To List             ${logs}  ${line}
46   END   
47   [Return]                      ${logs}
48
49 Component Should Be Ready
50   [Documentation]  Given a Kubernetes controller object as returned from the KubernetesEntity
51   ...              library routines (such as 'Deployment' or Statefulset'), check whether the
52   ...              entity is ready
53   [Arguments]      ${entity}
54   Should Be Equal  ${entity.status.replicas}           ${entity.status.ready_replicas}
55   # This doesn't seem to make sense for statefulsets
56   ${status} =      Run Keyword If                      '${entity.kind}' == 'Deployment'
57   ...              Most Recent Availability Condition  @{entity.status.conditions}
58   ...  ELSE
59   ...              Set Variable                        True
60   Should Be Equal As Strings  ${status}  True  ignore_case=True
61   [Return]         ${status}
62   
63 #
64 # Robot metatools
65 Run Keyword If Present
66   [Documentation]  Run the given keyword, ignoring errors and returning status, if it exists.
67   ...              Returns "PASS" if the keyword does not exist.
68   [Arguments]            ${kw}
69   ${exists}  ${u} =      Run Keyword And Ignore Error
70   ...                    Keyword Should Exist          ${kw}
71   ${status}  ${u} =      Run Keyword If                '${exists}' == 'PASS'
72   ...                    Run Keyword And Ignore Error  ${kw}
73   ...                    ELSE
74   ...                    Set Variable   PASS  unused
75   [Return]               ${status}
76
77 #
78 # Data manipulation
79 Toggle Flag
80     [Documentation]      Apply "not" to a boolean flag
81     [Arguments]          ${flag}
82     ${bFlag} =           Convert To Boolean  ${flag}
83     ${bFlag} =           Set Variable If     ${bFlag}  ${false}  ${true}
84     [Return]             ${bFlag}
85
86 Most Recent Match
87     [Arguments]    ${list}        ${regex}
88     ${matches} =   Get Matches    ${list}     regexp=${regex}
89     Should Not Be Empty           ${matches}  No log entries matching ${regex}
90     ${match} =     Get From List  ${matches}  -1
91     [Return]       ${match}
92   
93 Pluck
94      [Documentation]  Get the values of a specific key from a list of dictionaries
95      [Arguments]      ${k}      ${l}
96      @{names} =       Evaluate  filter(lambda v: v != None, [i.get('${k}', None) for i in ${l}])
97      [Return]         ${names}
98
99 Subtract From List
100      [Documentation]  Remove the elements of the second argument from the first
101      [Arguments]      ${x}  ${y}
102      ${diff} =        Run Keyword If  ${y}
103      ...              Evaluate  filter(lambda v: v not in ${y}, ${x})
104      ...              ELSE
105      ...              Set Variable    ${x}
106      [Return]         ${diff}