Skip to content

Commit 6885332

Browse files
committed
Update test fixtures for better usability
- Replaced the start-gitlab.sh script with a docker-compose.yml file - Optimized the testing section of the README for common use cases - Made it so make testacc works by default, without needing to set GITLAB_TOKEN or GITLAB_BASE_URL. (These variables are still configurable.) - Removed references to the "license" directory, which does not work on MacOS due to the conflict with the LICENSE file. The license is now expected to be in the project root and named Gitlab-license.txt.
1 parent 0c64a2b commit 6885332

File tree

8 files changed

+228
-143
lines changed

8 files changed

+228
-143
lines changed

.github/workflows/acceptance-tests.yml

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ jobs:
1818
# https://help.github.com/en/actions/reference/workflow-commands-for-github-actions
1919
- name: Set build variables
2020
run: |
21-
echo "MAKE_TARGET=testacc" >> $GITHUB_ENV
2221
echo "GO_FLAGS=-mod=vendor" >> $GITHUB_ENV
2322
echo "GO111MODULE=on" >> $GITHUB_ENV
24-
echo "GITLAB_TOKEN=20char-testing-token" >> $GITHUB_ENV
25-
echo "GITLAB_BASE_URL=http://127.0.0.1:8080/api/v4" >> $GITHUB_ENV
2623
27-
- name: Start Gitlab and run acceptance tests
28-
run: |
29-
bash scripts/start-gitlab.sh
30-
make $MAKE_TARGET
24+
- name: Start Gitlab
25+
run: make testacc-up
26+
27+
- name: Run acceptance tests
28+
run: make testacc
3129

3230
acceptance-ee:
3331
timeout-minutes: 60
@@ -48,21 +46,17 @@ jobs:
4846
openssl version
4947
mkdir license
5048
[[ -n "${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}" ]] && echo decrypt
51-
[[ -n "${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}" ]] && openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in Gitlab-license.encrypted -out license/Gitlab-license.txt -pass "pass:${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}"
52-
chmod 666 license/Gitlab-license.txt || true
53-
echo "GITLAB_LICENSE_FILE=Gitlab-license.txt" >> $GITHUB_ENV
49+
[[ -n "${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}" ]] && openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in Gitlab-license.encrypted -out Gitlab-license.txt -pass "pass:${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}"
50+
chmod 666 Gitlab-license.txt || true
5451
5552
# https://help.github.com/en/actions/reference/workflow-commands-for-github-actions
5653
- name: Set build variables
5754
run: |
58-
echo "MAKE_TARGET=testacc" >> $GITHUB_ENV
5955
echo "GO_FLAGS=-mod=vendor" >> $GITHUB_ENV
6056
echo "GO111MODULE=on" >> $GITHUB_ENV
61-
echo "GITLAB_TOKEN=20char-testing-token" >> $GITHUB_ENV
62-
echo "GITLAB_BASE_URL=http://127.0.0.1:8080/api/v4" >> $GITHUB_ENV
63-
echo "TF_LOG=DEBUG" >> $GITHUB_ENV
6457
65-
- name: Start Gitlab and run acceptance tests
66-
run: |
67-
bash scripts/start-gitlab.sh
68-
make $MAKE_TARGET
58+
- name: Start Gitlab
59+
run: make testacc-up
60+
61+
- name: Run acceptance tests
62+
run: make testacc

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ website/vendor
3232
!command/test-fixtures/**/.terraform/
3333

