apt source loading and tests for Packages::Apt probe
Robert 'phaylon' Sedlacek [Fri, 15 Jun 2012 02:28:34 +0000 (02:28 +0000)]
lib/System/Introspector/Probe/Packages/Apt.pm
t/data/apt/sources.list [new file with mode: 0644]
t/data/apt/sources.list.d/other.list [new file with mode: 0644]
t/packages-apt.t

index 2b8ccb4..5d5ecf2 100644 (file)
@@ -1,16 +1,24 @@
 package System::Introspector::Probe::Packages::Apt;
 use Moo;
+use File::Basename;
 
 use System::Introspector::Util qw(
     handle_from_command
     transform_exceptions
+    output_from_file
+    files_from_dir
 );
 
 has apt_lists_dir => (is => 'ro', builder => 1);
 has apt_update_after => (is => 'ro', default => sub { 86400 });
 has apt_update => (is => 'ro');
 
-sub _build_apt_lists_dir { '/var/lib/apt/lists' }
+has apt_sources => (is => 'ro', builder => 1);
+has apt_sources_dir => (is => 'ro', builder => 1);
+
+sub _build_apt_lists_dir   { '/var/lib/apt/lists' }
+sub _build_apt_sources     { '/etc/apt/sources.list' }
+sub _build_apt_sources_dir { '/etc/apt/sources.list.d' }
 
 sub gather {
     my ($self) = @_;
@@ -27,6 +35,9 @@ sub gather {
         upgradable => transform_exceptions {
             return { actions => $self->_gather_upgradable };
         },
+        sources => transform_exceptions {
+            return { config => $self->_gather_sources };
+        },
     };
 }
 
@@ -55,6 +66,31 @@ sub _open_apt_get_upgrade_simulation_pipe {
     return handle_from_command 'apt-get -s upgrade';
 }
 
+sub _gather_sources {
+    my ($self) = @_;
+    my $sources_dir = $self->apt_sources_dir;
+    return {
+        'sources_list' => $self->_fetch_source_list($self->apt_sources),
+        'sources_list_dir' => (-e $sources_dir) ? transform_exceptions {
+            return +{ files => +{ map {
+                ($_, $self->_fetch_source_list("$sources_dir/$_"));
+            } files_from_dir $sources_dir } };
+        } : {},
+    };
+}
+
+sub _fetch_source_list {
+    my ($self, $file) = @_;
+    return transform_exceptions {
+        return {
+            file => {
+                file_name => $file,
+                body => scalar(output_from_file $file),
+            },
+        };
+    };
+}
+
 sub _gather_upgradable {
     my ($self) = @_;
     my $pipe = $self->_open_apt_get_upgrade_simulation_pipe;
diff --git a/t/data/apt/sources.list b/t/data/apt/sources.list
new file mode 100644 (file)
index 0000000..85f9236
--- /dev/null
@@ -0,0 +1,2 @@
+deb http://main.example.com foo
+deb http://main.example.com bar
diff --git a/t/data/apt/sources.list.d/other.list b/t/data/apt/sources.list.d/other.list
new file mode 100644 (file)
index 0000000..265d8c2
--- /dev/null
@@ -0,0 +1,2 @@
+deb http://other.example.com foo
+deb http://other.example.com bar
index 63b8b67..74aa7d6 100644 (file)
@@ -17,9 +17,13 @@ ok(
 
 do {
     local $ENV{PATH} = join ':', "$FindBin::Bin/bin", $ENV{PATH};
+    my $source_list = "$FindBin::Bin/data/apt/sources.list";
+    my $source_list_dir = "$FindBin::Bin/data/apt/sources.list.d";
     my $probe = System::Introspector::Probe::Packages::Apt->new(
         apt_update => 1,
         apt_update_after => 0,
+        apt_sources => $source_list,
+        apt_sources_dir => $source_list_dir,
     );
     my $data = $probe->gather;
     is_deeply $data->{upgradable}, {
@@ -30,6 +34,30 @@ do {
     }, 'upgradable packages';
     ok $data->{update}{last}, 'has last update time';
     ok $data->{update}{run}, 'has apt run state';
+    is_deeply $data->{sources}, {
+        config => {
+            sources_list => {
+                file => {
+                    file_name => $source_list,
+                    body => join "", map "$_\n",
+                        "deb http://main.example.com foo",
+                        "deb http://main.example.com bar",
+                },
+            },
+            sources_list_dir => {
+                files => {
+                    "other.list" => {
+                        file => {
+                            file_name => "$source_list_dir/other.list",
+                            body => join "", map "$_\n",
+                                "deb http://other.example.com foo",
+                                "deb http://other.example.com bar",
+                        },
+                    },
+                },
+            }
+        },
+    }, 'sources';
 };
 
 done_testing;