Skip to content
Snippets Groups Projects
Commit 421d7544 authored by Ludovico de Nittis's avatar Ludovico de Nittis :palm_tree:
Browse files

Add automated tests for container

parent 46f1693f
No related branches found
No related tags found
1 merge request!305Add Flatpak version information
Pipeline #12228 passed
/*
* Copyright © 2021 Collabora Ltd.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* Include it before steam-runtime-tools.h so that its backport of
* G_DEFINE_AUTOPTR_CLEANUP_FUNC will be visible to it */
#include "steam-runtime-tools/glib-backports-internal.h"
#include <steam-runtime-tools/steam-runtime-tools.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "steam-runtime-tools/container-internal.h"
#include "steam-runtime-tools/utils-internal.h"
#include "test-utils.h"
static const char *argv0;
static gchar *global_sysroots;
typedef struct
{
gchar *builddir;
} Fixture;
typedef struct
{
int unused;
} Config;
static void
setup (Fixture *f,
gconstpointer context)
{
G_GNUC_UNUSED const Config *config = context;
f->builddir = g_strdup (g_getenv ("G_TEST_BUILDDIR"));
if (f->builddir == NULL)
f->builddir = g_path_get_dirname (argv0);
}
static void
teardown (Fixture *f,
gconstpointer context)
{
G_GNUC_UNUSED const Config *config = context;
g_free (f->builddir);
}
/*
* Test basic functionality of the SrtContainerInfo object.
*/
static void
test_object (Fixture *f,
gconstpointer context)
{
g_autoptr(SrtContainerInfo) container = NULL;
SrtContainerType type;
g_autofree gchar *flatpak_version = NULL;
g_autofree gchar *host_directory = NULL;
container = _srt_container_info_new (SRT_CONTAINER_TYPE_FLATPAK,
"1.10.2",
"/run/host");
g_assert_cmpint (srt_container_info_get_container_type (container), ==,
SRT_CONTAINER_TYPE_FLATPAK);
g_assert_cmpstr (srt_container_info_get_flatpak_version (container), ==, "1.10.2");
g_assert_cmpstr (srt_container_info_get_container_host_directory (container), ==,
"/run/host");
g_object_get (container,
"type", &type,
"flatpak-version", &flatpak_version,
"host-directory", &host_directory,
NULL);
g_assert_cmpint (type, ==, SRT_CONTAINER_TYPE_FLATPAK);
g_assert_cmpstr (flatpak_version, ==, "1.10.2");
g_assert_cmpstr (host_directory, ==, "/run/host");
}
typedef struct
{
const char *description;
const char *sysroot;
SrtContainerType type;
const char *host_directory;
const char *flatpak_version;
} ContainerTest;
static const ContainerTest container_tests[] =
{
{
.description = "Has /.dockerenv",
.sysroot = "debian-unstable",
.type = SRT_CONTAINER_TYPE_DOCKER,
},
{
.description = "Has an unknown value in /run/systemd/container",
.sysroot = "debian10",
.type = SRT_CONTAINER_TYPE_UNKNOWN,
},
{
.description = "Has 'docker' in /run/systemd/container",
.sysroot = "fedora",
.type = SRT_CONTAINER_TYPE_DOCKER,
},
{
.description = "Has /.flatpak-info and /run/host",
.sysroot = "flatpak-example",
.type = SRT_CONTAINER_TYPE_FLATPAK,
.host_directory = "/run/host",
.flatpak_version = "1.10.2",
},
{
.description = "Has /run/host",
.sysroot = "invalid-os-release",
.type = SRT_CONTAINER_TYPE_UNKNOWN,
.host_directory = "/run/host",
},
{
.description = "Has no evidence of being a container",
.sysroot = "no-os-release",
.type = SRT_CONTAINER_TYPE_NONE,
},
{
.description = "Has /run/pressure-vessel",
.sysroot = "steamrt",
.type = SRT_CONTAINER_TYPE_PRESSURE_VESSEL,
},
{
.description = "Has a Docker-looking /proc/1/cgroup",
.sysroot = "steamrt-unofficial",
.type = SRT_CONTAINER_TYPE_DOCKER,
},
{
.description = "Has 'podman' in /run/host/container-manager",
.sysroot = "podman-example",
.type = SRT_CONTAINER_TYPE_PODMAN,
.host_directory = "/run/host",
},
};
static void
test_containers (Fixture *f,
gconstpointer context)
{
gsize i, j;
for (i = 0; i < G_N_ELEMENTS (container_tests); i++)
{
const ContainerTest *test = &container_tests[i];
g_autoptr(SrtSystemInfo) info;
g_autofree gchar *expected_host = NULL;
g_autofree gchar *sysroot = NULL;
g_test_message ("%s: %s", test->sysroot, test->description);
sysroot = g_build_filename (global_sysroots, test->sysroot, NULL);
info = srt_system_info_new (NULL);
g_assert_nonnull (info);
srt_system_info_set_sysroot (info, sysroot);
if (test->host_directory == NULL)
expected_host = NULL;
else
expected_host = g_build_filename (sysroot, test->host_directory, NULL);
for (j = 0; j < 2; j++)
{
g_autoptr(SrtContainerInfo) container = NULL;
g_autofree gchar *host_dir_dup = NULL;
container = srt_system_info_check_container (info);
g_assert_nonnull (container);
g_assert_cmpint (srt_system_info_get_container_type (info), ==,
test->type);
g_assert_cmpint (srt_container_info_get_container_type (container), ==,
test->type);
host_dir_dup = srt_system_info_dup_container_host_directory (info);
g_assert_cmpstr (host_dir_dup, ==, expected_host);
g_assert_cmpstr (srt_container_info_get_container_host_directory (container),
==, expected_host);
g_assert_cmpstr (srt_container_info_get_flatpak_version (container),
==, test->flatpak_version);
}
}
}
int
main (int argc,
char **argv)
{
argv0 = argv[0];
global_sysroots = _srt_global_setup_sysroots (argv0);
g_test_init (&argc, &argv, NULL);
g_test_add ("/container/object", Fixture, NULL,
setup, test_object, teardown);
g_test_add ("/container/containers", Fixture, NULL,
setup, test_containers, teardown);
return g_test_run ();
}
...@@ -117,6 +117,7 @@ no-os-release/custom_path64/dri ...@@ -117,6 +117,7 @@ no-os-release/custom_path64/dri
no-os-release/custom_path64/va no-os-release/custom_path64/va
no-os-release/usr/lib/dri no-os-release/usr/lib/dri
no-os-release/usr/lib/vdpau no-os-release/usr/lib/vdpau
podman-example/run/host
steamrt/etc steamrt/etc
steamrt/overrides/bin steamrt/overrides/bin
steamrt/overrides/lib/x86_64-linux-gnu steamrt/overrides/lib/x86_64-linux-gnu
...@@ -206,7 +207,6 @@ fedora/usr/lib64/libva.so.2 ...@@ -206,7 +207,6 @@ fedora/usr/lib64/libva.so.2
fedora/usr/lib64/libvdpau.so.1 fedora/usr/lib64/libvdpau.so.1
fedora/usr/lib64/vdpau/libvdpau_r300.so fedora/usr/lib64/vdpau/libvdpau_r300.so
fedora/usr/lib64/vdpau/libvdpau_radeonsi.so fedora/usr/lib64/vdpau/libvdpau_radeonsi.so
flatpak-example/.flatpak-info
flatpak-example/usr/lib/dri/r300_dri.so flatpak-example/usr/lib/dri/r300_dri.so
flatpak-example/usr/lib/dri/r600_drv_video.so flatpak-example/usr/lib/dri/r600_drv_video.so
flatpak-example/usr/lib/x86_64-mock-abi/GL/lib/dri/i965_dri.so flatpak-example/usr/lib/x86_64-mock-abi/GL/lib/dri/i965_dri.so
...@@ -307,6 +307,23 @@ for name, target in { ...@@ -307,6 +307,23 @@ for name, target in {
except FileExistsError: except FileExistsError:
pass pass
with open('flatpak-example/.flatpak-info', 'w') as writer:
writer.write('''\
[Application]
name=com.valvesoftware.Steam
runtime=runtime/org.freedesktop.Platform/x86_64/20.08
[Instance]
branch=stable
arch=x86_64
flatpak-version=1.10.2
session-bus-proxy=true
system-bus-proxy=true
devel=true''')
with open('podman-example/run/host/container-manager', 'w') as writer:
writer.write("podman")
with open('debian10/custom_path/Single-good-layer.json', 'w') as writer: with open('debian10/custom_path/Single-good-layer.json', 'w') as writer:
writer.write('''\ writer.write('''\
{ {
......
{ {
"container" : { "container" : {
"type" : "flatpak", "type" : "flatpak",
"flatpak_version" : "1.10.2",
"host" : { "host" : {
"path" : null "path" : null
} }
......
...@@ -29,6 +29,7 @@ test_env.prepend('PATH', join_paths(meson.current_build_dir(), '..', 'bin')) ...@@ -29,6 +29,7 @@ test_env.prepend('PATH', join_paths(meson.current_build_dir(), '..', 'bin'))
tests = [ tests = [
{'name': 'architecture'}, {'name': 'architecture'},
{'name': 'container'},
{'name': 'desktop-entry'}, {'name': 'desktop-entry'},
{'name': 'graphics'}, {'name': 'graphics'},
{ {
......
...@@ -2388,75 +2388,6 @@ steamscript_env (Fixture *f, ...@@ -2388,75 +2388,6 @@ steamscript_env (Fixture *f,
g_strfreev (envp); g_strfreev (envp);
} }
typedef struct
{
const char *description;
const char *sysroot;
SrtContainerType type;
const char *host_directory;
} ContainerTest;
static const ContainerTest container_tests[] =
{
{ "Has /.dockerenv",
"debian-unstable", SRT_CONTAINER_TYPE_DOCKER, NULL },
{ "Has an unknown value in /run/systemd/container",
"debian10", SRT_CONTAINER_TYPE_UNKNOWN, NULL },
{ "Has 'docker' in /run/systemd/container",
"fedora", SRT_CONTAINER_TYPE_DOCKER, NULL },
{ "Has /.flatpak-info and /run/host",
"flatpak-example", SRT_CONTAINER_TYPE_FLATPAK, "/run/host" },
{ "Has /run/host",
"invalid-os-release", SRT_CONTAINER_TYPE_UNKNOWN, "/run/host" },
{ "Has no evidence of being a container",
"no-os-release", SRT_CONTAINER_TYPE_NONE, NULL },
{ "Has /run/pressure-vessel",
"steamrt", SRT_CONTAINER_TYPE_PRESSURE_VESSEL, NULL },
{ "Has a Docker-looking /proc/1/cgroup",
"steamrt-unofficial", SRT_CONTAINER_TYPE_DOCKER, NULL },
};
static void
test_containers (Fixture *f,
gconstpointer context)
{
gsize i, j;
for (i = 0; i < G_N_ELEMENTS (container_tests); i++)
{
const ContainerTest *test = &container_tests[i];
SrtSystemInfo *info;
gchar *sysroot, *got, *expected;
g_test_message ("%s: %s", test->sysroot, test->description);
sysroot = g_build_filename (f->sysroots, test->sysroot, NULL);
info = srt_system_info_new (NULL);
g_assert_nonnull (info);
srt_system_info_set_sysroot (info, sysroot);
for (j = 0; j < 2; j++)
{
g_assert_cmpint (srt_system_info_get_container_type (info), ==,
test->type);
got = srt_system_info_dup_container_host_directory (info);
if (test->host_directory == NULL)
expected = NULL;
else
expected = g_build_filename (sysroot, test->host_directory, NULL);
g_assert_cmpstr (got, ==, expected);
g_free (got);
g_free (expected);
}
g_object_unref (info);
g_free (sysroot);
}
}
/* For the purpose of this test an array that is NULL, and one with just one /* For the purpose of this test an array that is NULL, and one with just one
* NULL element, are considered to be equal */ * NULL element, are considered to be equal */
static void static void
...@@ -2527,6 +2458,7 @@ typedef struct ...@@ -2527,6 +2458,7 @@ typedef struct
{ {
SrtContainerType type; SrtContainerType type;
const gchar *host_path; const gchar *host_path;
const gchar *flatpak_version;
} ContTest; } ContTest;
typedef struct typedef struct
...@@ -3097,6 +3029,7 @@ static const JsonTest json_test[] = ...@@ -3097,6 +3029,7 @@ static const JsonTest json_test[] =
.container = .container =
{ {
.type = SRT_CONTAINER_TYPE_FLATPAK, .type = SRT_CONTAINER_TYPE_FLATPAK,
.flatpak_version = "1.10.2",
}, },
.driver_environment = .driver_environment =
{ {
...@@ -3506,6 +3439,7 @@ json_parsing (Fixture *f, ...@@ -3506,6 +3439,7 @@ json_parsing (Fixture *f,
g_autoptr(SrtObjectList) portal_backends = NULL; g_autoptr(SrtObjectList) portal_backends = NULL;
g_autoptr(SrtObjectList) explicit_layers = NULL; g_autoptr(SrtObjectList) explicit_layers = NULL;
g_autoptr(SrtObjectList) implicit_layers = NULL; g_autoptr(SrtObjectList) implicit_layers = NULL;
g_autoptr(SrtContainerInfo) container = NULL;
g_autofree gchar *portal_messages = NULL; g_autofree gchar *portal_messages = NULL;
g_autofree gchar *steamscript_path = NULL; g_autofree gchar *steamscript_path = NULL;
g_autofree gchar *steamscript_version = NULL; g_autofree gchar *steamscript_version = NULL;
...@@ -3560,9 +3494,12 @@ json_parsing (Fixture *f, ...@@ -3560,9 +3494,12 @@ json_parsing (Fixture *f,
g_assert_cmpstr (t->os_release.name, ==, name); g_assert_cmpstr (t->os_release.name, ==, name);
g_assert_cmpstr (t->os_release.pretty_name, ==, pretty_name); g_assert_cmpstr (t->os_release.pretty_name, ==, pretty_name);
container = srt_system_info_check_container (info);
host_directory = srt_system_info_dup_container_host_directory (info); host_directory = srt_system_info_dup_container_host_directory (info);
g_assert_cmpint (t->container.type, ==, srt_system_info_get_container_type (info)); g_assert_cmpint (t->container.type, ==, srt_system_info_get_container_type (info));
g_assert_cmpstr (t->container.host_path, ==, host_directory); g_assert_cmpstr (t->container.host_path, ==, host_directory);
g_assert_cmpstr (t->container.flatpak_version, ==,
srt_container_info_get_flatpak_version (container));
driver_environment_list = srt_system_info_list_driver_environment (info); driver_environment_list = srt_system_info_list_driver_environment (info);
assert_equal_strings_arrays (t->driver_environment, assert_equal_strings_arrays (t->driver_environment,
...@@ -4088,9 +4025,6 @@ main (int argc, ...@@ -4088,9 +4025,6 @@ main (int argc,
g_test_add ("/system-info/steamscript_env", Fixture, NULL, g_test_add ("/system-info/steamscript_env", Fixture, NULL,
setup, steamscript_env, teardown); setup, steamscript_env, teardown);
g_test_add ("/system-info/containers", Fixture, NULL,
setup, test_containers, teardown);
g_test_add ("/system-info/json_parsing", Fixture, NULL, g_test_add ("/system-info/json_parsing", Fixture, NULL,
setup, json_parsing, teardown); setup, json_parsing, teardown);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment