Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(MODULES-7856) Allow optional repositories based on puppet version #258

Merged
merged 3 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ file named `.fixtures.yml` in the root of the project. You can specify a alterna
You can use the `MODULE_WORKING_DIR` environment variable to specify a diffent location when installing module fixtures via the forge. By default the
working directory is `<module directory>/spec/fixtures/work-dir`.

When specifying the repo source of the fixture you have a few options as to which revision of the codebase you wish to use.
When specifying the repo source of the fixture you have a few options as to which revision of the codebase you wish to use, and optionally, the puppet versions where the fixture is needed.

* repo - the url to the repo
* scm - options include git or hg. This is an optional step as the helper code will figure out which scm is used.
Expand All @@ -215,6 +215,11 @@ When specifying the repo source of the fixture you have a few options as to whic
```yaml
flags: --verbose
```
* puppet_version - versions of puppet for which the fixture should be installed. Ruby version constraints are supported. Only works when the `semantic_puppet` gem is available (shipped with puppet 4.0 and up, by default).

```yaml
puppet_version: '>= 6.0.0'
```

**Note:** ref and branch can be used together to get a specific revision on a specific branch

Expand Down Expand Up @@ -263,6 +268,16 @@ fixtures:
ref: "2.6.0"
```

Only install the `yumrepo_core` module when testing against Puppet 6 or greater:

```yaml
fixtures:
repositories:
yumrepo_core:
repo: "git://github.com/puppetlabs/puppetlabs-yumrepo_core"
puppet_version: ">= 6.0.0"
```

Move manifests and siblings to root directory when they are inside a `code` directory:

```yaml
Expand Down
14 changes: 14 additions & 0 deletions lib/puppetlabs_spec_helper/tasks/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def fixtures(category)
# final option list
opts = defaults.merge(opts)

next unless include_repo?(opts['puppet_version'])

real_target = eval('"' + opts['target'] + '"')
real_source = eval('"' + opts['repo'] + '"')

Expand All @@ -100,6 +102,18 @@ def fixtures(category)
result
end

def include_repo?(version_range)
if version_range && defined?(SemanticPuppet)
puppet_spec = Gem::Specification.find_by_name('puppet')
puppet_version = SemanticPuppet::Version.parse(puppet_spec.version.to_s)

constraint = SemanticPuppet::VersionRange.parse(version_range)
constraint.include?(puppet_version)
else
true
end
end

def clone_repo(scm, remote, target, _subdir = nil, ref = nil, branch = nil, flags = nil)
args = []
case scm
Expand Down
48 changes: 48 additions & 0 deletions spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,53 @@
})
end
end
context 'when file specifies puppet version' do
def stub_fixtures(data)
allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return(data)
end

it 'includes the fixture if the puppet version matches', if: Gem::Version.new(Puppet::PUPPETVERSION) > Gem::Version.new('4') do
stub_fixtures(
'fixtures' => {
'forge_modules' => {
'stdlib' => {
'repo' => 'puppetlabs-stdlib',
'puppet_version' => Puppet::PUPPETVERSION,
},
},
},
)
expect(subject.fixtures('forge_modules')).to include('puppetlabs-stdlib')
end

it 'excludes the fixture if the puppet version does not match', if: Gem::Version.new(Puppet::PUPPETVERSION) > Gem::Version.new('4') do
stub_fixtures(
'fixtures' => {
'forge_modules' => {
'stdlib' => {
'repo' => 'puppetlabs-stdlib',
'puppet_version' => '>= 999.9.9',
},
},
},
)
expect(subject.fixtures('forge_modules')).to eq({})
end

it 'includes the fixture on obsolete puppet versions', if: Gem::Version.new(Puppet::PUPPETVERSION) <= Gem::Version.new('4') do
stub_fixtures(
'fixtures' => {
'forge_modules' => {
'stdlib' => {
'repo' => 'puppetlabs-stdlib',
'puppet_version' => Puppet::PUPPETVERSION,
},
},
},
)
expect(subject.fixtures('forge_modules')).to include('puppetlabs-stdlib')
end
end
end
end