~ted/ubuntu-app-launch/snap-icon-unbreak

« back to all changes in this revision

Viewing changes to tests/failure-test.cc

Updated to failure-is-an-option

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 *     Ted Gould <ted.gould@canonical.com>
 
18
 */
 
19
 
 
20
#include <gtest/gtest.h>
 
21
#include <glib/gstdio.h>
 
22
#include <gio/gio.h>
 
23
#include <upstart-app-launch.h>
 
24
 
 
25
class FailureTest : public ::testing::Test
 
26
{
 
27
        private:
 
28
                GTestDBus * testbus = NULL;
 
29
 
 
30
        protected:
 
31
                virtual void SetUp() {
 
32
                        testbus = g_test_dbus_new(G_TEST_DBUS_NONE);
 
33
                        g_test_dbus_up(testbus);
 
34
                }
 
35
 
 
36
                virtual void TearDown() {
 
37
                        g_test_dbus_down(testbus);
 
38
                        g_clear_object(&testbus);
 
39
                        return;
 
40
                }
 
41
 
 
42
                static gboolean pause_helper (gpointer pmainloop) {
 
43
                        g_main_loop_quit(static_cast<GMainLoop *>(pmainloop));
 
44
                        return G_SOURCE_REMOVE;
 
45
                }
 
46
 
 
47
                void pause (guint time) {
 
48
                        if (time > 0) {
 
49
                                GMainLoop * mainloop = g_main_loop_new(NULL, FALSE);
 
50
                                g_timeout_add(time, pause_helper, mainloop);
 
51
 
 
52
                                g_main_loop_run(mainloop);
 
53
 
 
54
                                g_main_loop_unref(mainloop);
 
55
                        }
 
56
 
 
57
                        while (g_main_pending()) {
 
58
                                g_main_iteration(TRUE);
 
59
                        }
 
60
                }
 
61
};
 
62
 
 
63
static void
 
64
failed_observer (const gchar * appid, upstart_app_launch_app_failed_t reason, gpointer user_data)
 
65
{
 
66
        if (reason == UPSTART_APP_LAUNCH_APP_FAILED_CRASH) {
 
67
                std::string * last = static_cast<std::string *>(user_data);
 
68
                *last = appid;
 
69
        }
 
70
        return;
 
71
}
 
72
 
 
73
TEST_F(FailureTest, CrashTest)
 
74
{
 
75
        g_setenv("EXIT_STATUS", "-100", TRUE);
 
76
        g_setenv("JOB", "application-click", TRUE);
 
77
        g_setenv("INSTANCE", "foo", TRUE);
 
78
 
 
79
        std::string last_observer;
 
80
        ASSERT_TRUE(upstart_app_launch_observer_add_app_failed(failed_observer, &last_observer));
 
81
 
 
82
        /* Status based */
 
83
        ASSERT_TRUE(g_spawn_command_line_sync(APP_FAILED_TOOL, NULL, NULL, NULL, NULL));
 
84
        pause(100);
 
85
 
 
86
        EXPECT_EQ("foo", last_observer);
 
87
 
 
88
        last_observer.clear();
 
89
        g_unsetenv("EXIT_STATUS");
 
90
        g_setenv("EXIT_SIGNAL", "KILL", TRUE);
 
91
 
 
92
        /* Signal based */
 
93
        ASSERT_TRUE(g_spawn_command_line_sync(APP_FAILED_TOOL, NULL, NULL, NULL, NULL));
 
94
        pause(100);
 
95
 
 
96
        EXPECT_EQ("foo", last_observer);
 
97
 
 
98
        ASSERT_TRUE(upstart_app_launch_observer_delete_app_failed(failed_observer, &last_observer));
 
99
 
 
100
        return;
 
101
}
 
102
 
 
103
TEST_F(FailureTest, LegacyTest)
 
104
{
 
105
        g_setenv("EXIT_STATUS", "-100", TRUE);
 
106
        g_setenv("JOB", "application-legacy", TRUE);
 
107
        g_setenv("INSTANCE", "foo-1234", TRUE);
 
108
 
 
109
        std::string last_observer;
 
110
        ASSERT_TRUE(upstart_app_launch_observer_add_app_failed(failed_observer, &last_observer));
 
111
 
 
112
        /* Status based */
 
113
        ASSERT_TRUE(g_spawn_command_line_sync(APP_FAILED_TOOL, NULL, NULL, NULL, NULL));
 
114
        pause(100);
 
115
 
 
116
        EXPECT_EQ("foo", last_observer);
 
117
 
 
118
        ASSERT_TRUE(upstart_app_launch_observer_delete_app_failed(failed_observer, &last_observer));
 
119
 
 
120
        return;
 
121
}
 
122
 
 
123
static void
 
124
failed_start_observer (const gchar * appid, upstart_app_launch_app_failed_t reason, gpointer user_data)
 
125
{
 
126
        if (reason == UPSTART_APP_LAUNCH_APP_FAILED_START_FAILURE) {
 
127
                std::string * last = static_cast<std::string *>(user_data);
 
128
                *last = appid;
 
129
        }
 
130
        return;
 
131
}
 
132
 
 
133
TEST_F(FailureTest, StartTest)
 
134
{
 
135
        g_setenv("JOB", "application-click", TRUE);
 
136
        g_setenv("INSTANCE", "foo", TRUE);
 
137
        g_unsetenv("EXIT_STATUS");
 
138
        g_unsetenv("EXIT_SIGNAL");
 
139
 
 
140
        std::string last_observer;
 
141
        ASSERT_TRUE(upstart_app_launch_observer_add_app_failed(failed_start_observer, &last_observer));
 
142
 
 
143
        ASSERT_TRUE(g_spawn_command_line_sync(APP_FAILED_TOOL, NULL, NULL, NULL, NULL));
 
144
        pause(100);
 
145
 
 
146
        EXPECT_EQ("foo", last_observer);
 
147
 
 
148
        ASSERT_TRUE(upstart_app_launch_observer_delete_app_failed(failed_start_observer, &last_observer));
 
149
 
 
150
        return;
 
151
}