Fixes for podman client (client tunnel discovery, client creation and tests)

We have to pass ssh_client object to tobiko/podman/_shell.py/discover_podman_socket
otherwise that methid has no way to get the actual socket url:

In [27]: import six
    ...:
    ...: import tobiko
    ...: from tobiko.tripleo import overcloud
    ...: from tobiko.shell import sh
    ...:
    ...:

In [28]: ssh_client = overcloud.overcloud_ssh_client('controller-0')

In [30]:     cmd = "systemctl list-sockets | grep podman | awk '{print $1}'"
    ...:

In [31]:     result = sh.execute(cmd, stdin=False, stdout=True, stderr=True,expect_exit_status=None)
    ...:
    ...:

In [32]: result
Out[32]: <tobiko.shell.sh._execute.ShellExecuteResult at 0x7f3a232f2b70>

In [33]: result.stdout
Out[33]: '0 sockets listed.\nPass --all to see loaded but inactive sockets, too.\n'

In [35]:     ssh_client = overcloud.overcloud_ssh_client('controller-1')
    ...:
    ...:

In [36]: output = sh.execute("systemctl list-sockets | grep podman | awk '{print $1}'" ,ssh_client=ssh_cli
    ...: ent).stdout

In [37]: output
Out[37]: '/run/podman/io.podman\n'

import six
if six.PY3:
    from podman import client as podman_client
    from podman.libs import containers

    from tobiko import podman
    from tobiko.openstack import topology
six.PY3
ssh_client = None

for node in topology.list_openstack_nodes(group='controller'):
               ssh_client = node.ssh_client

client = podman.get_podman_client(
                ssh_client=ssh_client).connect()

from tobiko.podman import _exception
from tobiko.podman import _shell
from tobiko.shell import ssh
import podman

Now we get this error:
~/tobiko/test/lib/python3.6/site-packages/podman/client.py in factory(cls, uri, interface, *args, **kwargs)
     55         local_path = urlparse(uri).path
     56         if local_path == '':
---> 57             raise ValueError('path is required for uri,'
     58                              ' expected format "unix://path_to_socket"')
     59

this is because :

uri=_shell.discover_podman_socket(ssh_client=ssh_client)
uri = ssh.get_port_forward_url(ssh_client=ssh_client, url=uri)
 uri
Out[18]: 'tcp://127.0.0.1:38005'

the Base client checks for 'path' in :  urlparse(uri2)
Out[27]: ParseResult(scheme='tcp', netloc='localhost:38005', path='', params='', query='', fragment='')
and throws an error because uri is a net link, not file.

even If I use socat to create a file link :
[stack@undercloud-0 ~]$ socat PIPE:/tmp/podman_tunnel TCP:127.0.0.1:38005

It still gives me an error becsause of permissions :

uri_via_socat='unix:/tmp/podman_tunnel'

In [34]: client = podman.Client(uri=uri_via_socat)
    ...:

~/tobiko/test/lib/python3.6/site-packages/varlink/client.py in open_unix()
    510                 s = socket.socket(socket.AF_UNIX)
    511                 s.setblocking(True)
--> 512                 s.connect(address)
    513                 return s
    514

ConnectionRefusedError: [Errno 111] Connection refused

