From 29b5c4da1e44534352d29536bb6ad1c721406b8a Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Thu, 3 Sep 2015 10:54:20 +0100 Subject: Avoid nested GMainLoops by reading from the file on startup Restore some tests for this functionality. --- tests/test-timezone-timedated.cpp | 100 ++++++++++++++++++++++++++++++++------ tests/timedated-fixture.h | 3 +- 2 files changed, 85 insertions(+), 18 deletions(-) (limited to 'tests') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index 7086e96..5cfc311 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -22,43 +22,99 @@ #include -#include // fopen() -//#include // chmod() -#include // sync() - using unity::indicator::datetime::TimedatedTimezone; +/*** +**** +***/ + +#define TIMEZONE_FILE (SANDBOX"/timezone") + +class TimezoneFixture: public TimedateFixture +{ + private: + + typedef TimedateFixture super; + + protected: + + void SetUp() override + { + super::SetUp(); + } + + void TearDown() override + { + super::TearDown(); + } + + public: + + /* convenience func to set the timezone file */ + void set_file(const std::string& text) + { + g_debug("set_file %s %s", TIMEZONE_FILE, text.c_str()); + auto fp = fopen(TIMEZONE_FILE, "w+"); + fprintf(fp, "%s\n", text.c_str()); + fclose(fp); + sync(); + } +}; + /** - * Test that timezone-file picks up the initial value + * Test that timezone-timedated warns, but doesn't crash, if the timezone file doesn't exist */ -TEST_F(TimedateFixture, InitialValue) +TEST_F(TimezoneFixture, NoFile) { - const std::string expected_timezone = "America/Chicago"; - set_timezone(expected_timezone); - TimedatedTimezone tz; + remove(TIMEZONE_FILE); + ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + + TimedatedTimezone tz(TIMEZONE_FILE); + testLogCount(G_LOG_LEVEL_WARNING, 1); +} + +/** + * Test that timezone-timedated gives a default of UTC if the file doesn't exist + */ +TEST_F(TimezoneFixture, DefaultValueNoFile) +{ + const std::string expected_timezone = "Etc/Utc"; + remove(TIMEZONE_FILE); + ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + + TimedatedTimezone tz(TIMEZONE_FILE); ASSERT_EQ(expected_timezone, tz.timezone.get()); } +/** + * Test that timezone-timedated picks up the initial value + */ +TEST_F(TimezoneFixture, InitialValue) +{ + const std::string expected_timezone = "America/Chicago"; + set_file(expected_timezone); + TimedatedTimezone tz(TIMEZONE_FILE); +} + /** * Test that changing the tz after we are running works. */ -TEST_F(TimedateFixture, ChangedValue) +TEST_F(TimezoneFixture, ChangedValue) { const std::string initial_timezone = "America/Chicago"; const std::string changed_timezone = "America/New_York"; - GMainLoop *l = g_main_loop_new(nullptr, FALSE); - set_timezone(initial_timezone); + set_file(initial_timezone); - TimedatedTimezone tz; + TimedatedTimezone tz(TIMEZONE_FILE); ASSERT_EQ(initial_timezone, tz.timezone.get()); bool changed = false; tz.timezone.changed().connect( - [&changed, this, l](const std::string& s){ + [&changed, this](const std::string& s){ g_message("timezone changed to %s", s.c_str()); changed = true; - g_main_loop_quit(l); + g_main_loop_quit(loop); }); g_idle_add([](gpointer gself){ @@ -66,8 +122,20 @@ TEST_F(TimedateFixture, ChangedValue) return G_SOURCE_REMOVE; }, this); - g_main_loop_run(l); + g_main_loop_run(loop); ASSERT_TRUE(changed); ASSERT_EQ(changed_timezone, tz.timezone.get()); } + +/** + * Test that timezone-timedated picks up the initial value + */ +TEST_F(TimezoneFixture, IgnoreComments) +{ + const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000"; + const std::string expected_timezone = "Europe/Berlin"; + set_file(comment + "\n" + expected_timezone); + TimedatedTimezone tz(TIMEZONE_FILE); + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} diff --git a/tests/timedated-fixture.h b/tests/timedated-fixture.h index 0e89ea1..5ec425d 100644 --- a/tests/timedated-fixture.h +++ b/tests/timedated-fixture.h @@ -183,8 +183,6 @@ private: &local_error); g_assert_no_error (local_error); g_variant_unref(child); - - g_main_loop_quit(self->loop); } protected: @@ -215,6 +213,7 @@ protected: node_info = nullptr; object_register_id = 0; own_name = 0; + proxy = nullptr; // bring up the test bus bus = g_test_dbus_new(G_TEST_DBUS_NONE); -- cgit v1.2.3