diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-21 15:09:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-21 15:09:05 +0300 |
commit | cbf0385d17af276030868aa260af02f3f90ee683 (patch) | |
tree | f2953cb7d17d319ee917c67937d79f341c3be3d0 /spec | |
parent | ea037b91577f1b645267df9e034f6da3e389626c (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
19 files changed, 562 insertions, 478 deletions
diff --git a/spec/features/projects/files/dockerfile_dropdown_spec.rb b/spec/features/projects/files/dockerfile_dropdown_spec.rb index 17258f7042f..dec3d16f72f 100644 --- a/spec/features/projects/files/dockerfile_dropdown_spec.rb +++ b/spec/features/projects/files/dockerfile_dropdown_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Projects > Files > User wants to add a Dockerfile file', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/297400' do +RSpec.describe 'Projects > Files > User wants to add a Dockerfile file', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/299405' do before do project = create(:project, :repository) sign_in project.owner diff --git a/spec/features/projects/files/gitignore_dropdown_spec.rb b/spec/features/projects/files/gitignore_dropdown_spec.rb index 5a39f2bcd98..9271dd9e2a0 100644 --- a/spec/features/projects/files/gitignore_dropdown_spec.rb +++ b/spec/features/projects/files/gitignore_dropdown_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Projects > Files > User wants to add a .gitignore file' do +RSpec.describe 'Projects > Files > User wants to add a .gitignore file', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/299405' do before do project = create(:project, :repository) sign_in project.owner diff --git a/spec/features/projects/files/gitlab_ci_yml_dropdown_spec.rb b/spec/features/projects/files/gitlab_ci_yml_dropdown_spec.rb index 879cb6a65c8..2ad157ebd96 100644 --- a/spec/features/projects/files/gitlab_ci_yml_dropdown_spec.rb +++ b/spec/features/projects/files/gitlab_ci_yml_dropdown_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file' do +RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/299405' do before do project = create(:project, :repository) sign_in project.owner diff --git a/spec/frontend/groups/members/mock_data.js b/spec/frontend/groups/members/mock_data.js deleted file mode 100644 index b84c9c6d446..00000000000 --- a/spec/frontend/groups/members/mock_data.js +++ /dev/null @@ -1,33 +0,0 @@ -export const membersJsonString = - '[{"requested_at":null,"can_update":true,"can_remove":true,"can_override":false,"access_level":{"integer_value":50,"string_value":"Owner"},"source":{"id":323,"name":"My group / my subgroup","web_url":"http://127.0.0.1:3000/groups/my-group/my-subgroup"},"user":{"id":1,"name":"Administrator","username":"root","web_url":"http://127.0.0.1:3000/root","avatar_url":"https://www.gravatar.com/avatar/4816142ef496f956a277bedf1a40607b?s=80\u0026d=identicon","blocked":false,"two_factor_enabled":false},"id":524,"created_at":"2020-08-21T21:33:27.631Z","expires_at":null,"using_license":false,"group_sso":false,"group_managed_account":false}]'; - -export const membersParsed = [ - { - requestedAt: null, - canUpdate: true, - canRemove: true, - canOverride: false, - accessLevel: { integerValue: 50, stringValue: 'Owner' }, - source: { - id: 323, - name: 'My group / my subgroup', - webUrl: 'http://127.0.0.1:3000/groups/my-group/my-subgroup', - }, - user: { - id: 1, - name: 'Administrator', - username: 'root', - webUrl: 'http://127.0.0.1:3000/root', - avatarUrl: - 'https://www.gravatar.com/avatar/4816142ef496f956a277bedf1a40607b?s=80&d=identicon', - blocked: false, - twoFactorEnabled: false, - }, - id: 524, - createdAt: '2020-08-21T21:33:27.631Z', - expiresAt: null, - usingLicense: false, - groupSso: false, - groupManagedAccount: false, - }, -]; diff --git a/spec/frontend/groups/members/utils_spec.js b/spec/frontend/groups/members/utils_spec.js index 68945174e9d..0912e66e3e8 100644 --- a/spec/frontend/groups/members/utils_spec.js +++ b/spec/frontend/groups/members/utils_spec.js @@ -1,53 +1,14 @@ -import { membersJsonString, membersParsed } from './mock_data'; -import { - parseDataAttributes, - memberRequestFormatter, - groupLinkRequestFormatter, -} from '~/groups/members/utils'; +import { groupMemberRequestFormatter } from '~/groups/members/utils'; describe('group member utils', () => { - describe('parseDataAttributes', () => { - let el; - - beforeEach(() => { - el = document.createElement('div'); - el.setAttribute('data-members', membersJsonString); - el.setAttribute('data-group-id', '234'); - el.setAttribute('data-can-manage-members', 'true'); - }); - - afterEach(() => { - el = null; - }); - - it('correctly parses the data attributes', () => { - expect(parseDataAttributes(el)).toEqual({ - members: membersParsed, - sourceId: 234, - canManageMembers: true, - }); - }); - }); - - describe('memberRequestFormatter', () => { + describe('groupMemberRequestFormatter', () => { it('returns expected format', () => { expect( - memberRequestFormatter({ + groupMemberRequestFormatter({ accessLevel: 50, expires_at: '2020-10-16', }), ).toEqual({ group_member: { access_level: 50, expires_at: '2020-10-16' } }); }); }); - - describe('groupLinkRequestFormatter', () => { - it('returns expected format', () => { - expect( - groupLinkRequestFormatter({ - accessLevel: 50, - expires_at: '2020-10-16', - }), - ).toEqual({ group_link: { group_access: 50, expires_at: '2020-10-16' } }); - }); - }); }); diff --git a/spec/frontend/ide/components/commit_sidebar/form_spec.js b/spec/frontend/ide/components/commit_sidebar/form_spec.js index cec3fdfc05e..857d8f400a7 100644 --- a/spec/frontend/ide/components/commit_sidebar/form_spec.js +++ b/spec/frontend/ide/components/commit_sidebar/form_spec.js @@ -4,6 +4,7 @@ import { GlModal } from '@gitlab/ui'; import { projectData } from 'jest/ide/mock_data'; import { stubComponent } from 'helpers/stub_component'; import waitForPromises from 'helpers/wait_for_promises'; +import { createMockDirective, getBinding } from 'helpers/vue_mock_directive'; import { createStore } from '~/ide/stores'; import { COMMIT_TO_NEW_BRANCH } from '~/ide/stores/modules/commit/constants'; import CommitForm from '~/ide/components/commit_sidebar/form.vue'; @@ -23,6 +24,9 @@ describe('IDE commit form', () => { const createComponent = () => { wrapper = shallowMount(CommitForm, { store, + directives: { + GlTooltip: createMockDirective(), + }, stubs: { GlModal: stubComponent(GlModal), }, @@ -39,8 +43,21 @@ describe('IDE commit form', () => { store.state.currentActivityView = leftSidebarViews.edit.name; }; const findBeginCommitButton = () => wrapper.find('[data-testid="begin-commit-button"]'); + const findBeginCommitButtonTooltip = () => + wrapper.find('[data-testid="begin-commit-button-tooltip"]'); + const findBeginCommitButtonData = () => ({ + disabled: findBeginCommitButton().props('disabled'), + tooltip: getBinding(findBeginCommitButtonTooltip().element, 'gl-tooltip').value.title, + }); const findCommitButton = () => wrapper.find('[data-testid="commit-button"]'); + const findCommitButtonTooltip = () => wrapper.find('[data-testid="commit-button-tooltip"]'); + const findCommitButtonData = () => ({ + disabled: findCommitButton().props('disabled'), + tooltip: getBinding(findCommitButtonTooltip().element, 'gl-tooltip').value.title, + }); + const clickCommitButton = () => findCommitButton().vm.$emit('click'); const findForm = () => wrapper.find('form'); + const submitForm = () => findForm().trigger('submit'); const findCommitMessageInput = () => wrapper.find(CommitMessageField); const setCommitMessageInput = (val) => findCommitMessageInput().vm.$emit('input', val); const findDiscardDraftButton = () => wrapper.find('[data-testid="discard-draft"]'); @@ -52,27 +69,40 @@ describe('IDE commit form', () => { store.state.currentBranchId = 'master'; Vue.set(store.state.projects, 'abcproject', { ...projectData, + userPermissions: { pushCode: true }, }); }); afterEach(() => { wrapper.destroy(); - wrapper = null; }); + // Notes: + // - When there are no changes, there is no commit button so there's nothing to test :) describe.each` - desc | stagedFiles | disabled - ${'when there are changes'} | ${['test']} | ${false} - ${'when there are no changes'} | ${[]} | ${true} - `('$desc', ({ stagedFiles, disabled }) => { + desc | stagedFiles | userPermissions | viewFn | buttonFn | disabled | tooltip + ${'when there are no changes'} | ${[]} | ${{ pushCode: true }} | ${goToEditView} | ${findBeginCommitButtonData} | ${true} | ${''} + ${'when there are changes'} | ${['test']} | ${{ pushCode: true }} | ${goToEditView} | ${findBeginCommitButtonData} | ${false} | ${''} + ${'when there are changes'} | ${['test']} | ${{ pushCode: true }} | ${goToCommitView} | ${findCommitButtonData} | ${false} | ${''} + ${'when user cannot push'} | ${['test']} | ${{ pushCode: false }} | ${goToEditView} | ${findBeginCommitButtonData} | ${true} | ${CommitForm.MSG_CANNOT_PUSH_CODE} + ${'when user cannot push'} | ${['test']} | ${{ pushCode: false }} | ${goToCommitView} | ${findCommitButtonData} | ${true} | ${CommitForm.MSG_CANNOT_PUSH_CODE} + `('$desc', ({ stagedFiles, userPermissions, viewFn, buttonFn, disabled, tooltip }) => { beforeEach(async () => { store.state.stagedFiles = stagedFiles; + store.state.projects.abcproject.userPermissions = userPermissions; createComponent(); }); - it(`begin button disabled=${disabled}`, async () => { - expect(findBeginCommitButton().props('disabled')).toBe(disabled); + it(`at view=${viewFn.name}, ${buttonFn.name} has disabled=${disabled} tooltip=${tooltip}`, async () => { + viewFn(); + + await wrapper.vm.$nextTick(); + + expect(buttonFn()).toEqual({ + disabled, + tooltip, + }); }); }); @@ -252,12 +282,21 @@ describe('IDE commit form', () => { jest.spyOn(store, 'dispatch').mockResolvedValue(); }); - it('calls commitChanges', () => { - findCommitButton().vm.$emit('click'); + it.each([clickCommitButton, submitForm])('when %p, commits changes', (fn) => { + fn(); expect(store.dispatch).toHaveBeenCalledWith('commit/commitChanges', undefined); }); + it('when cannot push code, submitting does nothing', async () => { + store.state.projects.abcproject.userPermissions.pushCode = false; + await wrapper.vm.$nextTick(); + + submitForm(); + + expect(store.dispatch).not.toHaveBeenCalled(); + }); + it.each` createError | props ${() => createCodeownersCommitError('test message')} | ${{ actionPrimary: { text: 'Create new branch' } }} diff --git a/spec/frontend/ide/components/ide_spec.js b/spec/frontend/ide/components/ide_spec.js index 805fa898611..ef8c55c8b1c 100644 --- a/spec/frontend/ide/components/ide_spec.js +++ b/spec/frontend/ide/components/ide_spec.js @@ -1,9 +1,10 @@ import Vuex from 'vuex'; import { createLocalVue, shallowMount } from '@vue/test-utils'; +import { GlAlert } from '@gitlab/ui'; import waitForPromises from 'helpers/wait_for_promises'; import { createStore } from '~/ide/stores'; import ErrorMessage from '~/ide/components/error_message.vue'; -import ide from '~/ide/components/ide.vue'; +import Ide from '~/ide/components/ide.vue'; import { file } from '../helpers'; import { projectData } from '../mock_data'; @@ -15,12 +16,12 @@ describe('WebIDE', () => { let wrapper; - function createComponent({ projData = emptyProjData, state = {} } = {}) { + const createComponent = ({ projData = emptyProjData, state = {} } = {}) => { const store = createStore(); store.state.currentProjectId = 'abcproject'; store.state.currentBranchId = 'master'; - store.state.projects.abcproject = { ...projData }; + store.state.projects.abcproject = projData && { ...projData }; store.state.trees['abcproject/master'] = { tree: [], loading: false, @@ -29,11 +30,13 @@ describe('WebIDE', () => { store.state[key] = state[key]; }); - return shallowMount(ide, { + wrapper = shallowMount(Ide, { store, localVue, }); - } + }; + + const findAlert = () => wrapper.find(GlAlert); afterEach(() => { wrapper.destroy(); @@ -42,7 +45,7 @@ describe('WebIDE', () => { describe('ide component, empty repo', () => { beforeEach(() => { - wrapper = createComponent({ + createComponent({ projData: { empty_repo: true, }, @@ -63,7 +66,7 @@ describe('WebIDE', () => { `( 'should error message exists=$exists when errorMessage=$errorMessage', async ({ errorMessage, exists }) => { - wrapper = createComponent({ + createComponent({ state: { errorMessage, }, @@ -78,12 +81,12 @@ describe('WebIDE', () => { describe('onBeforeUnload', () => { it('returns undefined when no staged files or changed files', () => { - wrapper = createComponent(); + createComponent(); expect(wrapper.vm.onBeforeUnload()).toBe(undefined); }); it('returns warning text when their are changed files', () => { - wrapper = createComponent({ + createComponent({ state: { changedFiles: [file()], }, @@ -93,7 +96,7 @@ describe('WebIDE', () => { }); it('returns warning text when their are staged files', () => { - wrapper = createComponent({ + createComponent({ state: { stagedFiles: [file()], }, @@ -104,7 +107,7 @@ describe('WebIDE', () => { it('updates event object', () => { const event = {}; - wrapper = createComponent({ + createComponent({ state: { stagedFiles: [file()], }, @@ -118,7 +121,7 @@ describe('WebIDE', () => { describe('non-existent branch', () => { it('does not render "New file" button for non-existent branch when repo is not empty', () => { - wrapper = createComponent({ + createComponent({ state: { projects: {}, }, @@ -130,7 +133,7 @@ describe('WebIDE', () => { describe('branch with files', () => { beforeEach(() => { - wrapper = createComponent({ + createComponent({ projData: { empty_repo: false, }, @@ -142,4 +145,31 @@ describe('WebIDE', () => { }); }); }); + + it('when user cannot push code, shows alert', () => { + createComponent({ + projData: { + userPermissions: { + pushCode: false, + }, + }, + }); + + expect(findAlert().props()).toMatchObject({ + dismissible: false, + }); + expect(findAlert().text()).toBe(Ide.MSG_CANNOT_PUSH_CODE); + }); + + it.each` + desc | projData + ${'when user can push code'} | ${{ userPermissions: { pushCode: true } }} + ${'when project is not ready'} | ${null} + `('$desc, no alert is shown', ({ projData }) => { + createComponent({ + projData, + }); + + expect(findAlert().exists()).toBe(false); + }); }); diff --git a/spec/frontend/ide/stores/getters_spec.js b/spec/frontend/ide/stores/getters_spec.js index 1787f9e9361..1289c1aec75 100644 --- a/spec/frontend/ide/stores/getters_spec.js +++ b/spec/frontend/ide/stores/getters_spec.js @@ -2,6 +2,7 @@ import { TEST_HOST } from 'helpers/test_constants'; import * as getters from '~/ide/stores/getters'; import { createStore } from '~/ide/stores'; import { file } from '../helpers'; +import { DEFAULT_PERMISSIONS } from '../../../../app/assets/javascripts/ide/constants'; const TEST_PROJECT_ID = 'test_project'; @@ -386,7 +387,9 @@ describe('IDE store getters', () => { describe('findProjectPermissions', () => { it('returns false if project not found', () => { - expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toEqual({}); + expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toEqual( + DEFAULT_PERMISSIONS, + ); }); it('finds permission in given project', () => { diff --git a/spec/frontend/groups/members/components/app_spec.js b/spec/frontend/members/components/app_spec.js index 9847dacbec8..7b95e5ebd44 100644 --- a/spec/frontend/groups/members/components/app_spec.js +++ b/spec/frontend/members/components/app_spec.js @@ -2,13 +2,13 @@ import { shallowMount, createLocalVue } from '@vue/test-utils'; import { nextTick } from 'vue'; import Vuex from 'vuex'; import { GlAlert } from '@gitlab/ui'; -import App from '~/groups/members/components/app.vue'; +import MembersApp from '~/members/components/app.vue'; import FilterSortContainer from '~/members/components/filter_sort/filter_sort_container.vue'; import * as commonUtils from '~/lib/utils/common_utils'; import { RECEIVE_MEMBER_ROLE_ERROR, HIDE_ERROR } from '~/members/store/mutation_types'; import mutations from '~/members/store/mutations'; -describe('GroupMembersApp', () => { +describe('MembersApp', () => { const localVue = createLocalVue(); localVue.use(Vuex); @@ -25,7 +25,7 @@ describe('GroupMembersApp', () => { mutations, }); - wrapper = shallowMount(App, { + wrapper = shallowMount(MembersApp, { localVue, store, ...options, diff --git a/spec/frontend/groups/members/index_spec.js b/spec/frontend/members/index_spec.js index 5c717e53229..d73e3b03f07 100644 --- a/spec/frontend/groups/members/index_spec.js +++ b/spec/frontend/members/index_spec.js @@ -1,15 +1,15 @@ import { createWrapper } from '@vue/test-utils'; -import { initGroupMembersApp } from '~/groups/members'; -import GroupMembersApp from '~/groups/members/components/app.vue'; -import { membersJsonString, membersParsed } from './mock_data'; +import { initMembersApp } from '~/members/index'; +import MembersApp from '~/members/components/app.vue'; +import { membersJsonString, members } from './mock_data'; -describe('initGroupMembersApp', () => { +describe('initMembersApp', () => { let el; let vm; let wrapper; const setup = () => { - vm = initGroupMembersApp(el, { + vm = initMembersApp(el, { tableFields: ['account'], tableAttrs: { table: { 'data-qa-selector': 'members_list' } }, tableSortableFields: ['account'], @@ -22,7 +22,7 @@ describe('initGroupMembersApp', () => { beforeEach(() => { el = document.createElement('div'); el.setAttribute('data-members', membersJsonString); - el.setAttribute('data-group-id', '234'); + el.setAttribute('data-source-id', '234'); el.setAttribute('data-can-manage-members', 'true'); el.setAttribute('data-member-path', '/groups/foo-bar/-/group_members/:id'); @@ -36,10 +36,10 @@ describe('initGroupMembersApp', () => { wrapper = null; }); - it('renders `GroupMembersApp`', () => { + it('renders `MembersApp`', () => { setup(); - expect(wrapper.find(GroupMembersApp).exists()).toBe(true); + expect(wrapper.find(MembersApp).exists()).toBe(true); }); it('sets `currentUserId` in Vuex store', () => { @@ -57,7 +57,7 @@ describe('initGroupMembersApp', () => { }); }); - it('parses and sets `data-group-id` as `sourceId` in Vuex store', () => { + it('parses and sets `data-source-id` as `sourceId` in Vuex store', () => { setup(); expect(vm.$store.state.sourceId).toBe(234); @@ -72,7 +72,7 @@ describe('initGroupMembersApp', () => { it('parses and sets `members` in Vuex store', () => { setup(); - expect(vm.$store.state.members).toEqual(membersParsed); + expect(vm.$store.state.members).toEqual(members); }); it('sets `tableFields` in Vuex store', () => { diff --git a/spec/frontend/members/mock_data.js b/spec/frontend/members/mock_data.js index e668f2a1998..5c00745a590 100644 --- a/spec/frontend/members/mock_data.js +++ b/spec/frontend/members/mock_data.js @@ -69,3 +69,5 @@ export const accessRequest = { }; export const members = [member]; + +export const membersJsonString = JSON.stringify(members); diff --git a/spec/frontend/members/utils_spec.js b/spec/frontend/members/utils_spec.js index 7cd4e735b55..f123b2c6a91 100644 --- a/spec/frontend/members/utils_spec.js +++ b/spec/frontend/members/utils_spec.js @@ -9,9 +9,11 @@ import { canOverride, parseSortParam, buildSortHref, + parseDataAttributes, + groupLinkRequestFormatter, } from '~/members/utils'; import { DEFAULT_SORT } from '~/members/constants'; -import { member as memberMock, group, invite } from './mock_data'; +import { member as memberMock, group, invite, membersJsonString, members } from './mock_data'; const DIRECT_MEMBER_ID = 178; const INHERITED_MEMBER_ID = 179; @@ -229,4 +231,38 @@ describe('Members Utils', () => { }); }); }); + + describe('parseDataAttributes', () => { + let el; + + beforeEach(() => { + el = document.createElement('div'); + el.setAttribute('data-members', membersJsonString); + el.setAttribute('data-source-id', '234'); + el.setAttribute('data-can-manage-members', 'true'); + }); + + afterEach(() => { + el = null; + }); + + it('correctly parses the data attributes', () => { + expect(parseDataAttributes(el)).toEqual({ + members, + sourceId: 234, + canManageMembers: true, + }); + }); + }); + + describe('groupLinkRequestFormatter', () => { + it('returns expected format', () => { + expect( + groupLinkRequestFormatter({ + accessLevel: 50, + expires_at: '2020-10-16', + }), + ).toEqual({ group_link: { group_access: 50, expires_at: '2020-10-16' } }); + }); + }); }); diff --git a/spec/helpers/groups/group_members_helper_spec.rb b/spec/helpers/groups/group_members_helper_spec.rb index d75124b6da7..e83585e5efb 100644 --- a/spec/helpers/groups/group_members_helper_spec.rb +++ b/spec/helpers/groups/group_members_helper_spec.rb @@ -23,11 +23,11 @@ RSpec.describe Groups::GroupMembersHelper do end end - describe '#linked_groups_data_json' do + describe '#group_group_links_data_json' do include_context 'group_group_link' it 'matches json schema' do - json = helper.linked_groups_data_json(shared_group.shared_with_group_links) + json = helper.group_group_links_data_json(shared_group.shared_with_group_links) expect(json).to match_schema('group_group_links') end @@ -81,13 +81,13 @@ RSpec.describe Groups::GroupMembersHelper do expect(helper.group_members_list_data_attributes(group, present_members([group_member]))).to include({ members: helper.members_data_json(group, present_members([group_member])), member_path: '/groups/foo-bar/-/group_members/:id', - group_id: group.id, + source_id: group.id, can_manage_members: 'true' }) end end - describe '#linked_groups_list_data_attributes' do + describe '#group_group_links_list_data_attributes' do include_context 'group_group_link' before do @@ -95,10 +95,10 @@ RSpec.describe Groups::GroupMembersHelper do end it 'returns expected hash' do - expect(helper.linked_groups_list_data_attributes(shared_group)).to include({ - members: helper.linked_groups_data_json(shared_group.shared_with_group_links), + expect(helper.group_group_links_list_data_attributes(shared_group)).to include({ + members: helper.group_group_links_data_json(shared_group.shared_with_group_links), member_path: '/groups/foo-bar/-/group_links/:id', - group_id: shared_group.id + source_id: shared_group.id }) end end diff --git a/spec/lib/atlassian/jira_connect/client_spec.rb b/spec/lib/atlassian/jira_connect/client_spec.rb index 2a4363ff2af..5c8d4282118 100644 --- a/spec/lib/atlassian/jira_connect/client_spec.rb +++ b/spec/lib/atlassian/jira_connect/client_spec.rb @@ -18,15 +18,15 @@ RSpec.describe Atlassian::JiraConnect::Client do end end - around do |example| - freeze_time { example.run } - end - describe '.generate_update_sequence_id' do - it 'returns monotonic_time converted it to integer' do - allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(1.0) + it 'returns unix time in microseconds as integer', :aggregate_failures do + travel_to(Time.utc(1970, 1, 1, 0, 0, 1)) do + expect(described_class.generate_update_sequence_id).to eq(1000) + end - expect(described_class.generate_update_sequence_id).to eq(1) + travel_to(Time.utc(1970, 1, 1, 0, 0, 5)) do + expect(described_class.generate_update_sequence_id).to eq(5000) + end end end diff --git a/spec/services/projects/cleanup_service_spec.rb b/spec/services/projects/cleanup_service_spec.rb index 6fd29813d98..f2c052d9397 100644 --- a/spec/services/projects/cleanup_service_spec.rb +++ b/spec/services/projects/cleanup_service_spec.rb @@ -88,7 +88,7 @@ RSpec.describe Projects::CleanupService do end it 'runs garbage collection on the repository' do - expect_next_instance_of(GitGarbageCollectWorker) do |worker| + expect_next_instance_of(Projects::GitGarbageCollectWorker) do |worker| expect(worker).to receive(:perform).with(project.id, :prune, "project_cleanup:gc:#{project.id}") end diff --git a/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb b/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb index f201c7b1780..30f38b67732 100644 --- a/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb +++ b/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb @@ -71,7 +71,7 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| it 'does not enqueue a GC run' do expect { subject.execute } - .not_to change(GitGarbageCollectWorker.jobs, :count) + .not_to change(Projects::GitGarbageCollectWorker.jobs, :count) end end @@ -84,12 +84,12 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| stub_application_setting(housekeeping_enabled: false) expect { subject.execute } - .not_to change(GitGarbageCollectWorker.jobs, :count) + .not_to change(Projects::GitGarbageCollectWorker.jobs, :count) end it 'enqueues a GC run' do expect { subject.execute } - .to change(GitGarbageCollectWorker.jobs, :count).by(1) + .to change(Projects::GitGarbageCollectWorker.jobs, :count).by(1) end end end diff --git a/spec/support/shared_examples/services/repositories/housekeeping_shared_examples.rb b/spec/support/shared_examples/services/repositories/housekeeping_shared_examples.rb index a174ae94b75..ad0dc389aeb 100644 --- a/spec/support/shared_examples/services/repositories/housekeeping_shared_examples.rb +++ b/spec/support/shared_examples/services/repositories/housekeeping_shared_examples.rb @@ -9,10 +9,10 @@ RSpec.shared_examples 'housekeeps repository' do expect(subject).to receive(:try_obtain_lease).and_return(:the_uuid) expect(subject).to receive(:lease_key).and_return(:the_lease_key) expect(subject).to receive(:task).and_return(:incremental_repack) - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :incremental_repack, :the_lease_key, :the_uuid).and_call_original + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :incremental_repack, :the_lease_key, :the_uuid).and_call_original Sidekiq::Testing.fake! do - expect { subject.execute }.to change(GitGarbageCollectWorker.jobs, :size).by(1) + expect { subject.execute }.to change(Projects::GitGarbageCollectWorker.jobs, :size).by(1) end end @@ -38,7 +38,7 @@ RSpec.shared_examples 'housekeeps repository' do end it 'does not enqueue a job' do - expect(GitGarbageCollectWorker).not_to receive(:perform_async) + expect(Projects::GitGarbageCollectWorker).not_to receive(:perform_async) expect { subject.execute }.to raise_error(Repositories::HousekeepingService::LeaseTaken) end @@ -63,16 +63,16 @@ RSpec.shared_examples 'housekeeps repository' do allow(subject).to receive(:lease_key).and_return(:the_lease_key) # At push 200 - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :gc, :the_lease_key, :the_uuid) + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :gc, :the_lease_key, :the_uuid) .once # At push 50, 100, 150 - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :full_repack, :the_lease_key, :the_uuid) + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :full_repack, :the_lease_key, :the_uuid) .exactly(3).times # At push 10, 20, ... (except those above) - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :incremental_repack, :the_lease_key, :the_uuid) + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :incremental_repack, :the_lease_key, :the_uuid) .exactly(16).times # At push 6, 12, 18, ... (except those above) - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :pack_refs, :the_lease_key, :the_uuid) + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :pack_refs, :the_lease_key, :the_uuid) .exactly(27).times 201.times do @@ -90,7 +90,7 @@ RSpec.shared_examples 'housekeeps repository' do allow(housekeeping).to receive(:try_obtain_lease).and_return(:gc_uuid) allow(housekeeping).to receive(:lease_key).and_return(:gc_lease_key) - expect(GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :gc, :gc_lease_key, :gc_uuid).twice + expect(Projects::GitGarbageCollectWorker).to receive(:perform_async).with(resource.id, :gc, :gc_lease_key, :gc_uuid).twice 2.times do housekeeping.execute diff --git a/spec/workers/git_garbage_collect_worker_spec.rb b/spec/workers/git_garbage_collect_worker_spec.rb index 13089549086..3df64c35166 100644 --- a/spec/workers/git_garbage_collect_worker_spec.rb +++ b/spec/workers/git_garbage_collect_worker_spec.rb @@ -5,350 +5,22 @@ require 'fileutils' require 'spec_helper' RSpec.describe GitGarbageCollectWorker do - include GitHelpers - let_it_be(:project) { create(:project, :repository) } - let(:shell) { Gitlab::Shell.new } - let!(:lease_uuid) { SecureRandom.uuid } - let!(:lease_key) { "project_housekeeping:#{project.id}" } - let(:params) { [project.id, task, lease_key, lease_uuid] } - - subject { described_class.new } - - shared_examples 'it calls Gitaly' do - specify do - expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(gitaly_task) - .and_return(nil) - - subject.perform(*params) - end - end - shared_examples 'it updates the project statistics' do - it 'updates the project statistics' do - expect_next_instance_of(Projects::UpdateStatisticsService, project, nil, statistics: [:repository_size, :lfs_objects_size]) do |service| - expect(service).to receive(:execute).and_call_original - end - - subject.perform(*params) - end + let(:lease_uuid) { SecureRandom.uuid } + let(:lease_key) { "project_housekeeping:#{project.id}" } + let(:task) { :full_repack } + let(:params) { [project.id, task, lease_key, lease_uuid] } - it 'does nothing if the database is read-only' do - allow(Gitlab::Database).to receive(:read_only?) { true } - - expect_any_instance_of(Projects::UpdateStatisticsService).not_to receive(:execute) - - subject.perform(*params) - end - end + subject { described_class.new } describe "#perform" do - let(:gitaly_task) { :garbage_collect } - let(:task) { :gc } - - context 'with active lease_uuid' do - before do - allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid) - end - - it_behaves_like 'it calls Gitaly' - it_behaves_like 'it updates the project statistics' - - it "flushes ref caches when the task if 'gc'" do - expect(subject).to receive(:renew_lease).with(lease_key, lease_uuid).and_call_original - expect_any_instance_of(Repository).to receive(:expire_branches_cache).and_call_original - expect_any_instance_of(Repository).to receive(:branch_names).and_call_original - expect_any_instance_of(Repository).to receive(:has_visible_content?).and_call_original - expect_any_instance_of(Gitlab::Git::Repository).to receive(:has_visible_content?).and_call_original - - subject.perform(*params) - end - - it 'handles gRPC errors' do - expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:garbage_collect).and_raise(GRPC::NotFound) - - expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository) - end - end - - context 'with different lease than the active one' do - before do - allow(subject).to receive(:get_lease_uuid).and_return(SecureRandom.uuid) - end - - it 'returns silently' do - expect_any_instance_of(Repository).not_to receive(:expire_branches_cache).and_call_original - expect_any_instance_of(Repository).not_to receive(:branch_names).and_call_original - expect_any_instance_of(Repository).not_to receive(:has_visible_content?).and_call_original - - subject.perform(*params) - end - end - - context 'with no active lease' do - let(:params) { [project.id] } - - before do - allow(subject).to receive(:get_lease_uuid).and_return(false) - end - - context 'when is able to get the lease' do - before do - allow(subject).to receive(:try_obtain_lease).and_return(SecureRandom.uuid) - end - - it_behaves_like 'it calls Gitaly' - it_behaves_like 'it updates the project statistics' - - it "flushes ref caches when the task if 'gc'" do - expect(subject).to receive(:get_lease_uuid).with("git_gc:#{task}:#{project.id}").and_return(false) - expect_any_instance_of(Repository).to receive(:expire_branches_cache).and_call_original - expect_any_instance_of(Repository).to receive(:branch_names).and_call_original - expect_any_instance_of(Repository).to receive(:has_visible_content?).and_call_original - expect_any_instance_of(Gitlab::Git::Repository).to receive(:has_visible_content?).and_call_original - - subject.perform(*params) - end - - context 'when the repository has joined a pool' do - let!(:pool) { create(:pool_repository, :ready) } - let(:project) { pool.source_project } - - it 'ensures the repositories are linked' do - expect_any_instance_of(PoolRepository).to receive(:link_repository).once - - subject.perform(*params) - end - end - - context 'LFS object garbage collection' do - before do - stub_lfs_setting(enabled: true) - end - - let_it_be(:lfs_reference) { create(:lfs_objects_project, project: project) } - let(:lfs_object) { lfs_reference.lfs_object } - - it 'cleans up unreferenced LFS objects' do - expect_next_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences) do |svc| - expect(svc.project).to eq(project) - expect(svc.dry_run).to be_falsy - expect(svc).to receive(:run!).and_call_original - end - - subject.perform(*params) - - expect(project.lfs_objects.reload).not_to include(lfs_object) - end - - it 'catches and logs exceptions' do - expect_any_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences) - .to receive(:run!) - .and_raise(/Failed/) - - expect(Gitlab::GitLogger).to receive(:warn) - expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) - - subject.perform(*params) - end - - it 'does nothing if the database is read-only' do - allow(Gitlab::Database).to receive(:read_only?) { true } - expect_any_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences).not_to receive(:run!) - - subject.perform(*params) - - expect(project.lfs_objects.reload).to include(lfs_object) - end - end - end - - context 'when no lease can be obtained' do - before do - expect(subject).to receive(:try_obtain_lease).and_return(false) - end - - it 'returns silently' do - expect(subject).not_to receive(:command) - expect_any_instance_of(Repository).not_to receive(:expire_branches_cache).and_call_original - expect_any_instance_of(Repository).not_to receive(:branch_names).and_call_original - expect_any_instance_of(Repository).not_to receive(:has_visible_content?).and_call_original - - subject.perform(*params) - end - end - end - - context "repack_full" do - let(:task) { :full_repack } - let(:gitaly_task) { :repack_full } - - before do - expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + it 'calls the Projects::GitGarbageGitGarbageCollectWorker with the same params' do + expect_next_instance_of(Projects::GitGarbageCollectWorker) do |instance| + expect(instance).to receive(:perform).with(*params) end - it_behaves_like 'it calls Gitaly' - it_behaves_like 'it updates the project statistics' - end - - context "pack_refs" do - let(:task) { :pack_refs } - let(:gitaly_task) { :pack_refs } - - before do - expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) - end - - it "calls Gitaly" do - expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(task) - .and_return(nil) - - subject.perform(*params) - end - - it 'does not update the project statistics' do - expect(Projects::UpdateStatisticsService).not_to receive(:new) - - subject.perform(*params) - end - end - - context "repack_incremental" do - let(:task) { :incremental_repack } - let(:gitaly_task) { :repack_incremental } - - before do - expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) - end - - it_behaves_like 'it calls Gitaly' - it_behaves_like 'it updates the project statistics' - end - - shared_examples 'gc tasks' do - before do - allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid) - allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled) - end - - it 'incremental repack adds a new packfile' do - create_objects(project) - before_packs = packs(project) - - expect(before_packs.count).to be >= 1 - - subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid) - after_packs = packs(project) - - # Exactly one new pack should have been created - expect(after_packs.count).to eq(before_packs.count + 1) - - # Previously existing packs are still around - expect(before_packs & after_packs).to eq(before_packs) - end - - it 'full repack consolidates into 1 packfile' do - create_objects(project) - subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid) - before_packs = packs(project) - - expect(before_packs.count).to be >= 2 - - subject.perform(project.id, 'full_repack', lease_key, lease_uuid) - after_packs = packs(project) - - expect(after_packs.count).to eq(1) - - # Previously existing packs should be gone now - expect(after_packs - before_packs).to eq(after_packs) - - expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled) - end - - it 'gc consolidates into 1 packfile and updates packed-refs' do - create_objects(project) - before_packs = packs(project) - before_packed_refs = packed_refs(project) - - expect(before_packs.count).to be >= 1 - - expect_any_instance_of(Gitlab::GitalyClient::RepositoryService) - .to receive(:garbage_collect) - .with(bitmaps_enabled, prune: false) - .and_call_original - - subject.perform(project.id, 'gc', lease_key, lease_uuid) - after_packed_refs = packed_refs(project) - after_packs = packs(project) - - expect(after_packs.count).to eq(1) - - # Previously existing packs should be gone now - expect(after_packs - before_packs).to eq(after_packs) - - # The packed-refs file should have been updated during 'git gc' - expect(before_packed_refs).not_to eq(after_packed_refs) - - expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled) - end - - it 'cleans up repository after finishing' do - expect_any_instance_of(Project).to receive(:cleanup).and_call_original - - subject.perform(project.id, 'gc', lease_key, lease_uuid) - end - - it 'prune calls garbage_collect with the option prune: true' do - expect_any_instance_of(Gitlab::GitalyClient::RepositoryService) - .to receive(:garbage_collect) - .with(bitmaps_enabled, prune: true) - .and_return(nil) - - subject.perform(project.id, 'prune', lease_key, lease_uuid) - end - end - - context 'with bitmaps enabled' do - let(:bitmaps_enabled) { true } - - include_examples 'gc tasks' - end - - context 'with bitmaps disabled' do - let(:bitmaps_enabled) { false } - - include_examples 'gc tasks' - end - end - - # Create a new commit on a random new branch - def create_objects(project) - rugged = rugged_repo(project.repository) - old_commit = rugged.branches.first.target - new_commit_sha = Rugged::Commit.create( - rugged, - message: "hello world #{SecureRandom.hex(6)}", - author: { email: 'foo@bar', name: 'baz' }, - committer: { email: 'foo@bar', name: 'baz' }, - tree: old_commit.tree, - parents: [old_commit] - ) - rugged.references.create("refs/heads/#{SecureRandom.hex(6)}", new_commit_sha) - end - - def packs(project) - Gitlab::GitalyClient::StorageSettings.allow_disk_access do - Dir["#{project.repository.path_to_repo}/objects/pack/*.pack"] + subject.perform(*params) end end - - def packed_refs(project) - path = "#{project.repository.path_to_repo}/packed-refs" - FileUtils.touch(path) - File.read(path) - end - - def bitmap_path(pack) - pack.sub(/\.pack\z/, '.bitmap') - end end diff --git a/spec/workers/projects/git_garbage_collect_worker_spec.rb b/spec/workers/projects/git_garbage_collect_worker_spec.rb new file mode 100644 index 00000000000..9ef1cce117e --- /dev/null +++ b/spec/workers/projects/git_garbage_collect_worker_spec.rb @@ -0,0 +1,374 @@ +# frozen_string_literal: true + +require 'fileutils' +require 'spec_helper' + +RSpec.describe Projects::GitGarbageCollectWorker do + include GitHelpers + + let_it_be(:project) { create(:project, :repository) } + + let!(:lease_uuid) { SecureRandom.uuid } + let!(:lease_key) { "project_housekeeping:#{project.id}" } + let(:params) { [project.id, task, lease_key, lease_uuid] } + let(:shell) { Gitlab::Shell.new } + let(:repository) { project.repository } + + subject { described_class.new } + + before do + allow(subject).to receive(:find_project).and_return(project) + end + + shared_examples 'it calls Gitaly' do + specify do + repository_service = instance_double(Gitlab::GitalyClient::RepositoryService) + + expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service) + expect(repository_service).to receive(gitaly_task) + + subject.perform(*params) + end + end + + shared_examples 'it updates the project statistics' do + it 'updates the project statistics' do + expect_next_instance_of(Projects::UpdateStatisticsService, project, nil, statistics: [:repository_size, :lfs_objects_size]) do |service| + expect(service).to receive(:execute) + end + + subject.perform(*params) + end + + it 'does nothing if the database is read-only' do + allow(Gitlab::Database).to receive(:read_only?) { true } + + expect(Projects::UpdateStatisticsService).not_to receive(:new) + + subject.perform(*params) + end + end + + describe '#perform', :aggregate_failures do + let(:gitaly_task) { :garbage_collect } + let(:task) { :gc } + + context 'with active lease_uuid' do + before do + allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + end + + it_behaves_like 'it calls Gitaly' + it_behaves_like 'it updates the project statistics' + + it "flushes ref caches when the task if 'gc'" do + expect(subject).to receive(:renew_lease).with(lease_key, lease_uuid).and_call_original + expect(repository).to receive(:expire_branches_cache).and_call_original + expect(repository).to receive(:branch_names).and_call_original + expect(repository).to receive(:has_visible_content?).and_call_original + expect(repository.raw_repository).to receive(:has_visible_content?).and_call_original + + subject.perform(*params) + end + + it 'handles gRPC errors' do + allow_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance| + allow(instance).to receive(:garbage_collect).and_raise(GRPC::NotFound) + end + + expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository) + end + end + + context 'with different lease than the active one' do + before do + allow(subject).to receive(:get_lease_uuid).and_return(SecureRandom.uuid) + end + + it 'returns silently' do + expect(repository).not_to receive(:expire_branches_cache).and_call_original + expect(repository).not_to receive(:branch_names).and_call_original + expect(repository).not_to receive(:has_visible_content?).and_call_original + + subject.perform(*params) + end + end + + context 'with no active lease' do + let(:params) { [project.id] } + + before do + allow(subject).to receive(:get_lease_uuid).and_return(false) + end + + context 'when is able to get the lease' do + before do + allow(subject).to receive(:try_obtain_lease).and_return(SecureRandom.uuid) + end + + it_behaves_like 'it calls Gitaly' + it_behaves_like 'it updates the project statistics' + + it "flushes ref caches when the task if 'gc'" do + expect(subject).to receive(:get_lease_uuid).with("git_gc:#{task}:#{project.id}").and_return(false) + expect(repository).to receive(:expire_branches_cache).and_call_original + expect(repository).to receive(:branch_names).and_call_original + expect(repository).to receive(:has_visible_content?).and_call_original + expect(repository.raw_repository).to receive(:has_visible_content?).and_call_original + + subject.perform(*params) + end + + context 'when the repository has joined a pool' do + let!(:pool) { create(:pool_repository, :ready) } + let(:project) { pool.source_project } + + it 'ensures the repositories are linked' do + expect(project.pool_repository).to receive(:link_repository).once + + subject.perform(*params) + end + end + + context 'LFS object garbage collection' do + before do + stub_lfs_setting(enabled: true) + end + + let_it_be(:lfs_reference) { create(:lfs_objects_project, project: project) } + let(:lfs_object) { lfs_reference.lfs_object } + + it 'cleans up unreferenced LFS objects' do + expect_next_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences) do |svc| + expect(svc.project).to eq(project) + expect(svc.dry_run).to be_falsy + expect(svc).to receive(:run!).and_call_original + end + + subject.perform(*params) + + expect(project.lfs_objects.reload).not_to include(lfs_object) + end + + it 'catches and logs exceptions' do + allow_next_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences) do |svc| + allow(svg).to receive(:run!).and_raise(/Failed/) + end + + expect(Gitlab::GitLogger).to receive(:warn) + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) + + subject.perform(*params) + end + + it 'does nothing if the database is read-only' do + allow(Gitlab::Database).to receive(:read_only?) { true } + expect(Gitlab::Cleanup::OrphanLfsFileReferences).not_to receive(:new) + + subject.perform(*params) + + expect(project.lfs_objects.reload).to include(lfs_object) + end + end + end + + context 'when no lease can be obtained' do + it 'returns silently' do + expect(subject).to receive(:try_obtain_lease).and_return(false) + + expect(subject).not_to receive(:command) + expect(repository).not_to receive(:expire_branches_cache).and_call_original + expect(repository).not_to receive(:branch_names).and_call_original + expect(repository).not_to receive(:has_visible_content?).and_call_original + + subject.perform(*params) + end + end + end + + context 'repack_full' do + let(:task) { :full_repack } + let(:gitaly_task) { :repack_full } + + before do + expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + end + + it_behaves_like 'it calls Gitaly' + it_behaves_like 'it updates the project statistics' + end + + context 'pack_refs' do + let(:task) { :pack_refs } + let(:gitaly_task) { :pack_refs } + + before do + expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + end + + it 'calls Gitaly' do + repository_service = instance_double(Gitlab::GitalyClient::RefService) + + expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service) + expect(repository_service).to receive(gitaly_task) + + subject.perform(*params) + end + + it 'does not update the project statistics' do + expect(Projects::UpdateStatisticsService).not_to receive(:new) + + subject.perform(*params) + end + end + + context 'repack_incremental' do + let(:task) { :incremental_repack } + let(:gitaly_task) { :repack_incremental } + + before do + expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + end + + it_behaves_like 'it calls Gitaly' + it_behaves_like 'it updates the project statistics' + end + + shared_examples 'gc tasks' do + before do + allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid) + allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled) + end + + it 'incremental repack adds a new packfile' do + create_objects(project) + before_packs = packs(project) + + expect(before_packs.count).to be >= 1 + + subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid) + after_packs = packs(project) + + # Exactly one new pack should have been created + expect(after_packs.count).to eq(before_packs.count + 1) + + # Previously existing packs are still around + expect(before_packs & after_packs).to eq(before_packs) + end + + it 'full repack consolidates into 1 packfile' do + create_objects(project) + subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid) + before_packs = packs(project) + + expect(before_packs.count).to be >= 2 + + subject.perform(project.id, 'full_repack', lease_key, lease_uuid) + after_packs = packs(project) + + expect(after_packs.count).to eq(1) + + # Previously existing packs should be gone now + expect(after_packs - before_packs).to eq(after_packs) + + expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled) + end + + it 'gc consolidates into 1 packfile and updates packed-refs' do + create_objects(project) + before_packs = packs(project) + before_packed_refs = packed_refs(project) + + expect(before_packs.count).to be >= 1 + + # It's quite difficult to use `expect_next_instance_of` in this place + # because the RepositoryService is instantiated several times to do + # some repository calls like `exists?`, `create_repository`, ... . + # Therefore, since we're instantiating the object several times, + # RSpec has troubles figuring out which instance is the next and which + # one we want to mock. + # Besides, at this point, we actually want to perform the call to Gitaly, + # otherwise we would just use `instance_double` like in other parts of the + # spec file. + expect_any_instance_of(Gitlab::GitalyClient::RepositoryService) # rubocop:disable RSpec/AnyInstanceOf + .to receive(:garbage_collect) + .with(bitmaps_enabled, prune: false) + .and_call_original + + subject.perform(project.id, 'gc', lease_key, lease_uuid) + after_packed_refs = packed_refs(project) + after_packs = packs(project) + + expect(after_packs.count).to eq(1) + + # Previously existing packs should be gone now + expect(after_packs - before_packs).to eq(after_packs) + + # The packed-refs file should have been updated during 'git gc' + expect(before_packed_refs).not_to eq(after_packed_refs) + + expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled) + end + + it 'cleans up repository after finishing' do + expect(project).to receive(:cleanup).and_call_original + + subject.perform(project.id, 'gc', lease_key, lease_uuid) + end + + it 'prune calls garbage_collect with the option prune: true' do + repository_service = instance_double(Gitlab::GitalyClient::RepositoryService) + + expect(subject).to receive(:get_gitaly_client).with(:prune, repository.raw_repository).and_return(repository_service) + expect(repository_service).to receive(:garbage_collect).with(bitmaps_enabled, prune: true) + + subject.perform(project.id, 'prune', lease_key, lease_uuid) + end + end + + context 'with bitmaps enabled' do + let(:bitmaps_enabled) { true } + + include_examples 'gc tasks' + end + + context 'with bitmaps disabled' do + let(:bitmaps_enabled) { false } + + include_examples 'gc tasks' + end + end + + # Create a new commit on a random new branch + def create_objects(project) + rugged = rugged_repo(project.repository) + old_commit = rugged.branches.first.target + new_commit_sha = Rugged::Commit.create( + rugged, + message: "hello world #{SecureRandom.hex(6)}", + author: { email: 'foo@bar', name: 'baz' }, + committer: { email: 'foo@bar', name: 'baz' }, + tree: old_commit.tree, + parents: [old_commit] + ) + rugged.references.create("refs/heads/#{SecureRandom.hex(6)}", new_commit_sha) + end + + def packs(project) + Dir["#{path_to_repo}/objects/pack/*.pack"] + end + + def packed_refs(project) + path = File.join(path_to_repo, 'packed-refs') + FileUtils.touch(path) + File.read(path) + end + + def path_to_repo + @path_to_repo ||= File.join(TestEnv.repos_path, project.repository.relative_path) + end + + def bitmap_path(pack) + pack.sub(/\.pack\z/, '.bitmap') + end +end |