1 # This test runs gitlab and performs the following tests:
6 # - Creating Merge Requests and merging them
7 # - Opening and closing issues.
8 # - Downloading repository archives as tar.gz and tar.bz2
10 # [nixpkgs]$ nix-build -A nixosTests.gitlab
15 inherit (import ./ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
16 initialRootPassword = "notproduction";
19 aliceUsername = "alice";
21 alicePassword = "R5twyCgU0uXC71wT9BBTCqLs6HFZ7h3L";
23 aliceProjectName = "test-alice";
27 bobPassword = "XwkkBbl2SiIwabQzgcoaTbhsotijEEtF";
31 meta.maintainers = with lib.maintainers; [ globin yayayayaka ];
35 imports = [ common/user-account.nix ];
37 virtualisation.memorySize = if pkgs.stdenv.is64bit then 4096 else 2047;
38 virtualisation.cores = 4;
39 virtualisation.useNixStoreImage = true;
40 virtualisation.writableStore = false;
42 systemd.services.gitlab.serviceConfig.Restart = lib.mkForce "no";
43 systemd.services.gitlab-workhorse.serviceConfig.Restart = lib.mkForce "no";
44 systemd.services.gitaly.serviceConfig.Restart = lib.mkForce "no";
45 systemd.services.gitlab-sidekiq.serviceConfig.Restart = lib.mkForce "no";
49 recommendedProxySettings = true;
52 locations."/".proxyPass = "http://unix:/run/gitlab/gitlab-workhorse.socket";
57 services.openssh.enable = true;
64 systemd.services.gitlab-backup.environment.BACKUP = "dump";
68 databasePasswordFile = pkgs.writeText "dbPassword" "xo0daiF4";
69 initialRootPasswordFile = pkgs.writeText "rootPassword" initialRootPassword;
73 settings.pages-domain = "localhost";
79 address = "alice@localhost";
87 secretFile = pkgs.writeText "secret" "Aig5zaic";
88 otpFile = pkgs.writeText "otpsecret" "Riew9mue";
89 dbFile = pkgs.writeText "dbsecret" "we2quaeZ";
90 jwsFile = pkgs.runCommand "oidcKeyBase" {} "${pkgs.openssl}/bin/openssl genrsa 2048 > $out";
96 testScript = { nodes, ... }:
98 auth = pkgs.writeText "auth.json" (builtins.toJSON {
99 grant_type = "password";
101 password = initialRootPassword;
104 createUserAlice = pkgs.writeText "create-user-alice.json" (builtins.toJSON rec {
105 username = aliceUsername;
107 email = "alice@localhost";
108 password = alicePassword;
109 skip_confirmation = true;
112 createUserBob = pkgs.writeText "create-user-bob.json" (builtins.toJSON rec {
113 username = bobUsername;
115 email = "bob@localhost";
116 password = bobPassword;
117 skip_confirmation = true;
120 aliceAuth = pkgs.writeText "alice-auth.json" (builtins.toJSON {
121 grant_type = "password";
122 username = aliceUsername;
123 password = alicePassword;
126 bobAuth = pkgs.writeText "bob-auth.json" (builtins.toJSON {
127 grant_type = "password";
128 username = bobUsername;
129 password = bobPassword;
132 aliceAddSSHKey = pkgs.writeText "alice-add-ssh-key.json" (builtins.toJSON {
134 title = "snakeoil@nixos";
135 key = snakeOilPublicKey;
138 createProjectAlice = pkgs.writeText "create-project-alice.json" (builtins.toJSON {
139 name = aliceProjectName;
140 visibility = "public";
143 putFile = pkgs.writeText "put-file.json" (builtins.toJSON {
145 author_email = "author@example.com";
146 author_name = "Firstname Lastname";
147 content = "some content";
148 commit_message = "create a new file";
151 mergeRequest = pkgs.writeText "merge-request.json" (builtins.toJSON {
153 target_project_id = aliceProjectId;
154 source_branch = "master";
155 target_branch = "master";
156 title = "Add some other file";
159 newIssue = pkgs.writeText "new-issue.json" (builtins.toJSON {
160 title = "useful issue title";
163 closeIssue = pkgs.writeText "close-issue.json" (builtins.toJSON {
165 state_event = "close";
168 # Wait for all GitLab services to be fully started.
170 gitlab.wait_for_unit("gitaly.service")
171 gitlab.wait_for_unit("gitlab-workhorse.service")
172 gitlab.wait_for_unit("gitlab-mailroom.service")
173 gitlab.wait_for_unit("gitlab.service")
174 gitlab.wait_for_unit("gitlab-pages.service")
175 gitlab.wait_for_unit("gitlab-sidekiq.service")
176 gitlab.wait_for_file("${nodes.gitlab.services.gitlab.statePath}/tmp/sockets/gitlab.socket")
177 gitlab.wait_until_succeeds("curl -sSf http://gitlab/users/sign_in")
180 # The actual test of GitLab. Only push data to GitLab if
181 # `doSetup` is is true.
183 GIT_SSH_COMMAND = "ssh -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/dev/null"
186 "curl -isSf http://gitlab | grep -i location | grep http://gitlab/users/sign_in"
189 "${pkgs.sudo}/bin/sudo -u gitlab -H gitlab-rake gitlab:check 1>&2"
192 "echo \"Authorization: Bearer $(curl -X POST -H 'Content-Type: application/json' -d @${auth} http://gitlab/oauth/token | ${pkgs.jq}/bin/jq -r '.access_token')\" >/tmp/headers"
194 '' + lib.optionalString doSetup ''
195 with subtest("Create user Alice"):
197 """[ "$(curl -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -H @/tmp/headers -d @${createUserAlice} http://gitlab/api/v4/users)" = "201" ]"""
200 "echo \"Authorization: Bearer $(curl -X POST -H 'Content-Type: application/json' -d @${aliceAuth} http://gitlab/oauth/token | ${pkgs.jq}/bin/jq -r '.access_token')\" >/tmp/headers-alice"
203 with subtest("Create user Bob"):
205 """ [ "$(curl -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -H @/tmp/headers -d @${createUserBob} http://gitlab/api/v4/users)" = "201" ]"""
208 "echo \"Authorization: Bearer $(curl -X POST -H 'Content-Type: application/json' -d @${bobAuth} http://gitlab/oauth/token | ${pkgs.jq}/bin/jq -r '.access_token')\" >/tmp/headers-bob"
211 with subtest("Setup Git and SSH for Alice"):
212 gitlab.succeed("git config --global user.name Alice")
213 gitlab.succeed("git config --global user.email alice@nixos.invalid")
214 gitlab.succeed("mkdir -m 700 /root/.ssh")
215 gitlab.succeed("cat ${snakeOilPrivateKey} > /root/.ssh/id_ecdsa")
216 gitlab.succeed("chmod 600 /root/.ssh/id_ecdsa")
223 -H 'Content-Type: application/json' \
224 -H @/tmp/headers-alice -d @${aliceAddSSHKey} \
225 http://gitlab/api/v4/user/keys)" = "201" ]
229 with subtest("Create a new repository"):
230 # Alice creates a new repository
237 -H 'Content-Type: application/json' \
238 -H @/tmp/headers-alice \
239 -d @${createProjectAlice} \
240 http://gitlab/api/v4/projects)" = "201" ]
244 # Alice commits an initial commit
251 -H 'Content-Type: application/json' \
252 -H @/tmp/headers-alice \
254 http://gitlab/api/v4/projects/${aliceProjectId}/repository/files/some-file.txt)" = "201" ]"""
257 with subtest("git clone over HTTP"):
259 """git clone http://gitlab/alice/${aliceProjectName}.git clone-via-http""",
263 with subtest("Push a commit via SSH"):
265 f"""GIT_SSH_COMMAND="{GIT_SSH_COMMAND}" git clone gitlab@gitlab:alice/${aliceProjectName}.git""",
269 """echo "a commit sent over ssh" > ${aliceProjectName}/ssh.txt"""
273 cd ${aliceProjectName} || exit 1
279 cd ${aliceProjectName} || exit 1
280 git commit -m "Add a commit to be sent over ssh"
285 cd ${aliceProjectName} || exit 1
286 GIT_SSH_COMMAND="{GIT_SSH_COMMAND}" git push --set-upstream origin master
291 with subtest("Fork a project"):
292 # Bob forks Alice's project
299 -H 'Content-Type: application/json' \
300 -H @/tmp/headers-bob \
301 http://gitlab/api/v4/projects/${aliceProjectId}/fork)" = "201" ]
305 # Bob creates a commit
306 gitlab.wait_until_succeeds(
312 -H 'Content-Type: application/json' \
313 -H @/tmp/headers-bob \
315 http://gitlab/api/v4/projects/${bobProjectId}/repository/files/some-other-file.txt)" = "201" ]
319 with subtest("Create a Merge Request"):
320 # Bob opens a merge request against Alice's repository
321 gitlab.wait_until_succeeds(
327 -H 'Content-Type: application/json' \
328 -H @/tmp/headers-bob \
329 -d @${mergeRequest} \
330 http://gitlab/api/v4/projects/${bobProjectId}/merge_requests)" = "201" ]
334 # Alice merges the MR
335 gitlab.wait_until_succeeds(
341 -H 'Content-Type: application/json' \
342 -H @/tmp/headers-alice \
343 -d @${mergeRequest} \
344 http://gitlab/api/v4/projects/${aliceProjectId}/merge_requests/1/merge)" = "200" ]
348 with subtest("Create an Issue"):
349 # Bob opens an issue on Alice's repository
355 -H 'Content-Type: application/json' \
356 -H @/tmp/headers-bob \
358 http://gitlab/api/v4/projects/${aliceProjectId}/issues)" = "201" ]
362 # Alice closes the issue
363 gitlab.wait_until_succeeds(
369 -H 'Content-Type: application/json' \
370 -H @/tmp/headers-alice -d @${closeIssue} http://gitlab/api/v4/projects/${aliceProjectId}/issues/1)" = "200" ]
374 with subtest("Download archive.tar.gz"):
380 -H @/tmp/headers-alice \
381 http://gitlab/api/v4/projects/${aliceProjectId}/repository/archive.tar.gz)" = "200" ]
387 -H @/tmp/headers-alice \
388 http://gitlab/api/v4/projects/${aliceProjectId}/repository/archive.tar.gz > /tmp/archive.tar.gz
391 gitlab.succeed("test -s /tmp/archive.tar.gz")
393 with subtest("Download archive.tar.bz2"):
399 -H @/tmp/headers-alice \
400 http://gitlab/api/v4/projects/${aliceProjectId}/repository/archive.tar.bz2)" = "200" ]
406 -H @/tmp/headers-alice \
407 http://gitlab/api/v4/projects/${aliceProjectId}/repository/archive.tar.bz2 > /tmp/archive.tar.bz2
410 gitlab.succeed("test -s /tmp/archive.tar.bz2")
419 gitlab.systemctl("start gitlab-backup.service")
420 gitlab.wait_for_unit("gitlab-backup.service")
421 gitlab.wait_for_file("${nodes.gitlab.services.gitlab.statePath}/backup/dump_gitlab_backup.tar")
422 gitlab.systemctl("stop postgresql.service gitlab.target")
424 "find ${nodes.gitlab.services.gitlab.statePath} -mindepth 1 -maxdepth 1 -not -name backup -execdir rm -r {} +"
426 gitlab.succeed("systemd-tmpfiles --create")
427 gitlab.succeed("rm -rf ${nodes.gitlab.services.postgresql.dataDir}")
428 gitlab.systemctl("start gitlab-config.service gitaly.service gitlab-postgresql.service")
429 gitlab.wait_for_file("${nodes.gitlab.services.gitlab.statePath}/tmp/sockets/gitaly.socket")
431 "sudo -u gitlab -H gitlab-rake gitlab:backup:restore RAILS_ENV=production BACKUP=dump force=yes"
433 gitlab.systemctl("start gitlab.target")