From 9c5120146e8724a2b8c0ec504eecbcd6407b757d Mon Sep 17 00:00:00 2001 From: Josef Kopriva Date: Tue, 22 Oct 2024 10:10:36 +0200 Subject: [PATCH] RHTAP-3056 Fixes after rebase Signed-off-by: Josef Kopriva --- src/apis/ci/jenkins.ts | 5 -- src/apis/git-providers/gitlab.ts | 54 ------------------ src/utils/test.utils.ts | 14 ++--- .../test-config/github_positive_suite.ts | 21 ------- .../suites-config/gitlab_gitlabci_suite.ts | 57 ++----------------- yarn.lock | 2 +- 6 files changed, 10 insertions(+), 143 deletions(-) diff --git a/src/apis/ci/jenkins.ts b/src/apis/ci/jenkins.ts index 95ba5a7..a77865f 100644 --- a/src/apis/ci/jenkins.ts +++ b/src/apis/ci/jenkins.ts @@ -92,10 +92,6 @@ export class JenkinsCI extends Utils { } return response.status === 200; } catch (error) { -<<<<<<< HEAD - console.error('Error checking job existence:', error); - throw error; -======= if (axios.isAxiosError(error)) { if (error.response && error.response.status === 404) { return false; @@ -107,7 +103,6 @@ export class JenkinsCI extends Utils { console.error('Error checking job existence:', error); throw error; } ->>>>>>> ada4e54 (RHTAP-3056 GitLab CI tests fixes and refactoring.) } } diff --git a/src/apis/git-providers/gitlab.ts b/src/apis/git-providers/gitlab.ts index 36754d3..b4d75a7 100644 --- a/src/apis/git-providers/gitlab.ts +++ b/src/apis/git-providers/gitlab.ts @@ -83,24 +83,6 @@ export class GitLabProvider extends Utils { } } - /** - * checkIfRepositoryHaveFile - */ - public async checkIfRepositoryHaveFile(repositoryID: number, filePath: string): Promise { - try { - await this.gitlab.RepositoryFiles.show(repositoryID, filePath, 'main'); - return true; - } catch (error: any) { - if (error.response && error.response.status === 404) { - console.log('File does not exist.'); - return false; - } else { - console.error('Error checking file existence:', error); - return false; - } - } - } - /** * name */ @@ -139,42 +121,6 @@ export class GitLabProvider extends Utils { return await this.commitReplacementStringInFile(repositoryID, branchName, 'Jenkinsfile', 'Update creds for Gitlab', stringToFind, replacementString); } - public async enableACSJenkins(repositoryID: number, branchName: string): Promise { - return await this.commitReplacementStringInFile(repositoryID, branchName, 'rhtap/env.sh', 'Update ACS scan for Gitlab', `DISABLE_ACS=true`, `DISABLE_ACS=false`); - } - - public async commitReplacementStringInFile(repositoryID: number, branchName: string, filePath: string, commitMessage: string, textToReplace: string, replacement: string): Promise { - try { - // Get the current content of the file - const file = await this.gitlab.RepositoryFiles.show(repositoryID, filePath, branchName); - const fileContent = Buffer.from(file.content, 'base64').toString('utf-8'); - - // Replace specific text - const updatedContent = fileContent.replace(textToReplace,replacement); - - // Encode the updated content to base64 - const encodedContent = Buffer.from(updatedContent).toString('base64'); - - // Create a commit to update the file - await this.gitlab.RepositoryFiles.edit( - repositoryID, - filePath, - branchName, - encodedContent, - commitMessage, - { - encoding: 'base64', - } - ); - - console.log(`${filePath} updated successfully for username.`); - return true; - } catch (error: any) { - console.error('Error updating ${filePath}:', error); - return false; - } - } - public async createMergeRequestWithPromotionImage(repositoryID: number, targetBranch: string, componentName: string, fromEnvironment: string, toEnvironment: string): Promise { diff --git a/src/utils/test.utils.ts b/src/utils/test.utils.ts index f5ac5ac..bc1f9ae 100644 --- a/src/utils/test.utils.ts +++ b/src/utils/test.utils.ts @@ -3,12 +3,9 @@ import { GitHubProvider } from "../../src/apis/git-providers/github"; import { Kubernetes } from "../../src/apis/kubernetes/kube"; import { DeveloperHubClient } from "../../src/apis/backstage/developer-hub"; import { JenkinsCI } from "../../src/apis/ci/jenkins"; -<<<<<<< HEAD import { ScaffolderScaffoldOptions } from "@backstage/plugin-scaffolder-react"; -======= import { syncArgoApplication } from "./argocd"; import { TaskIdReponse } from "../../src/apis/backstage/types"; ->>>>>>> ada4e54 (RHTAP-3056 GitLab CI tests fixes and refactoring.) export async function cleanAfterTestGitHub(gitHubClient: GitHubProvider, kubeClient: Kubernetes, rootNamespace: string, githubOrganization: string, repositoryName: string) { @@ -102,12 +99,6 @@ export async function getGitLabProvider(kubeClient: Kubernetes) { } } -<<<<<<< HEAD -export async function checkEnvVariablesGitLab(componentRootNamespace: string, gitLabOrganization: string, quayImageOrg: string, developmentNamespace: string, kubeClient: Kubernetes) { - if (componentRootNamespace === '') { - throw new Error("The 'APPLICATION_TEST_NAMESPACE' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } -======= export async function getCosignPassword(kubeClient: Kubernetes) { if (process.env.COSIGN_SECRET_PASSWORD) { return process.env.COSIGN_SECRET_PASSWORD; @@ -161,7 +152,10 @@ export async function waitForArgoSyncAndRouteContent(kubeClient: Kubernetes, bac } ->>>>>>> ada4e54 (RHTAP-3056 GitLab CI tests fixes and refactoring.) +export async function checkEnvVariablesGitLab(componentRootNamespace: string, gitLabOrganization: string, quayImageOrg: string, developmentNamespace: string, kubeClient: Kubernetes) { + if (componentRootNamespace === '') { + throw new Error("The 'APPLICATION_TEST_NAMESPACE' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); + } if (gitLabOrganization === '') { throw new Error("The 'GITLAB_ORGANIZATION' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); diff --git a/tests/gpts/github/test-config/github_positive_suite.ts b/tests/gpts/github/test-config/github_positive_suite.ts index 6416582..a53fae6 100644 --- a/tests/gpts/github/test-config/github_positive_suite.ts +++ b/tests/gpts/github/test-config/github_positive_suite.ts @@ -47,28 +47,7 @@ export const gitHubBasicGoldenPathTemplateTests = (gptTemplate: string) => { gitHubClient = await getGitHubClient(kubeClient); backstageClient = await getDeveloperHubClient(kubeClient); -<<<<<<< HEAD - await checkEnvVariablesGitHub(componentRootNamespace, githubOrganization, quayImageOrg, developmentNamespace, kubeClient); -======= - if (componentRootNamespace === '') { - throw new Error("The 'APPLICATION_TEST_NAMESPACE' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - if (githubOrganization === '') { - throw new Error("The 'GITHUB_ORGANIZATION' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - if (quayImageOrg === '') { - throw new Error("The 'QUAY_IMAGE_ORG' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - const namespaceExists = await kubeClient.namespaceExists(developmentNamespace) - - if (!namespaceExists) { - throw new Error(`The development namespace was not created. Make sure you have created ${developmentNamespace} is created and all secrets are created. Example: 'https://github.com/jduimovich/rhdh/blob/main/default-rhtap-ns-configure'`); - } ->>>>>>> 745a31e (RHTAP-3056 GitLab CI tests) }) /** diff --git a/tests/gpts/gitlab/suites-config/gitlab_gitlabci_suite.ts b/tests/gpts/gitlab/suites-config/gitlab_gitlabci_suite.ts index 65c2ca2..5cf93c1 100644 --- a/tests/gpts/gitlab/suites-config/gitlab_gitlabci_suite.ts +++ b/tests/gpts/gitlab/suites-config/gitlab_gitlabci_suite.ts @@ -3,9 +3,8 @@ import { DeveloperHubClient } from "../../../../src/apis/backstage/developer-hub import { TaskIdReponse } from "../../../../src/apis/backstage/types"; import { GitLabProvider } from "../../../../src/apis/git-providers/gitlab"; import { Kubernetes } from "../../../../src/apis/kubernetes/kube"; -import { ScaffolderScaffoldOptions } from "@backstage/plugin-scaffolder-react"; import { generateRandomChars } from "../../../../src/utils/generator"; -import { cleanAfterTestGitLab, getCosignPassword, getCosignPrivateKey, getCosignPublicKey, getDeveloperHubClient, getGitLabProvider, getRHTAPRootNamespace, waitForArgoSyncAndRouteContent, waitForComponentCreation} from "../../../../src/utils/test.utils"; +import { checkEnvVariablesGitLab, cleanAfterTestGitLab, createTaskCreatorOptionsGitlab, getCosignPassword, getCosignPrivateKey, getCosignPublicKey, getDeveloperHubClient, getGitLabProvider, getRHTAPRootNamespace, waitForArgoSyncAndRouteContent, waitForComponentCreation } from "../../../../src/utils/test.utils"; /** * 1. Creates a component in Red Hat Developer Hub. @@ -48,60 +47,14 @@ export const gitLabProviderGitLabCITests = (softwareTemplateName: string, string gitLabProvider = await getGitLabProvider(kubeClient); backstageClient = await getDeveloperHubClient(kubeClient); - if (componentRootNamespace === '') { - throw new Error("The 'APPLICATION_TEST_NAMESPACE' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - if (gitLabOrganization === '') { - throw new Error("The 'GITLAB_ORGANIZATION' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - if (quayImageOrg === '') { - throw new Error("The 'QUAY_IMAGE_ORG' environment variable is not set. Please ensure that the environment variable is defined properly or you have cluster connection."); - } - - const namespaceExists = await kubeClient.namespaceExists(developmentNamespace) - - if (!namespaceExists) { - throw new Error(`The development namespace was not created. Make sure you have created ${developmentNamespace} is created and all secrets are created. Example: 'https://github.com/jduimovich/rhdh/blob/main/default-rhtap-ns-configure'`); - } + await checkEnvVariablesGitLab(componentRootNamespace, gitLabOrganization, quayImageOrg, developmentNamespace, kubeClient); }) /** * Creates a task in Developer Hub to generate a new component using specified git and kube options. - * - * @param {string} templateRef Refers to the Developer Hub template name. - * @param {object} values Set of options to create the component. - * @param {string} values.branch Default git branch for the component. - * @param {string} values.gitlabServer GitLab server URL. - * @param {string} values.hostType Type of host (e.g., GitLab). - * @param {string} values.imageName Registry image name for the component to be pushed. - * @param {string} values.imageOrg Registry organization name for the component to be pushed. - * @param {string} values.imageRegistry Image registry provider. Default is Quay.io. - * @param {string} values.name Name of the repository to be created in GitLab. - * @param {string} values.namespace Kubernetes namespace where ArgoCD will create component manifests. - * @param {string} values.owner Developer Hub username who initiates the task. - * @param {string} values.repoName Name of the GitLab repository. - * @param {string} values.repoOwner Owner of the GitLab repository. */ it(`creates ${softwareTemplateName} component`, async () => { - const taskCreatorOptions: ScaffolderScaffoldOptions = { - templateRef: `template:default/${softwareTemplateName}`, - values: { - branch: 'main', - glHost: 'gitlab.com', - hostType: 'GitLab', - imageName: quayImageName, - imageOrg: quayImageOrg, - imageRegistry: imageRegistry, - name: repositoryName, - namespace: componentRootNamespace, - owner: "user:guest", - repoName: repositoryName, - glOwner: gitLabOrganization, - ciType: "gitlabci" - } - }; + const taskCreatorOptions = await createTaskCreatorOptionsGitlab(softwareTemplateName, quayImageName, quayImageOrg, imageRegistry, gitLabOrganization, repositoryName, componentRootNamespace, "gitlabci"); // Creating a task in Developer Hub to scaffold the component developerHubTask = await backstageClient.createDeveloperHubTask(taskCreatorOptions); @@ -145,8 +98,8 @@ export const gitLabProviderGitLabCITests = (softwareTemplateName: string, string }, 600000); /** - * Waits for the specified ArgoCD application associated with the DeveloperHub task to be synchronized in the cluster. -*/ + * Waits for the specified ArgoCD application associated with the DeveloperHub task to be synchronized in the cluster. + */ it(`Setup creds for ${softwareTemplateName} pipeline`, async () => { await gitLabProvider.setEnvironmentVariable(gitlabRepositoryID, "COSIGN_PUBLIC_KEY", await getCosignPublicKey(kubeClient)); await gitLabProvider.setEnvironmentVariable(gitlabRepositoryID, "COSIGN_SECRET_KEY", await getCosignPrivateKey(kubeClient)); diff --git a/yarn.lock b/yarn.lock index 51041e5..55d1ef9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8037,4 +8037,4 @@ zod@^3.22.4: zwitch@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-2.0.4.tgz#c827d4b0acb76fc3e685a4c6ec2902d51070e9d7" - integrity sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A== + integrity sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A== \ No newline at end of file