summaryrefslogtreecommitdiffstats
path: root/tests/adhoc/smoke01.sh
blob: 207cec3ccd7423f75b0893c7dd87f532ddf5410e (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
#!/bin/bash
# Copyright 2015-2016 AT&T Intellectual Property, Inc
#  
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#  
# http://www.apache.org/licenses/LICENSE-2.0
#  
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# What this is: A basic test to validate an OPNFV install. Creates an image,
# test_public and private networks, a router, and launches two VMs connected to the 
# private network and thru the router, to the internet.
#
# Status: this is a work in progress, under test. Automated ping test to the 
# internet and between VMs has not yet been implemented.
#
# Prerequisites:
#   OpenStack installed.
#   Environment setting script (e.g. admin-openrc.sh) available.
#   OpenStack clients installed e.g. via setup_osc.sh in the OPNFV Models repo.
#
# How to use:
#   Install Congress test server per https://wiki.opnfv.org/copper/academy
#   $ bash ~/git/copper/tests/adhoc/smoke01.sh <openrc>
#     <openrc>: path to your openrc script
#   After test, cleanup with
#   $ bash ~/git/copper/tests/adhoc/smoke01-clean.sh
#
# Status: this is a work in progress, under test. 

trap 'fail' ERR

pass() {
  log "Hooray!"
  set +x #echo off
  exit 0
}

# Use this to trigger fail() at the right places
# if [ "$RESULT" == "Test Failed!" ]; then fail; fi
fail() {
  log "$1"
  set +x
  exit 1
}

function log() {
  f=$(caller 0 | awk '{print $2}')
  l=$(caller 0 | awk '{print $1}')
  echo; echo "$f:$l ($(date)) $1"
}

unclean() {
  fail "Unclean environment!"
}

# Find external network if any, and details
function get_external_net () {
  networks=($(openstack network list | grep -v "+" | grep -v ID | awk '{print $4}'))
  for name in ${networks[@]}; do
    log "Checking network \"$name\""
    external=$(openstack network show ${name} | grep 'router:external' | grep -c "External")
    if [[ $external -eq 1 ]]; then 
      log "Found external network \"$name\"" 
      EXTERNAL_NETWORK_NAME=$name
      EXTERNAL_SUBNET_ID=$(openstack network show $name | awk "/ subnets / { print \$4 }")
      break
    fi
  done
  if [[ ! $EXTERNAL_NETWORK_NAME ]]; then 
    fail "External network not found"
  fi
}

function wait_active() {
  log "Wait for $1 to go ACTIVE"
  COUNTER=10
  status=""
  until [[ $COUNTER -eq 0  || "$status" == "ACTIVE" ]]; do
    status=$(openstack server show $1 | awk "/ status / { print \$4 }")
    COUNTER=$((COUNTER-1))
    sleep 5
  done
  if [[ "$status" != "ACTIVE" ]]; then 
    fail "Timeout on $1 becoming active"
  fi
}

function smoke01() {
  source $1

  if [[ -z $(openstack image list | awk "/ cirros-0.3.3-x86_64 / { print \$2 }") ]]; then
    log "Create cirros-0.3.3-x86_64 image"
    wget http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img \
      -O ~/cirros-0.3.3-x86_64-disk.img
    glance image-create --name cirros-0.3.3-x86_64 --disk-format qcow2 \
      --container-format bare
    image_id=$(openstack image list | awk "/ cirros-0.3.3-x86_64 / { print \$2 }")
    glance image-upload --file ~/cirros-0.3.3-x86_64-disk.img $image_id
  fi

  log "Find name and subnet of external network"
  get_external_net

  log "Create floating IP for external subnet"
  FLOATING_IP_ID=$(openstack floating ip create $EXTERNAL_NETWORK_NAME | awk "/ id / { print \$4 }")
  FLOATING_IP=$(openstack floating ip show $FLOATING_IP_ID | awk "/ floating_ip_address / { print \$4 }" | cut -d - -f 1)
  # Save ID to pass to cleanup script
  echo "FLOATING_IP_ID=$FLOATING_IP_ID" >/tmp/SMOKE01_VARS.sh

  if [[ -z $(openstack network list | awk "/ internal / { print \$2 }") ]]; then 
    log "Create internal network"
    openstack network create internal

    log "Create internal subnet"
    openstack subnet create --network internal --subnet-range 10.0.0.0/24 \
      --gateway 10.0.0.1 --dhcp --allocation-pool start=10.0.0.2,end=10.0.0.254 \
      --dns-nameserver 8.8.8.8 internal
  fi

  log "Get ID of internal network"
  internal_net=$(openstack network show internal | awk '/ id / {print $4}')

  if [[ -z $(openstack router list | awk "/ public_router / { print \$2 }") ]]; then 
    log "Create router"
    openstack router create public_router

    log "Create router gateway"
    openstack router set --external-gateway $EXTERNAL_NETWORK_NAME public_router

    log "Add router interface for internal network"
    openstack port create --network internal \
      --fixed-ip subnet=internal,ip-address=10.0.0.1 \
      public_router_internal_port
    openstack router add port public_router public_router_internal_port
  fi

  log "Create smoke01 security group"
  openstack security group create smoke01

  log "Add rule to smoke01 security group"
  openstack security group rule create --ingress --protocol=TCP \
    --remote-ip 0.0.0.0/0 --dst-port 22:22 smoke01
  openstack security group rule create --ingress --protocol=ICMP \
    --remote-ip 0.0.0.0/0 smoke01
  openstack security group rule create --egress --protocol=TCP \
    --remote-ip 0.0.0.0/0 --dst-port 22:22 smoke01
  openstack security group rule create --egress --protocol=ICMP \
    --remote-ip 0.0.0.0/0 smoke01

  log "Create Nova key pair"
  if [[ -f /tmp/smoke01 ]]; then rm /tmp/smoke01; fi
  ssh-keygen -t rsa -N "" -f /tmp/smoke01 -C smokem@ifyagotem
  chmod 600 /tmp/smoke01
  openstack keypair create --public-key /tmp/smoke01.pub smoke01

  log "Create Nova flavor"
  openstack flavor create --ram 512 --disk 1 --vcpus 1 smoke01.tiny

  log "Boot cirros1"
  openstack server create --config-drive True --flavor smoke01.tiny --image cirros-0.3.3-x86_64 --nic net-id=$internal_net --security-group smoke01 --key-name smoke01 cirros1
  wait_active cirros1
  # metadata is accessible by logging into cirros1 after floating IP assignment
  # ssh -i /tmp/smoke01 -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no cirros@$FLOATING_IP
  # from the local metadata service, via: curl http://169.254.169.254/latest/meta-data
  # from the config drive, via
  #    sudo mount /dev/sr0 /mnt/
  #    find /mnt/openstack/latest -name *.json -exec grep -H { {} + | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'

  log "Associate floating IP to cirros1"
  openstack server add floating ip cirros1 $FLOATING_IP

  log "Boot cirros2"
  openstack server create --config-drive True --flavor smoke01.tiny --image cirros-0.3.3-x86_64 --nic net-id=$internal_net --security-group smoke01 --key-name smoke01 cirros2
  wait_active cirros2

  log "Verify public network connectivity"
  RESULT=$(ssh -i /tmp/smoke01 -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no cirros@$FLOATING_IP "ping -c 3 8.8.8.8; exit" | awk "/ 0% packet loss/ { print \$1 }")
  if [[ "$RESULT" != "3" ]]; then
    fail "Could not verify inter-VM pings"
  fi
}

smoke01 $1
pass