3434
license/*
35+
Gitlab-license.txt

GNUmakefile

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
TEST?=./gitlab
2+
SERVICE?=gitlab-ce
3+
GITLAB_TOKEN?=ACCTEST
4+
GITLAB_BASE_URL?=http://127.0.0.1:8080/api/v4
25
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
3-
PKG_NAME=gitlab
6+
7+
ifdef RUN
8+
TESTARGS += -test.run $(RUN)
9+
endif
410

511
default: build
612

@@ -12,14 +18,15 @@ test: fmtcheck
1218
echo $(TEST) | \
1319
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
1420

15-
testacc-prepareenv:
16-
MAKE_TARGET=testacc GITLAB_TOKEN=ACCTEST LC_ALL=en_US sh -c "'$(CURDIR)/scripts/start-gitlab.sh'"
21+
testacc-up:
22+
docker-compose up -d $(SERVICE)
23+
./scripts/await-healthy.sh
1724

18-
testacc-cleanenv:
19-
docker stop gitlab
25+
testacc-down:
26+
docker-compose down
2027

2128
testacc: fmtcheck
22-
TF_ACC=1 go test -v $(TEST) $(TESTARGS) -timeout 40m
29+
TF_ACC=1 GITLAB_TOKEN=$(GITLAB_TOKEN) GITLAB_BASE_URL=$(GITLAB_BASE_URL) go test -v $(TEST) $(TESTARGS) -timeout 40m
2330

2431
vet:
2532
@echo "go vet ."
@@ -39,14 +46,4 @@ fmtcheck:
3946
errcheck:
4047
@sh -c "'$(CURDIR)/scripts/errcheck.sh'"
4148

42-
43-
test-compile:
44-
@if [ "$(TEST)" = "./..." ]; then \
45-
echo "ERROR: Set TEST to a specific package. For example,"; \
46-
echo " make test-compile TEST=./$(PKG_NAME)"; \
47-
exit 1; \
48-
fi
49-
go test -c $(TEST) $(TESTARGS)
50-
51-
.PHONY: build test testacc vet fmt fmtcheck errcheck test-compile
52-
49+
.PHONY: default build test testacc-up testacc-down testacc vet fmt fmtcheck errcheck

README.md

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,101 @@ Requirements
1717
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
1818
- [Go](https://golang.org/doc/install) >= 1.14 (to build the provider plugin)
1919

20-
Building The Provider
21-
---------------------
20+
## Developing The Provider
2221

23-
Clone repository to: `$GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab`
22+
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.14+ is *required*).
2423

25-
```sh
26-
$ mkdir -p $GOPATH/src/github.com/gitlabhq; cd $GOPATH/src/github.com/gitlabhq
27-
$ git clone [email protected]:gitlabhq/terraform-provider-gitlab
28-
```
24+
1. Clone the git repository.
2925

30-
Enter the provider directory and build the provider
26+
```sh
27+
$ git clone [email protected]:gitlabhq/terraform-provider-gitlab
28+
$ cd terraform-provider-gitlab
29+
```
3130

32-
```sh
33-
$ cd $GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab
34-
$ make build
35-
```
31+
2. Build the provider with `make build`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
3632

37-
Using the provider
38-
----------------------
33+
```sh
34+
$ make build
35+
```
3936

40-
# Developing the Provider
37+
### Running Tests
4138

42-
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.14+ is *required*).
39+
The acceptance tests can run against a Gitlab instance where you have a token with administrator permissions (likely not gitlab.com).
4340

44-
To compile the provider, run `make build`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
41+
#### Option 1: Run tests against a local Gitlab container with docker-compose
4542

46-
```sh
47-
$ make build
48-
...
49-
$ $GOPATH/bin/terraform-provider-gitlab
50-
...
51-
```
43+
This option is the easiest and requires [docker-compose](https://docs.docker.com/compose/install/) (version 1.13+) to be installed on your machine.
44+
45+
1. Start the Gitlab container. It will take about 5 minutes for the container to become healthy.
46+
47+
```sh
48+
$ make testacc-up
49+
```
5250

53-
### Running tests
51+
2. Run the acceptance tests. The full suite takes 10-20 minutes to run.
5452

55-
The Terraform Provider only has acceptance tests, these can run against a gitlab instance where you have a token with administrator permissions (likely not gitlab.com).
56-
There is excellent documentation on [how to run gitlab from docker at gitlab.com](https://docs.gitlab.com/omnibus/docker/)
53+
```sh
54+
$ make testacc
55+
```
5756

58-
In order to run the full suite of acceptance tests, export the environment variables:
57+
3. Stop the Gitlab container.
5958

60-
- `GITLAB_TOKEN` //token for account with admin priviliges
61-
- `GITLAB_BASE_URL` //URL with api part e.g. `http://localhost:8929/api/v4/`
59+
```sh
60+
$ make testacc-down
61+
```
6262

63-
and run `make testacc`.
63+
#### Option 2: Run tests against your own Gitlab instance
64+
65+
If you have your own hosted Gitlab instance, you can run the tests against it directly.
6466

6567
```sh
66-
$ make testacc
68+
$ make testacc GITLAB_TOKEN=example123 GITLAB_BASE_URL=https://example.com/api/v4
6769
```
6870

69-
### Gitlab Community Edition and Gitlab Entreprise Edition
71+
`GITLAB_TOKEN` must be a valid token for an account with admin privileges.
72+
73+
#### Testing Tips
74+
75+
* **Gitlab Community Edition and Gitlab Enterprise Edition:**
76+
77+
This module supports both Gitlab CE and Gitlab EE. We run tests on Gitlab EE,
78+
but can't run them on pull requests from forks.
79+
80+
Features that only work on one flavour can use the following helpers as
81+
SkipFunc: `isRunningInEE` and `isRunningInCE`. You can see an example of this
82+
for [gitlab_project_level_mr_approvals](gitlab/resource_gitlab_project_level_mr_approvals_test.go)
83+
tests.
84+
85+
* **Run EE tests:**
86+
87+
If you have a `Gitlab-license.txt` you can run Gitlab EE, which will enable the full suite of tests:
88+
89+
```sh
90+
$ make testacc-up SERVICE=gitlab-ee
91+
```
92+
93+
* **Run a single test:**
94+
95+
You can pass a pattern to the `RUN` variable to run a reduced number of tests. For example:
96+
97+
```sh
98+
$ make testacc RUN=TestAccGitlabGroup
99+
```
100+
101+
...will run all tests for the `gitlab_group` resource.
102+
103+
* **Debug a test in an IDE:**
104+
105+
First start the Gitlab container with `make testacc-up`.
106+
Then run the desired Go test as you would normally from your IDE, but configure your run configuration to set these environment variables:
107+
108+
```
109+
GITLAB_TOKEN=ACCTEST
110+
GITLAB_BASE_URL=http://127.0.0.1:8080/api/v4
111+
TF_ACC=1
112+
```
70113

71-
This module supports both Gitlab CE and Gitlab EE. We run tests on Gitlab EE,
72-
but can't run them on pull requests from forks.
114+
* **Useful HashiCorp documentation:**
73115

74-
Features that only work on one flavour can use the following helpers as
75-
SkipFunc: `isRunningInEE` and `isRunningInCE`. You can see an example of this
76-
for [gitlab_project_push_rules](gitlab/resource_gitlab_project_push_rules_test.go)
77-
tests.
116+
Refer to [HashiCorp's testing guide](https://www.terraform.io/docs/extend/testing/index.html)
117+
and [HashiCorp's testing best practices](https://www.terraform.io/docs/extend/best-practices/testing.html).

docker-compose.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: '3'
2+
3+
# Acceptance tests depend on a running GitLab instance.
4+
# Only one of these services should be run at a time.
5+
services:
6+
gitlab-ce:
7+
image: gitlab/gitlab-ce
8+
restart: always
9+
ports:
10+
- 8080:80
11+
environment:
12+
GITLAB_ROOT_PASSWORD: adminadmin
13+
labels:
14+
terraform-provider-gitlab/owned: ''
15+
volumes:
16+
- config-ce:/etc/gitlab
17+
- logs-ce:/var/log/gitlab
18+
- data-ce:/var/opt/gitlab
19+
- ./scripts/healthcheck-and-setup.sh:/healthcheck-and-setup.sh
20+
healthcheck:
21+
test: /healthcheck-and-setup.sh
22+
interval: 10s
23+
timeout: 2m
24+
25+
gitlab-ee:
26+
image: gitlab/gitlab-ee
27+
restart: always
28+
ports:
29+
- 8080:80
30+
environment:
31+
GITLAB_ROOT_PASSWORD: adminadmin
32+
GITLAB_LICENSE_FILE: /Gitlab-license.txt
33+
labels:
34+
terraform-provider-gitlab/owned: ''
35+
volumes:
36+
- config-ee:/etc/gitlab
37+
- logs-ee:/var/log/gitlab
38+
- data-ee:/var/opt/gitlab
39+
- ./scripts/healthcheck-and-setup.sh:/healthcheck-and-setup.sh
40+
- ./Gitlab-license.txt:/Gitlab-license.txt
41+
healthcheck:
42+
test: /healthcheck-and-setup.sh
43+
interval: 10s
44+
timeout: 2m
45+
46+
volumes:
47+
config-ce:
48+
logs-ce:
49+
data-ce:
50+
config-ee:
51+
logs-ee:
52+
data-ee:

scripts/await-healthy.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
3+
echo 'Waiting for GitLab container to become healthy'
4+
5+
until test -n "$(docker ps --quiet --filter label=terraform-provider-gitlab/owned --filter health=healthy)"; do
6+
printf '.'
7+
sleep 5
8+
done
9+
10+
echo 'GitLab is healthy'

scripts/healthcheck-and-setup.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env sh
2+
3+
# This script is intended to be used as a Docker HEALTHCHECK for the GitLab container.
4+
# It prepares GitLab prior to running acceptance tests.
5+
#
6+
# This is a known workaround for docker-compose lacking lifecycle hooks.
7+
# See: https://github.com/docker/compose/issues/1809#issuecomment-657815188
8+
9+
set -e
10+
11+
# Check for a successful HTTP status code from GitLab.
12+
curl --silent --show-error --fail --output /dev/null 127.0.0.1:80
13+
14+
# Because this script runs on a regular health check interval,
15+
# this file functions as a marker that tells us if initialization already finished.
16+
done=/var/gitlab-acctest-initialized
17+
18+
test -f $done || {
19+
echo 'Initializing GitLab for acceptance tests'
20+
21+
echo 'Creating access token'
22+
(
23+
printf 'terraform_token = PersonalAccessToken.create('
24+
printf 'user_id: 1, '
25+
printf 'scopes: [:api, :read_user], '
26+
printf 'name: :terraform);'
27+
printf "terraform_token.set_token('ACCTEST');"
28+
printf 'terraform_token.save!;'
29+
) | gitlab-rails console
30+
31+
# 2020-09-07: Currently Gitlab (version 13.3.6 ) doesn't allow in admin API
32+
# ability to set a group as instance level templates.
33+
# To test resource_gitlab_project_test template features we add
34+
# group, project myrails and admin settings directly in scripts/start-gitlab.sh
35+
# Once Gitlab add admin template in API we could manage group/project/settings
36+
# directly in tests like TestAccGitlabProject_basic.
37+
# Works on CE too
38+
39+
echo 'Creating an instance level template group with a simple template based on rails'
40+
(
41+
printf 'group_template = Group.new('
42+
printf 'name: :terraform, '
43+
printf 'path: :terraform);'
44+
printf 'group_template.save!;'
45+
printf 'application_settings = ApplicationSetting.find_by "";'
46+
printf 'application_settings.custom_project_templates_group_id = group_template.id;'
47+
printf 'application_settings.save!;'
48+
printf 'attrs = {'
49+
printf 'name: :myrails, '
50+
printf 'path: :myrails, '
51+
printf 'namespace_id: group_template.id, '
52+
printf 'template_name: :rails, '
53+
printf 'id: 999};'
54+
printf 'project = ::Projects::CreateService.new(User.find_by_username("root"), attrs).execute;'
55+
printf 'project.saved?;'
56+
) | gitlab-rails console
57+
58+
touch $done
59+
}
60+
61+
echo 'GitLab is ready for acceptance tests'

0 commit comments

Comments
 (0)