Skip to content

Commit

Permalink
RHTAP-3056 Fixes after rebase
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Kopriva <[email protected]>
  • Loading branch information
jkopriva committed Oct 22, 2024
1 parent d8df155 commit 9c51201
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 143 deletions.
5 changes: 0 additions & 5 deletions src/apis/ci/jenkins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.)
}
}

Expand Down
54 changes: 0 additions & 54 deletions src/apis/git-providers/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ export class GitLabProvider extends Utils {
}
}

/**
* checkIfRepositoryHaveFile
*/
public async checkIfRepositoryHaveFile(repositoryID: number, filePath: string): Promise<boolean> {
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
*/
Expand Down Expand Up @@ -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<boolean> {
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<boolean> {
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<number> {

Expand Down
14 changes: 4 additions & 10 deletions src/utils/test.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand Down
21 changes: 0 additions & 21 deletions tests/gpts/github/test-config/github_positive_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

/**
Expand Down
57 changes: 5 additions & 52 deletions tests/gpts/gitlab/suites-config/gitlab_gitlabci_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

0 comments on commit 9c51201

Please sign in to comment.