File: facility_tests-fd.adb

package info (click to toggle)
libalog 0.6.2-7.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 784 kB
  • sloc: ada: 4,353; makefile: 99; sh: 18; ansic: 5
file content (385 lines) | stat: -rw-r--r-- 12,789 bytes parent folder | download | duplicates (3)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
--
--  Copyright (c) 2008-2009,
--  Reto Buerki, Adrian-Ken Rueegsegger
--
--  This file is part of Alog.
--
--  Alog is free software; you can redistribute it and/or modify
--  it under the terms of the GNU Lesser General Public License as published
--  by the Free Software Foundation; either version 2.1 of the License, or
--  (at your option) any later version.
--
--  Alog 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 Lesser General Public License for more details.
--
--  You should have received a copy of the GNU Lesser General Public License
--  along with Alog; if not, write to the Free Software
--  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
--  MA  02110-1301  USA
--

with Ada.Text_IO;
with Ada.Directories;

with Ahven; use Ahven;

with Alog.Helpers;
with Alog.Log_Request;
with Alog.Dst_Filter;
with Alog.Facilities.File_Descriptor;

package body Facility_Tests.FD is

   use Alog;
   use Alog.Log_Request;
   use Alog.Facilities;

   -------------------------------------------------------------------------

   procedure Dst_Loglevel_Handling is
      F        : File_Descriptor.Instance;
      F_Name   : constant String := "Dst_Facility";
      Testfile : constant String := "./obj/Dst_Loglevel_Fd";
      Reffile  : constant String := "./data/Dst_Loglevel_Fd.ref";
   begin
      F.Toggle_Write_Timestamp (State => False);
      F.Toggle_Write_Loglevel (State => True);
      F.Set_Logfile (Path => Testfile);

      F.Set_Name (Name => F_Name);

      Dst_Filter.Set_Loglevel (Name  => F_Name,
                               Level => Warning);

      F.Process
        (Request => Create
           (Level   => Warning,
            Message => "Testmessage"));
      F.Process
        (Request => Create
           (Level   => Info,
            Source  => "Test",
            Message => "Testmessage"));

      F.Close_Logfile;
      Dst_Filter.Reset;

      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "files not equal");

      Ada.Directories.Delete_File (Name => Testfile);
   end Dst_Loglevel_Handling;

   -------------------------------------------------------------------------

   procedure Initialize (T : in out Testcase) is
   begin
      T.Set_Name (Name => "Tests for FD Facility");
      T.Add_Test_Routine
        (Routine => Set_Valid_Logfile_Fd'Access,
         Name    => "set valid logfile");
      T.Add_Test_Routine
        (Routine => Set_Invalid_Logfile_Fd'Access,
         Name    => "set invalid logfile");
      T.Add_Test_Routine
        (Routine => Set_Stderr'Access,
         Name    => "set standard error as logfile");
      T.Add_Test_Routine
        (Routine => Write_Message_Fd'Access,
         Name    => "log a fd message");
      T.Add_Test_Routine
        (Routine => Teardown_Fd'Access,
         Name    => "teardown fd facility");
      T.Add_Test_Routine
        (Routine => Toggle_Write_Timestamp_Fd'Access,
         Name    => "toggle fd timestamp writing");
      T.Add_Test_Routine
        (Routine => Toggle_Write_Loglevel_Fd'Access,
         Name    => "toggle fd loglevel writing");
      T.Add_Test_Routine
        (Routine => Toggle_Write_Source_Fd'Access,
         Name    => "toggle fd source writing");
      T.Add_Test_Routine
        (Routine => Trim_Loglevels_Fd'Access,
         Name    => "fd loglevel align");
      T.Add_Test_Routine
        (Routine => Verify_Append'Access,
         Name    => "append logic");
      T.Add_Test_Routine
        (Routine => Dst_Loglevel_Handling'Access,
         Name    => "destination loglevels");
   end Initialize;

   -------------------------------------------------------------------------

   procedure Set_Invalid_Logfile_Fd is
      F : File_Descriptor.Instance;
   begin
      F.Set_Logfile (Path => "./");
      Fail (Message => "expected Name_Error");

   exception
      when File_Descriptor.Open_File_Error =>
         null;
   end Set_Invalid_Logfile_Fd;

   -------------------------------------------------------------------------

   procedure Set_Stderr is
      use type Ada.Text_IO.File_Access;

      F : File_Descriptor.Instance;
   begin
      Assert (Condition => F.Get_Logfile /= Ada.Text_IO.Standard_Error,
              Message   => "logfile already set to stderr");
      F.Set_Log_Stderr;
      Assert (Condition => F.Get_Logfile = Ada.Text_IO.Standard_Error,
              Message   => "logfile not set to stderr");
   end Set_Stderr;

   -------------------------------------------------------------------------

   procedure Set_Valid_Logfile_Fd is
      use Ada.Text_IO;
      F : File_Descriptor.Instance;
   begin
      F.Set_Logfile (Path => "./obj/Set_Valid_Logfile");
      Assert (Condition => Is_Open (F.Get_Logfile.all),
              Message   => "could not set logfile!");
      F.Close_Logfile (Remove => True);
   end Set_Valid_Logfile_Fd;

   -------------------------------------------------------------------------

   procedure Teardown_Fd is
      use Ada.Text_IO;
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Teardown_Fd";
   begin
      F.Set_Logfile (Path => Testfile);
      Assert (Condition => Is_Open (File => F.Get_Logfile.all),
              Message   => "could not set logfile!");
      F.Teardown;
      Assert (Condition => not Is_Open (File => F.Get_Logfile.all),
              Message   => "logfile still open!");

      Ada.Directories.Delete_File (Name => Testfile);
   end Teardown_Fd;

   -------------------------------------------------------------------------

   procedure Toggle_Write_Loglevel_Fd is
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Toggle_Write_Loglevel_Fd";
      Reffile  : constant String := "./data/Toggle_Write_Loglevel_Fd.ref";
   begin
      F.Toggle_Write_Timestamp (State => False);
      F.Toggle_Write_Loglevel (State => False);
      F.Set_Logfile (Path => Testfile);
      F.Process
        (Request => Create
           (Level   => Info,
            Message => "This is a message without loglevel"));

      F.Close_Logfile;

      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "unable to disable");

      Ada.Directories.Delete_File (Name => Testfile);
   end Toggle_Write_Loglevel_Fd;

   -------------------------------------------------------------------------

   procedure Toggle_Write_Source_Fd is
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Toggle_Write_Source_Fd";
      Reffile  : constant String := "./data/Toggle_Write_Source_Fd.ref";
   begin
      F.Toggle_Write_Timestamp (State => False);
      F.Toggle_Write_Loglevel (State => True);
      F.Set_Logfile (Path => Testfile);

      F.Process
        (Request => Create
           (Level   => Warning,
            Message => "No source given"));
      F.Process
        (Request => Create
           (Level   => Info,
            Source  => "Test",
            Message => "Source 'Test'"));

      F.Toggle_Write_Source (State => False);

      F.Process
        (Request => Create
           (Level   => Info,
            Source  => "Test",
            Message => "Source 'Test', source writing disabled"));

      F.Close_Logfile;

      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "file mismatch");

      Ada.Directories.Delete_File (Name => Testfile);
   end Toggle_Write_Source_Fd;

   -------------------------------------------------------------------------

   procedure Toggle_Write_Timestamp_Fd is
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Toggle_Write_Timestamp_Fd";
      Reffile  : constant String := "./data/Toggle_Write_Timestamp_Fd.ref";
   begin
      F.Toggle_Write_Timestamp (State => False);
      F.Set_Logfile (Path => Testfile);
      F.Process
        (Request => Create
           (Level   => Info,
            Message => "This is a message without timestamp"));

      F.Close_Logfile;

      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "unable to disable");

      Ada.Directories.Delete_File (Name => Testfile);
   end Toggle_Write_Timestamp_Fd;

   -------------------------------------------------------------------------

   procedure Trim_Loglevels_Fd is
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Trim_Loglevels_Fd";
      Reffile  : constant String := "./data/Trim_Loglevels_Fd.ref";
   begin
      F.Toggle_Write_Timestamp (State => False);
      F.Toggle_Write_Loglevel (State => True);
      F.Set_Logfile (Path => Testfile);
      for Lvl in Alog.Log_Level loop
         F.Process
           (Request => Create
              (Level   => Lvl,
               Message => "Testmessage"));
      end loop;

      F.Close_Logfile;
      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "alignment incorrect");

      Ada.Directories.Delete_File (Name => Testfile);
   end Trim_Loglevels_Fd;

   -------------------------------------------------------------------------

   procedure Verify_Append is
      F1_Name : constant String := "Facility1";
      F2_Name : constant String := "Facility2";
   begin
      Append :
      declare
         F1       : File_Descriptor.Instance;
         F2       : File_Descriptor.Instance;
         Testfile : constant String := "./obj/Log_Append_Fd";
         Reffile  : constant String := "./data/Log_Append_Fd.ref";
      begin
         F1.Toggle_Write_Timestamp (State => False);
         F1.Set_Logfile (Path => Testfile);
         F1.Process
           (Request => Create
              (Level   => Info,
               Message => F1_Name));
         F1.Close_Logfile;

         F2.Toggle_Write_Timestamp (State => False);
         F2.Set_Logfile (Path => Testfile);
         F2.Process
           (Request => Create
              (Level   => Info,
               Message => F2_Name));
         F2.Close_Logfile;

         Assert (Condition => Helpers.Assert_Files_Equal
                 (Filename1 => Testfile,
                  Filename2 => Reffile),
                 Message   => "append does not work");

         Ada.Directories.Delete_File (Name => Testfile);
      end Append;

      Overwrite :
      declare
         F1       : File_Descriptor.Instance;
         F2       : File_Descriptor.Instance;
         Testfile : constant String := "./obj/Log_Overwrite_Fd";
         Reffile  : constant String := "./data/Log_Overwrite_Fd.ref";
      begin
         F1.Toggle_Write_Timestamp (State => False);
         F1.Set_Logfile (Path => Testfile);
         F1.Process
           (Request => Create
              (Level   => Info,
               Message => F1_Name));
         F1.Close_Logfile;

         F2.Toggle_Write_Timestamp (State => False);
         F2.Set_Logfile (Path   => Testfile,
                         Append => False);
         F2.Process
           (Request => Create
              (Level   => Info,
               Message => F2_Name));
         F2.Close_Logfile;

         Assert (Condition => Helpers.Assert_Files_Equal
                 (Filename1 => Testfile,
                  Filename2 => Reffile),
                 Message   => "overwrite does not work");

         Ada.Directories.Delete_File (Name => Testfile);
      end Overwrite;
   end Verify_Append;

   -------------------------------------------------------------------------

   procedure Write_Message_Fd is
      F        : File_Descriptor.Instance;
      Testfile : constant String := "./obj/Write_Message_Fd";
      Reffile  : constant String := "./data/Write_Message_Fd.ref";
   begin
      --  We have to disable timestamps, since its changing all
      --  the time :)
      F.Toggle_Write_Timestamp (State => False);

      --  Open logfile, write test message.
      F.Set_Logfile (Path => Testfile);
      F.Process
        (Request => Create
           (Level   => Info,
            Message => "This is a test log-message"));

      F.Close_Logfile;

      Assert (Condition => Helpers.Assert_Files_Equal
              (Filename1 => Reffile,
               Filename2 => Testfile),
              Message   => "files not equal");

      Ada.Directories.Delete_File (Name => Testfile);
   end Write_Message_Fd;

end Facility_Tests.FD;