This just works :
import podman
podman_client = podman.Client(uri='unix:/tmp/podman.sock', remote_uri='ssh://heat-admin@controller-0/run/podman/io.podman',identity_file='~/.ssh/id_rsa')
list(map(print, podman_client.images.list()))
[...]
{'id': 'c076fddbbcb3a1b7aa8765a51bb315938a9964849c5a09753b4f3596cddb7b87',

suggestion:
not to go through loops to make podman client use the netcat tunnel
and let it use it's native podman clients' ssh tunnel instead , thus reducing code vulnerability

podman.Client(uri='unix:/tmp/podman.sock', remote_uri='ssh://{}@{}/run/podman/io.podman'.format(ssh_client.connect_parameters['username'],ssh_client.host),identity_file='~/.ssh/id_rsa')

this works :
In [78]: list(map(print, podman_client.images.list()))
debug1: Connection to port -2 forwarding to /run/podman/io.podman port -2 requested.
debug1: channel 1: new [direct-streamlocal@openssh.com]
{'id': 'c076fddbbcb3a1b7aa8765a51bb315938a9964849c5a09753b4f3596cddb7b87',

lastly :
fix the list containers test in :
tobiko/tests/functional/podman/test_client.py

this will always return true , if the for loop
comes up with an empty list then there will nothing to fail this test on,
so we have to verify we get an actual container list first...

Change-Id: If069a640f0fc5251e5879cb2fd65115e299337f3
This commit is contained in:
pkomarov 2019-12-25 02:48:16 +02:00 committed by Federico Ressi
parent 78fcbbf119
commit e8eb4ff759
4 changed files with 15 additions and 58 deletions

View File

@ -80,10 +80,14 @@ class PodmanClientFixture(tobiko.SharedFixture):
return client
def create_client(self):
uri = self.discover_podman_socket()
if self.ssh_client:
uri = ssh.get_port_forward_url(ssh_client=self.ssh_client, url=uri)
client = podman.Client(uri=uri)
podman_remote_socket = self.discover_podman_socket()
remote_uri = 'ssh://{username}@{host}{socket}'.format(
username=self.ssh_client.connect_parameters['username'],
host=self.ssh_client.host,
socket=podman_remote_socket)
client = podman.Client(uri=podman_remote_socket,
remote_uri=remote_uri,
identity_file='~/.ssh/id_rsa')
client.system.ping()
return client

View File

@ -19,10 +19,11 @@ from tobiko.podman import _exception
from tobiko.shell import sh
def discover_podman_socket(**execute_params):
def discover_podman_socket(ssh_client=None, **execute_params):
cmd = "systemctl list-sockets | grep podman | awk '{print $1}'"
result = sh.execute(cmd, stdin=False, stdout=True, stderr=True,
expect_exit_status=None, **execute_params)
expect_exit_status=None, ssh_client=ssh_client,
**execute_params)
if result.exit_status or not result.stdout:
raise _exception.PodmanSocketNotFoundError(details=result.stderr)
try:

View File

@ -66,6 +66,8 @@ if six.PY3:
client.ping()
def test_list_podman_containers(self):
for container in podman.list_podman_containers(
ssh_client=self.ssh_client):
podman_containers_list = podman.list_podman_containers(
ssh_client=self.ssh_client)
self.assertTrue(podman_containers_list)
for container in podman_containers_list:
self.assertIsInstance(container, containers.Container)

View File

@ -1,50 +0,0 @@
# Copyright 2018 Red Hat
#
# 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.
from __future__ import absolute_import
import mock
import six
# We need to ignore this code under py2
# it's not compatible and parser will failed even if we use
# the `unittest.skipIf` decorator, because during the test discovery
# stestr and unittest will load this test
# module before running it and it will load podman
# too which isn't compatible in version leather than python 3
# Also the varlink mock module isn't compatible with py27, is using
# annotations syntaxe to generate varlink interface for the mocked service
# and it will raise related exceptions too.
# For all these reasons we can't run podman tests under a python 2 environment
if six.PY3:
from tobiko import podman
from tobiko.tests import unit
from varlink import mock as varlink_mock
class TestPodmanClient(unit.TobikoUnitTest):
@varlink_mock.mockedservice(
fake_service=unit.mocked_service.ServicePod,
fake_types=unit.mocked_service.types,
name='io.podman',
address='unix:@podmantests'
)
@mock.patch(
'tobiko.podman._client.PodmanClientFixture.discover_podman_socket'
)
def test_init(self, mocked_discover_podman_socket):
mocked_discover_podman_socket.return_value = 'unix:@podmantests'
client = podman.get_podman_client().connect()
pods = client.pods.get('135d71b9495f')
self.assertEqual(pods["numberofcontainers"], "2")