-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move environment loading into its own classes
- Loading branch information
Showing
7 changed files
with
211 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'compliance_engine' | ||
require 'compliance_engine/module_loader' | ||
|
||
# Load compliance engine data from a Puppet environment | ||
class ComplianceEngine::EnvironmentLoader | ||
# Initialize an EnvironmentLoader from the components of a Puppet `modulepath` | ||
# | ||
# @param paths [Array] the paths to search for Puppet modules | ||
# @param root [String] the root directory to search for Puppet modules | ||
# @param fileclass [File] the class to use for file operations (default: `File`) | ||
# @param dirclass [Dir] the class to use for directory operations (default: `Dir`) | ||
def initialize(*paths, root: nil, fileclass: File, dirclass: Dir) | ||
raise ArgumentError, 'No paths specified' if paths.empty? | ||
@modulepath ||= paths | ||
modules = paths.map do |path| | ||
root ||= path | ||
dirclass.entries(root) | ||
.grep(%r{\A[a-z][a-z0-9_]*\Z}) | ||
.select { |child| fileclass.directory?(File.join(root, child)) } | ||
.map { |child| File.join(root, child) } | ||
rescue | ||
[] | ||
end | ||
modules.flatten! | ||
@modules = modules.map { |path| ComplianceEngine::ModuleLoader.new(path, fileclass: fileclass, dirclass: dirclass) } | ||
end | ||
|
||
attr_reader :modulepath, :modules | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'compliance_engine' | ||
require 'compliance_engine/environment_loader' | ||
require 'zip/filesystem' | ||
|
||
# Load compliance engine data from a zip file containing a Puppet environment | ||
class ComplianceEngine::EnvironmentLoader::Zip < ComplianceEngine::EnvironmentLoader | ||
# Initialize a ComplianceEngine::EnvironmentLoader::Zip object from a zip | ||
# file and an optional root directory. | ||
# | ||
# @param path [String] the path to the zip file containing the Puppet environment | ||
# @param root [String] a directory within the zip file to use as the root of the environment | ||
def initialize(path, root: '/'.dup) | ||
@modulepath = path | ||
|
||
::Zip::File.open(path) do |zipfile| | ||
dir = zipfile.dir | ||
file = zipfile.file | ||
|
||
super(path, root: root, fileclass: file, dirclass: dir) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
spec/classes/compliance_engine/environment_loader/zip_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'compliance_engine' | ||
require 'compliance_engine/environment_loader/zip' | ||
|
||
RSpec.describe ComplianceEngine::EnvironmentLoader::Zip do | ||
before(:each) do | ||
allow(Dir).to receive(:entries).and_call_original | ||
end | ||
|
||
context 'with no paths' do | ||
it 'does not initialize' do | ||
expect { described_class.new }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'with an invalid zip' do | ||
subject(:environment_loader) { described_class.new(path) } | ||
|
||
let(:path) { '/path/to/module.zip' } | ||
|
||
before(:each) do | ||
allow(::Zip::File).to receive(:open).with(path).and_raise(Zip::Error) | ||
end | ||
|
||
it 'does not initialize' do | ||
expect { described_class.new(path) }.to raise_error(Zip::Error) | ||
end | ||
end | ||
|
||
context 'with a valid zip' do | ||
subject(:environment_loader) { described_class.new(path) } | ||
|
||
let(:path) { File.expand_path('../../../fixtures/test_environment.zip', __dir__) } | ||
|
||
before(:each) do | ||
allow(ComplianceEngine::ModuleLoader).to receive(:new).and_return(instance_double(ComplianceEngine::ModuleLoader)) | ||
end | ||
|
||
it 'initializes' do | ||
expect(environment_loader).to be_instance_of(described_class) | ||
end | ||
|
||
it 'is not empty' do | ||
expect(environment_loader.modules).to be_instance_of(Array) | ||
expect(environment_loader.modules.count).to eq(2) | ||
expect(environment_loader.modulepath).to eq(path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'compliance_engine' | ||
require 'compliance_engine/environment_loader' | ||
|
||
RSpec.describe ComplianceEngine::EnvironmentLoader do | ||
before(:each) do | ||
allow(Dir).to receive(:entries).and_call_original | ||
end | ||
|
||
context 'with no paths' do | ||
it 'does not initialize' do | ||
expect { described_class.new }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'with an invalid path' do | ||
subject(:environment_loader) { described_class.new(path) } | ||
|
||
let(:path) { '/path/to/module' } | ||
|
||
before(:each) do | ||
allow(Dir).to receive(:entries).with(path).and_raise(Errno::ENOTDIR) | ||
end | ||
|
||
it 'initializes' do | ||
expect(environment_loader).to be_instance_of(described_class) | ||
end | ||
|
||
it 'is empty' do | ||
expect(environment_loader.modules).to be_empty | ||
end | ||
end | ||
|
||
context 'with valid paths' do | ||
subject(:environment_loader) { described_class.new(*paths) } | ||
|
||
let(:paths) { ['/path1', '/path2'] } | ||
|
||
before(:each) do | ||
allow(Dir).to receive(:entries).with('/path1').and_return(['.', '..', 'a']) | ||
allow(Dir).to receive(:entries).with('/path2').and_return(['.', '..', 'b']) | ||
allow(File).to receive(:directory?).with('/path1/a').and_return(true) | ||
allow(File).to receive(:directory?).with('/path1/b').and_return(true) | ||
allow(ComplianceEngine::ModuleLoader).to receive(:new).and_return(instance_double(ComplianceEngine::ModuleLoader)) | ||
end | ||
|
||
it 'initializes' do | ||
expect(environment_loader).to be_instance_of(described_class) | ||
end | ||
|
||
it 'is not empty' do | ||
expect(environment_loader.modules).to be_instance_of(Array) | ||
expect(environment_loader.modules.count).to eq(2) | ||
expect(environment_loader.modulepath).to eq(paths) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters