From 53721ecc9f3d3d2c0b65b06d313d57e266ba31ad Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 8 Apr 2016 15:46:50 -0500 Subject: pull the timezone from timedate1 regardless of whether it appears on the bus before or after we startup --- tests/test-timezone-timedated.cpp | 184 +++++++++++++++++++++----------------- 1 file changed, 104 insertions(+), 80 deletions(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index 2fdec12..b293e59 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -1,9 +1,5 @@ - /* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr + * Copyright © 2014-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -16,127 +12,155 @@ * * You should have received a copy of the GNU General Public License along * with this program. If not, see . + * + * Authors: + * Charles Kerr + * Ted Gould */ -#include "timedated-fixture.h" +#include "glib-fixture.h" +#include #include -using ayatana::indicator::datetime::TimedatedTimezone; +#include -/*** -**** -***/ -#define TIMEZONE_FILE (SANDBOX"/timezone") +using namespace ayatana::indicator::datetime; + -class TimezoneFixture: public TimedateFixture +struct Timedate1Fixture: public GlibFixture { - private: +private: + + typedef GlibFixture super; - typedef TimedateFixture super; +protected: - protected: + GDBusConnection* m_bus {}; + GTestDBus* m_test_bus {}; void SetUp() override { - super::SetUp(); + super::SetUp(); + + // use a fake bus + m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE); + g_test_dbus_up(m_test_bus); + const char * address = g_test_dbus_get_bus_address(m_test_bus); + g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true); + g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true); + g_debug("test_dbus's address is %s", address); + + // get the bus + m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr); + g_dbus_connection_set_exit_on_close(m_bus, FALSE); + g_object_add_weak_pointer(G_OBJECT(m_bus), (gpointer*)&m_bus); } void TearDown() override { - super::TearDown(); + g_clear_object(&m_bus); + g_clear_object(&m_test_bus); + + super::TearDown(); } - public: + void start_timedate1(const std::string& tzid) + { + // start the store + auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str()); + const gchar* child_argv[] = { "python3", "-m", "dbusmock", "--template", "timedated", "--parameters", json_parameters, nullptr }; + GError* error = nullptr; + g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error); + g_assert_no_error(error); + g_free(json_parameters); + + // wait for it to appear on the bus + wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME); + } + + bool wait_for_tzid(Timezone& tz, const std::string& tzid) + { + return wait_for([&tz, &tzid](){return tzid == tz.timezone.get();}); + } - /* convenience func to set the timezone file */ - void set_file(const std::string& text) + void set_timedate1_timezone(const std::string& tzid) { - 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(); + GError* error {}; + auto v = g_dbus_connection_call_sync( + m_bus, + Bus::Timedate1::BUSNAME, + Bus::Timedate1::ADDR, + Bus::Timedate1::IFACE, + Bus::Timedate1::Methods::SET_TIMEZONE, + g_variant_new("(sb)", tzid.c_str(), FALSE), + nullptr, + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + &error); + g_clear_pointer(&v, g_variant_unref); + g_assert_no_error(error); } }; -/** - * Test that timezone-timedated warns, but doesn't crash, if the timezone file doesn't exist - */ -TEST_F(TimezoneFixture, NoFile) -{ - remove(TIMEZONE_FILE); - ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); +/*** +**** +***/ - expectLogMessage(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*No such file or directory*"); - TimedatedTimezone tz(TIMEZONE_FILE); +TEST_F(Timedate1Fixture, HelloWorld) +{ } /** - * Test that timezone-timedated gives a default of UTC if the file doesn't exist + * Test that we get a default timezone of UTC if timedate1 isn't available on the bus */ -TEST_F(TimezoneFixture, DefaultValueNoFile) +TEST_F(Timedate1Fixture, DefaultTimezone) { - const std::string expected_timezone = "Etc/Utc"; - remove(TIMEZONE_FILE); - ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + const std::string expected_tzid{"Etc/Utc"}; - expectLogMessage(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*No such file or directory*"); - TimedatedTimezone tz(TIMEZONE_FILE); - ASSERT_EQ(expected_timezone, tz.timezone.get()); + TimedatedTimezone tmp; + EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); } /** - * Test that timezone-timedated picks up the initial value + * Test that we get the right tzid if timedated shows up on the bus BEFORE we start */ -TEST_F(TimezoneFixture, InitialValue) +TEST_F(Timedate1Fixture, Timedate1First) { - const std::string expected_timezone = "America/Chicago"; - set_file(expected_timezone); - TimedatedTimezone tz(TIMEZONE_FILE); + const std::string expected_tzid{"America/Chicago"}; + + start_timedate1(expected_tzid); + TimedatedTimezone tmp; + EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); } /** - * Test that changing the tz after we are running works. + * Test that we get the right tzid if timedated shows up on the bus AFTER we start */ -TEST_F(TimezoneFixture, ChangedValue) +TEST_F(Timedate1Fixture, Timedate1Last) { - const std::string initial_timezone = "America/Chicago"; - const std::string changed_timezone = "America/New_York"; - - set_file(initial_timezone); + const std::string expected_tzid("America/Los_Angeles"); - TimedatedTimezone tz(TIMEZONE_FILE); - ASSERT_EQ(initial_timezone, tz.timezone.get()); - - bool changed = false; - tz.timezone.changed().connect( - [&changed, this](const std::string& s){ - g_message("timezone changed to %s", s.c_str()); - changed = true; - g_main_loop_quit(loop); - }); - - g_idle_add([](gpointer gself){ - static_cast(gself)->set_timezone("America/New_York"); - return G_SOURCE_REMOVE; - }, this); - - g_main_loop_run(loop); - - ASSERT_TRUE(changed); - ASSERT_EQ(changed_timezone, tz.timezone.get()); + TimedatedTimezone tmp; + start_timedate1(expected_tzid); + EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); } /** - * Test that timezone-timedated picks up the initial value + * Test that the timezone core::Property changes when timedate1's property changes */ -TEST_F(TimezoneFixture, IgnoreComments) +TEST_F(Timedate1Fixture, TimezoneChange) { - 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()); + const std::vector expected_tzids{"America/Los_Angeles", "America/Chicago", "Etc/Utc"}; + + TimedatedTimezone tmp; + start_timedate1("America/New_York"); + + for(const auto& expected_tzid : expected_tzids) + { + set_timedate1_timezone(expected_tzid); + EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); + } } -- cgit v1.2.3 From 33ea73976adc6a55bf8ae852758e03306755b4e0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 8 Apr 2016 15:59:41 -0500 Subject: in test-timezone-timedated, slightly cleaner mechanism for tzid waiting --- tests/test-timezone-timedated.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index b293e59..e9fff0f 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -80,9 +80,9 @@ protected: wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME); } - bool wait_for_tzid(Timezone& tz, const std::string& tzid) + bool wait_for_tzid(const std::string& tzid, Timezone& tz) { - return wait_for([&tz, &tzid](){return tzid == tz.timezone.get();}); + return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();}); } void set_timedate1_timezone(const std::string& tzid) @@ -105,6 +105,11 @@ protected: } }; +#define EXPECT_TZID(expected_tzid, tmp) \ + EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \ + << "expected " << expected_tzid \ + << " got " << tmp.timezone.get(); + /*** **** ***/ @@ -114,18 +119,18 @@ TEST_F(Timedate1Fixture, HelloWorld) } /** - * Test that we get a default timezone of UTC if timedate1 isn't available on the bus + * Test that the tzid is right if timedated isn't available */ TEST_F(Timedate1Fixture, DefaultTimezone) { const std::string expected_tzid{"Etc/Utc"}; TimedatedTimezone tmp; - EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); + EXPECT_TZID(expected_tzid, tmp); } /** - * Test that we get the right tzid if timedated shows up on the bus BEFORE we start + * Test that the tzid is right if timedated shows BEFORE we start */ TEST_F(Timedate1Fixture, Timedate1First) { @@ -133,11 +138,11 @@ TEST_F(Timedate1Fixture, Timedate1First) start_timedate1(expected_tzid); TimedatedTimezone tmp; - EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); + EXPECT_TZID(expected_tzid, tmp); } /** - * Test that we get the right tzid if timedated shows up on the bus AFTER we start + * Test that the tzid is right if timedated shows AFTER we start */ TEST_F(Timedate1Fixture, Timedate1Last) { @@ -145,11 +150,11 @@ TEST_F(Timedate1Fixture, Timedate1Last) TimedatedTimezone tmp; start_timedate1(expected_tzid); - EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); + EXPECT_TZID(expected_tzid, tmp); } /** - * Test that the timezone core::Property changes when timedate1's property changes + * Test that the tzid is right if timedated's property changes */ TEST_F(Timedate1Fixture, TimezoneChange) { @@ -161,6 +166,6 @@ TEST_F(Timedate1Fixture, TimezoneChange) for(const auto& expected_tzid : expected_tzids) { set_timedate1_timezone(expected_tzid); - EXPECT_TRUE(wait_for_tzid(tmp, expected_tzid)) << "expected " << expected_tzid << " got " << tmp.timezone.get(); + EXPECT_TZID(expected_tzid, tmp); } } -- cgit v1.2.3 From fc5d3e97e21ee13c24b3fc78cddc2d8db43d58bc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 8 Apr 2016 16:36:14 -0500 Subject: in tests/test-timezone-timedated, fix copy-paste error in comments --- tests/test-timezone-timedated.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index e9fff0f..e613806 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -68,7 +68,7 @@ protected: void start_timedate1(const std::string& tzid) { - // start the store + // start dbusmock with the timedated template auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str()); const gchar* child_argv[] = { "python3", "-m", "dbusmock", "--template", "timedated", "--parameters", json_parameters, nullptr }; GError* error = nullptr; -- cgit v1.2.3 From 907a22aaab79104150406e5d4de08aaeca0dc602 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 9 Apr 2016 19:05:39 -0500 Subject: in tests/, remove the handrolled timedate1 bus mock. It doesn't add anything over the dbusmock template --- tests/test-timezone-timedated.cpp | 98 +++------------------------------------ 1 file changed, 7 insertions(+), 91 deletions(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index e613806..8c56459 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -18,110 +18,26 @@ * Ted Gould */ -#include "glib-fixture.h" +#include "timedated-fixture.h" -#include #include -#include - - using namespace ayatana::indicator::datetime; - -struct Timedate1Fixture: public GlibFixture -{ -private: - - typedef GlibFixture super; - -protected: - - GDBusConnection* m_bus {}; - GTestDBus* m_test_bus {}; - - void SetUp() override - { - super::SetUp(); - - // use a fake bus - m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE); - g_test_dbus_up(m_test_bus); - const char * address = g_test_dbus_get_bus_address(m_test_bus); - g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true); - g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true); - g_debug("test_dbus's address is %s", address); - - // get the bus - m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr); - g_dbus_connection_set_exit_on_close(m_bus, FALSE); - g_object_add_weak_pointer(G_OBJECT(m_bus), (gpointer*)&m_bus); - } - - void TearDown() override - { - g_clear_object(&m_bus); - g_clear_object(&m_test_bus); - - super::TearDown(); - } - - void start_timedate1(const std::string& tzid) - { - // start dbusmock with the timedated template - auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str()); - const gchar* child_argv[] = { "python3", "-m", "dbusmock", "--template", "timedated", "--parameters", json_parameters, nullptr }; - GError* error = nullptr; - g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error); - g_assert_no_error(error); - g_free(json_parameters); - - // wait for it to appear on the bus - wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME); - } - - bool wait_for_tzid(const std::string& tzid, Timezone& tz) - { - return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();}); - } - - void set_timedate1_timezone(const std::string& tzid) - { - GError* error {}; - auto v = g_dbus_connection_call_sync( - m_bus, - Bus::Timedate1::BUSNAME, - Bus::Timedate1::ADDR, - Bus::Timedate1::IFACE, - Bus::Timedate1::Methods::SET_TIMEZONE, - g_variant_new("(sb)", tzid.c_str(), FALSE), - nullptr, - G_DBUS_CALL_FLAGS_NONE, - -1, - nullptr, - &error); - g_clear_pointer(&v, g_variant_unref); - g_assert_no_error(error); - } -}; - -#define EXPECT_TZID(expected_tzid, tmp) \ - EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \ - << "expected " << expected_tzid \ - << " got " << tmp.timezone.get(); +using TestTimedatedFixture = TimedatedFixture; /*** **** ***/ -TEST_F(Timedate1Fixture, HelloWorld) +TEST_F(TestTimedatedFixture, HelloWorld) { } /** * Test that the tzid is right if timedated isn't available */ -TEST_F(Timedate1Fixture, DefaultTimezone) +TEST_F(TestTimedatedFixture, DefaultTimezone) { const std::string expected_tzid{"Etc/Utc"}; @@ -132,7 +48,7 @@ TEST_F(Timedate1Fixture, DefaultTimezone) /** * Test that the tzid is right if timedated shows BEFORE we start */ -TEST_F(Timedate1Fixture, Timedate1First) +TEST_F(TestTimedatedFixture, Timedate1First) { const std::string expected_tzid{"America/Chicago"}; @@ -144,7 +60,7 @@ TEST_F(Timedate1Fixture, Timedate1First) /** * Test that the tzid is right if timedated shows AFTER we start */ -TEST_F(Timedate1Fixture, Timedate1Last) +TEST_F(TestTimedatedFixture, Timedate1Last) { const std::string expected_tzid("America/Los_Angeles"); @@ -156,7 +72,7 @@ TEST_F(Timedate1Fixture, Timedate1Last) /** * Test that the tzid is right if timedated's property changes */ -TEST_F(Timedate1Fixture, TimezoneChange) +TEST_F(TestTimedatedFixture, TimezoneChange) { const std::vector expected_tzids{"America/Los_Angeles", "America/Chicago", "Etc/Utc"}; -- cgit v1.2.3 From 7967430f1b1e11677baab5ebb941d738a54f198f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 12 Apr 2016 12:05:53 -0500 Subject: sync tests to new ctor arguments for TimedatedTimezone and LiveTimezones --- tests/test-timezone-timedated.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index 8c56459..c686c39 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -41,7 +41,7 @@ TEST_F(TestTimedatedFixture, DefaultTimezone) { const std::string expected_tzid{"Etc/Utc"}; - TimedatedTimezone tmp; + TimedatedTimezone tmp {m_bus}; EXPECT_TZID(expected_tzid, tmp); } @@ -53,7 +53,7 @@ TEST_F(TestTimedatedFixture, Timedate1First) const std::string expected_tzid{"America/Chicago"}; start_timedate1(expected_tzid); - TimedatedTimezone tmp; + TimedatedTimezone tmp {m_bus}; EXPECT_TZID(expected_tzid, tmp); } @@ -64,7 +64,7 @@ TEST_F(TestTimedatedFixture, Timedate1Last) { const std::string expected_tzid("America/Los_Angeles"); - TimedatedTimezone tmp; + TimedatedTimezone tmp {m_bus}; start_timedate1(expected_tzid); EXPECT_TZID(expected_tzid, tmp); } @@ -76,7 +76,7 @@ TEST_F(TestTimedatedFixture, TimezoneChange) { const std::vector expected_tzids{"America/Los_Angeles", "America/Chicago", "Etc/Utc"}; - TimedatedTimezone tmp; + TimedatedTimezone tmp {m_bus}; start_timedate1("America/New_York"); for(const auto& expected_tzid : expected_tzids) -- cgit v1.2.3