Ensure identical types for comparing port number/string

Fix an oversight and regression in commit c97fced794
("Close previously opened ports on port config change").

The comparison between an integer and a string (returned
by .split()) is always different and thus when upgrading
the charm 'port 80' is closed.

Make sure the types are set to str. Right now it should
only be needed for port and not opened_port_number; but
let's future proof both sides of the comparison.

(Update: using str() vs int() as apparently int() might
fail but str() should always work no matter what it got;
thanks, Alex Kavanagh!)

Before:

    $ juju run --unit ceph-radosgw/0 opened-ports
    80/tcp

    $ juju upgrade-charm --path . ceph-radosgw

    $ juju run --unit ceph-radosgw/0 opened-ports
    $

    @ log:
    2021-04-05 15:08:04 INFO juju-log Closed port 80 in favor of port 80

    $ python3 -q
    >>> x=80
    >>> y='80/tcp'
    >>> z=y.split('/')[0]
    >>> z
    '80'
    >>> x
    80
    >>> x != z
    True
    >>> x=str(x)
    >>> x != z
    False

After:

    $ juju run --unit ceph-radosgw/1 opened-ports
    80/tcp

    $ juju upgrade-charm --path . ceph-radosgw

    $ juju run --unit ceph-radosgw/1 opened-ports
    80/tcp

Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
Change-Id: I2bcdfec1459ea45d8f57b850b7fd935c360cc7c1
This commit is contained in:
Mauricio Faria de Oliveira 2021-04-05 12:29:21 -03:00
parent 73c096998b
commit 358121d74c
1 changed files with 1 additions and 1 deletions

View File

@ -253,7 +253,7 @@ def config_changed():
open_port(port)
for opened_port in opened_ports():
opened_port_number = opened_port.split('/')[0]
if opened_port_number != port:
if str(opened_port_number) != str(port):
close_port(opened_port_number)
log('Closed port %s in favor of port %s' %
(opened_port_number, port))