~canonical-platform-qa/snappy-ecosystem-tests/fixing_ci

« back to all changes in this revision

Viewing changes to tests/utils/snapcraft.py

  • Committer: iahmad
  • Date: 2017-02-03 07:02:58 UTC
  • Revision ID: iftikhar.ahmad@canonical.com-20170203070258-g46zkumbqhzp527t
snapcraft wrapper, login and logout tests using snapcraft and webinterface, baseclass for selenium based web tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import pexpect
 
2
import sys
 
3
import os
 
4
import storeconfig
 
5
 
 
6
#login credentials exported by shell environment
 
7
LOGIN_EMAIL, LOGIN_PASSWORD = storeconfig.get_store_credentials()
 
8
 
 
9
 
 
10
def set_snapcraft_environment():
 
11
    """set the environment variables for snapcraft communication with store,
 
12
    this will be staging by default. if STAGING environment variable is
 
13
    set to 0, then it will be set to production"""
 
14
    if storeconfig.is_staging():
 
15
        os.environ["UBUNTU_STORE_SEARCH_ROOT_URL"] = 'https://search.apps.staging.ubuntu.com/'
 
16
        os.environ["UBUNTU_STORE_UPLOAD_ROOT_URL"] = 'https://upload.apps.staging.ubuntu.com/'
 
17
        os.environ["UBUNTU_SSO_API_ROOT_URL"] = 'https://login.staging.ubuntu.com/api/v2/'
 
18
        os.environ["UBUNTU_STORE_API_ROOT_URL"] = 'https://myapps.developer.staging.ubuntu.com/dev/api/'
 
19
    else:
 
20
        os.environ["UBUNTU_STORE_SEARCH_ROOT_URL"] = 'https://search.apps.ubuntu.com/'
 
21
        os.environ["UBUNTU_STORE_UPLOAD_ROOT_URL"] = 'https://upload.apps.ubuntu.com/'
 
22
        os.environ["UBUNTU_SSO_API_ROOT_URL"] = 'https://login.ubuntu.com/api/v2/'
 
23
        os.environ["UBUNTU_STORE_API_ROOT_URL"] = 'https://myapps.developer.ubuntu.com/dev/api/'
 
24
 
 
25
 
 
26
class Snapcraft(object):
 
27
    def __init__(self):
 
28
        """Create new snapcraft instance."""
 
29
        self._login = False
 
30
        set_snapcraft_environment()
 
31
        self._cleanup()
 
32
 
 
33
    def _cleanup(self):
 
34
        self.logout()
 
35
 
 
36
    def logout(self):
 
37
        """logout of snapcraft store session"""
 
38
        child = pexpect.spawn("snapcraft logout")
 
39
        err = child.expect('Credentials cleared.')
 
40
        child.terminate(True)
 
41
        child.wait()
 
42
        if err is not 0:
 
43
            raise ValueError("Failed to logout")
 
44
 
 
45
        self._login = False
 
46
 
 
47
    def login(self):
 
48
        """login to store using the credential environment variables"""
 
49
        child = pexpect.spawn("snapcraft login")
 
50
        child.expect('Email: ')
 
51
        child.sendline(LOGIN_EMAIL)
 
52
        child.expect('Password:')
 
53
        child.sendline(LOGIN_PASSWORD)
 
54
        err = child.expect('Login successful')
 
55
        if err is not 0:
 
56
            raise ValueError("Failed to login")
 
57
 
 
58
        self._login = True
 
59
 
 
60
    def listRegistered(self):
 
61
        """call snapcraft list-registered command, raise exception if not logged in"""
 
62
        if self._login is False:
 
63
            raise ValueError("User is not logged in, please login before using this command")
 
64
        return pexpect.spawnu("snapcraft list-registered").read()
 
65
 
 
66