|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 2 | +import type { Mocked } from 'vitest' |
| 3 | +import { load } from 'js-yaml' |
| 4 | +import { ArgoCDControllerService } from './argocd-controller.service' |
| 5 | +import type { ArgoCDDatastoreService, ProjectWithDetails } from './argocd-datastore.service' |
| 6 | +import type { ConfigurationService } from '@/cpin-module/infrastructure/configuration/configuration.service' |
| 7 | +import type { GitlabService } from '../gitlab/gitlab.service' |
| 8 | +import type { VaultService } from '../vault/vault.service' |
| 9 | + |
| 10 | +const mockArgoCDDatastore = { |
| 11 | + getAllProjects: vi.fn(), |
| 12 | +} as unknown as Mocked<ArgoCDDatastoreService> |
| 13 | + |
| 14 | +const mockConfigService = { |
| 15 | + keycloakControllerPurgeOrphans: true, |
| 16 | + argoNamespace: 'argocd', |
| 17 | + argocdUrl: 'http://argocd', |
| 18 | + argocdExtraRepositories: 'repo3', |
| 19 | + dsoEnvChartVersion: 'dso-env-1.6.0', |
| 20 | + dsoNsChartVersion: 'dso-ns-1.1.5', |
| 21 | +} as unknown as Mocked<ConfigurationService> |
| 22 | + |
| 23 | +const mockGitlabService = { |
| 24 | + getOrCreateInfraProject: vi.fn(), |
| 25 | + getPublicGroupUrl: vi.fn(), |
| 26 | + getPublicRepoUrl: vi.fn(), |
| 27 | + commitCreateOrUpdate: vi.fn(), |
| 28 | + commitDelete: vi.fn(), |
| 29 | + listFiles: vi.fn(), |
| 30 | +} as unknown as Mocked<GitlabService> |
| 31 | + |
| 32 | +const mockVaultService = { |
| 33 | + getProjectValues: vi.fn(), |
| 34 | +} as unknown as Mocked<VaultService> |
| 35 | + |
| 36 | +describe('argoCDControllerService', () => { |
| 37 | + let service: ArgoCDControllerService |
| 38 | + let datastore: Mocked<ArgoCDDatastoreService> |
| 39 | + let gitlabService: Mocked<GitlabService> |
| 40 | + let vaultService: Mocked<VaultService> |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + service = new ArgoCDControllerService( |
| 44 | + mockArgoCDDatastore, |
| 45 | + mockConfigService, |
| 46 | + mockGitlabService, |
| 47 | + mockVaultService, |
| 48 | + ) |
| 49 | + datastore = mockArgoCDDatastore |
| 50 | + gitlabService = mockGitlabService |
| 51 | + vaultService = mockVaultService |
| 52 | + vi.clearAllMocks() |
| 53 | + }) |
| 54 | + |
| 55 | + it('should be defined', () => { |
| 56 | + expect(service).toBeDefined() |
| 57 | + }) |
| 58 | + |
| 59 | + describe('reconcile', () => { |
| 60 | + it('should sync project environments', async () => { |
| 61 | + const mockProject = { |
| 62 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 63 | + slug: 'project-1', |
| 64 | + name: 'Project 1', |
| 65 | + environments: [ |
| 66 | + { id: '123e4567-e89b-12d3-a456-426614174001', name: 'dev', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }, |
| 67 | + { id: '123e4567-e89b-12d3-a456-426614174002', name: 'prod', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }, |
| 68 | + ], |
| 69 | + clusters: [ |
| 70 | + { id: 'c1', label: 'cluster-1', zone: { slug: 'zone-1' } }, |
| 71 | + ], |
| 72 | + repositories: [ |
| 73 | + { |
| 74 | + id: 'repo-1', |
| 75 | + internalRepoName: 'infra-repo', |
| 76 | + url: 'http://gitlab/infra-repo', |
| 77 | + isInfra: true, |
| 78 | + }, |
| 79 | + ], |
| 80 | + plugins: [{ pluginName: 'argocd', key: 'extraRepositories', value: 'repo2' }], |
| 81 | + } as unknown as ProjectWithDetails |
| 82 | + |
| 83 | + datastore.getAllProjects.mockResolvedValue([mockProject]) |
| 84 | + gitlabService.getOrCreateInfraProject.mockResolvedValue({ id: 100, http_url_to_repo: 'http://gitlab/infra' }) |
| 85 | + gitlabService.getPublicGroupUrl.mockResolvedValue('http://gitlab/group') |
| 86 | + gitlabService.getPublicRepoUrl.mockResolvedValue('http://gitlab/infra-repo') |
| 87 | + gitlabService.listFiles.mockResolvedValue([]) |
| 88 | + vaultService.getProjectValues.mockResolvedValue({ secret: 'value' }) |
| 89 | + |
| 90 | + const results = await (service as any).reconcile() |
| 91 | + |
| 92 | + expect(results).toHaveLength(3) // 2 envs + 1 cleanup (1 zone) |
| 93 | + |
| 94 | + // Verify Gitlab calls |
| 95 | + expect(gitlabService.commitCreateOrUpdate).toHaveBeenCalledTimes(2) |
| 96 | + |
| 97 | + const calls = gitlabService.commitCreateOrUpdate.mock.calls |
| 98 | + const devCall = calls.find(c => c[2] === 'Project 1/cluster-1/dev/values.yaml') |
| 99 | + expect(devCall).toBeDefined() |
| 100 | + |
| 101 | + const content = load(devCall![1]) as any |
| 102 | + expect(content).toMatchObject({ |
| 103 | + common: { |
| 104 | + 'dso/project': 'Project 1', |
| 105 | + 'dso/project.slug': 'project-1', |
| 106 | + 'dso/environment': 'dev', |
| 107 | + }, |
| 108 | + argocd: { |
| 109 | + namespace: 'argocd', |
| 110 | + project: expect.stringMatching(/^project-1-dev-[a-f0-9]{4}$/), |
| 111 | + }, |
| 112 | + environment: { |
| 113 | + valueFileRepository: 'http://gitlab/infra', |
| 114 | + valueFilePath: 'Project 1/cluster-1/dev/values.yaml', |
| 115 | + roGroup: '/project-project-1/console/dev/RO', |
| 116 | + rwGroup: '/project-project-1/console/dev/RW', |
| 117 | + }, |
| 118 | + application: { |
| 119 | + quota: { |
| 120 | + cpu: 1, |
| 121 | + gpu: 0, |
| 122 | + memory: '1Gi', |
| 123 | + }, |
| 124 | + sourceRepositories: expect.arrayContaining([ |
| 125 | + expect.stringContaining('repo3'), |
| 126 | + expect.stringContaining('repo2'), |
| 127 | + expect.stringContaining('http://gitlab/group'), |
| 128 | + ]), |
| 129 | + destination: { |
| 130 | + namespace: expect.any(String), |
| 131 | + name: 'cluster-1', |
| 132 | + }, |
| 133 | + autosync: true, |
| 134 | + vault: { secret: 'value' }, |
| 135 | + repositories: [ |
| 136 | + { |
| 137 | + repoURL: 'http://gitlab/infra-repo', |
| 138 | + targetRevision: 'HEAD', |
| 139 | + path: '.', |
| 140 | + valueFiles: [], |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + it('should handle errors gracefully', async () => { |
| 148 | + const mockProject = { |
| 149 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 150 | + slug: 'project-1', |
| 151 | + name: 'Project 1', |
| 152 | + environments: [{ id: '123e4567-e89b-12d3-a456-426614174001', name: 'dev', clusterId: 'c1', cpu: 1, gpu: 0, memory: 1, autosync: true }], |
| 153 | + clusters: [ |
| 154 | + { id: 'c1', label: 'cluster-1', zone: { slug: 'zone-1' } }, |
| 155 | + ], |
| 156 | + } as unknown as ProjectWithDetails |
| 157 | + |
| 158 | + datastore.getAllProjects.mockResolvedValue([mockProject]) |
| 159 | + gitlabService.getOrCreateInfraProject.mockRejectedValue(new Error('Sync failed')) |
| 160 | + |
| 161 | + const results = await (service as any).reconcile() |
| 162 | + |
| 163 | + // 1 env (fails) + 1 cleanup (fails because getOrCreateInfraProject fails) |
| 164 | + expect(results).toHaveLength(2) |
| 165 | + const failed = results.filter((r: any) => r.status === 'rejected') |
| 166 | + expect(failed).toHaveLength(2) |
| 167 | + }) |
| 168 | + }) |
| 169 | +}) |
0 commit comments