From 154a51245ff2c5c8c3690d3a0035b710b69d0c3d Mon Sep 17 00:00:00 2001
From: Muayyad alsadi <alsadi@gmail.com>
Date: Sat, 22 Jan 2022 00:15:05 +0200
Subject: [PATCH] FIXES #386: make sure volumes are present in top level

---
 podman_compose.py | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/podman_compose.py b/podman_compose.py
index eb2f5a9..8df239f 100755
--- a/podman_compose.py
+++ b/podman_compose.py
@@ -398,14 +398,20 @@ def mount_desc_to_volume_args(compose, mount_desc, srv_name, cnt_name):
     if opts: args += ':' + ','.join(opts)
     return args
 
-def get_mount_args(compose, cnt, volume):
+def get_mnt_dict(compose, cnt, volume):
     proj_name = compose.project_name
     srv_name = cnt['_service']
     basedir = compose.dirname
-    if is_str(volume): volume = parse_short_mount(volume, basedir)
-    mount_type = volume["type"]
+    if is_str(volume):
+        volume = parse_short_mount(volume, basedir)
+    return fix_mount_dict(compose, volume, proj_name, srv_name)
 
-    assert_volume(compose, fix_mount_dict(compose, volume, proj_name, srv_name))
+def get_mount_args(compose, cnt, volume):
+    volume = get_mnt_dict(compose, cnt, volume)
+    proj_name = compose.project_name
+    srv_name = cnt['_service']
+    mount_type = volume["type"]
+    assert_volume(compose, volume)
     if compose._prefer_volume_over_mount:
         if mount_type == 'tmpfs':
             # TODO: --tmpfs /tmp:rw,size=787448k,mode=1777
@@ -1230,6 +1236,12 @@ class PodmanCompose:
                 cnt['_service'] = service_name
                 cnt['_project'] = project_name
                 given_containers.append(cnt)
+                volumes = cnt.get("volumes", None) or []
+                for volume in volumes:
+                    mnt_dict = get_mnt_dict(self, cnt, volume)
+                    if mnt_dict.get("type", None)=="volume" and mnt_dict["source"] and mnt_dict["source"] not in self.vols:
+                        vol_name = mnt_dict["source"]
+                        raise RuntimeError(f"volume [{vol_name}] not defined in top level")
         self.container_names_by_service = container_names_by_service
         container_by_name = dict([(c["name"], c) for c in given_containers])
         #log("deps:", [(c["name"], c["_deps"]) for c in given_containers])