summaryrefslogtreecommitdiff
path: root/tests/test-sysroot.js
blob: d4f67ef4b3b25feb631989e0cfa58ef9054f42cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
// SPDX-License-Identifier: LGPL-2.0+
//
// 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, see <https://www.gnu.org/licenses/>.

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("G_TEST_SRCDIR");
    let libtestPath = GLib.build_filenamev([testdatadir, 'tests/libtest.sh'])
    let proc = Gio.Subprocess.new(['bash', '-c', 'set -xeuo pipefail; . ' + GLib.shell_quote(libtestPath) + '; ' + shellCode], 0);
    proc.wait_check(null);
}

print('1..1')

libtestExec('setup_os_repository archive syslinux');

GLib.setenv("OSTREE_SYSROOT_DEBUG", "mutable-deployments", true);

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

let runtimeRef = 'testos/buildmain/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);
sysrootRepo.remote_add('testos', 'file://' + upstreamRepo.get_path().get_path(),
		       GLib.Variant.new('a{sv}', {'gpg-verify': GLib.Variant.new('b', false),
						  'branches': GLib.Variant.new('as', [runtimeRef])}), null);
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

[,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);

print("ok test-sysroot")