File: Manager.pm

package info (click to toggle)
libhostfile-manager-perl 0.09-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 462; makefile: 5; sh: 1
file content (356 lines) | stat: -rw-r--r-- 11,058 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
package Test::Hostfile::Manager;

use strict;
use warnings;
use Test::Most;
use Test::NoWarnings qw/had_no_warnings/;
use File::Slurp;
use base 'Test::Class';

sub class { 'Hostfile::Manager'; }

sub startup : Tests(startup => 1) {
    my $test = shift;
    use_ok $test->class;
}

sub constructor : Tests(3) {
    my $test  = shift;
    my $class = $test->class;
    can_ok $class, 'new';
    ok my $manager = $class->new, '... and the constructor should succeed';
    isa_ok $manager, $class, '... and the object it returns';
}

sub path_prefix : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $default_prefix = '/etc/hostfiles/';
    my $new_prefix     = '/etc/hostfiles/2/';

    can_ok $manager,    'path_prefix';
    is $default_prefix, $manager->path_prefix,
      '... and path_prefix should start out with default value';

    $manager->path_prefix($new_prefix);
    is $manager->path_prefix, $new_prefix,
      '... and setting its value should succeed';
}

sub hostfile_path : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $default_hostfile_path = '/etc/hosts';
    my $new_hostfile_path     = '/etc/hosts2';

    can_ok $manager,           'hostfile_path';
    is $default_hostfile_path, $manager->hostfile_path,
      '... and hostfile_path should start out with default value';

    $manager->hostfile_path($new_hostfile_path);
    is $manager->hostfile_path, $new_hostfile_path,
      '... and setting its value should succeed';
}

sub hostfile : Tests(4) {
    my $test = shift;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    my $manager = $test->class->new( hostfile_path => $file );

    can_ok $manager, 'hostfile';
    is $content,     $manager->hostfile,
      '... and hostfile should start out with content of file at hostfile_path';
    throws_ok { $manager->hostfile('foobar') } qr/^Cannot assign a value/,
      '... and settings its value should NOT succeed';
    is $content, $manager->hostfile,
      '... and settings its value did not succeed';
}

sub hostfile_is_lazy : Tests(2) {
    my $test = shift;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    my $manager = $test->class->new( hostfile_path => 'non_existent' );
    $manager->hostfile_path($file);

    can_ok $manager, 'hostfile';
    is $content,     $manager->hostfile,
'... and hostfile should start out with content of file at hostfile_path, even when constructed with a different hostfile_path';
}

sub hostfile_cannot_be_set_in_constructor : Tests(1) {
    my $test = shift;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    my $manager = $test->class->new(
        hostfile_path => $file,
        hostfile      => 'this should be ignored'
    );

    is $content, $manager->hostfile,
      'hostfile should start out with content of file at hostfile_path';
}

sub load_hostfile : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    can_ok $manager, 'load_hostfile';
    ok $manager->load_hostfile($file),
      '... and load_hostfile indicates success';
    is $content, $manager->hostfile,
      '... and load_hostfile actually loaded the file';
}

sub load_hostfile_uses_hostfile_path : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);
    $manager->hostfile_path($file);

    can_ok $manager, 'load_hostfile';
    ok $manager->load_hostfile, '... and load_hostfile indicates success';
    is $content, $manager->hostfile,
      '... and load_hostfile actually loaded the file';
}

sub load_hostfile_requires_hostfile_existence : Tests(2) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $file = 't/fixtures/hosts/non_existent';

    can_ok $manager, 'load_hostfile';
    throws_ok { $manager->load_hostfile($file) } qr/^Hostfile must exist/,
      '... and load_hostfile chokes when hostfile missing';
}

sub get_fragment : Tests(2) {
    my $test = shift;

    my $hostfile = 't/fixtures/hosts/1';
    my $prefix   = 't/fixtures/fragments/';
    my $fragment = 'f1';

    my $manager =
      $test->class->new( path_prefix => $prefix, hostfile_path => $hostfile );

    can_ok $manager, 'get_fragment';
    is read_file( $prefix . $fragment ), $manager->get_fragment($fragment),
      '... and get_fragment returns fragment content';
}

sub get_fragment_returns_undef_when_fragment_missing : Tests(2) {
    my $test = shift;

    my $hostfile = 't/fixtures/hosts/1';
    my $prefix   = 't/fixtures/fragments/';
    my $fragment = 'non_existent';

    my $manager =
      $test->class->new( path_prefix => $prefix, hostfile_path => $hostfile );

    can_ok $manager, 'get_fragment';
    is undef, $manager->get_fragment($fragment),
      '... and get_fragment undef when fragment file missing';
}

sub block : Tests(2) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $fragment_name = 'f1';
    my $block_regexp =
qr/#+\s*BEGIN: $fragment_name[\r\n](.*)#+\s*END: $fragment_name[\r\n]/ms;

    can_ok $manager,  'block';
    is $manager->block($fragment_name), $block_regexp;
}

