<commit_before>#!/usr/bin/env gjs
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;

const OSTree = imports.gi.OSTree;

function assertEquals(a, b) {
    if (a != b)
	throw new Error("assertion failed " + JSON.stringify(a) + " == " + JSON.stringify(b));
}

function assertNotEquals(a, b) {
    if (a == b)
	throw new Error("assertion failed " + JSON.stringify(a) + " != " + JSON.stringify(b));
}

function libtestExec(shellCode) {
    let testdatadir = GLib.getenv("TESTDATADIR");
    let libtestPath = GLib.build_filenamev([testdatadir, 'libtest.sh'])
    let cmdline = 'bash -c ' + GLib.shell_quote('. ' + GLib.shell_quote(libtestPath) + '; ' + shellCode);
    print("shellcode=" +cmdline);
    let [,stdout,stderr,estatus] = GLib.spawn_command_line_sync(cmdline);
    print(stderr);
    GLib.spawn_check_exit_status(estatus);
}

libtestExec('setup_os_repository archive-z2 syslinux');

let upstreamRepo = OSTree.Repo.new(Gio.File.new_for_path('testos-repo'));
upstreamRepo.open(null);

let runtimeRef = 'testos/buildmaster/x86_64-runtime';
let [,rev] = upstreamRepo.resolve_rev(runtimeRef, false);

print("testos => " + rev);

//// TEST: We should have no deployments

let sysroot = OSTree.Sysroot.new(Gio.File.new_for_path('sysroot'));
sysroot.load(null);
let deployments = sysroot.get_deployments();
assertEquals(deployments.length, 0);

//// Add the remote, and do a pull

let [,sysrootRepo] = sysroot.get_repo(null);
let sysrootRepoConfig = sysrootRepo.get_config();
let testosRefSection = 'remote "testos"';
sysrootRepoConfig.set_string(testosRefSection, 'url', 'file://' + upstreamRepo.get_path().get_path());
sysrootRepoConfig.set_boolean(testosRefSection, 'gpg-verify', false);
sysrootRepoConfig.set_string_list(testosRefSection, 'branches', [runtimeRef]);

sysrootRepo.pull('testos', null, 0, null, null);

//// TEST: We can deploy one tree

let mergeDeployment = sysroot.get_merge_deployment('testos');

let origin = sysroot.origin_new_from_refspec(runtimeRef);
let [,deployment] = sysroot.deploy_tree('testos', rev, origin,
					mergeDeployment, null,
					null);
let newDeployments = deployments;
deployments = null;
newDeployments.unshift(deployment);
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, newDeployments.length);
assertEquals(deployments[0].get_csum(), deployment.get_csum());

let deploymentPath = sysroot.get_deployment_directory(deployment);
assertEquals(deploymentPath.query_exists(null), true);

print("OK one deployment");

/// TEST: We can delete the deployment, going back to empty
sysroot.write_deployments([], null);

print("OK empty deployments");

assertEquals(deploymentPath.query_exists(null), false);

//// Ok, redeploy, then add a new revision upstream and pull it

let [,deployment] = sysroot.deploy_tree('testos', rev, origin,
					mergeDeployment, null,
					null);
newDeployments = deployments;
deployments = null;
newDeployments.unshift(deployment);
print(JSON.stringify(newDeployments));
sysroot.write_deployments(newDeployments, null);

libtestExec('os_repository_new_commit');

sysrootRepo.pull('testos', null, 0, null, null);

let [,newRev] = upstreamRepo.resolve_rev(runtimeRef, false);

print("testos => " + newRev);
assertNotEquals(rev, newRev);

mergeDeployment = sysroot.get_merge_deployment('testos');
assertEquals(mergeDeployment.get_csum(), deployment.get_csum());
let [,newDeployment] = sysroot.deploy_tree('testos', newRev, origin,
					   mergeDeployment, null,
					   null);
newDeployments = [newDeployment, mergeDeployment];
assertNotEquals(mergeDeployment.get_bootcsum(), newDeployment.get_bootcsum());
assertNotEquals(mergeDeployment.get_csum(), newDeployment.get_csum());
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, 2);
assertEquals(deploymentPath.query_exists(null), true);
let newDeploymentPath = sysroot.get_deployment_directory(newDeployment);
assertEquals(newDeploymentPath.query_exists(null), true);

print("OK two deployments");

libtestExec('os_repository_new_commit 0 1');

sysrootRepo.pull('testos', null, 0, null, null);

let [,thirdRev] = sysrootRepo.resolve_rev(runtimeRef, false);
assertNotEquals(newRev, thirdRev);

mergeDeployment = sysroot.get_merge_deployment('testos');
let [,thirdDeployment] = sysroot.deploy_tree('testos', thirdRev, origin,
					     mergeDeployment, null,
					     null);
assertEquals(mergeDeployment.get_bootcsum(), thirdDeployment.get_bootcsum());
assertNotEquals(mergeDeployment.get_csum(), thirdDeployment.get_csum());
newDeployments = [deployment, newDeployment, thirdDeployment];
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, 3);
<commit_msg>test-sysroot: Use GSystem to spawn subprocess<commit_after>#!/usr/bin/env gjs
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;

const GSystem = imports.gi.GSystem;
const OSTree = imports.gi.OSTree;

