Repository: mesos Updated Branches: refs/heads/master 9b2bbba89 -> 9b5895606
Added a Version class to stout. Currently there is no facility in Mesos for checking compatibility of various Mesos components that could have been built at different times with potentially different Mesos versions. This requirement is especially important for doing various compatibility checks between Mesos and Mesos modules (WIP). Review: https://reviews.apache.org/r/25597 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9b589560 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9b589560 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9b589560 Branch: refs/heads/master Commit: 9b5895606f6bd0ffba0171503b738554091791ab Parents: 9b2bbba Author: Kapil Arya <[email protected]> Authored: Tue Sep 16 14:06:47 2014 -0700 Committer: Benjamin Mahler <[email protected]> Committed: Tue Sep 16 14:11:13 2014 -0700 ---------------------------------------------------------------------- 3rdparty/libprocess/3rdparty/Makefile.am | 3 +- 3rdparty/libprocess/3rdparty/stout/Makefile.am | 3 +- .../3rdparty/stout/include/Makefile.am | 3 +- .../3rdparty/stout/include/stout/os.hpp | 1 + .../3rdparty/stout/include/stout/version.hpp | 123 +++++++++++++++++++ .../3rdparty/stout/tests/version_tests.cpp | 60 +++++++++ 6 files changed, 190 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/Makefile.am b/3rdparty/libprocess/3rdparty/Makefile.am index db9766d..bd1dc8d 100644 --- a/3rdparty/libprocess/3rdparty/Makefile.am +++ b/3rdparty/libprocess/3rdparty/Makefile.am @@ -173,7 +173,8 @@ stout_tests_SOURCES = \ $(STOUT)/tests/strings_tests.cpp \ $(STOUT)/tests/subcommand_tests.cpp \ $(STOUT)/tests/thread_tests.cpp \ - $(STOUT)/tests/uuid_tests.cpp + $(STOUT)/tests/uuid_tests.cpp \ + $(STOUT)/tests/version_tests.cpp if OS_LINUX stout_tests_SOURCES += $(STOUT)/tests/proc_tests.cpp http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/stout/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am index b6464de..2ee5a0b 100644 --- a/3rdparty/libprocess/3rdparty/stout/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am @@ -43,4 +43,5 @@ EXTRA_DIST = \ tests/strings_tests.cpp \ tests/subcommand_tests.cpp \ tests/thread_tests.cpp \ - tests/uuid_tests.cpp + tests/uuid_tests.cpp \ + tests/version_tests.cpp http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/stout/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am index 6fa5b74..d4a8ad4 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am @@ -70,4 +70,5 @@ nobase_include_HEADERS = \ stout/tuple.hpp \ stout/unreachable.hpp \ stout/utils.hpp \ - stout/uuid.hpp + stout/uuid.hpp \ + stout/version.hpp http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index 5bbf829..02a1e58 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -1181,6 +1181,7 @@ inline Try<std::string> sysname() // The OS release level. +// TODO(karya): Replace struct Release with Version from version.hpp. struct Release { bool operator == (const Release& other) http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/stout/include/stout/version.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/version.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/version.hpp new file mode 100644 index 0000000..2a2fead --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/version.hpp @@ -0,0 +1,123 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STOUT_VERSION_HPP__ +#define __STOUT_VERSION_HPP__ + +#include <ostream> + +#include <stout/error.hpp> +#include <stout/numify.hpp> +#include <stout/stringify.hpp> +#include <stout/strings.hpp> +#include <stout/try.hpp> + +// This class provides convenience routines for version checks. +// TODO(karya): Consider adding support for more than 3 components, +// and compatibility operators. +class Version +{ +public: + // Expect the string in the following format: + // <major>[.<minor>[.<patch>]] + // Missing components are treated as zero. + static Try<Version> parse(const std::string& s) + { + const int maxComponents = 3; + + std::vector<std::string> split = strings::split(s, "."); + + if (split.size() > maxComponents) { + return Error("Version string has " + stringify(split.size()) + + " components; maximum " + stringify(maxComponents) + + " components allowed"); + } + + int components[maxComponents] = {0}; + + for (size_t i = 0; i < split.size(); i++) { + Try<int> result = numify<int>(split[i]); + if (result.isError()) { + return Error("Invalid version component '" + split[i] + "': " + + result.error()); + } + components[i] = result.get(); + } + + return Version(components[0], components[1], components[2]); + } + + Version(int major, int minor, int patch) + : major_(major), minor_(minor), patch_(patch) {} + + bool operator == (const Version &other) const + { + return major_ == other.major_ && + minor_ == other.minor_ && + patch_ == other.patch_; + } + + bool operator != (const Version &other) const + { + return !(*this == other); + } + + bool operator < (const Version &other) const + { + // Lexicographic ordering. + if (major_ != other.major_) { + return major_ < other.major_; + } else if (minor_ != other.minor_) { + return minor_ < other.minor_; + } else { + return patch_ < other.patch_; + } + } + + bool operator > (const Version &other) const + { + // Lexicographic ordering. + if (major_ != other.major_) { + return major_ > other.major_; + } else if (minor_ != other.minor_) { + return minor_ > other.minor_; + } else { + return patch_ > other.patch_; + } + } + + bool operator <= (const Version &other) const + { + return *this < other || *this == other; + } + + bool operator >= (const Version &other) const + { + return *this > other || *this == other; + } + + friend inline std::ostream& operator << (std::ostream& s, const Version& v); + +private: + const int major_; + const int minor_; + const int patch_; +}; + + +inline std::ostream& operator << (std::ostream& s, const Version& v) +{ + return s << v.major_ << "." << v.minor_ << "." << v.patch_; +} + +#endif // __STOUT_VERSION_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/9b589560/3rdparty/libprocess/3rdparty/stout/tests/version_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/version_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/version_tests.cpp new file mode 100644 index 0000000..9f53431 --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/tests/version_tests.cpp @@ -0,0 +1,60 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stout/gtest.hpp> +#include <stout/stringify.hpp> +#include <stout/version.hpp> + + +// Verify version comparison operations. +TEST(VersionTest, Comparison) +{ + Version version1(0, 10, 4); + Version version2(0, 20, 3); + Try<Version> version3 = Version::parse("0.20.3"); + + EXPECT_EQ(version2, version3.get()); + EXPECT_NE(version1, version2); + EXPECT_LT(version1, version2); + EXPECT_LE(version1, version2); + EXPECT_LE(version2, version3.get()); + EXPECT_GT(version2, version1); + EXPECT_GE(version2, version1); + EXPECT_GE(version3.get(), version1); + + EXPECT_EQ(stringify(version2), "0.20.3"); +} + + +// Verify version parser. +TEST(VersionTest, Parse) +{ + Try<Version> version1 = Version::parse("1.20.3"); + Try<Version> version2 = Version::parse("1.20"); + Try<Version> version3 = Version::parse("1"); + + EXPECT_GT(version1.get(), version2.get()); + EXPECT_GT(version2.get(), version3.get()); + EXPECT_EQ(stringify(version2.get()), "1.20.0"); + EXPECT_EQ(stringify(version3.get()), "1.0.0"); + + EXPECT_ERROR(Version::parse("0.a.b")); + EXPECT_ERROR(Version::parse("")); + EXPECT_ERROR(Version::parse("a")); + EXPECT_ERROR(Version::parse("1.")); + EXPECT_ERROR(Version::parse(".1.2")); + EXPECT_ERROR(Version::parse("0.1.2.3")); +}