sub write_hostfile : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    $manager->load_hostfile($file);

    can_ok $manager, 'write_hostfile';

    my $file2 = 't/fixtures/hosts/write_test';
    unlink($file2);

    $manager->hostfile_path($file2);
    ok $manager->write_hostfile, '... and write_hostfile returns ok';
    is $content, read_file($file2), "... and hostfile written to $file2";

    unlink($file2);
}

sub write_hostfile_requires_writable : Tests(3) {
    my $test    = shift;
    my $manager = $test->class->new;

    my $file    = 't/fixtures/hosts/1';
    my $content = read_file($file);

    $manager->load_hostfile($file);

    can_ok $manager, 'write_hostfile';

    SKIP: {
        skip 'Cannot test writable requirements as root', 2 if ($< == 0);
        my $file2 = 't/fixtures/hosts/write_test';
        write_file( $file2, '' );
        chmod 0444, $file2;

        $manager->hostfile_path($file2);
        throws_ok { $manager->write_hostfile } qr/^Unable to write hostfile/,
            '... and write_hostfile chokes when trying to write to file without permissions';
        is '', read_file($file2), "... and hostfile NOT written to $file2";

        unlink($file2);
    }
}

sub fragment_enabled : Tests(3) {
    my $test = shift;

    my $path   = 't/fixtures/hosts/2';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'fragment_enabled';
    ok $manager->fragment_enabled('f1'),
      '... and fragment_enabled returns ok when fragment is indeed enabled';
    ok !$manager->fragment_enabled('f2'),
      '... and fragment_enabled returns not_ok when fragment is not enabled';
}

sub enable_fragment : Tests(3) {
    my $test = shift;

    my $path   = 't/fixtures/hosts/1';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'enable_fragment';
    ok $manager->enable_fragment('f1'),
      '... and enable_fragment returns ok when fragment is newly enabled';
    ok $manager->fragment_enabled('f1'), '... and fragment is indeed enabled';
}

sub enable_fragment_does_not_leave_multiple_entries : Tests(4) {
    my $test = shift;

    my $path    = 't/fixtures/hosts/2';
    my $content = read_file($path);
    my $prefix  = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'enable_fragment';
    ok $manager->enable_fragment('f1'),
      '... and enable_fragment returns ok when fragment is newly enabled';
    ok $manager->fragment_enabled('f1'), '... and fragment is indeed enabled';
    is $manager->hostfile, $content, '... and fragment only appears once';
}

sub enable_fragment_does_not_warn_if_fragment_not_loaded : Tests(1) {
    my $test = shift;

    my $path   = 't/fixtures/hosts/2';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    had_no_warnings $manager->enable_fragment('non_existent'),
'... and enable_fragment does not complain excessively when enabling missing fragment';
}

sub disable_fragment : Tests(4) {
    my $test = shift;

    my $path   = 't/fixtures/hosts/2';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'disable_fragment';
    ok $manager->fragment_enabled('f1'),
      '... and fragment_enabled returns ok when fragment is indeed enabled';
    ok $manager->disable_fragment('f1'),
      '... and disable_fragment returns ok when fragment is newly disabled';
    ok !$manager->fragment_enabled('f1'),
      '... and fragment is indeed disabled';
}

sub fragment_list : Tests(2) {
    my $test = shift;

    my $prefix    = 't/fixtures/fragments/';
    my @fragments = ('f1', 'f1a');
    my $manager   = $test->class->new( path_prefix => $prefix );

    can_ok $manager, 'fragment_list';
    my @fl = $manager->fragment_list;
    is_deeply \@fl, \@fragments,
      '... and fragment list matches expectation';
}

sub toggle_fragment : Tests(6) {
    my $test = shift;

    my $path   = 't/fixtures/hosts/2';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'toggle_fragment';
    ok $manager->fragment_enabled('f1'),
      '... and fragment_enabled returns ok when fragment is enabled';
    ok $manager->toggle_fragment('f1'),
      '... and toggle_fragment returns ok when fragment is newly toggled';
    ok !$manager->fragment_enabled('f1'),
      '... and fragment is disabled';
    ok $manager->toggle_fragment('f1'),
      '... and toggle_fragment returns ok when fragment is newly toggled';
    ok $manager->fragment_enabled('f1'),
      '... and fragment is enabled';
}

sub fragment_status_flag : Tests(4) {
    my $test = shift;

    my $path = 't/fixtures/hosts/3';
    my $prefix = 't/fixtures/fragments/';
    my $manager =
      $test->class->new( hostfile_path => $path, path_prefix => $prefix );

    can_ok $manager, 'fragment_status_flag';
    is $manager->fragment_status_flag('f1'), '+',
      '... and fragment_status_flag returns \'+\' when fragment is enabled and unmodified';
    is $manager->fragment_status_flag('f1a'), '*',
      '... and fragment_status_flag returns \'*\' when fragment is enabled and modified';
    is $manager->fragment_status_flag('f2'), ' ',
      '... and fragment_status_flag returns \' \' when fragment is disabled';
}

1;