BjornMagnussonXA | e60d04e | 2021-12-27 13:38:01 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # ============LICENSE_START=============================================== |
| 4 | # Copyright (C) 2021 Nordix Foundation. All rights reserved. |
| 5 | # ======================================================================== |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END================================================= |
| 18 | # |
| 19 | |
| 20 | # This is a script that contains function to handle helm on localhost |
| 21 | |
| 22 | |
| 23 | ################ Test engine functions ################ |
| 24 | |
| 25 | # Create the image var used during the test |
| 26 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 27 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 28 | __LOCALHELM_imagesetup() { |
| 29 | : |
| 30 | } |
| 31 | |
| 32 | # Pull image from remote repo or use locally built image |
| 33 | # arg: <pull-policy-override> <pull-policy-original> |
| 34 | # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images |
| 35 | # <pull-policy-original> Shall be used for images that does not allow overriding |
| 36 | # Both var may contain: 'remote', 'remote-remove' or 'local' |
| 37 | __LOCALHELM_imagepull() { |
| 38 | : |
| 39 | } |
| 40 | |
| 41 | # Build image (only for simulator or interfaces stubs owned by the test environment) |
| 42 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 43 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 44 | __LOCALHELM_imagebuild() { |
| 45 | : |
| 46 | } |
| 47 | |
| 48 | # Generate a string for each included image using the app display name and a docker images format string |
| 49 | # If a custom image repo is used then also the source image from the local repo is listed |
| 50 | # arg: <docker-images-format-string> <file-to-append> |
| 51 | __LOCALHELM_image_data() { |
| 52 | : |
| 53 | } |
| 54 | |
| 55 | # Scale kubernetes resources to zero |
| 56 | # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action. |
| 57 | # This function is called for apps fully managed by the test script |
| 58 | __LOCALHELM_kube_scale_zero() { |
| 59 | : |
| 60 | } |
| 61 | |
| 62 | # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action. |
| 63 | # This function is called for prestarted apps not managed by the test script. |
| 64 | __LOCALHELM_kube_scale_zero_and_wait() { |
| 65 | : |
| 66 | } |
| 67 | |
| 68 | # Delete all kube resouces for the app |
| 69 | # This function is called for apps managed by the test script. |
| 70 | __LOCALHELM_kube_delete_all() { |
| 71 | : |
| 72 | } |
| 73 | |
| 74 | # Store docker logs |
| 75 | # This function is called for apps managed by the test script. |
| 76 | # args: <log-dir> <file-prexix> |
| 77 | __LOCALHELM_store_docker_logs() { |
| 78 | : |
| 79 | } |
| 80 | |
| 81 | # Initial setup of protocol, host and ports |
| 82 | # This function is called for apps managed by the test script. |
| 83 | # args: - |
| 84 | __LOCALHELM_initial_setup() { |
| 85 | : |
| 86 | } |
| 87 | |
| 88 | # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers |
| 89 | # For docker, the namespace shall be excluded |
| 90 | # This function is called for apps managed by the test script as well as for prestarted apps. |
| 91 | # args: - |
| 92 | __LOCALHELM_statisics_setup() { |
| 93 | : |
| 94 | } |
| 95 | |
| 96 | # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied |
| 97 | # args: - |
| 98 | __LOCALHELM_test_requirements() { |
| 99 | tmp=$(which helm) |
| 100 | if [ $? -ne 0 ]; then |
| 101 | echo $RED" Helm3 is required for running this test. Pls install helm3" |
| 102 | exit 1 |
| 103 | fi |
| 104 | tmp_version=$(helm version | grep 'v3') |
| 105 | if [ -z "$tmp_version" ]; then |
| 106 | echo $RED" Helm3 is required for running this test. Pls install helm3" |
| 107 | exit 1 |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | ####################################################### |
| 112 | |
| 113 | |
| 114 | # Create a dummy helmchart |
| 115 | # arg: <chart-name> |
| 116 | localhelm_create_test_chart() { |
| 117 | __log_conf_start $@ |
| 118 | if [ $# -ne 1 ]; then |
| 119 | __print_err "<path-to-chart-dir>" $@ |
| 120 | return 1 |
| 121 | fi |
| 122 | if [[ "$1" == *"/"* ]]; then |
| 123 | echo -e $RED"Chart name cannot contain '/'" |
| 124 | __log_conf_fail_general |
| 125 | return 1 |
| 126 | fi |
| 127 | helm create $TESTENV_TEMP_FILES/$1 | indent1 |
| 128 | if [ $? -ne 0 ]; then |
| 129 | __log_conf_fail_general |
| 130 | return 1 |
| 131 | fi |
| 132 | __log_conf_ok |
| 133 | return 0 |
| 134 | } |
| 135 | |
| 136 | # Package a created helmchart |
| 137 | # arg: <chart-name> |
| 138 | localhelm_package_test_chart() { |
| 139 | __log_conf_start $@ |
| 140 | if [ $# -ne 1 ]; then |
| 141 | __print_err "<path-to-chart-dir>" $@ |
| 142 | return 1 |
| 143 | fi |
| 144 | if [[ "$1" == *"/"* ]]; then |
| 145 | echo -e $RED"Chart name cannot contain '/'" |
| 146 | __log_conf_fail_general |
| 147 | return 1 |
| 148 | fi |
| 149 | helm package -d $TESTENV_TEMP_FILES $TESTENV_TEMP_FILES/$1 | indent1 |
| 150 | if [ $? -ne 0 ]; then |
| 151 | __log_conf_fail_general |
| 152 | return 1 |
| 153 | fi |
| 154 | __log_conf_ok |
| 155 | return 0 |
| 156 | } |
| 157 | |
| 158 | # Check if a release is installed |
| 159 | # arg: INSTALLED|NOTINSTALLED <release-name> <name-space> |
| 160 | localhelm_installed_chart_release() { |
| 161 | __log_test_start $@ |
| 162 | if [ $# -ne 3 ]; then |
| 163 | __print_err "INSTALLED|NOTINSTALLED <release-name> <name-space>" $@ |
| 164 | return 1 |
| 165 | fi |
| 166 | if [ $1 != "INSTALLED" ] && [ $1 != "NOTINSTALLED" ]; then |
| 167 | __print_err "INSTALLED|NOTINSTALLED <release-name> <name-space>" $@ |
| 168 | return 1 |
| 169 | fi |
| 170 | |
| 171 | filter="helm ls -n $3 --filter ^$2" |
| 172 | res=$($filter -q) |
| 173 | if [ $? -ne 0 ]; then |
| 174 | __log_test_fail_general "Failed to list helm releases" |
| 175 | return 1 |
| 176 | fi |
| 177 | if [ $1 == "INSTALLED" ]; then |
| 178 | if [ "$res" != $2 ]; then |
| 179 | echo -e "$RED Release $2 does not exists $ERED" |
| 180 | __log_test_fail_general |
| 181 | return 1 |
| 182 | fi |
| 183 | elif [ $1 == "NOTINSTALLED" ]; then |
| 184 | if [ "$res" == $2 ]; then |
| 185 | __log_test_fail_general "Release $2 exists" |
| 186 | return 1 |
| 187 | fi |
| 188 | fi |
| 189 | echo " Currently installed releases in namespace $3" |
| 190 | helm ls -n $3 | indent2 |
| 191 | __log_test_pass |
| 192 | return 0 |
| 193 | } |