~ximion/listaller/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* tests-updater.vala
 *
 * Copyright (C) 2012-2014 Matthias Klumpp <matthias@tenstral.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 * 	Matthias Klumpp <matthias@tenstral.net>
 */

using GLib;
using Gee;
using Listaller;

private string datadir;

void msg (string s) {
	stdout.printf (s + "\n");
}

void test_upd_message_cb (MessageItem item) {
	msg ("Received message:");
	msg (" " + item.to_string ());
	assert (item.mtype == MessageEnum.INFO);
}

void test_upd_error_code_cb (ErrorItem item) {
	msg ("Received error:");
	msg (" " + item.to_string ());
	// skip all permission-errors
	if (item.error != ErrorEnum.OPERATION_NOT_ALLOWED)
		error (item.details);
}

void print_app_arraylist (ArrayList<AppItem> appList, string label = "") {
	if (label != "")
		msg ("Application List [%s]:".printf (label));
	else
		msg ("Application List:");

	foreach (AppItem app in appList) {
		stdout.printf ("%s\n", app.to_string ());
	}
	msg ("END");
}

private string foobar_fake_config_ipath;

void test_foobar_installation () {
	bool ret;
	string ipkfilename = Path.build_filename (datadir, "FooBar-1.0_%s.ipk".printf (Utils.system_machine_generic ()), null);

	Setup setup = new Setup (ipkfilename);
	setup.error_code.connect (test_upd_error_code_cb);
	setup.message.connect (test_upd_message_cb);

	ret = setup.initialize ();
	assert (ret == true);

	ret = setup.set_install_mode (IPK.InstallMode.PRIVATE);
	assert (ret == true);

	ret = setup.run_installation ();
	assert (ret == true);

	AppItem? app = setup.get_current_application ();
	assert (app != null);

	var vs = new VarSolver (app.unique_id);
	foobar_fake_config_ipath = vs.substitute_vars_auto ("%INST%/foo_testconf.conf", setup.settings);
}

void test_manager_installed_apps () {
	Manager mgr = new Manager ();
	mgr.settings.current_mode = IPK.InstallMode.PRIVATE;

	ArrayList<AppItem> app_list;
	mgr.filter_applications (AppState.INSTALLED_SHARED | AppState.INSTALLED_PRIVATE, out app_list);
	print_app_arraylist (app_list, "Installed apps (pre-update)");
	// we should have exactly 1 app installed (FooBar)
	assert (app_list.size == 1);

	// test version
	assert (app_list[0].version == "1.0");

	// test example file
	var fake_conf = new KeyFile ();
	fake_conf.load_from_file (foobar_fake_config_ipath, KeyFileFlags.NONE);
	string str = fake_conf.get_string ("Foobar", "Version");

	assert (str == "1.0");
}

void test_refresh_repo_cache () {
	// refresh app cache
	Repo.Manager repomgr = new Repo.Manager ();
	repomgr.refresh_cache ();

	// check new app info
	Manager mgr = new Manager ();
	mgr.settings.current_mode = IPK.InstallMode.PRIVATE;

	ArrayList<AppItem> app_list;
	mgr.filter_applications (AppState.AVAILABLE, out app_list);
	print_app_arraylist (app_list, "Available apps");
	// we should now have more than one app
	assert (app_list.size > 1);
}

void test_updater_update () {
	Updater upd = new Updater (false);
	upd.settings.current_mode = IPK.InstallMode.PRIVATE;
	upd.find_updates ();

	message ("Found updates: %i", upd.available_updates.size);
	assert (upd.available_updates.size >= 1);

	upd.apply_updates_all ();

	foreach (UpdateItem item in upd.available_updates)
		assert (item.completed == true);

	upd.find_updates ();
	assert (upd.available_updates.size == 0);

	// now test installed apps *after* update (version should have been increased)
	Manager mgr = new Manager ();
	mgr.settings.current_mode = IPK.InstallMode.PRIVATE;

	ArrayList<AppItem> app_list;
	mgr.filter_applications (AppState.INSTALLED_SHARED | AppState.INSTALLED_PRIVATE, out app_list);
	print_app_arraylist (app_list, "Installed apps (post-update)");
	// we should still have exactly 1 app installed
	assert (app_list.size == 1);

	// test version
	assert (app_list[0].version == "1.2");

	// test example file
	var fake_conf = new KeyFile ();
	fake_conf.load_from_file (foobar_fake_config_ipath, KeyFileFlags.NONE);
	string str = fake_conf.get_string ("Foobar", "Version");

	assert (str == "1.2");
}


int main (string[] args) {
	msg ("=== Running Updater Tests ===");
	datadir = args[1];
	assert (datadir != null);
	datadir = Path.build_filename (datadir, "testdata", null);
	assert (FileUtils.test (datadir, FileTest.EXISTS) != false);

	var tenv = new TestEnvironment ("updater");
	tenv.init (ref args);
	tenv.create_environment ();
	tenv.listaller_set_unittestmode (true);

	// prepare updater environment
	test_foobar_installation ();
	test_manager_installed_apps ();

	// perform updater tests
	test_refresh_repo_cache ();
	test_updater_update ();

	tenv.run ();

	return 0;
}