function assertEquals(a, b) {
    if (a != b)
	throw new Error("assertion failed " + JSON.stringify(a) + " == " + JSON.stringify(b));
}

function assertNotEquals(a, b) {
    if (a == b)
	throw new Error("assertion failed " + JSON.stringify(a) + " != " + JSON.stringify(b));
}

function libtestExec(shellCode) {
    let testdatadir = GLib.getenv("TESTDATADIR");
    let libtestPath = GLib.build_filenamev([testdatadir, 'libtest.sh'])
    let proc = GSystem.Subprocess.new_simple_argv(['bash', '-c',
						   '. ' + GLib.shell_quote(libtestPath) + '; ' + shellCode],
						  GSystem.SubprocessStreamDisposition.INHERIT,
						  GSystem.SubprocessStreamDisposition.INHERIT,
						  null);
    proc.init(null);
    proc.wait_sync_check(null);
}

libtestExec('setup_os_repository archive-z2 syslinux');

let upstreamRepo = OSTree.Repo.new(Gio.File.new_for_path('testos-repo'));
upstreamRepo.open(null);

let runtimeRef = 'testos/buildmaster/x86_64-runtime';
let [,rev] = upstreamRepo.resolve_rev(runtimeRef, false);

print("testos => " + rev);

//// TEST: We should have no deployments

let sysroot = OSTree.Sysroot.new(Gio.File.new_for_path('sysroot'));
sysroot.load(null);
let deployments = sysroot.get_deployments();
assertEquals(deployments.length, 0);

//// Add the remote, and do a pull

let [,sysrootRepo] = sysroot.get_repo(null);
let sysrootRepoConfig = sysrootRepo.get_config();
let testosRefSection = 'remote "testos"';
sysrootRepoConfig.set_string(testosRefSection, 'url', 'file://' + upstreamRepo.get_path().get_path());
sysrootRepoConfig.set_boolean(testosRefSection, 'gpg-verify', false);
sysrootRepoConfig.set_string_list(testosRefSection, 'branches', [runtimeRef]);

sysrootRepo.pull('testos', null, 0, null, null);

//// TEST: We can deploy one tree

let mergeDeployment = sysroot.get_merge_deployment('testos');

let origin = sysroot.origin_new_from_refspec(runtimeRef);
let [,deployment] = sysroot.deploy_tree('testos', rev, origin,
					mergeDeployment, null,
					null);
let newDeployments = deployments;
deployments = null;
newDeployments.unshift(deployment);
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, newDeployments.length);
assertEquals(deployments[0].get_csum(), deployment.get_csum());

let deploymentPath = sysroot.get_deployment_directory(deployment);
assertEquals(deploymentPath.query_exists(null), true);

print("OK one deployment");

/// TEST: We can delete the deployment, going back to empty
sysroot.write_deployments([], null);

print("OK empty deployments");

assertEquals(deploymentPath.query_exists(null), false);

//// Ok, redeploy, then add a new revision upstream and pull it

let [,deployment] = sysroot.deploy_tree('testos', rev, origin,
					mergeDeployment, null,
					null);
newDeployments = deployments;
deployments = null;
newDeployments.unshift(deployment);
print(JSON.stringify(newDeployments));
sysroot.write_deployments(newDeployments, null);

libtestExec('os_repository_new_commit');

sysrootRepo.pull('testos', null, 0, null, null);

let [,newRev] = upstreamRepo.resolve_rev(runtimeRef, false);

print("testos => " + newRev);
assertNotEquals(rev, newRev);

mergeDeployment = sysroot.get_merge_deployment('testos');
assertEquals(mergeDeployment.get_csum(), deployment.get_csum());
let [,newDeployment] = sysroot.deploy_tree('testos', newRev, origin,
					   mergeDeployment, null,
					   null);
newDeployments = [newDeployment, mergeDeployment];
assertNotEquals(mergeDeployment.get_bootcsum(), newDeployment.get_bootcsum());
assertNotEquals(mergeDeployment.get_csum(), newDeployment.get_csum());
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, 2);
assertEquals(deploymentPath.query_exists(null), true);
let newDeploymentPath = sysroot.get_deployment_directory(newDeployment);
assertEquals(newDeploymentPath.query_exists(null), true);

print("OK two deployments");

libtestExec('os_repository_new_commit 0 1');

sysrootRepo.pull('testos', null, 0, null, null);

let [,thirdRev] = sysrootRepo.resolve_rev(runtimeRef, false);
assertNotEquals(newRev, thirdRev);

mergeDeployment = sysroot.get_merge_deployment('testos');
let [,thirdDeployment] = sysroot.deploy_tree('testos', thirdRev, origin,
					     mergeDeployment, null,
					     null);
assertEquals(mergeDeployment.get_bootcsum(), thirdDeployment.get_bootcsum());
assertNotEquals(mergeDeployment.get_csum(), thirdDeployment.get_csum());
newDeployments = [deployment, newDeployment, thirdDeployment];
sysroot.write_deployments(newDeployments, null);
deployments = sysroot.get_deployments();
assertEquals(deployments.length, 3);

{'fraction_non_alphanumeric': 0.1294190797655001, 'fraction_numerical': 0.005862497779356901, 'mean_word_length': 5.898897058823529, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4587704', 'n_tokens_mistral': 3738, 'n_tokens_neox': 3561, 'n_words': 835}