2013-10-03 03:47:38 +04:00
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
2018-01-30 22:26:26 +03:00
// SPDX-License-Identifier: LGPL-2.0+
//
2013-10-03 03:47:38 +04:00
// 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
2021-12-07 04:20:55 +03:00
// License along with this library. If not, see <https://www.gnu.org/licenses/>.
2013-10-03 03:47:38 +04:00
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 ) ) ;
}
2013-10-04 02:33:18 +04:00
function assertNotEquals ( a , b ) {
if ( a == b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " != " + JSON . stringify ( b ) ) ;
}
2013-10-03 03:47:38 +04:00
function libtestExec ( shellCode ) {
2016-03-02 18:28:04 +03:00
let testdatadir = GLib . getenv ( "G_TEST_SRCDIR" ) ;
2017-07-20 17:32:44 +03:00
let libtestPath = GLib . build _filenamev ( [ testdatadir , 'tests/libtest.sh' ] )
2016-08-09 20:52:43 +03:00
let proc = Gio . Subprocess . new ( [ 'bash' , '-c' , 'set -xeuo pipefail; . ' + GLib . shell _quote ( libtestPath ) + '; ' + shellCode ] , 0 ) ;
proc . wait _check ( null ) ;
2013-10-03 03:47:38 +04:00
}
2017-07-20 17:32:44 +03:00
print ( '1..1' )
2017-09-01 23:15:33 +03:00
libtestExec ( 'setup_os_repository archive syslinux' ) ;
2013-10-03 03:47:38 +04:00
2017-08-30 22:06:45 +03:00
GLib . setenv ( "OSTREE_SYSROOT_DEBUG" , "mutable-deployments" , true ) ;
2013-10-03 03:47:38 +04:00
let upstreamRepo = OSTree . Repo . new ( Gio . File . new _for _path ( 'testos-repo' ) ) ;
upstreamRepo . open ( null ) ;
2021-05-07 17:42:37 +03:00
let runtimeRef = 'testos/buildmain/x86_64-runtime' ;
2013-10-04 02:33:18 +04:00
let [ , rev ] = upstreamRepo . resolve _rev ( runtimeRef , false ) ;
2013-10-03 03:47:38 +04:00
print ( "testos => " + rev ) ;
2013-10-04 02:33:18 +04:00
//// TEST: We should have no deployments
2013-10-03 03:47:38 +04:00
let sysroot = OSTree . Sysroot . new ( Gio . File . new _for _path ( 'sysroot' ) ) ;
sysroot . load ( null ) ;
let deployments = sysroot . get _deployments ( ) ;
assertEquals ( deployments . length , 0 ) ;
2013-10-04 02:33:18 +04:00
//// Add the remote, and do a pull
let [ , sysrootRepo ] = sysroot . get _repo ( null ) ;
2014-12-17 18:43:01 +03:00
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 ) ;
2013-10-24 17:10:34 +04:00
sysrootRepo . pull ( 'testos' , null , 0 , null , null ) ;
2013-10-04 02:33:18 +04:00
//// TEST: We can deploy one tree
let mergeDeployment = sysroot . get _merge _deployment ( 'testos' ) ;
let origin = sysroot . origin _new _from _refspec ( runtimeRef ) ;
2014-01-19 02:50:22 +04:00
let [ , deployment ] = sysroot . deploy _tree ( 'testos' , rev , origin ,
mergeDeployment , null ,
null ) ;
2013-10-04 02:33:18 +04:00
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
2017-09-15 17:32:55 +03:00
[ , deployment ] = sysroot . deploy _tree ( 'testos' , rev , origin ,
mergeDeployment , null ,
null ) ;
2013-10-04 02:33:18 +04:00
newDeployments = deployments ;
deployments = null ;
newDeployments . unshift ( deployment ) ;
print ( JSON . stringify ( newDeployments ) ) ;
sysroot . write _deployments ( newDeployments , null ) ;
libtestExec ( 'os_repository_new_commit' ) ;
2013-10-24 17:10:34 +04:00
sysrootRepo . pull ( 'testos' , null , 0 , null , null ) ;
2013-10-04 02:33:18 +04:00
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 ( ) ) ;
2014-01-19 02:50:22 +04:00
let [ , newDeployment ] = sysroot . deploy _tree ( 'testos' , newRev , origin ,
mergeDeployment , null ,
null ) ;
2013-10-04 02:33:18 +04:00
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' ) ;
2013-10-24 17:10:34 +04:00
sysrootRepo . pull ( 'testos' , null , 0 , null , null ) ;
2013-10-04 02:33:18 +04:00
let [ , thirdRev ] = sysrootRepo . resolve _rev ( runtimeRef , false ) ;
assertNotEquals ( newRev , thirdRev ) ;
mergeDeployment = sysroot . get _merge _deployment ( 'testos' ) ;
2014-01-19 02:50:22 +04:00
let [ , thirdDeployment ] = sysroot . deploy _tree ( 'testos' , thirdRev , origin ,
mergeDeployment , null ,
null ) ;
2013-10-04 02:33:18 +04:00
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 ) ;
2017-07-20 17:32:44 +03:00
print ( "ok test-sysroot" )