File: 06_stderr_stdout.t

package info (click to toggle)
libdaemon-control-perl 0.001010-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 312 kB
  • sloc: perl: 2,359; makefile: 2
file content (123 lines) | stat: -rw-r--r-- 3,855 bytes parent folder | download
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
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
use File::Temp;

my ( $file, $ilib );

# Let's make it so people can test in t/ or in the dist directory.
my $daemon = '05_stderr_stdout.pl';
if ( -f "t/bin/$daemon" ) { # Dist Directory.
    $file = "t/bin/$daemon";
    $ilib = "lib";
} elsif ( -f "bin/$daemon" ) {
    $file = "bin/$daemon";
    $ilib = "../lib";
} else {
    die "Tests should be run in the dist directory or t/";
}

sub get_command_output {
    my ( @command ) = @_;
    open my $lf, "-|", @command
        or die "Couldn't get pipe to '@command': $!";
    my $content = do { local $/; <$lf> };
    close $lf;
    return $content;
}

{
    diag 'Test STDOUT and STDERR when we use plain strings as arguments';
    my $out;
    my $stdout = File::Temp->new; # object stringifies to the filename
    my $stderr = File::Temp->new;
    my $cmd = "$^X -I$ilib $file $stdout $stderr";

    ok $out = get_command_output("$cmd start"), "Started perl daemon";
    like $out, qr/\[Started\]/, "Daemon started.";

    sleep 2; # chill out for a bit, or we might miss writes to files

    ok $out
    = get_command_output("$cmd status" ), "Get status of system daemon.";
    like $out, qr/\[Not Running\]/, "Daemon is stopped.";

    # Check data written by the daemon
    open my $fh, '<', $stdout
        or die "Failed to open stdout file ($stdout) for inspection: $!";
    like do { local $/; <$fh>; }, qr/STDOUT output success/,
        "STDOUT file contains expected data";

    open $fh, '<', $stderr
        or die "Failed to open stderr file ($stderr) for inspection: $!";

    is do { local $/; <$fh>; }, "STDERR output success\n",
        "STDERR file contains expected data";

}

{
    diag 'Test STDOUT and STDERR when we use custom arrayrefs as arguments';
    # We're passing 'custom' argument so our daemon knows to use arrayrefs
    # Consult the code of the daemon for details

    my $out;
    my $stdout = File::Temp->new; # object stringifies to the filename
    my $stderr = File::Temp->new;
    my $cmd = "$^X -I$ilib $file custom $stdout $stderr";

    ok $out = get_command_output("$cmd start"), "Started perl daemon";
    like $out, qr/\[Started\]/, "Daemon started.";

    sleep 2; # chill out for a bit, or we might miss writes to files

    ok $out
    = get_command_output("$cmd status" ), "Get status of system daemon.";
    like $out, qr/\[Not Running\]/, "Daemon is stopped.";


    # Check daemon's first write
    open my $fh, '<', $stdout
        or die "Failed to open stdout file ($stdout) for inspection: $!";
    like do { local $/; <$fh>; }, qr/STDOUT output success/,
        "STDOUT file contains expected data";

    open $fh, '<', $stderr
        or die "Failed to open stderr file ($stderr) for inspection: $!";

    is do { local $/; <$fh>; }, "STDERR output success\n",
        "STDERR file contains expected data";


    # Restart so we'd get a second STD[OUT|ERR] write
    ok $out
    = get_command_output("$cmd start"), "Get status of system daemon.";
    like $out, qr/\[Started\]/s, "Daemon restarted.";

    sleep 2; # chill out for a bit, or we might miss writes to files

    ok $out
    = get_command_output("$cmd status" ), "Get status of system daemon.";
    like $out, qr/\[Not Running\]/, "Daemon is stopped.";

    # Check daemon's second write
    open $fh, '<', $stdout
        or die "Failed to open stdout file ($stdout) for inspection: $!";
    like do { local $/; <$fh>; },
        qr/^STDOUT output success(?!.*STDOUT output success)/s,
        "STDOUT file contains expected data";

    open $fh, '<', $stderr
        or die "Failed to open stderr file ($stderr) for inspection: $!";

    like do { local $/; <$fh>; },
        qr/^STDERR output success(?!.*STDERR output success)/s,
        "STDERR file contains expected data";
}

unlink 'pid_tmp';

done_testing;