Skip to content

Commit

Permalink
Add tests for custom_hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yakatz committed Feb 13, 2024
1 parent 51c2480 commit e4e12b8
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 22 deletions.
59 changes: 44 additions & 15 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

### Defined types

* [`gitlab::custom_hook`](#gitlab--custom_hook): Manage custom hook files within a GitLab project. Custom hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create different custom hook types for the same project - one each for pre-receive, post-receive and update.
* [`gitlab::global_hook`](#gitlab--global_hook): Manage global chain loaded hook files for all GitLab projects. Hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create multipe hooks per type as long as their names are unique. Support for chained (global) hooks is introduced in GitLab Shell 4.1.0 and GitLab 8.15.
* [`gitlab::custom_hook`](#gitlab--custom_hook): Manage custom hook files within a GitLab project.
* [`gitlab::global_hook`](#gitlab--global_hook): Manage global chain loaded hook files for all GitLab projects.
* [`gitlab::system_hook`](#gitlab--system_hook): A file hook will run on each event so it's up to you to filter events or projects

### Tasks
Expand Down Expand Up @@ -1185,7 +1185,8 @@ Default value: `$gitlab::skip_post_deployment_migrations`

### <a name="gitlab--custom_hook"></a>`gitlab::custom_hook`

Manage custom hook files within a GitLab project. Custom hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create different custom hook types for the same project - one each for pre-receive, post-receive and update.
Custom hooks can be created as a pre-receive, post-receive, or update hook.
It is possible to create different custom hook types for the same project - one each for pre-receive, post-receive and update.

#### Examples

Expand All @@ -1200,28 +1201,42 @@ gitlab::custom_hook { 'my_custom_hook':
}
```

##### Calculate hashed storage path

```puppet
gitlab::custom_hook { 'my_custom_hook':
project => 93,
type => 'post-receive',
source => 'puppet:///modules/my_module/post-receive',
}
# Hook path will be `@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d`
```

#### Parameters

The following parameters are available in the `gitlab::custom_hook` defined type:

* [`namespace`](#-gitlab--custom_hook--namespace)
* [`project`](#-gitlab--custom_hook--project)
* [`namespace`](#-gitlab--custom_hook--namespace)
* [`type`](#-gitlab--custom_hook--type)
* [`content`](#-gitlab--custom_hook--content)
* [`source`](#-gitlab--custom_hook--source)
* [`repos_path`](#-gitlab--custom_hook--repos_path)
* [`hashed_storage`](#-gitlab--custom_hook--hashed_storage)

##### <a name="-gitlab--custom_hook--namespace"></a>`namespace`
##### <a name="-gitlab--custom_hook--project"></a>`project`

Data type: `String`
Data type: `Variant[String,Integer]`

The GitLab group namespace for the project.
The GitLab project name, or the hashed directory name or project ID number

##### <a name="-gitlab--custom_hook--project"></a>`project`
##### <a name="-gitlab--custom_hook--namespace"></a>`namespace`

Data type: `String`
Data type: `Optional[String]`

The GitLab project name.
The GitLab group namespace for the project.

Default value: `undef`

##### <a name="-gitlab--custom_hook--type"></a>`type`

Expand All @@ -1233,15 +1248,17 @@ The custom hook type. Should be one of pre-receive, post-receive, or update.

Data type: `Optional[String]`

Specify the custom hook contents either as a string or using the template function. If this paramter is specified source parameter must not be present.
Specify the custom hook contents either as a string or using the template function. If this paramter is specified source
parameter must not be present.

Default value: `undef`

##### <a name="-gitlab--custom_hook--source"></a>`source`

Data type: `Optional[String]`

Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not be present.
Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not
be present.

Default value: `undef`

Expand All @@ -1253,9 +1270,19 @@ The GitLab shell repos path. This defaults to '/var/opt/gitlab/git-data/reposito

Default value: `undef`

##### <a name="-gitlab--custom_hook--hashed_storage"></a>`hashed_storage`

Data type: `Boolean`

Whether to treat the project name as a hashed storage directory name or ID number

Default value: `false`

### <a name="gitlab--global_hook"></a>`gitlab::global_hook`

Manage global chain loaded hook files for all GitLab projects. Hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create multipe hooks per type as long as their names are unique. Support for chained (global) hooks is introduced in GitLab Shell 4.1.0 and GitLab 8.15.
Hooks can be created as a pre-receive, post-receive, or update hook.
It's possible to create multipe hooks per type as long as their names are unique.
Support for chained (global) hooks is introduced in GitLab Shell 4.1.0 and GitLab 8.15.

#### Examples

Expand Down Expand Up @@ -1295,15 +1322,17 @@ Default value: `$gitlab::custom_hooks_dir`

Data type: `Optional[String[1]]`

Specify the custom hook contents either as a string or using the template function. If this paramter is specified source parameter must not be present.
Specify the custom hook contents either as a string or using the template function. If this paramter is specified source
parameter must not be present.

Default value: `undef`

##### <a name="-gitlab--global_hook--source"></a>`source`

Data type: `Optional[Pattern[/^puppet:/]]`

Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not be present.
Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not
be present.

Default value: `undef`

Expand Down
15 changes: 11 additions & 4 deletions manifests/custom_hook.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# @summary Manage custom hook files within a GitLab project. Custom hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create different custom hook types for the same project - one each for pre-receive, post-receive and update.
# @summary
# Manage custom hook files within a GitLab project.
#
# Custom hooks can be created as a pre-receive, post-receive, or update hook.
# It is possible to create different custom hook types for the same project - one each for pre-receive, post-receive and update.
#
# @example Custom hook usage
# gitlab::custom_hook { 'my_custom_hook':
Expand All @@ -19,18 +23,21 @@
# @param project The GitLab project name, or the hashed directory name or project ID number
# @param namespace The GitLab group namespace for the project.
# @param type The custom hook type. Should be one of pre-receive, post-receive, or update.
# @param content Specify the custom hook contents either as a string or using the template function. If this paramter is specified source parameter must not be present.
# @param source Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not be present.
# @param content Specify the custom hook contents either as a string or using the template function. If this paramter is specified source
# parameter must not be present.
# @param source Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not
# be present.
# @param repos_path The GitLab shell repos path. This defaults to '/var/opt/gitlab/git-data/repositories' if not present.
# @param hashed_storage Whether to treat the project name as a hashed storage directory name or ID number
#
define gitlab::custom_hook (
Variant[String,Integer] $project,
Enum['update', 'post-receive', 'pre-receive'] $type,
Optional[String] $namespace = undef,
Optional[String] $content = undef,
Optional[String] $source = undef,
Optional[Stdlib::Absolutepath] $repos_path = undef,
Optional[Boolean] $hashed_storage = false,
Boolean $hashed_storage = false,
) {
if $repos_path {
$_repos_path = $repos_path
Expand Down
14 changes: 11 additions & 3 deletions manifests/global_hook.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# @summary Manage global chain loaded hook files for all GitLab projects. Hooks can be created as a pre-receive, post-receive, or update hook. It's possible to create multipe hooks per type as long as their names are unique. Support for chained (global) hooks is introduced in GitLab Shell 4.1.0 and GitLab 8.15.
# @summary
# Manage global chain loaded hook files for all GitLab projects.
#
# Hooks can be created as a pre-receive, post-receive, or update hook.
# It's possible to create multipe hooks per type as long as their names are unique.
# Support for chained (global) hooks is introduced in GitLab Shell 4.1.0 and GitLab 8.15.
#
# @example Global hook usage
# gitlab::custom_hook { 'my_custom_hook':
Expand All @@ -8,8 +13,11 @@
#
# @param type The custom hook type. Should be one of pre-receive, post-receive, or update.
# @param custom_hooks_dir The GitLab shell repos path. This defaults to '/opt/gitlab/embedded/service/gitlab-shell/hooks' if not present.
# @param content Specify the custom hook contents either as a string or using the template function. If this paramter is specified source parameter must not be present.
# @param source Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not be present.
# @param content Specify the custom hook contents either as a string or using the template function. If this paramter is specified source
# parameter must not be present.
# @param source Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not
# be present.
#
define gitlab::global_hook (
Enum['post-receive', 'pre-receive', 'update'] $type,
Stdlib::Absolutepath $custom_hooks_dir = $gitlab::custom_hooks_dir,
Expand Down
119 changes: 119 additions & 0 deletions spec/defines/custom_hook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
require 'spec_helper'

Check failure on line 1 in spec/defines/custom_hook_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/FrozenStringLiteralComment: Missing frozen string literal comment.

describe 'gitlab::custom_hook' do
let(:title) { 'test-hook' }

let(:pre_condition) do
<<-MANIFEST
class { 'gitlab':
repository_configuration => {},
}
MANIFEST
end

['post-receive', 'pre-receive', 'update'].each do |type|
context "with type => #{type} and source" do
let(:source) { 'puppet:///modules/my_module/post-receive' }
let(:params) do
{
type: type,
repos_path: '/custom/hooks/dir',
source: source,
namespace: 'foo',
project: 'bar'
}
end

it { is_expected.to compile }

it do
is_expected.to contain_file("/custom/hooks/dir/foo/bar.git/custom_hooks").

Check failure on line 30 in spec/defines/custom_hook_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
with_ensure('directory')
end

it do
is_expected.to contain_file("/custom/hooks/dir/foo/bar.git/custom_hooks/#{type}").
with_ensure('file').
with_source(source)
end
end

context "with type => #{type} and content" do
let(:content) { "#!/usr/bin/env bash\ntest 0" }
let(:params) do
{
type: type,
repos_path: '/custom/hooks/dir',
content: content,
namespace: 'foo',
project: 'bar'
}
end

it { is_expected.to compile }

it do
is_expected.to contain_file("/custom/hooks/dir/foo/bar.git/custom_hooks").

Check failure on line 56 in spec/defines/custom_hook_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
with_ensure('directory')
end

it do
is_expected.to contain_file("/custom/hooks/dir/foo/bar.git/custom_hooks/#{type}").
with_ensure('file').
with_content(content)
end
end

context "with type => #{type} and project hash" do
let(:content) { "#!/usr/bin/env bash\ntest 0" }
let(:params) do
{
type: type,
repos_path: '/custom/hooks/dir',
content: content,
hashed_storage: true,
project: '6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d'
}
end

it { is_expected.to compile }

it do
is_expected.to contain_file("/custom/hooks/dir/@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d.git/custom_hooks").

Check failure on line 82 in spec/defines/custom_hook_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
with_ensure('directory')
end

it do
is_expected.to contain_file("/custom/hooks/dir/@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d.git/custom_hooks/#{type}").
with_ensure('file').
with_content(content)
end
end

context "with type => #{type} and project id" do
let(:content) { "#!/usr/bin/env bash\ntest 0" }
let(:params) do
{
type: type,
repos_path: '/custom/hooks/dir',
content: content,
hashed_storage: true,
project: 93
}
end

it { is_expected.to compile }

it do
is_expected.to contain_file("/custom/hooks/dir/@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d.git/custom_hooks").

Check failure on line 108 in spec/defines/custom_hook_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
with_ensure('directory')
end

it do
is_expected.to contain_file("/custom/hooks/dir/@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d.git/custom_hooks/#{type}").
with_ensure('file').
with_content(content)
end
end
end
end

0 comments on commit e4e12b8

Please sign in to comment.