Repository: ionos-cloud/dim (language: python)

Issue:
Fix pytest tests
There are quite a few `pytest` tests failing, we should fix the code/tests.
> 10 failed, 184 passed, 332 warnings in 112.17s (0:01:52)

- [ ] #205
  > FAILED tests/allocator_test.py::AllocatorTest::test_delegation_not_subnet - sqlalchemy.exc.IntegrityError: (MySQLdb._exceptions.IntegrityError) (1062, "Duplicate entry '83076749736557242056487941267521536-64-384' for key 'address'")

- [x] [tests/commands_test.py
](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/commands_test.py#L10-L31) `CommandsTest::test_pool_report`
  >FAILED tests/commands_test.py::CommandsTest::test_pool_report - AssertionError: assert '444 IPs are still free' in 'Pool pool\ndays                     1                    7                   30\nusage                   24          ...             64\n444.0 IPs are still free\nBased on data from the last 30 days, the pool will be full in 208.1 days.\n'

  The called function does division and returns a float of number of IPs, but there can't be fractions of IPs, code should be adjusted to return `int`.

- [x] [tests/commands_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/commands_test.py#L33-L50) `CommandsTest::test_pool_reportv6`
  > FAILED tests/commands_test.py::CommandsTest::test_pool_reportv6 - AssertionError: assert '448 /64 blocks are still free' in 'Pool pool\ndays                     1                    7                   30\nusage                   20          ...      60\n448.0 /64 blocks are still free\nBased on data from the last 30 days, the pool will be full in 224.0 days.\n'

  The called function does division and returns a float of number of IPs, but there can't be fractions of IPs, code should be adjusted to return `int`.

- [x] [tests/dns_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dns_test.py#L8-L10) `test_validate_strings`
  > FAILED tests/dns_test.py::test_validate_strings - ValueError: Invalid escape sequence: \223

  Related to #72 and #176

- [x] [tests/dns_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dns_test.py#L197-L205) `ZoneTest::test_subzone`
  > FAILED tests/dns_test.py::ZoneTest::test_subzone - dim.errors.InvalidZoneError: Zone company.com does not exist`

  Test creates the parent zone with wrong name.

- [x] [tests/dns_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dns_test.py#L576-L591) `TXT::test_parse`
  > FAILED tests/dns_test.py::TXT::test_parse - dim.errors.InvalidParameterError: Invalid escape sequence: \100`

  Related to #72 and #176

- [x] [tests/dns_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dns_test.py#L771-L785) `RRReferencesTest::test_edit_comment_ttl`
  > FAILED tests/dns_test.py::RRReferencesTest::test_edit_comment_ttl - AssertionError: assert {'graph': {1:...}], 'root': 2} == {'graph': {1:...}], 'root': 2}`

- [x] [tests/dns_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dns_test.py#L738-L769) `RRReferencesTest::test_get_references`
  > FAILED tests/dns_test.py::RRReferencesTest::test_get_references - AssertionError: assert {'graph': {1:...}], 'root': 2} == {'graph': {1:...}], 'root': 2}`

- [x] [tests/dnssec_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/dnssec_test.py#L15-L19) `test_ds_hash`
  > FAILED tests/dnssec_test.py::DNSKEYTest::test_ds_hash - TypeError: owner must be of type bytes, got <class 'str'>`

  The called function expects bytes for `owner`, but the test sets `str`; adjust to `.encode('utf-8')`.

- [x] [tests/pool_test.py](https://github.com/1and1/dim/blob/0b818c418a8184a5d09b28dd41a48344cb463f7d/dim-testsuite/tests/pool_test.py#L224-L274) `PoolTest::test_priority`
  > FAILED tests/pool_test.py::PoolTest::test_priority - TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'`
ndcli list pool does not sort list of subnet by priority
```
% ndcli list pool MORE-R221   
prio subnet           gateway free total
   6 87.106.61.0/24            187   256
  25 87.106.62.0/24            210   256
  23 87.106.63.0/24            208   256
  10 87.106.64.0/24            199   256
  19 87.106.65.0/24            205   256
  35 87.106.66.0/24            218   256
  22 87.106.67.0/24            207   256
   8 87.106.68.0/24            195   256
  26 87.106.69.0/24            210   256
  15 87.106.70.0/24            202   256
  28 87.106.71.0/24            213   256
  16 87.106.72.0/24            202   256
  38 87.106.73.0/24            223   256
  30 87.106.82.0/24            214   256
  42 87.106.226.0/24           226   256
  31 212.227.58.0/24           214   256
   3 213.165.87.0/24           178   256
   7 213.165.89.0/24           190   256
  12 213.165.92.0/24           200   256
   1 213.165.95.0/24            87   256
  32 217.160.24.0/24           214   256
   4 212.227.59.0/24           178   256
  17 87.106.234.0/24           203   256
  33 87.106.230.0/24           217   256
   9 87.106.188.0/24           196   256
   5 87.106.56.0/24            184   256
  11 87.106.57.0/24            199   256
  20 87.106.58.0/24            205   256
  13 87.106.59.0/24            201   256
  34 87.106.92.0/24            217   256
  43 87.106.93.0/24            226   256
  40 82.165.172.0/24           224   256
   2 212.227.250.0/24          154   256
  46 213.165.94.0/24           229   256
  14 87.106.222.0/24           201   256
  24 217.160.22.0/24           208   256
  36 217.160.23.0/24           218   256
  44 217.160.3.0/24            227   256
  39 213.165.93.0/24           223   256
  37 217.160.21.0/24           218   256
  41 217.160.20.0/24           225   256
  45 217.160.79.0/24           228   256
  18 87.106.94.0/24            204   256
  29 87.106.95.0/24            213   256
  27 87.106.74.0/24            212   256
  21 87.106.75.0/24            206   256
INFO - Total free IPs: 9418
```

create test case
add sort to code


Discussion / hints:

Fix patch:
diff --git a/dim/dim/models/ip.py b/dim/dim/models/ip.py
index 3d818377..dd83d423 100644
--- a/dim/dim/models/ip.py
+++ b/dim/dim/models/ip.py
@@ -120,7 +120,7 @@ class Pool(db.Model, WithAttr):
     vlan = relationship(Vlan)
     owner = relationship('Group')
     layer3domain = relationship(Layer3Domain)
-    ipblocks = relationship("Ipblock", backref="pool")
+    ipblocks = relationship("Ipblock", backref="pool", order_by='Ipblock.priority')
     subnets = synonym('ipblocks')
 
     @property


Test patch:
diff --git a/dim-testsuite/t/pool-list-4.t b/dim-testsuite/t/pool-list-4.t
new file mode 100644
index 00000000..331de285
--- /dev/null
+++ b/dim-testsuite/t/pool-list-4.t
@@ -0,0 +1,52 @@
+# verify a pool's subnets are listed ordered by priority
+$ ndcli create pool some-pool
+$ ndcli create container 10.0.0.0/8
+INFO - Creating container 10.0.0.0/8 in layer3domain default
+$ ndcli modify pool some-pool add subnet 10.0.0.0/24
+INFO - Created subnet 10.0.0.0/24 in layer3domain default
+WARNING - Creating zone 0.0.10.in-addr.arpa without profile
+WARNING - Primary NS for this Domain is now localhost.
+$ ndcli list pool some-pool
+INFO - Total free IPs: 254
+prio subnet      gateway free total
+   1 10.0.0.0/24         254   256
+
+$ ndcli modify pool some-pool add subnet 10.0.9.0/24
+INFO - Created subnet 10.0.9.0/24 in layer3domain default
+WARNING - Creating zone 9.0.10.in-addr.arpa without profile
+WARNING - Primary NS for this Domain is now localhost.
+
+$ ndcli modify pool some-pool add subnet 10.0.5.0/24
+INFO - Created subnet 10.0.5.0/24 in layer3domain default
+WARNING - Creating zone 5.0.10.in-addr.arpa without profile
+WARNING - Primary NS for this Domain is now localhost.
+
+$ ndcli list pool some-pool
+INFO - Total free IPs: 762
+prio subnet      gateway free total
+   1 10.0.0.0/24          254   256
+   2 10.0.9.0/24          254   256
+   3 10.0.5.0/24          254   256
+
+$ ndcli modify pool some-pool add subnet 10.0.10.0/24
+INFO - Created subnet 10.0.10.0/24 in layer3domain default
+WARNING - Creating zone 10.0.10.in-addr.arpa without profile
+WARNING - Primary NS for this Domain is now localhost.
+
+$ ndcli list pool some-pool
+INFO - Total free IPs: 1016
+prio subnet      gateway free total
+   1 10.0.0.0/24          254   256
+   2 10.0.9.0/24          254   256
+   3 10.0.5.0/24          254   256
+   4 10.0.10.0/24         254   256
+
+$ ndcli modify pool some-pool subnet 10.0.10.0/24 set prio 1
+
+$ ndcli list pool some-pool
+INFO - Total free IPs: 1016
+prio subnet       gateway free total
+   1 10.0.10.0/24          254   256
+   2 10.0.0.0/24           254   256
+   3 10.0.9.0/24           254   256
+   4 10.0.5.0/24           254   256
diff --git a/dim-testsuite/tests/util.py b/dim-testsuite/tests/util.py
index 73493689..2061b488 100644
--- a/dim-testsuite/tests/util.py
+++ b/dim-testsuite/tests/util.py
@@ -48,10 +48,11 @@ def assertDictSubset(self, actual, expected, msg=None):
         if not (missing or mismatched):
             return
 
+        msg = ''
         if missing:
-            msg = 'Missing: %s' % ','.join(safe_repr(m) for m in missing)
+            msg += 'Missing: %s' % ','.join(safe_repr(m) for m in missing)
         if mismatched:
-            if msg:
+            if len(msg) > 0:
                 msg += '; '
             msg += 'Mismatched values: %s' % ','.join(mismatched)
         self.fail(msg)