summaryrefslogtreecommitdiffstats
path: root/kud/tests/plugin_fw_v2.sh
blob: d6254ac32e9cd81c946b1491502778284a4f8d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2020
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

set -o errexit
set -o nounset
set -o pipefail

source _common_test.sh
source _functions.sh
source _functions.sh

# TODO KUBECONFIG may be a list of paths
kubeconfig_path="${KUBECONFIG:-$HOME/.kube/config}"

clusters="${KUD_PLUGIN_FW_CLUSTERS:-$(cat <<EOF
[
  {
    "metadata": {
      "name": "edge01",
      "description": "description of edge01",
      "userData1": "edge01 user data 1",
      "userData2": "edge01 user data 2"
    },
    "file": "$kubeconfig_path"
  }
]
EOF
)}"

function cluster_names {
    echo $clusters | jq -e -r '.[].metadata.name'
}

function cluster_metadata {
    cat<<EOF | jq .
{
  "metadata": $(echo $clusters | jq -e -r --arg name "$1" '.[]|select(.metadata.name==$name)|.metadata')
}
EOF
}

function cluster_file {
    echo $clusters | jq -e -r --arg name "$1" '.[]|select(.metadata.name==$name)|.file'
}

ARGS=()
while [[ $# -gt 0 ]]; do
    arg="$1"

    case $arg in
        "--external" )
            service_host=$(kubectl cluster-info | grep "Kubernetes master" | \
                awk -F ":" '{print $2}' | awk -F "//" '{print $2}')
            shift
            ;;
        * )
            ARGS+=("$1")
            shift
            ;;
    esac
done
set -- "${ARGS[@]}" # restore positional parameters

service_host=${service_host:-"localhost"}

CSAR_DIR="/opt/csar"
csar_id="4bf66240-a0be-4ce2-aebd-a01df7725f16"

demo_folder=$test_folder/../demo

function populate_CSAR_compositevfw_helm {
    _checks_args "$1"
    pushd "${CSAR_DIR}/$1"
    print_msg "Create Helm Chart Archives for compositevfw"
    rm -f *.tar.gz
    tar -czf packetgen.tar.gz -C $demo_folder/composite-firewall packetgen
    tar -czf firewall.tar.gz -C $demo_folder/composite-firewall firewall
    tar -czf sink.tar.gz -C $demo_folder/composite-firewall sink
    tar -czf profile.tar.gz -C $demo_folder/composite-firewall manifest.yaml override_values.yaml
    popd
}

project="testvfw"
composite_app="compositevfw"
version="v1"
deployment_intent_group="vfw_deployment_intent_group"

function setup {
    install_deps
    populate_CSAR_compositevfw_helm "$csar_id"
    cat <<EOF >plugin_fw_v2_config.yaml
orchestrator:
  host: ${service_host}
  port: 30415
clm:
  host: ${service_host}
  port: 30461
ncm:
  host: ${service_host}
  port: 30431
ovnaction:
  host: ${service_host}
  port: 30471
dcm:
  host: ${service_host}
  port: 30477
gac:
  host: ${service_host}
  port: 30491
dtc:
 host: ${service_host}
 port: 30481
EOF
    cat <<EOF >plugin_fw_v2_values.yaml
ClusterProvider: vfw-cluster-provider
ClusterLabel: LabelA
Clusters:
EOF
    echo $clusters | jq -r '.[] | "- Name: \(.metadata.name)\n  KubeConfig: \(.file)"' >>plugin_fw_v2_values.yaml
    cat <<EOF >>plugin_fw_v2_values.yaml
EmcoProviderNetwork: emco-private-net
UnprotectedProviderNetwork: unprotected-private-net
ProtectedNetwork: protected-private-net
Project: ${project}
LogicalCloud: lcadmin
CompositeApp: ${composite_app}
Version: ${version}
PackagesPath: ${CSAR_DIR}/${csar_id}
CompositeProfile: vfw_composite-profile
DeploymentIntentGroup: ${deployment_intent_group}
Release: fw0
DeploymentIntentsInGroup: vfw_deploy_intents
GenericPlacementIntent: generic-placement-intent
OvnActionIntent: vfw_ovnaction_intent
EOF
}

function call_emcoctl {
    rc=$1
    shift
    # retry due to known issue with emcoctl and instantiating/terminating multiple resources
    try=0
    until [[ $(emcoctl $@ | awk '/Response Code:/ {code=$3} END{print code}') =~ $rc ]]; do
        if [[ $try -lt 10 ]]; then
            sleep 1s
        else
            return 1
        fi
        try=$((try + 1))
    done
    return 0
}

function createData {
    call_emcoctl 2.. --config plugin_fw_v2_config.yaml apply -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
}

function getData {
    emcoctl --config plugin_fw_v2_config.yaml get -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
}

function deleteData {
    call_emcoctl 4.. --config plugin_fw_v2_config.yaml delete -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
}

function statusVfw {
    emcoctl --config plugin_fw_v2_config.yaml get projects/${project}/composite-apps/${composite_app}/${version}/deployment-intent-groups/${deployment_intent_group}/status
}

function waitForVfw {
    for try in {0..59}; do
        sleep 1
        new_phase="$(emcoctl --config plugin_fw_v2_config.yaml get projects/${project}/composite-apps/${composite_app}/${version}/deployment-intent-groups/${deployment_intent_group}/status | awk '/Response: / {print $2}' | jq -r .status)"
        echo "$(date +%H:%M:%S) - Filter=[$*] : $new_phase"
        if [[ "$new_phase" == "$1" ]]; then
            return 0
        fi
    done
}

function usage {
    echo "Usage: $0 setup|create|get|destroy|status"
    echo "    setup - creates the emcoctl files and packages needed for vfw"
    echo "    create - creates all ncm, ovnaction, clm resources needed for vfw"
    echo "    get - queries all resources in ncm, ovnaction, clm resources created for vfw"
    echo "    destroy - deletes all resources in ncm, ovnaction, clm resources created for vfw"
    echo "    status - get status of deployed resources"
    echo ""
    echo "    a reasonable test sequence:"
    echo "    1.  setup"
    echo "    2.  create"
    echo "    3.  destroy"

    exit
}

if [[ "$#" -gt 0 ]] ; then
    case "$1" in
        "setup" ) setup ;;
        "create" ) createData ;;
        "get" ) getData ;;
        "status" ) statusVfw ;;
        "wait" ) waitForVfw "Instantiated" ;;
        "delete" ) deleteData ;;
        *) usage ;;
    esac
else
    setup
    createData

    print_msg "[BEGIN] Basic checks for instantiated resource"
    print_msg "Wait for deployment to be instantiated"
    waitForVfw "Instantiated"
    for name in $(cluster_names); do
        print_msg "Check that networks were created on cluster $name"
        file=$(cluster_file "$name")
        KUBECONFIG=$file kubectl get network protected-private-net -o name
        KUBECONFIG=$file kubectl get providernetwork emco-private-net -o name
        KUBECONFIG=$file kubectl get providernetwork unprotected-private-net -o name
    done
    for name in $(cluster_names); do
        print_msg "Wait for all pods to start on cluster $name"
        file=$(cluster_file "$name")
        KUBECONFIG=$file kubectl wait pod -l release=fw0 --for=condition=Ready
    done
    # TODO: Provide some health check to verify vFW work
    print_msg "Not waiting for vFW to fully install as no further checks are implemented in testcase"
    #print_msg "Waiting 8minutes for vFW installation"
    #sleep 8m
    print_msg "[END] Basic checks for instantiated resource"

    print_msg "Delete deployment"
    deleteData
fi