@ -37,7 +37,7 @@ func TestFindLayer(t *testing.T) {
layer , err := datastore . FindLayer ( "layer-0" , false , false )
if assert . Nil ( t , err ) && assert . NotNil ( t , layer ) {
assert . Equal ( t , "layer-0" , layer . Name )
assert . Nil ( t , layer . Namespace )
assert . Len ( t , layer . Namespace s, 0 )
assert . Nil ( t , layer . Parent )
assert . Equal ( t , 1 , layer . EngineVersion )
assert . Len ( t , layer . Features , 0 )
@ -52,7 +52,7 @@ func TestFindLayer(t *testing.T) {
layer , err = datastore . FindLayer ( "layer-1" , false , false )
if assert . Nil ( t , err ) && assert . NotNil ( t , layer ) {
assert . Equal ( t , layer . Name , "layer-1" )
assert . Equal ( t , "debian:7" , layer . Namespace . Name )
assertExpectedNamespaceName ( t , & layer , [ ] string { "debian:7" } )
if assert . NotNil ( t , layer . Parent ) {
assert . Equal ( t , "layer-0" , layer . Parent . Name )
}
@ -100,6 +100,27 @@ func TestFindLayer(t *testing.T) {
}
}
}
// Testing Multiple namespaces layer-3b has debian:7 and debian:8 namespaces
layer , err = datastore . FindLayer ( "layer-3b" , true , true )
if assert . Nil ( t , err ) && assert . NotNil ( t , layer ) && assert . Len ( t , layer . Features , 2 ) {
assert . Equal ( t , "layer-3b" , layer . Name )
// validate the namespace
assertExpectedNamespaceName ( t , & layer , [ ] string { "debian:7" , "debian:8" } )
for _ , featureVersion := range layer . Features {
switch featureVersion . Feature . Namespace . Name {
case "debian:7" :
assert . Equal ( t , "wechat" , featureVersion . Feature . Name )
assert . Equal ( t , "0.5" , featureVersion . Version )
case "debian:8" :
assert . Equal ( t , "openssl" , featureVersion . Feature . Name )
assert . Equal ( t , "1.0" , featureVersion . Version )
default :
t . Errorf ( "unexpected package %s for layer-3b" , featureVersion . Feature . Name )
}
}
}
}
func TestInsertLayer ( t * testing . T ) {
@ -205,19 +226,19 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
{
Name : "TestInsertLayer2" ,
Parent : & database . Layer { Name : "TestInsertLayer1" } ,
Namespace : & database . Namespace {
Namespace s: [ ] database . Namespace { database . Namespace {
Name : "TestInsertLayerNamespace1" ,
VersionFormat : dpkg . ParserName ,
} ,
} } ,
} ,
// This layer changes the namespace and adds Features.
{
Name : "TestInsertLayer3" ,
Parent : & database . Layer { Name : "TestInsertLayer2" } ,
Namespace : & database . Namespace {
Namespace s: [ ] database . Namespace { database . Namespace {
Name : "TestInsertLayerNamespace2" ,
VersionFormat : dpkg . ParserName ,
} ,
} } ,
Features : [ ] database . FeatureVersion { f1 , f2 , f3 } ,
} ,
// This layer covers the case where the last layer doesn't provide any new Feature.
@ -232,10 +253,10 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
{
Name : "TestInsertLayer4b" ,
Parent : & database . Layer { Name : "TestInsertLayer3" } ,
Namespace : & database . Namespace {
Namespace s: [ ] database . Namespace { database . Namespace {
Name : "TestInsertLayerNamespace3" ,
VersionFormat : dpkg . ParserName ,
} ,
} } ,
Features : [ ] database . FeatureVersion {
// Deletes TestInsertLayerFeature1.
// Keep TestInsertLayerFeature2 (old Namespace should be kept):
@ -264,10 +285,9 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
assert . Nil ( t , err )
}
// layer inherits all namespaces from its ancestries
l4a := retrievedLayers [ "TestInsertLayer4a" ]
if assert . NotNil ( t , l4a . Namespace ) {
assert . Equal ( t , "TestInsertLayerNamespace2" , l4a . Namespace . Name )
}
assertExpectedNamespaceName ( t , & l4a , [ ] string { "TestInsertLayerNamespace2" , "TestInsertLayerNamespace1" } )
assert . Len ( t , l4a . Features , 3 )
for _ , featureVersion := range l4a . Features {
if cmpFV ( featureVersion , f1 ) && cmpFV ( featureVersion , f2 ) && cmpFV ( featureVersion , f3 ) {
@ -276,9 +296,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
}
l4b := retrievedLayers [ "TestInsertLayer4b" ]
if assert . NotNil ( t , l4b . Namespace ) {
assert . Equal ( t , "TestInsertLayerNamespace3" , l4b . Namespace . Name )
}
assertExpectedNamespaceName ( t , & l4b , [ ] string { "TestInsertLayerNamespace1" , "TestInsertLayerNamespace2" , "TestInsertLayerNamespace3" } )
assert . Len ( t , l4b . Features , 3 )
for _ , featureVersion := range l4b . Features {
if cmpFV ( featureVersion , f2 ) && cmpFV ( featureVersion , f5 ) && cmpFV ( featureVersion , f6 ) {
@ -303,10 +321,10 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
l3u := database . Layer {
Name : l3 . Name ,
Parent : l3 . Parent ,
Namespace : & database . Namespace {
Namespace s: [ ] database . Namespace { database . Namespace {
Name : "TestInsertLayerNamespaceUpdated1" ,
VersionFormat : dpkg . ParserName ,
} ,
} } ,
Features : [ ] database . FeatureVersion { f7 } ,
}
@ -323,7 +341,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
l3uf , err := datastore . FindLayer ( l3u . Name , true , false )
if assert . Nil ( t , err ) {
assert . Equal ( t , l3 . Namespace . Name , l3uf . Namespace . Name )
assertSameNamespaceName ( t , & l3 , & l3uf )
assert . Equal ( t , l3 . EngineVersion , l3uf . EngineVersion )
assert . Len ( t , l3uf . Features , len ( l3 . Features ) )
}
@ -336,7 +354,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
l3uf , err = datastore . FindLayer ( l3u . Name , true , false )
if assert . Nil ( t , err ) {
assert . Equal ( t , l3u . Namespace . Name , l3uf . Namespace . Name )
assertSameNamespaceName ( t , & l3u , & l3uf )
assert . Equal ( t , l3u . EngineVersion , l3uf . EngineVersion )
if assert . Len ( t , l3uf . Features , 1 ) {
assert . True ( t , cmpFV ( l3uf . Features [ 0 ] , f7 ) , "Updated layer should have %#v but actually have %#v" , f7 , l3uf . Features [ 0 ] )
@ -352,7 +370,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
l4uf , err := datastore . FindLayer ( l3u . Name , true , false )
if assert . Nil ( t , err ) {
assert . Equal ( t , l3u . Namespace . Name , l4uf . Namespace . Name )
assertSameNamespaceName ( t , & l3u , & l4uf )
assert . Equal ( t , l4u . EngineVersion , l4uf . EngineVersion )
if assert . Len ( t , l4uf . Features , 1 ) {
assert . True ( t , cmpFV ( l3uf . Features [ 0 ] , f7 ) , "Updated layer should have %#v but actually have %#v" , f7 , l4uf . Features [ 0 ] )
@ -360,21 +378,56 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
}
}
func assertSameNamespaceName ( t * testing . T , layer1 * database . Layer , layer2 * database . Layer ) {
assert . Len ( t , compareStringLists ( extractNamespaceName ( layer1 ) , extractNamespaceName ( layer2 ) ) , 0 )
}
func assertExpectedNamespaceName ( t * testing . T , layer * database . Layer , expectedNames [ ] string ) {
assert . Len ( t , compareStringLists ( extractNamespaceName ( layer ) , expectedNames ) , 0 )
}
func extractNamespaceName ( layer * database . Layer ) [ ] string {
slist := make ( [ ] string , 0 , len ( layer . Namespaces ) )
for _ , ns := range layer . Namespaces {
slist = append ( slist , ns . Name )
}
return slist
}
func testInsertLayerDelete ( t * testing . T , datastore database . Datastore ) {
err := datastore . DeleteLayer ( "TestInsertLayerX" )
assert . Equal ( t , commonerr . ErrNotFound , err )
// ensure layer_namespace table is cleaned up once a layer is removed
layer3 , err := datastore . FindLayer ( "TestInsertLayer3" , false , false )
layer4a , err := datastore . FindLayer ( "TestInsertLayer4a" , false , false )
layer4b , err := datastore . FindLayer ( "TestInsertLayer4b" , false , false )
err = datastore . DeleteLayer ( "TestInsertLayer3" )
assert . Nil ( t , err )
_ , err = datastore . FindLayer ( "TestInsertLayer3" , false , false )
assert . Equal ( t , commonerr . ErrNotFound , err )
assertNotInLayerNamespace ( t , layer3 . ID , datastore )
_ , err = datastore . FindLayer ( "TestInsertLayer4a" , false , false )
assert . Equal ( t , commonerr . ErrNotFound , err )
assertNotInLayerNamespace ( t , layer4a . ID , datastore )
_ , err = datastore . FindLayer ( "TestInsertLayer4b" , true , false )
assert . Equal ( t , commonerr . ErrNotFound , err )
assertNotInLayerNamespace ( t , layer4b . ID , datastore )
}
func assertNotInLayerNamespace ( t * testing . T , layerID int , datastore database . Datastore ) {
pg , ok := datastore . ( * pgSQL )
if ! assert . True ( t , ok ) {
return
}
tx , err := pg . Begin ( )
if ! assert . Nil ( t , err ) {
return
}
rows , err := tx . Query ( searchLayerNamespace , layerID )
assert . False ( t , rows . Next ( ) )
}
func cmpFV ( a , b database . FeatureVersion ) bool {