From 546c8327fa6054e11cebec221657862d2c085bfa Mon Sep 17 00:00:00 2001 From: Andy Doan Date: Sat, 29 Jun 2019 15:16:44 -0500 Subject: [PATCH 1/3] aktualizr-lite: Fix logic for finding latest version You can't assume the targets will be ordered latest->oldest. This creates a version comparison helper that tries its best to compare things based on the glibc function: http://man7.org/linux/man-pages/man3/strverscmp.3.html Signed-off-by: Andy Doan --- src/aktualizr_lite/CMakeLists.txt | 7 +++++-- src/aktualizr_lite/main.cc | 13 ++++++++++++- src/aktualizr_lite/test_lite.sh | 5 ++++- src/aktualizr_lite/version.h | 10 ++++++++++ src/aktualizr_lite/version_test.cc | 28 ++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/aktualizr_lite/version.h create mode 100644 src/aktualizr_lite/version_test.cc diff --git a/src/aktualizr_lite/CMakeLists.txt b/src/aktualizr_lite/CMakeLists.txt index f11319c7d0..85828cb981 100644 --- a/src/aktualizr_lite/CMakeLists.txt +++ b/src/aktualizr_lite/CMakeLists.txt @@ -1,5 +1,6 @@ if(BUILD_OSTREE) -add_executable(aktualizr-lite main.cc) +set(SOURCES main.cc version.h) +add_executable(aktualizr-lite ${SOURCES}) target_link_libraries(aktualizr-lite aktualizr_static_lib ${AKTUALIZR_EXTERNAL_LIBS}) install(TARGETS aktualizr-lite RUNTIME DESTINATION bin COMPONENT aktualizr-lite) @@ -18,5 +19,7 @@ add_test(test_aktualizr-lite endif(BUILD_OSTREE) -aktualizr_source_file_checks(main.cc) +add_aktualizr_test(NAME lite-version SOURCES version_test.cc) + +aktualizr_source_file_checks(${SOURCES} version_test.cc) # vim: set tabstop=4 shiftwidth=4 expandtab: diff --git a/src/aktualizr_lite/main.cc b/src/aktualizr_lite/main.cc index a96fccc37a..6c9b94eb7d 100644 --- a/src/aktualizr_lite/main.cc +++ b/src/aktualizr_lite/main.cc @@ -10,6 +10,7 @@ #include "config/config.h" #include "package_manager/ostreemanager.h" #include "primary/sotauptaneclient.h" +#include "version.h" namespace bpo = boost::program_options; @@ -111,15 +112,25 @@ static std::unique_ptr find_target(const std::shared_ptr latest = nullptr; for (auto &t : client->allTargets()) { for (auto const &it : t.hardwareIds()) { if (it == hwid) { - if (version == "latest" || version == t.filename() || version == t.custom_version()) { + if (find_latest) { + if (latest == nullptr || Version(latest->custom_version()) < Version(t.custom_version())) { + latest = std_::make_unique(t); + } + } else if (version == t.filename() || version == t.custom_version()) { return std_::make_unique(t); } } } } + if (find_latest) { + return latest; + } throw std::runtime_error("Unable to find update"); } diff --git a/src/aktualizr_lite/test_lite.sh b/src/aktualizr_lite/test_lite.sh index cea0f9f64b..a17e68b1c5 100755 --- a/src/aktualizr_lite/test_lite.sh +++ b/src/aktualizr_lite/test_lite.sh @@ -34,6 +34,7 @@ add_target() { fi cat >$custom_json < + +#include + +struct Version { + std::string raw_ver; + Version(const std::string& version) : raw_ver(std::move(version)) {} + + bool operator<(const Version& other) { return strverscmp(raw_ver.c_str(), other.raw_ver.c_str()) < 0; } +}; diff --git a/src/aktualizr_lite/version_test.cc b/src/aktualizr_lite/version_test.cc new file mode 100644 index 0000000000..68ac2fb257 --- /dev/null +++ b/src/aktualizr_lite/version_test.cc @@ -0,0 +1,28 @@ +#include + +#include "version.h" + +TEST(version, bad_versions) { + ASSERT_TRUE(Version("bar") < Version("foo")); + ASSERT_TRUE(Version("1.bar") < Version("2foo")); + ASSERT_TRUE(Version("1..0") < Version("1.1")); + ASSERT_TRUE(Version("1.-1") < Version("1.1")); + ASSERT_TRUE(Version("1.*bad #text") < Version("1.1")); // ord('*') < ord('1') +} + +TEST(version, good_versions) { + ASSERT_TRUE(Version("1.0.1") < Version("1.0.1.1")); + ASSERT_TRUE(Version("1.0.1") < Version("1.0.2")); + ASSERT_TRUE(Version("0.9") < Version("1.0.1")); + ASSERT_TRUE(Version("1.0.0.0") < Version("1.0.0.1")); + ASSERT_TRUE(Version("1") < Version("1.0.0.1")); + ASSERT_TRUE(Version("1.9.0") < Version("1.10")); +} + +#ifndef __NO_MAIN__ +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} +#endif From a147eb0a3ee3e8d820415044dee0bc64f575ed7a Mon Sep 17 00:00:00 2001 From: Andy Doan Date: Wed, 3 Jul 2019 09:28:58 -0500 Subject: [PATCH 2/3] CI fixes Signed-off-by: Andy Doan --- src/aktualizr_lite/CMakeLists.txt | 5 ++--- src/aktualizr_lite/version.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/aktualizr_lite/CMakeLists.txt b/src/aktualizr_lite/CMakeLists.txt index 85828cb981..b08079bfb7 100644 --- a/src/aktualizr_lite/CMakeLists.txt +++ b/src/aktualizr_lite/CMakeLists.txt @@ -1,6 +1,5 @@ if(BUILD_OSTREE) -set(SOURCES main.cc version.h) -add_executable(aktualizr-lite ${SOURCES}) +add_executable(aktualizr-lite main.cc version.h) target_link_libraries(aktualizr-lite aktualizr_static_lib ${AKTUALIZR_EXTERNAL_LIBS}) install(TARGETS aktualizr-lite RUNTIME DESTINATION bin COMPONENT aktualizr-lite) @@ -21,5 +20,5 @@ endif(BUILD_OSTREE) add_aktualizr_test(NAME lite-version SOURCES version_test.cc) -aktualizr_source_file_checks(${SOURCES} version_test.cc) +aktualizr_source_file_checks(main.cc version.h version_test.cc) # vim: set tabstop=4 shiftwidth=4 expandtab: diff --git a/src/aktualizr_lite/version.h b/src/aktualizr_lite/version.h index e44f9998f9..15e84f8d5e 100644 --- a/src/aktualizr_lite/version.h +++ b/src/aktualizr_lite/version.h @@ -4,7 +4,7 @@ struct Version { std::string raw_ver; - Version(const std::string& version) : raw_ver(std::move(version)) {} + Version(const std::string& version) : raw_ver(version) {} bool operator<(const Version& other) { return strverscmp(raw_ver.c_str(), other.raw_ver.c_str()) < 0; } }; From fa61e11a8bb5a4ee260d6efda09d7cb1cd84e367 Mon Sep 17 00:00:00 2001 From: Andy Doan Date: Fri, 5 Jul 2019 09:14:40 -0500 Subject: [PATCH 3/3] CI fix for clang-tidy Signed-off-by: Andy Doan --- src/aktualizr_lite/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aktualizr_lite/version.h b/src/aktualizr_lite/version.h index 15e84f8d5e..6c643e8a10 100644 --- a/src/aktualizr_lite/version.h +++ b/src/aktualizr_lite/version.h @@ -4,7 +4,7 @@ struct Version { std::string raw_ver; - Version(const std::string& version) : raw_ver(version) {} + Version(std::string version) : raw_ver(std::move(version)) {} bool operator<(const Version& other) { return strverscmp(raw_ver.c_str(), other.raw_ver.c_str()) < 0; } };