Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: init/tests/test_perf_log.c

Issue 524041: Changes to make upstart log performance stats to a file. (Closed)
Patch Set: More tab/space stuff Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « init/perf_log.c ('k') | make_pkg.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <nih/test.h>
6
7 #include <ctype.h>
8 #include <limits.h>
9
10 #include <nih/error.h>
11 #include <nih/file.h>
12
13 #include "perf_log.h"
14
15
16 static void
17 check_file_contents (const char *file,
18 const char *expected)
19 {
20 char *buf;
21 size_t len = 0;
22 buf = nih_file_read (NULL, file, &len);
23 if (! expected) {
24 NihError *err;
25 err = nih_error_get ();
26 TEST_EQ (err->number, ENOENT);
27 TEST_EQ (len, 0);
28 TEST_EQ_P (buf, NULL);
29 nih_free (err);
30 return;
31 }
32 TEST_NE_P (buf, NULL);
33 TEST_EQ (len, strlen(expected));
34 TEST_TRUE (strncmp(buf, expected, len) == 0);
35 nih_free (buf);
36 }
37
38
39 static void
40 create_test_file (const char *filename,
41 const char *contents)
42 {
43 FILE *fp;
44 fp = fopen(filename, "w");
45 TEST_NE_P (fp, NULL);
46 fwrite (contents, 1, strlen(contents), fp);
47 fclose (fp);
48 }
49
50
51 void
52 test_get_file_fields (void)
53 {
54 char test_file[PATH_MAX];
55 TEST_FUNCTION ("get_file_field");
56 TEST_FILENAME (test_file);
57 char **result;
58 int fields = 0;
59
60 TEST_FEATURE ("with NULL file");
61 TEST_ALLOC_FAIL {
62 result = get_file_fields (NULL, NULL, " ", &fields);
63 TEST_EQ_P (result, NULL);
64 TEST_EQ (fields, 0);
65 }
66
67 TEST_FEATURE ("with non-existent file");
68 TEST_ALLOC_FAIL {
69 result = get_file_fields (NULL, test_file, " ", &fields);
70 TEST_EQ_P (result, NULL);
71 TEST_EQ (fields, 0);
72 }
73
74 TEST_FEATURE ("regular space delimiter");
75 create_test_file (test_file, "0.1564 1234\n");
76 TEST_ALLOC_FAIL {
77 result = get_file_fields (NULL, test_file, " ", &fields);
78 if (! test_alloc_failed || result) {
79 /* if the allocation that failed is for the
80 file contents, allow NULL. */
81 TEST_EQ (fields, 2);
82 TEST_EQ_STR (result[0], "0.1564");
83 TEST_EQ_STR (result[1], "1234\n");
84 nih_free (result);
85 }
86 }
87
88 TEST_FEATURE ("repeated delimiters");
89 create_test_file (test_file, " 0.1564 1234\n");
90 result = get_file_fields (NULL, test_file, " \n", &fields);
91 TEST_EQ (fields, 2);
92 TEST_EQ_STR (result[0], "0.1564");
93 TEST_EQ_STR (result[1], "1234");
94 nih_free (result);
95
96 TEST_FEATURE ("non space delimiter");
97 create_test_file (test_file, "123,456");
98 result = get_file_fields (NULL, test_file, " ", &fields);
99 TEST_EQ (fields, 1);
100 TEST_EQ_STR (result[0], "123,456");
101 nih_free (result);
102 result = get_file_fields (NULL, test_file, ",", &fields);
103 TEST_EQ (fields, 2);
104 TEST_EQ_STR (result[0], "123");
105 TEST_EQ_STR (result[1], "456");
106 nih_free (result);
107
108 TEST_FEATURE ("read from special file");
109 result = get_file_fields (NULL, "/proc/uptime", " \n", &fields);
110 TEST_EQ (fields, 2);
111 TEST_TRUE (isdigit (result[0][0]));
112 TEST_TRUE (isdigit (result[1][0]));
113 nih_free (result);
114 }
115
116
117 void
118 test_perf_log_message (void)
119 {
120 char log_file[PATH_MAX];
121 char uptime_file[PATH_MAX];
122 char diskstats_file[PATH_MAX];
123 TEST_FUNCTION ("perf_log_message");
124 TEST_FILENAME (log_file);
125 TEST_FILENAME (uptime_file);
126 TEST_FILENAME (diskstats_file);
127
128 create_test_file(uptime_file, "a1 b\n");
129 create_test_file(diskstats_file, "a b c1 d e f g\n");
130
131 /* By setting a NULL log_file, we accumulate the log messages. */
132 perf_log_set_files (uptime_file, diskstats_file, NULL);
133
134 perf_log_message ("test %d\n", 1);
135
136 create_test_file (uptime_file, "a2 b\n");
137 create_test_file (diskstats_file, "a b c2 d e f g\n");
138
139 perf_log_message ("test %d\n", 2);
140
141 check_file_contents (log_file, NULL);
142
143 /* Set the log_file, which flushes. */
144 perf_log_set_files (uptime_file, diskstats_file, log_file);
145
146 check_file_contents (log_file, "a1 c1 test 1\na2 c2 test 2\n");
147
148 create_test_file (uptime_file, "a3 b\n");
149 create_test_file (diskstats_file, "a b c3 d e f g\n");
150
151 perf_log_message ("test %d\n", 3);
152
153 check_file_contents (log_file,
154 "a1 c1 test 1\na2 c2 test 2\na3 c3 test 3\n");
155
156 TEST_ALLOC_FAIL {
157 perf_log_message ("test");
158 }
159
160 /* Clear the log file. */
161 create_test_file (log_file, "");
162 TEST_FEATURE ("with bad input files");
163
164 create_test_file (uptime_file, "\n");
165 create_test_file (diskstats_file, "a b c\n");
166 perf_log_message ("test bad uptime\n");
167
168 check_file_contents (log_file,
169 "- c test bad uptime\n");
170
171 create_test_file (uptime_file, "a b\n");
172 create_test_file (diskstats_file, "a b\n");
173 perf_log_message ("test bad diskstats\n");
174
175 check_file_contents (log_file,
176 "- c test bad uptime\na - test bad diskstats\n");
177
178 TEST_FEATURE ("with unwritable output");
179 unlink (log_file);
180 TEST_EQ (mkdir (log_file, 0777), 0);
181 perf_log_message ("Cannot be written\n");
182 rmdir (log_file);
183 }
184
185
186 int
187 main (int argc,
188 char *argv[])
189 {
190 test_get_file_fields ();
191 test_perf_log_message ();
192
193 return 0;
194 }
OLDNEW
« no previous file with comments | « init/perf_log.c ('k') | make_pkg.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698