|
| 1 | +import { Test, type TestingModule } from '@nestjs/testing' |
| 2 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 3 | +import type { Mocked } from 'vitest' |
| 4 | +import { dump } from 'js-yaml' |
| 5 | +import { ArgoCDControllerService } from './argocd-controller.service' |
| 6 | +import { ArgoCDDatastoreService, type ProjectWithDetails } from './argocd-datastore.service' |
| 7 | +import { ConfigurationService } from '@/cpin-module/infrastructure/configuration/configuration.service' |
| 8 | +import { GitlabService } from '../gitlab/gitlab.service' |
| 9 | +import { VaultService } from '../vault/vault.service' |
| 10 | +import type { ProjectSchema } from '@gitbeaker/core' |
| 11 | +import { generateNamespaceName } from '@cpn-console/shared' |
| 12 | + |
| 13 | +function createArgoCDControllerServiceTestingModule() { |
| 14 | + return Test.createTestingModule({ |
| 15 | + providers: [ |
| 16 | + ArgoCDControllerService, |
| 17 | + { |
| 18 | + provide: ArgoCDDatastoreService, |
| 19 | + useValue: { |
| 20 | + getAllProjects: vi.fn(), |
| 21 | + } satisfies Partial<ArgoCDDatastoreService>, |
| 22 | + }, |
| 23 | + { |
| 24 | + provide: ConfigurationService, |
| 25 | + useValue: { |
| 26 | + argoNamespace: 'argocd', |
| 27 | + argocdUrl: 'http://argocd', |
| 28 | + argocdExtraRepositories: 'repo3', |
| 29 | + dsoEnvChartVersion: 'dso-env-1.6.0', |
| 30 | + dsoNsChartVersion: 'dso-ns-1.1.5', |
| 31 | + } satisfies Partial<ConfigurationService>, |
| 32 | + }, |
| 33 | + { |
| 34 | + provide: GitlabService, |
| 35 | + useValue: { |
| 36 | + getOrCreateInfraGroupRepo: vi.fn(), |
| 37 | + getProjectGroupPublicUrl: vi.fn(), |
| 38 | + getInfraGroupRepoPublicUrl: vi.fn(), |
| 39 | + maybeCommitUpdate: vi.fn(), |
| 40 | + maybeCommitDelete: vi.fn(), |
| 41 | + listFiles: vi.fn(), |
| 42 | + } satisfies Partial<GitlabService>, |
| 43 | + }, |
| 44 | + { |
| 45 | + provide: VaultService, |
| 46 | + useValue: { |
| 47 | + getProjectValues: vi.fn(), |
| 48 | + } satisfies Partial<VaultService>, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +describe('argoCDControllerService', () => { |
| 55 | + let service: ArgoCDControllerService |
| 56 | + let datastore: Mocked<ArgoCDDatastoreService> |
| 57 | + let gitlab: Mocked<GitlabService> |
| 58 | + let vault: Mocked<VaultService> |
| 59 | + |
| 60 | + beforeEach(async () => { |
| 61 | + vi.clearAllMocks() |
| 62 | + const module: TestingModule = await createArgoCDControllerServiceTestingModule().compile() |
| 63 | + service = module.get(ArgoCDControllerService) |
| 64 | + datastore = module.get(ArgoCDDatastoreService) |
| 65 | + gitlab = module.get(GitlabService) |
| 66 | + vault = module.get(VaultService) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should be defined', () => { |
| 70 | + expect(service).toBeDefined() |
| 71 | + }) |
| 72 | + |
| 73 | + describe('reconcile', () => { |
| 74 | + it('should sync project environments', async () => { |
| 75 | + const mockProject = { |
| 76 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 77 | + slug: 'project-1', |
| 78 | + name: 'Project 1', |
| 79 | + environments: [ |
| 80 | + { id: '123e4567-e89b-12d3-a456-426614174001', name: 'dev', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }, |
| 81 | + { id: '123e4567-e89b-12d3-a456-426614174002', name: 'prod', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }, |
| 82 | + ], |
| 83 | + clusters: [ |
| 84 | + { id: 'c1', label: 'cluster-1', zone: { slug: 'zone-1' } }, |
| 85 | + ], |
| 86 | + repositories: [ |
| 87 | + { |
| 88 | + id: 'repo-1', |
| 89 | + internalRepoName: 'infra-repo', |
| 90 | + url: 'http://gitlab/infra-repo', |
| 91 | + isInfra: true, |
| 92 | + }, |
| 93 | + ], |
| 94 | + plugins: [{ pluginName: 'argocd', key: 'extraRepositories', value: 'repo2' }], |
| 95 | + } as unknown as ProjectWithDetails |
| 96 | + |
| 97 | + datastore.getAllProjects.mockResolvedValue([mockProject]) |
| 98 | + gitlab.getOrCreateInfraGroupRepo.mockResolvedValue({ id: 100, http_url_to_repo: 'http://gitlab/infra' } as ProjectSchema) |
| 99 | + gitlab.getProjectGroupPublicUrl.mockResolvedValue('http://gitlab/group') |
| 100 | + gitlab.getInfraGroupRepoPublicUrl.mockResolvedValue('http://gitlab/infra-repo') |
| 101 | + gitlab.listFiles.mockResolvedValue([]) |
| 102 | + vault.getProjectValues.mockResolvedValue({ secret: 'value' }) |
| 103 | + |
| 104 | + const results = await service.reconcile() |
| 105 | + |
| 106 | + expect(results).toHaveLength(3) // 2 envs + 1 cleanup (1 zone) |
| 107 | + |
| 108 | + // Verify Gitlab calls |
| 109 | + expect(gitlab.maybeCommitUpdate).toHaveBeenCalledTimes(2) |
| 110 | + expect(gitlab.maybeCommitUpdate).toHaveBeenCalledWith( |
| 111 | + 100, |
| 112 | + [ |
| 113 | + { |
| 114 | + content: dump({ |
| 115 | + common: { |
| 116 | + 'dso/project': 'Project 1', |
| 117 | + 'dso/project.id': '123e4567-e89b-12d3-a456-426614174000', |
| 118 | + 'dso/project.slug': 'project-1', |
| 119 | + 'dso/environment': 'dev', |
| 120 | + 'dso/environment.id': '123e4567-e89b-12d3-a456-426614174001', |
| 121 | + }, |
| 122 | + argocd: { |
| 123 | + cluster: 'in-cluster', |
| 124 | + namespace: 'argocd', |
| 125 | + project: 'project-1-dev-6293', |
| 126 | + envChartVersion: 'dso-env-1.6.0', |
| 127 | + nsChartVersion: 'dso-ns-1.1.5', |
| 128 | + }, |
| 129 | + environment: { |
| 130 | + valueFileRepository: 'http://gitlab/infra', |
| 131 | + valueFileRevision: 'HEAD', |
| 132 | + valueFilePath: 'Project 1/cluster-1/dev/values.yaml', |
| 133 | + roGroup: '/project-project-1/console/dev/RO', |
| 134 | + rwGroup: '/project-project-1/console/dev/RW', |
| 135 | + }, |
| 136 | + application: { |
| 137 | + quota: { |
| 138 | + cpu: 1, |
| 139 | + gpu: 0, |
| 140 | + memory: '1Gi', |
| 141 | + }, |
| 142 | + sourceRepositories: [ |
| 143 | + 'http://gitlab/group/**', |
| 144 | + 'repo3', |
| 145 | + 'repo2', |
| 146 | + ], |
| 147 | + destination: { |
| 148 | + namespace: generateNamespaceName(mockProject.id, mockProject.environments[0].id), |
| 149 | + name: 'cluster-1', |
| 150 | + }, |
| 151 | + autosync: true, |
| 152 | + vault: { secret: 'value' }, |
| 153 | + repositories: [ |
| 154 | + { |
| 155 | + repoURL: 'http://gitlab/infra-repo', |
| 156 | + targetRevision: 'HEAD', |
| 157 | + path: '.', |
| 158 | + valueFiles: [], |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + }), |
| 163 | + filePath: 'Project 1/cluster-1/dev/values.yaml', |
| 164 | + }, |
| 165 | + ], |
| 166 | + 'ci: :robot_face: Update Project 1/cluster-1/dev/values.yaml', |
| 167 | + ) |
| 168 | + }) |
| 169 | + |
| 170 | + it('should handle errors gracefully', async () => { |
| 171 | + const mockProject = { |
| 172 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 173 | + slug: 'project-1', |
| 174 | + name: 'Project 1', |
| 175 | + environments: [{ id: '123e4567-e89b-12d3-a456-426614174001', name: 'dev', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }], |
| 176 | + clusters: [ |
| 177 | + { id: 'c1', label: 'cluster-1', zone: { slug: 'zone-1' } }, |
| 178 | + ], |
| 179 | + } as unknown as ProjectWithDetails |
| 180 | + |
| 181 | + datastore.getAllProjects.mockResolvedValue([mockProject]) |
| 182 | + gitlab.getOrCreateInfraGroupRepo.mockRejectedValue(new Error('Sync failed')) |
| 183 | + |
| 184 | + const results = await service.reconcile() |
| 185 | + |
| 186 | + // 1 env (fails) + 1 cleanup (fails because getOrCreateInfraProject fails) |
| 187 | + expect(results).toHaveLength(2) |
| 188 | + const failed = results.filter((r: any) => r.status === 'rejected') |
| 189 | + expect(failed).toHaveLength(2) |
| 190 | + }) |
| 191 | + }) |
| 192 | +}) |
0 commit comments