4c944b22cb4a3f597645a4eed4586c08e9b2cd92
[functest.git] / docker / prepare_env.sh
1 #!/bin/bash
2
3 #
4 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
5 #
6 # Installs the Functest framework within the Docker container
7 # and run the tests automatically
8 #
9
10 usage="Script to prepare the Functest environment.
11
12 usage:
13     bash $(basename "$0") [--offline] [-h|--help] [-t <test_name>]
14
15 where:
16     -o|--offline      optional offline mode (experimental)
17     -h|--help         show this help text
18
19 examples:
20     $(basename "$0")
21     $(basename "$0") --offline"
22
23 offline=false
24
25 # Parse parameters
26 while [[ $# > 0 ]]
27     do
28     key="$1"
29     case $key in
30         -h|--help)
31             echo "$usage"
32             exit 0
33             shift
34         ;;
35         -o|--offline)
36             offline=true
37         ;;
38         *)
39             error "unknown option $1"
40             exit 1
41         ;;
42     esac
43     shift # past argument or value
44 done
45
46 BASEDIR=`dirname $0`
47 source ${BASEDIR}/common.sh
48
49 # Support for Functest offline
50 # NOTE: Still not 100% working when running the tests
51
52 info "######### Preparing Functest environment #########"
53 if [ $offline == false ]; then
54     info "MODE: online"
55 else
56     info "MODE: offline"
57 fi
58
59
60 # Check if environment variables are set
61 info "Checking environment variables INSTALLER_TYPE and INSTALLER_IP"
62 if [ -z ${INSTALLER_TYPE} ]; then
63     error "Environment variable 'INSTALLER_TYPE' is not defined."
64 elif [[ ${INSTALLER_TYPE} =~ ^(fuel|foreman|compass|apex|juju)$ ]]; then
65     info "INSTALLER_TYPE env variable found: ${INSTALLER_TYPE}"
66 else
67     error "Invalid environment variable INSTALLER_TYPE=${INSTALLER_TYPE}"
68 fi
69
70 if [ -z ${INSTALLER_IP} ]; then
71     error "Environment variable 'INSTALLER_IP' is not defined."
72 fi
73 info "INSTALLER_IP env variable found: ${INSTALLER_IP}"
74
75
76 if [ $offline == false ]; then
77     # Update repos
78     info "Updating Functest repository...."
79     cd ${FUNCTEST_REPO_DIR}
80     if [ ${FUNCTEST_BRANCH} != "master" ]; then
81         info "Functest repo: checkout ${FUNCTEST_BRANCH} branch..."
82         git checkout ${FUNCTEST_BRANCH}
83     fi
84     info "Functest repo: pulling to latest..."
85     git pull
86     if [ ${FUNCTEST_COMMIT} != "latest" ]; then
87         info "Functest repo: given commit is ${FUNCTEST_COMMIT}. Reseting..."
88         git reset --hard ${FUNCTEST_COMMIT}
89     fi
90
91     info "Updating Releng repository...."
92     cd ${RELENG_REPO_DIR}
93     if [ ${RELENG_BRANCH} != "master" ]; then
94         info "Releng repo: checkout ${RELENG_BRANCH} branch..."
95         git checkout ${RELENG_BRANCH}
96     fi
97     info "Releng repo: pulling to latest..."
98     git pull
99     if [ ${RELENG_COMMIT} != "latest" ]; then
100         info "Releng repo: given commit is ${RELENG_COMMIT}. Reseting..."
101         git reset --hard ${RELENG_COMMIT}
102     fi
103
104     info "Updating Rally repository...."
105     cd ${RALLY_REPO_DIR}
106     if [ ${RALLY_BRANCH} != "master" ]; then
107         info "Rally repo: checkout ${RALLY_BRANCH} branch..."
108         git checkout ${RALLY_BRANCH}
109     fi
110     info "Rally repo: pulling to latest..."
111     git pull
112     # We leave the reset command for later.
113
114     info "Updating vIMS test repository...."
115     cd ${VIMS_REPO_DIR}
116     if [ ${VIMS_BRANCH} != "stable" ]; then
117         info "Releng repo: checkout ${VIMS_TEST_BRANCH} branch..."
118         git checkout ${VIMS_BRANCH}
119     fi
120     info "vIMS test repo: pulling to latest..."
121     git pull
122     if [ ${VIMS_COMMIT} != "latest" ]; then
123         info "vIMS test repo: given commit is ${VIMS_TEST_COMMIT}. Reseting..."
124         git reset --hard ${VIMS_COMMIT}
125     fi
126
127 fi
128
129 # We do this regardless if its online or offline mode.
130 # Assumption: the docker image contains a newer rally repo than the given commit.
131 if [ ${RALLY_COMMIT} != "latest" ]; then
132     cd ${RALLY_REPO_DIR}
133     info "Rally repo: given commit is ${RALLY_COMMIT}. Reseting..."
134     git reset --hard ${RALLY_COMMIT}
135 fi
136
137 # Ugly hack:
138 # After the 'git functest pull', we move the given yaml file to the repo directory,
139 # since some of the scripts will use that one, and not the one in
140 # /home/opnfv/functest/conf/
141 given_config_file=/home/opnfv/functest/conf/config_functest.yaml
142 default_config_file=$(find /home/opnfv/repos -name config_functest.yaml)
143 if [ -f ${given_config_file} ]; then
144     info "Copying given config_functest.yaml to the repository directory"
145     cp ${given_config_file} ${default_config_file}
146 else
147     info "config_functest.yaml not provided. Using default one: ${default_config_file}"
148 fi
149
150
151 # Create directories
152 mkdir -p ${FUNCTEST_CONF_DIR}
153 mkdir -p ${FUNCTEST_DATA_DIR}
154 mkdir -p ${FUNCTEST_RESULTS_DIR}/ODL
155
156
157
158 # Create Openstack credentials file
159 if [ ! -f ${FUNCTEST_CONF_DIR}/openstack.creds ]; then
160     ${REPOS_DIR}/releng/utils/fetch_os_creds.sh -d ${FUNCTEST_CONF_DIR}/openstack.creds \
161         -i ${INSTALLER_TYPE} -a ${INSTALLER_IP}
162     retval=$?
163     if [ $retval != 0 ]; then
164         error "Cannot retrieve credentials file from installation. Check logs."
165         exit $retval
166     fi
167 else
168     info "OpenStack credentials file given to the docker and stored in ${FUNCTEST_CONF_DIR}/openstack.creds."
169 fi
170 # Source credentials
171 source ${FUNCTEST_CONF_DIR}/openstack.creds
172
173
174 # Prepare Functest Environment
175 info "Functest: prepare Functest environment"
176 python ${FUNCTEST_REPO_DIR}/testcases/config_functest.py --debug ${FUNCTEST_REPO_DIR}/ start
177 retval=$?
178 if [ $retval != 0 ]; then
179     error "Error when configuring Functest environment"
180     exit $retval
181 fi
182
183 echo "1" > ${FUNCTEST_CONF_DIR}/env_active