test.py 11.2 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2018 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import os
29
import unittest
30 31 32 33
import paramiko
import contextlib
import base64
import hashlib
34
import subprocess
35

36 37
from six.moves.urllib.parse import urlparse
from six.moves.urllib.parse import quote
38
from six.moves.urllib.parse import urljoin
39
from six.moves.configparser import ConfigParser
40
import requests
41
import six
42

43
from slapos.recipe.librecipe import generateHashFromFiles
44
from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass
45
from slapos.util import bytes2str
46

47 48
setUpModule, SlapOSInstanceTestCase = makeModuleSetUpAndTestCaseClass(
    os.path.abspath(
49 50
        os.path.join(os.path.dirname(__file__), '..',
                     'software%s.cfg' % ("-py3" if six.PY3 else ""))))
51 52


53 54 55
class SlaprunnerTestCase(SlapOSInstanceTestCase):
  # Slaprunner uses unix sockets, so it needs short paths.
  __partition_reference__ = 's'
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
class TestWeb(SlaprunnerTestCase):
  def test_slaprunner(self):
    # slaprunner main interface is password protected
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    url = parameter_dict['url']
    resp = requests.get(url, verify=False)
    self.assertEqual(requests.codes.unauthorized, resp.status_code)
    resp = requests.get(
        url,
        verify=False,
        auth=(parameter_dict['init-user'], parameter_dict['init-password']))
    self.assertEqual(requests.codes.ok, resp.status_code)
    self.assertIn('SlapOS', resp.text)

  def test_shellinabox(self):
    # shellinabox exists at /shellinabox and is password protected
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    url = urljoin(parameter_dict['url'], '/shellinabox')
    resp = requests.get(url, verify=False)
    self.assertEqual(requests.codes.unauthorized, resp.status_code)
    resp = requests.get(
        url,
        verify=False,
        auth=(parameter_dict['init-user'], parameter_dict['init-password']))
    self.assertEqual(requests.codes.ok, resp.status_code)
    self.assertIn('ShellInABox', resp.text)
    self.assertNotIn('SlapOS', resp.text)

  def test_public_url(self):
    # ~/srv/runner/public/ is served over http
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    public_url = parameter_dict['public-url']

    hello_file = os.path.join(
        self.computer_partition_root_path,
        'srv',
        'runner',
        'public',
        'hello.html')
    self.addCleanup(os.remove, hello_file)
    with open(hello_file, 'w') as f:
      f.write('<b>Hello</b>')

    index = requests.get(public_url, verify=False)
    self.assertEqual(requests.codes.ok, index.status_code)
    self.assertIn('hello.html', index.text)

    hello = requests.get(urljoin(public_url, 'hello.html'), verify=False)
    self.assertEqual(requests.codes.ok, hello.status_code)
    self.assertIn('<b>Hello</b>', hello.text)

  # git seems broken, these are 404 now...
  @unittest.expectedFailure
  def test_git_private(self):
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    url = parameter_dict['git-private']
    resp = requests.get(url, verify=False)
    self.assertEqual(requests.codes.unauthorized, resp.status_code)
    resp = requests.get(
        url,
        verify=False,
        auth=(parameter_dict['init-user'], parameter_dict['init-password']))
    self.assertEqual(requests.codes.ok, resp.status_code)

  @unittest.expectedFailure
  def test_git_public(self):
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    url = parameter_dict['git-public']
    resp = requests.get(url, verify=False)
    self.assertEqual(requests.codes.ok, resp.status_code)


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
class TestSSH(SlaprunnerTestCase):
  @classmethod
  def getInstanceParameterDict(cls):
    cls.ssh_key = paramiko.RSAKey.generate(1024)
    return {
        'user-authorized-key': 'ssh-rsa {}'.format(cls.ssh_key.get_base64())
    }

  def test_connect(self):
    parameter_dict = self.computer_partition.getConnectionParameterDict()
    ssh_url = parameter_dict['ssh-url']
    parsed = urlparse(ssh_url)
    self.assertEqual('ssh', parsed.scheme)

    # username contain a fingerprint (only, so we simplify the parsing)
    #
    # relevant parts of the grammar defined in
    # https://tools.ietf.org/id/draft-salowey-secsh-uri-00.html
    #
    #   ssh-info      =  [ userinfo ] [";" c-param *("," c-param)]
    #   c-param       =  paramname "=" paramvalue
    ssh_info = parsed.username
    username, fingerprint_from_url = ssh_info.split(';fingerprint=')
    client = paramiko.SSHClient()

    self.assertTrue(fingerprint_from_url.startswith('ssh-rsa-'), '')
    fingerprint_from_url = fingerprint_from_url[len('ssh-rsa-'):]

    class KeyPolicy(object):
      """Accept server key and keep it in self.key for inspection
      """
      def missing_host_key(self, client, hostname, key):
        self.key = key

    key_policy = KeyPolicy()
    client.set_missing_host_key_policy(key_policy)

    with contextlib.closing(client):
      client.connect(
          username=username,
          hostname=parsed.hostname,
          port=parsed.port,
          pkey=self.ssh_key,
      )
      # Check fingerprint from server matches the published one.
      # Paramiko does not allow to get the fingerprint as SHA256 easily yet
      # https://github.com/paramiko/paramiko/pull/1103
      self.assertEqual(
          fingerprint_from_url,
          quote(
              # base64 encoded fingerprint adds an extra = at the end
              base64.b64encode(
                  hashlib.sha256(key_policy.key.asbytes()).digest())[:-1],
              # also encode /
              safe=''))

      # Check shell is usable
      channel = client.invoke_shell()
      channel.settimeout(30)
      received = ''
      while True:
191
        r = bytes2str(channel.recv(1024))
192 193 194 195 196 197 198 199 200 201 202
        self.logger.debug("received >%s<", r)
        if not r:
          break
        received += r
        if 'slaprunner shell' in received:
          break
      self.assertIn("Welcome to SlapOS slaprunner shell", received)

      # simple commands can also be executed ( this would be like `ssh bash -c 'pwd'` )
      self.assertEqual(
          self.computer_partition_root_path,
203
          bytes2str(client.exec_command("pwd")[1].read(1000)).strip())
204 205


206 207 208 209 210 211 212 213 214 215 216
class TestSlapOS(SlaprunnerTestCase):
  def test_slapos_command(self):
    # in ~/bin/slapos there is a wrapper setting configuration to use slapos from
    # the web runner.
    proxy_show_output = subprocess.check_output(
        (
            os.path.join(self.computer_partition_root_path, 'bin', 'slapos'),
            'proxy',
            'show',
        ),
        env={})
217
    self.assertIn(b'slaprunner', proxy_show_output)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

  def test_shared_part_list(self):
    # this slapos used shared_part_list
    cfg_parser = ConfigParser()
    with open(os.path.join(self.computer_partition_root_path,
                           'etc',
                           'slapos.cfg')) as f:
      cfg_parser.readfp(f)
    shared_part_list = cfg_parser.get('slapos', 'shared_part_list').splitlines()

    # web runner own shared parts. Note that there is intentionnaly a double
    # slash in this path, because slaprunner has double slash in paths since
    # early releases, including for the path of slapos repository that will be
    # used to develop and install software. If we fix this duplication, then
    # the URL of installed software will be different and it will get a different
    # hash and be reinstalled. To prevent this, we keep that // between srv and runner.
    self.assertEqual(
        '{}/srv//runner//shared'.format(self.computer_partition_root_path.rstrip('/')),
        shared_part_list[-1])

    # shared parts from outer slapos
    outer_shared_part_list = os.getenv('SLAPOS_TEST_SHARED_PART_LIST',
                                       '').split(os.pathsep)
    for outer_shared_part in outer_shared_part_list:
      self.assertIn(outer_shared_part, shared_part_list)


245
class ServicesTestCase(SlaprunnerTestCase):
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  def test_hashes(self):
    hash_files = [
      'software_release/buildout.cfg',
    ]
    expected_process_names = [
      'slaprunner-supervisord-{hash}-on-watch',
      'runner-sshd-{hash}-on-watch',
      'slaprunner-httpd-{hash}-on-watch',
      'gunicorn-{hash}-on-watch',
      'nginx-frontend-{hash}-on-watch',
      'certificate_authority-{hash}-on-watch',
      'shellinaboxd-{hash}-on-watch',
      'supervisord-{hash}-on-watch',
    ]

261 262 263 264
    with self.slap.instance_supervisor_rpc as supervisor:
      process_names = [
          process['name'] for process in supervisor.getAllProcessInfo()
      ]
265 266 267 268 269

    hash_files = [os.path.join(self.computer_partition_root_path, path)
                  for path in hash_files]

    for name in expected_process_names:
270
      h = generateHashFromFiles(hash_files)
271 272 273
      expected_process_name = name.format(hash=h)

      self.assertIn(expected_process_name, process_names)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301


class TestInstanceResilient(SlaprunnerTestCase):
  instance_max_retry = 20
  @classmethod
  def getInstanceSoftwareType(cls):
    return 'resilient'

  def test(self):
    # just check that keys returned on requested partition are for resilient
    self.assertSetEqual(
      set(self.computer_partition.getConnectionParameterDict().keys()),
      set([
        'backend-url',
        'feed-url-runner-1-pull',
        'feed-url-runner-1-push',
        'git-private-url',
        'git-public-url',
        'init-password',
        'init-user',
        'monitor-base-url',
        'monitor-setup-url',
        'public-url',
        'ssh-command',
        'takeover-runner-1-password',
        'takeover-runner-1-url',
        'url',
        'webdav-url']))
302 303 304 305 306 307 308 309 310 311 312 313


class TestCustomFrontend(SlaprunnerTestCase):
  @classmethod
  def getInstanceParameterDict(cls):
    return {
      'custom-frontend-backend-url': 'https://www.erp5.com',
      'custom-frontend-backend-type': 'redirect',
    }

  def test(self):
    parameter_dict = self.computer_partition.getConnectionParameterDict()
Thomas Gambier's avatar
Thomas Gambier committed
314
    # slapproxy returns the backend URL when requesting a slave frontend
315 316 317
    self.assertEqual(
      parameter_dict['custom-frontend-url'],
      'https://www.erp5.com')