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