Skip to content

Commit

Permalink
[server] Fix CapGitStatus job (#19855)
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl authored Jun 6, 2024
1 parent b0befdc commit d78575e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
93 changes: 93 additions & 0 deletions components/server/src/jobs/cap-git-status.spec.db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright (c) 2024 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import * as chai from "chai";
const expect = chai.expect;
import { suite, test, timeout } from "@testdeck/mocha";

import { WorkspaceInstance, WorkspaceInstanceRepoStatus } from "@gitpod/gitpod-protocol";
import { DBWorkspaceInstance, TypeORM, WorkspaceDB, resetDB } from "@gitpod/gitpod-db/lib";
import { Repository } from "typeorm";
import { CapGitStatus } from "./cap-git-status";
import { createTestContainer } from "../test/service-testing-container-module";
import { Container } from "inversify";
import { Experiments } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";

const workspaceId = "ws123";

@suite
class CapGitStatusTest {
container: Container;
db: WorkspaceDB;

async before() {
this.container = createTestContainer();
Experiments.configureTestingClient({
api_validate_git_status_length: true,
});
await this.wipeRepos();

this.db = this.container.get<WorkspaceDB>(WorkspaceDB);
}

async after() {
// await this.wipeRepos();
}

async wipeRepos() {
const typeorm = this.container.get<TypeORM>(TypeORM);
await resetDB(typeorm);
}

instance(nr: number) {
const gitStatus: WorkspaceInstanceRepoStatus = {
branch: "main",
latestCommit: "abc",
untrackedFiles: [],
};
for (let i = 0; i < 100; i++) {
gitStatus.untrackedFiles?.push(`file_${i}_${"a".repeat(100)}`);
}
gitStatus.totalUntrackedFiles = gitStatus.untrackedFiles?.length;

const instance: WorkspaceInstance = {
id: `wsi${nr}`,
workspaceId,
creationTime: new Date(2018, 2, 16, 10, 0, 0).toISOString(),
ideUrl: "lalalal",
region: "somewhere",
workspaceImage: "eu.gcr.io/gitpod-io/workspace-images@sha256:123",
configuration: {
ideImage: "eu.gcr.io/gitpod-io/ide@sha256:123",
},
status: {
phase: "stopped",
conditions: {},
version: 1,
},
gitStatus,
};
return instance;
}

@test(timeout(10000))
public async createUserAndFindById() {
await this.db.storeInstance(this.instance(1));

const job = this.container.get<CapGitStatus>(CapGitStatus);
expect(await findInstancesWithLengthyGitStatus(job, this.db), "instances with lengthy git status").to.equal(1);
expect(await job.run(), "number of capped instances").to.equal(1);
expect(await findInstancesWithLengthyGitStatus(job, this.db), "instances with lengthy git status").to.equal(0);
}
}

async function findInstancesWithLengthyGitStatus(job: CapGitStatus, db: WorkspaceDB): Promise<number> {
const repo = await ((db as any).getWorkspaceInstanceRepo() as Promise<Repository<DBWorkspaceInstance>>);
const instances = await job.findInstancesWithLengthyGitStatus(repo, 4096, 100);
return instances.length;
}

module.exports = new CapGitStatusTest();
7 changes: 4 additions & 3 deletions components/server/src/jobs/cap-git-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ export class CapGitStatus implements Job {
}

// Cap the git status (incl. status.repo, the old place where we stored it before)
const MARGIN = 200;
instances.forEach((i) => {
if (i.gitStatus) {
i.gitStatus = capGitStatus(i.gitStatus, GIT_STATUS_LENGTH_CAP_BYTES - MARGIN);
i.gitStatus = capGitStatus(i.gitStatus);
}
if (i.status) {
delete (i.status as any).repo;
Expand Down Expand Up @@ -80,7 +79,9 @@ export class CapGitStatus implements Job {
}
}

function capGitStatus(gitStatus: WorkspaceInstanceRepoStatus, maxLength: number): WorkspaceInstanceRepoStatus {
function capGitStatus(gitStatus: WorkspaceInstanceRepoStatus): WorkspaceInstanceRepoStatus {
const MARGIN = 500; // to account for attribute name's, and generic JSON overhead
const maxLength = GIT_STATUS_LENGTH_CAP_BYTES - MARGIN;
let bytesUsed = 0;
function capStr(str: string | undefined): string | undefined {
if (str === undefined) {
Expand Down

0 comments on commit d78575e

Please sign in to comment.