github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/mediawiki/tests/100-deploy (about) 1 #!/usr/bin/env python3 2 import amulet 3 import requests 4 import time 5 6 seconds = 900 7 8 d = amulet.Deployment(series='trusty') 9 10 #Setup the charms, and relationships 11 d.add('mysql') 12 d.add('memcached') 13 d.add('mediawiki') 14 d.configure('mediawiki', {'name': 'amulet-wiki'}) 15 d.configure('memcached', {'allow-ufw-ip6-softfail': True}) 16 d.relate('mysql:db', 'mediawiki:db') 17 d.relate('memcached:cache', 'mediawiki:cache') 18 d.expose('mediawiki') 19 20 21 # Perform the setup for the deployment. 22 try: 23 d.setup(seconds) 24 #pings every deployed unit 25 d.sentry.wait(seconds) 26 except amulet.helpers.TimeoutError: 27 message = 'The environment did not setup in %d seconds.' % seconds 28 amulet.raise_status(amulet.SKIP, msg=message) 29 except: 30 raise 31 32 mw_unit = d.sentry['mediawiki'][0] 33 mysql_unit = d.sentry['mysql'][0] 34 memcached_unit = d.sentry['memcached'][0] 35 36 ############################################################# 37 # Validate the DB Relationship using Amulet Sentry 38 ############################################################# 39 sql_relation = mysql_unit.relation('db', 'mediawiki:db') 40 41 # Validate that the database server was set for the configuration of MediaWiki 42 #Set search term for comparison, and cache the flag in the configuration file 43 output, code = mw_unit.run("cat /etc/mediawiki/LocalSettings.php \ 44 | grep wgDBserver | awk '{printf $3}'") 45 46 search_term = "\"{}\";".format(sql_relation['private-address']) 47 if search_term != output: 48 message = "Unable to Determine Remote MYSQL configuration, " \ 49 "expected: %s, got: %s" % (search_term, output) 50 51 amulet.raise_status(amulet.FAIL, msg=message) 52 53 ############################################################# 54 # Validate the Memcached Relationship using Amulet Sentry 55 ############################################################# 56 memcached_relation = memcached_unit.relation('cache', 'mediawiki:cache') 57 output, code = mw_unit.run("cat /etc/mediawiki/memcached_settings.php \ 58 | grep wgMemCachedServers | tr -d \'array\(\)\; | awk '{printf $3}'") 59 60 search_term = "%s:%s" % (memcached_relation['private-address'], 61 memcached_relation['port']) 62 63 ############################################################# 64 # Validate the installation configuration using Requests 65 ############################################################# 66 mw_ip = mw_unit.info['public-address'] 67 mw_url = "http://%s/mediawiki/index.php" % mw_ip 68 response = requests.get(mw_url) 69 70 if response.content.find(b"<title>amulet-wiki") == -1: 71 amulet.raise_status(amulet.FAIL, 72 "Unable to validate configuration for wiki-name") 73 74 # Add an admin (now that we validated a good db connection) 75 d.configure('mediawiki', {'admins': 'tom:swordfish'}) 76 # Give the config-changed hook 30s to settle 77 time.sleep(30) 78 login_url = "http://%s/mediawiki/api.php?action=login&lgname=tom&lgpassword=swordfish&format=json" % mw_ip 79 80 # Test that we can login with the newly created admin 81 with requests.Session() as s: 82 # hit the login url with credentials, retrieve a token to use for later validation 83 resp = s.post(login_url) 84 token = resp.json()['login']['token'] 85 86 # hit the login url again with creds+token to verify if we are successfully logged in 87 resp2 = s.post(login_url+'&lgtoken=%s' % token, cookies=resp.cookies) 88 result = resp2.json()['login']['result'] 89 if result != "Success": 90 amulet.raise_status(amulet.FAIL, "Unable to validate admin login: %s" % result)