2013-10-03 03:47:38 +04:00
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
// 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 ;
2014-03-19 17:13:28 +04:00
const GSystem = imports . gi . GSystem ;
2013-10-03 03:47:38 +04:00
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 ) {
let testdatadir = GLib . getenv ( "TESTDATADIR" ) ;
let libtestPath = GLib . build _filenamev ( [ testdatadir , 'libtest.sh' ] )
2014-03-19 17:13:28 +04:00
let proc = GSystem . Subprocess . new _simple _argv ( [ 'bash' , '-c' ,
'. ' + GLib . shell _quote ( libtestPath ) + '; ' + shellCode ] ,
GSystem . SubprocessStreamDisposition . INHERIT ,
GSystem . SubprocessStreamDisposition . INHERIT ,
null ) ;
proc . wait _sync _check ( null ) ;
2013-10-03 03:47:38 +04:00
}
libtestExec ( 'setup_os_repository archive-z2 syslinux' ) ;
let upstreamRepo = OSTree . Repo . new ( Gio . File . new _for _path ( 'testos-repo' ) ) ;
upstreamRepo . open ( null ) ;
2013-10-04 02:33:18 +04:00
let runtimeRef = 'testos/buildmaster/x86_64-runtime' ;
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
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
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 ) ;