cloud-setup: merge branch 'th/cloud-setup-fix-replace-addr'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/747
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -6,6 +6,7 @@
|
|||||||
*.gcno
|
*.gcno
|
||||||
*.gcda
|
*.gcda
|
||||||
*.la
|
*.la
|
||||||
|
*.a
|
||||||
*-*.gir
|
*-*.gir
|
||||||
*.typelib
|
*.typelib
|
||||||
*.stamp
|
*.stamp
|
||||||
@@ -70,11 +71,11 @@ test-*.trs
|
|||||||
/clients/cli/nmcli
|
/clients/cli/nmcli
|
||||||
/clients/cloud-setup/nm-cloud-setup
|
/clients/cloud-setup/nm-cloud-setup
|
||||||
/clients/cloud-setup/nm-cloud-setup.service
|
/clients/cloud-setup/nm-cloud-setup.service
|
||||||
|
/clients/cloud-setup/tests/test-cloud-setup-general
|
||||||
/clients/common/settings-docs.h
|
/clients/common/settings-docs.h
|
||||||
/clients/common/tests/test-clients-common
|
/clients/common/tests/test-clients-common
|
||||||
/clients/common/tests/test-libnm-core-aux
|
/clients/common/tests/test-libnm-core-aux
|
||||||
/clients/nm-online
|
/clients/nm-online
|
||||||
/clients/tui/newt/libnmt-newt.a
|
|
||||||
/clients/tui/nmtui
|
/clients/tui/nmtui
|
||||||
|
|
||||||
/data/NetworkManager.service
|
/data/NetworkManager.service
|
||||||
|
53
Makefile.am
53
Makefile.am
@@ -4990,10 +4990,9 @@ EXTRA_DIST += \
|
|||||||
|
|
||||||
if BUILD_NM_CLOUD_SETUP
|
if BUILD_NM_CLOUD_SETUP
|
||||||
|
|
||||||
libexec_PROGRAMS += clients/cloud-setup/nm-cloud-setup
|
noinst_LIBRARIES += clients/cloud-setup/libnm-cloud-setup-core.a
|
||||||
|
|
||||||
clients_cloud_setup_nm_cloud_setup_SOURCES = \
|
clients_cloud_setup_libnm_cloud_setup_core_a_SOURCES = \
|
||||||
clients/cloud-setup/main.c \
|
|
||||||
clients/cloud-setup/nm-cloud-setup-utils.c \
|
clients/cloud-setup/nm-cloud-setup-utils.c \
|
||||||
clients/cloud-setup/nm-cloud-setup-utils.h \
|
clients/cloud-setup/nm-cloud-setup-utils.h \
|
||||||
clients/cloud-setup/nm-http-client.c \
|
clients/cloud-setup/nm-http-client.c \
|
||||||
@@ -5008,6 +5007,21 @@ clients_cloud_setup_nm_cloud_setup_SOURCES = \
|
|||||||
clients/cloud-setup/nmcs-provider-azure.h \
|
clients/cloud-setup/nmcs-provider-azure.h \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
|
clients_cloud_setup_libnm_cloud_setup_core_a_CPPFLAGS = \
|
||||||
|
$(clients_cppflags) \
|
||||||
|
-DG_LOG_DOMAIN=\""nm-cloud-setup"\" \
|
||||||
|
$(LIBCURL_CFLAGS) \
|
||||||
|
$(NULL)
|
||||||
|
|
||||||
|
$(clients_cloud_setup_libnm_cloud_setup_core_a_OBJECTS): $(libnm_core_lib_h_pub_mkenums)
|
||||||
|
$(clients_cloud_setup_libnm_cloud_setup_core_a_OBJECTS): $(libnm_lib_h_pub_mkenums)
|
||||||
|
|
||||||
|
libexec_PROGRAMS += clients/cloud-setup/nm-cloud-setup
|
||||||
|
|
||||||
|
clients_cloud_setup_nm_cloud_setup_SOURCES = \
|
||||||
|
clients/cloud-setup/main.c \
|
||||||
|
$(NULL)
|
||||||
|
|
||||||
clients_cloud_setup_nm_cloud_setup_CPPFLAGS = \
|
clients_cloud_setup_nm_cloud_setup_CPPFLAGS = \
|
||||||
$(clients_cppflags) \
|
$(clients_cppflags) \
|
||||||
-DG_LOG_DOMAIN=\""nm-cloud-setup"\" \
|
-DG_LOG_DOMAIN=\""nm-cloud-setup"\" \
|
||||||
@@ -5020,6 +5034,7 @@ clients_cloud_setup_nm_cloud_setup_LDFLAGS = \
|
|||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
clients_cloud_setup_nm_cloud_setup_LDADD = \
|
clients_cloud_setup_nm_cloud_setup_LDADD = \
|
||||||
|
clients/cloud-setup/libnm-cloud-setup-core.a \
|
||||||
libnm/nm-libnm-aux/libnm-libnm-aux.la \
|
libnm/nm-libnm-aux/libnm-libnm-aux.la \
|
||||||
libnm-core/nm-libnm-core-aux/libnm-libnm-core-aux.la \
|
libnm-core/nm-libnm-core-aux/libnm-libnm-core-aux.la \
|
||||||
libnm-core/nm-libnm-core-intern/libnm-libnm-core-intern.la \
|
libnm-core/nm-libnm-core-intern/libnm-libnm-core-intern.la \
|
||||||
@@ -5064,11 +5079,43 @@ EXTRA_DIST += \
|
|||||||
clients/cloud-setup/meson.build \
|
clients/cloud-setup/meson.build \
|
||||||
clients/cloud-setup/nm-cloud-setup.service.in \
|
clients/cloud-setup/nm-cloud-setup.service.in \
|
||||||
clients/cloud-setup/nm-cloud-setup.timer \
|
clients/cloud-setup/nm-cloud-setup.timer \
|
||||||
|
clients/cloud-setup/tests/meson.build \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
CLEANFILES += \
|
CLEANFILES += \
|
||||||
clients/cloud-setup/nm-cloud-setup.service
|
clients/cloud-setup/nm-cloud-setup.service
|
||||||
|
|
||||||
|
check_programs += clients/cloud-setup/tests/test-cloud-setup-general
|
||||||
|
|
||||||
|
clients_cloud_setup_tests_test_cloud_setup_general_CPPFLAGS = \
|
||||||
|
$(clients_cppflags) \
|
||||||
|
-I$(srcdir)/clients/cloud-setup \
|
||||||
|
-DG_LOG_DOMAIN=\""tests"\" \
|
||||||
|
$(LIBCURL_CFLAGS) \
|
||||||
|
$(NULL)
|
||||||
|
|
||||||
|
clients_cloud_setup_tests_test_cloud_setup_general_LDFLAGS = \
|
||||||
|
$(CODE_COVERAGE_LDFLAGS) \
|
||||||
|
$(SANITIZER_EXEC_LDFLAGS) \
|
||||||
|
$(NULL)
|
||||||
|
|
||||||
|
clients_cloud_setup_tests_test_cloud_setup_general_LDADD = \
|
||||||
|
clients/cloud-setup/libnm-cloud-setup-core.a \
|
||||||
|
libnm/nm-libnm-aux/libnm-libnm-aux.la \
|
||||||
|
libnm-core/nm-libnm-core-aux/libnm-libnm-core-aux.la \
|
||||||
|
libnm-core/nm-libnm-core-intern/libnm-libnm-core-intern.la \
|
||||||
|
shared/nm-base/libnm-base.la \
|
||||||
|
shared/nm-glib-aux/libnm-glib-aux.la \
|
||||||
|
shared/nm-std-aux/libnm-std-aux.la \
|
||||||
|
shared/libcsiphash.la \
|
||||||
|
libnm/libnm.la \
|
||||||
|
$(GLIB_LIBS) \
|
||||||
|
$(LIBCURL_LIBS) \
|
||||||
|
$(NULL)
|
||||||
|
|
||||||
|
$(clients_cloud_setup_tests_test_cloud_setup_general_OBJECTS): $(libnm_core_lib_h_pub_mkenums)
|
||||||
|
$(clients_cloud_setup_tests_test_cloud_setup_general_OBJECTS): $(libnm_lib_h_pub_mkenums)
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
@@ -21,10 +21,9 @@ if install_systemdunitdir
|
|||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
executable(
|
libnm_cloud_setup_core = static_library(
|
||||||
'nm-cloud-setup',
|
'nm-cloud-setup-core',
|
||||||
files(
|
sources: files(
|
||||||
'main.c',
|
|
||||||
'nm-cloud-setup-utils.c',
|
'nm-cloud-setup-utils.c',
|
||||||
'nm-http-client.c',
|
'nm-http-client.c',
|
||||||
'nmcs-provider-ec2.c',
|
'nmcs-provider-ec2.c',
|
||||||
@@ -41,9 +40,35 @@ executable(
|
|||||||
c_args: [
|
c_args: [
|
||||||
'-DG_LOG_DOMAIN="nm-cloud-setup"',
|
'-DG_LOG_DOMAIN="nm-cloud-setup"',
|
||||||
],
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
libnm_cloud_setup_core_dep = declare_dependency(
|
||||||
|
include_directories: include_directories('.'),
|
||||||
|
link_with: libnm_cloud_setup_core,
|
||||||
|
)
|
||||||
|
|
||||||
|
executable(
|
||||||
|
'nm-cloud-setup',
|
||||||
|
files(
|
||||||
|
'main.c',
|
||||||
|
),
|
||||||
|
dependencies: [
|
||||||
|
libnmc_base_dep,
|
||||||
|
libnmc_dep,
|
||||||
|
libcurl_dep,
|
||||||
|
libnm_libnm_aux_dep,
|
||||||
|
libnm_cloud_setup_core_dep,
|
||||||
|
],
|
||||||
|
c_args: [
|
||||||
|
'-DG_LOG_DOMAIN="nm-cloud-setup"',
|
||||||
|
],
|
||||||
link_with: libnm_systemd_logging_stub,
|
link_with: libnm_systemd_logging_stub,
|
||||||
link_args: ldflags_linker_script_binary,
|
link_args: ldflags_linker_script_binary,
|
||||||
link_depends: linker_script_binary,
|
link_depends: linker_script_binary,
|
||||||
install: true,
|
install: true,
|
||||||
install_dir: nm_libexecdir,
|
install_dir: nm_libexecdir,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if enable_tests
|
||||||
|
subdir('tests')
|
||||||
|
endif
|
||||||
|
@@ -704,7 +704,7 @@ nmcs_setting_ip_replace_ipv4_addresses(NMSettingIPConfig *s_ip,
|
|||||||
|
|
||||||
i_next++;
|
i_next++;
|
||||||
}
|
}
|
||||||
if (any_changes) {
|
if (!any_changes) {
|
||||||
while (i_next < num) {
|
while (i_next < num) {
|
||||||
nm_setting_ip_config_remove_address(s_ip, --num);
|
nm_setting_ip_config_remove_address(s_ip, --num);
|
||||||
any_changes = TRUE;
|
any_changes = TRUE;
|
||||||
|
20
clients/cloud-setup/tests/meson.build
Normal file
20
clients/cloud-setup/tests/meson.build
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||||
|
|
||||||
|
exe = executable(
|
||||||
|
'test-cloud-setup-general',
|
||||||
|
'test-cloud-setup-general.c',
|
||||||
|
dependencies: [
|
||||||
|
libnmc_base_dep,
|
||||||
|
libnmc_dep,
|
||||||
|
libnm_cloud_setup_core_dep,
|
||||||
|
],
|
||||||
|
c_args: [
|
||||||
|
'-DG_LOG_DOMAIN="test"',
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
test(
|
||||||
|
'clients/cloud-setup/tests/test-cloud-setup-general',
|
||||||
|
test_script,
|
||||||
|
args: test_args + [exe.full_path()],
|
||||||
|
)
|
117
clients/cloud-setup/tests/test-cloud-setup-general.c
Normal file
117
clients/cloud-setup/tests/test-cloud-setup-general.c
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||||
|
|
||||||
|
#include "libnm/nm-default-client.h"
|
||||||
|
|
||||||
|
#include "nm-cloud-setup-utils.h"
|
||||||
|
#include "nm-libnm-core-intern/nm-libnm-core-utils.h"
|
||||||
|
|
||||||
|
#include "nm-utils/nm-test-utils.h"
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
static NMSettingIPConfig *
|
||||||
|
_ri4a_new_s_ip(const char *const *addrs)
|
||||||
|
{
|
||||||
|
NMSettingIPConfig *s_ip;
|
||||||
|
|
||||||
|
s_ip = NM_SETTING_IP_CONFIG(nm_setting_ip4_config_new());
|
||||||
|
|
||||||
|
for (; addrs && addrs[0]; addrs++) {
|
||||||
|
nm_auto_unref_ip_address NMIPAddress *a = nmtst_ip_address_new(AF_INET, addrs[0]);
|
||||||
|
|
||||||
|
nm_setting_ip_config_add_address(s_ip, a);
|
||||||
|
}
|
||||||
|
return s_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GPtrArray *
|
||||||
|
_ri4a_new_arr(const char *const *addrs)
|
||||||
|
{
|
||||||
|
GPtrArray *arr;
|
||||||
|
|
||||||
|
arr = g_ptr_array_new_with_free_func((GDestroyNotify) nm_ip_address_unref);
|
||||||
|
|
||||||
|
for (; addrs && addrs[0]; addrs++)
|
||||||
|
g_ptr_array_add(arr, nmtst_ip_address_new(AF_INET, addrs[0]));
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_test_ri4a(const char *const *addrs_before, const char *const *addrs_new)
|
||||||
|
{
|
||||||
|
gs_unref_object NMSettingIPConfig *s_ip = NULL;
|
||||||
|
gs_unref_ptrarray GPtrArray *arr = NULL;
|
||||||
|
const gsize n = NM_PTRARRAY_LEN(addrs_new);
|
||||||
|
gboolean changed;
|
||||||
|
gsize i;
|
||||||
|
|
||||||
|
s_ip = _ri4a_new_s_ip(addrs_before);
|
||||||
|
arr = _ri4a_new_arr(addrs_new);
|
||||||
|
|
||||||
|
g_assert_cmpint(nm_setting_ip_config_get_num_addresses(s_ip),
|
||||||
|
==,
|
||||||
|
NM_PTRARRAY_LEN(addrs_before));
|
||||||
|
g_assert_cmpint(arr->len, ==, n);
|
||||||
|
|
||||||
|
changed = nmcs_setting_ip_replace_ipv4_addresses(s_ip, (NMIPAddress **) arr->pdata, arr->len);
|
||||||
|
|
||||||
|
g_assert_cmpint(changed, !=, nm_utils_strv_equal(addrs_before, addrs_new));
|
||||||
|
g_assert_cmpint(nm_setting_ip_config_get_num_addresses(s_ip), ==, n);
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
NMIPAddress *a = arr->pdata[i];
|
||||||
|
NMIPAddress *b = nm_setting_ip_config_get_address(s_ip, i);
|
||||||
|
|
||||||
|
if (nmtst_get_rand_bool())
|
||||||
|
NM_SWAP(&a, &b);
|
||||||
|
|
||||||
|
g_assert(nm_ip_address_cmp_full(a, b, NM_IP_ADDRESS_CMP_FLAGS_WITH_ATTRS) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_replace_ipv4_addresses(void)
|
||||||
|
{
|
||||||
|
const char *const *const LISTS[] = {
|
||||||
|
NM_MAKE_STRV(),
|
||||||
|
NM_MAKE_STRV("192.168.5.1/24"),
|
||||||
|
NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24"),
|
||||||
|
NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24", "192.168.5.3/24"),
|
||||||
|
NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24", "192.168.5.3/24", "192.168.5.4/24"),
|
||||||
|
};
|
||||||
|
int i_run;
|
||||||
|
|
||||||
|
for (i_run = 0; i_run < 20; i_run++) {
|
||||||
|
gs_free const char **addrs_before = NULL;
|
||||||
|
gs_free const char **addrs_new = NULL;
|
||||||
|
|
||||||
|
addrs_before = nmtst_rand_perm_strv(LISTS[nmtst_get_rand_uint32() % G_N_ELEMENTS(LISTS)]);
|
||||||
|
addrs_new = nmtst_rand_perm_strv(LISTS[nmtst_get_rand_uint32() % G_N_ELEMENTS(LISTS)]);
|
||||||
|
|
||||||
|
_test_ri4a(addrs_before, addrs_new);
|
||||||
|
}
|
||||||
|
|
||||||
|
_test_ri4a(NM_MAKE_STRV(), NM_MAKE_STRV());
|
||||||
|
_test_ri4a(NM_MAKE_STRV(), NM_MAKE_STRV("192.168.5.1/24"));
|
||||||
|
_test_ri4a(NM_MAKE_STRV(), NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24"));
|
||||||
|
_test_ri4a(NM_MAKE_STRV("192.168.5.1/24"), NM_MAKE_STRV());
|
||||||
|
_test_ri4a(NM_MAKE_STRV("192.168.5.1/24"), NM_MAKE_STRV("192.168.5.1/24"));
|
||||||
|
_test_ri4a(NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24"), NM_MAKE_STRV());
|
||||||
|
_test_ri4a(NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24"), NM_MAKE_STRV("192.168.5.1/24"));
|
||||||
|
_test_ri4a(NM_MAKE_STRV("192.168.5.1/24", "192.168.5.2/24"), NM_MAKE_STRV("192.168.5.2/24"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
NMTST_DEFINE();
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
nmtst_init(&argc, &argv, TRUE);
|
||||||
|
|
||||||
|
g_test_add_func("/cloud-setup/general/replace-ipv4-addresses", test_replace_ipv4_addresses);
|
||||||
|
|
||||||
|
return g_test_run();
|
||||||
|
}
|
@@ -1001,6 +1001,23 @@ nmtst_rand_perm(GRand *rand, void *dst, const void *src, gsize elmt_size, gsize
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline const char **
|
||||||
|
nmtst_rand_perm_strv(const char *const *strv)
|
||||||
|
{
|
||||||
|
const char **res;
|
||||||
|
gsize n;
|
||||||
|
|
||||||
|
if (!strv)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* this returns a (scrambled) SHALLOW copy of the strv array! */
|
||||||
|
|
||||||
|
n = NM_PTRARRAY_LEN(strv);
|
||||||
|
res = (const char **) (nm_utils_strv_dup(strv, n, FALSE) ?: g_new0(char *, 1));
|
||||||
|
nmtst_rand_perm(NULL, res, res, sizeof(char *), n);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static inline GSList *
|
static inline GSList *
|
||||||
nmtst_rand_perm_gslist(GRand *rand, GSList *list)
|
nmtst_rand_perm_gslist(GRand *rand, GSList *list)
|
||||||
{
|
{
|
||||||
@@ -2762,4 +2779,29 @@ nmtst_keyfile_get_num_keys(GKeyFile *keyfile, const char *group_name)
|
|||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
#if defined(NM_SETTING_IP_CONFIG_H) && defined(__NM_SHARED_UTILS_H__)
|
||||||
|
|
||||||
|
static inline NMIPAddress *
|
||||||
|
nmtst_ip_address_new(int addr_family, const char *str)
|
||||||
|
{
|
||||||
|
NMIPAddr addr;
|
||||||
|
int plen;
|
||||||
|
GError * error = NULL;
|
||||||
|
NMIPAddress *a;
|
||||||
|
|
||||||
|
if (!nm_utils_parse_inaddr_prefix_bin(addr_family, str, &addr_family, &addr, &plen))
|
||||||
|
g_assert_not_reached();
|
||||||
|
|
||||||
|
if (plen == -1)
|
||||||
|
plen = addr_family == AF_INET ? 32 : 128;
|
||||||
|
|
||||||
|
a = nm_ip_address_new_binary(addr_family, &addr, plen, &error);
|
||||||
|
nmtst_assert_success(a, error);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
#endif /* __NM_TEST_UTILS_H__ */
|
#endif /* __NM_TEST_UTILS_H__ */
|
||||||
|
Reference in New Issue
Block a user