]>
git.ipfire.org Git - pbs.git/blob - tests/test.py
10 from pakfire
._pakfire
import Archive
12 from buildservice
import Backend
13 from buildservice
import builds
14 from buildservice
import database
15 from buildservice
import misc
16 from buildservice
import packages
17 from buildservice
import uploads
19 class TestCase(unittest
.IsolatedAsyncioTestCase
):
21 This is the base class for all tests
23 def source_path(self
, path
):
25 source_path
= os
.environ
["abs_top_srcdir"]
27 raise ValueError("source path is not set") from e
29 return os
.path
.join(source_path
, path
)
31 def _setup_database(self
):
33 Creates a new database and imports the default schema
36 schema
= self
.source_path("src/database.sql")
39 with
open(schema
) as f
:
42 # Database Server & Name
43 host
= os
.environ
.get("DB_HOST", socket
.gethostname())
44 name
= os
.environ
.get("DB_NAME", "pakfiretest")
47 username
= os
.environ
.get("DB_USER", "pakfiretest")
48 password
= os
.environ
.get("DB_PASS", "pakfiretest")
50 # Connect to the database
51 db
= database
.Connection(host
, name
, username
, password
)
53 with db
.transaction():
54 # Drop anything existing
55 db
.execute("DROP SCHEMA public CASCADE")
57 # Re-create the schema
58 db
.execute("CREATE SCHEMA public")
61 # Return the credentials
66 "password" : password
,
69 async def asyncSetUp(self
):
70 # Create a new temporary directory
71 self
.testdir
= tempfile
.TemporaryDirectory()
73 # Create a configuration file
74 conf
= configparser
.ConfigParser()
78 "basepath" : self
.testdir
.name
,
82 conf
["database"] = self
._setup
_database
()
84 with tempfile
.NamedTemporaryFile("w") as f
:
91 # Initialize the backend
92 self
.backend
= Backend(f
.name
, test
=True)
94 # Create handle to the database
95 self
.db
= self
.backend
.db
97 # Create some default objects
98 with self
.db
.transaction():
99 await self
._create
_default
_objects
()
101 async def asyncTearDown(self
):
102 # Dump any messages in the message queue
103 for message
in self
.backend
.messages
.queue
:
104 print(message
.message
)
106 # Dump a listing of all temporary files
107 with os
.scandir(self
.testdir
.name
) as listing
:
108 for entry
in sorted(listing
, key
=lambda e
: e
.path
):
109 print(" %s" % entry
.path
)
111 # Removing any temporary files
112 self
.testdir
.cleanup()
114 async def _create_default_objects(self
):
116 Creates some random objects that are created by default so
117 that we won't have to start from scratch each time...
120 self
.builder
= self
.backend
.builders
.create("test-builder01.ipfire.org")
123 self
.user
= self
.backend
.users
.create("tester", _attrs
={
124 "cn" : [b
"Joe Tester"],
125 "email" : [b
"joe.tester@ipfire.org"],
129 self
.user
.storage_quota
= 104857600 # 100 MiB
131 # Create a distribution
132 self
.distro
= self
.backend
.distros
.create("Default Test Distribution", "test", 1)
134 # Enable this distribution for aarch64 & x86_64
135 self
.distro
.arches
= ["aarch64", "x86_64"]
137 # Create a repository
138 self
.repo
= await self
.backend
.repos
.create(self
.distro
, "Default Test Repository")
140 async def _create_package(self
, path
):
142 Helper function to import a package from path
144 # Check if the file exists
145 self
.assertTrue(os
.path
.exists(path
))
148 upload
= await self
._create
_upload
(path
)
151 package
= await self
.backend
.packages
.create(upload
, distro
=self
.distro
)
153 # Check if we received the correct type
154 self
.assertIsInstance(package
, packages
.Package
)
158 async def _create_build(self
, path
, repo
=None, owner
=None):
160 Helper function to import a build from path
162 # Use the default repository if none set
166 # Import the package first
167 package
= await self
._create
_package
(path
)
170 build
= await self
.backend
.builds
.create(repo
, package
, owner
=owner
)
172 # Check if we received the correct type
173 self
.assertIsInstance(build
, builds
.Build
)
177 async def _create_upload(self
, path
, **kwargs
):
179 Helper function to create an upload from path
181 # Create the upload object
182 upload
= await self
.backend
.uploads
.create_from_local(path
, **kwargs
)
184 # Check if received the correct type
185 self
.assertIsInstance(upload
, uploads
.Upload
)