2018-02-09 14:32:10 +01:00
package shared
import (
"log"
"regexp"
"testing"
2018-03-14 14:38:40 -04:00
"github.com/lxc/lxd/shared"
2018-02-09 14:32:10 +01:00
)
func TestSetDefinitionDefaults ( t * testing . T ) {
def := Definition { }
2018-03-16 13:35:04 +01:00
def . SetDefaults ( )
2018-02-09 14:32:10 +01:00
2018-03-14 14:38:40 -04:00
uname , _ := shared . Uname ( )
if def . Image . Architecture != uname . Machine {
t . Fatalf ( "Expected image.arch to be '%s', got '%s'" , uname . Machine , def . Image . Architecture )
2018-02-09 14:32:10 +01:00
}
if def . Image . Expiry != "30d" {
t . Fatalf ( "Expected image.expiry to be '%s', got '%s'" , "30d" , def . Image . Expiry )
}
}
func TestValidateDefinition ( t * testing . T ) {
tests := [ ] struct {
name string
definition Definition
expected string
shouldFail bool
} {
{
"valid Definition" ,
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
2018-02-16 16:05:42 +01:00
Keys : [ ] string { "0xCODE" } ,
2018-02-09 14:32:10 +01:00
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
} ,
2018-03-09 15:06:01 +01:00
Files : [ ] DefinitionFile {
{
Generator : "dump" ,
} ,
} ,
2018-03-09 15:16:00 +01:00
Mappings : DefinitionMappings {
ArchitectureMap : "debian" ,
} ,
2018-02-09 14:32:10 +01:00
} ,
"" ,
false ,
} ,
{
2018-02-28 14:51:51 +01:00
"valid Definition without source.keys" ,
2018-02-09 14:32:10 +01:00
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
2018-02-28 14:51:51 +01:00
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
2018-02-09 14:32:10 +01:00
} ,
} ,
2018-02-28 14:51:51 +01:00
"" ,
false ,
} ,
{
2018-03-01 09:03:03 +01:00
"valid Defintion without source.url" ,
2018-02-09 14:32:10 +01:00
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
2018-03-01 09:03:03 +01:00
Downloader : "debootstrap" ,
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
2018-02-09 14:32:10 +01:00
} ,
} ,
2018-03-01 09:03:03 +01:00
"" ,
false ,
} ,
2018-03-09 15:16:00 +01:00
{
"invalid ArchitectureMap" ,
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
Keys : [ ] string { "0xCODE" } ,
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
} ,
Files : [ ] DefinitionFile {
{
Generator : "dump" ,
} ,
} ,
Mappings : DefinitionMappings {
ArchitectureMap : "foo" ,
} ,
} ,
"mappings.architecture_map must be one of .+" ,
true ,
} ,
2018-03-09 15:06:01 +01:00
{
"invalid generator" ,
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
Keys : [ ] string { "0xCODE" } ,
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
} ,
Files : [ ] DefinitionFile {
{
Generator : "foo" ,
} ,
} ,
} ,
"files\\.\\*\\.generator must be one of .+" ,
true ,
} ,
2018-03-01 09:03:03 +01:00
{
"empty image.distribution" ,
Definition { } ,
"image.distribution may not be empty" ,
2018-02-09 14:32:10 +01:00
true ,
} ,
2018-02-16 16:05:42 +01:00
{
2018-03-01 09:03:03 +01:00
"invalid source.downloader" ,
2018-02-16 16:05:42 +01:00
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
2018-03-01 09:03:03 +01:00
Downloader : "foo" ,
2018-02-16 16:05:42 +01:00
} ,
} ,
2018-03-01 09:03:03 +01:00
"source.downloader must be one of .+" ,
2018-02-16 16:05:42 +01:00
true ,
} ,
2018-02-09 14:32:10 +01:00
{
"invalid package.manager" ,
Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
2018-02-16 16:05:42 +01:00
Keys : [ ] string { "0xCODE" } ,
2018-02-09 14:32:10 +01:00
} ,
Packages : DefinitionPackages {
Manager : "foo" ,
} ,
} ,
"packages.manager must be one of .+" ,
true ,
} ,
}
for i , tt := range tests {
log . Printf ( "Running test #%d: %s" , i , tt . name )
2018-03-16 13:35:04 +01:00
tt . definition . SetDefaults ( )
err := tt . definition . Validate ( )
2018-02-09 14:32:10 +01:00
if ! tt . shouldFail && err != nil {
t . Fatalf ( "Validation failed: %s" , err )
} else if tt . shouldFail {
2018-02-28 14:51:51 +01:00
if err == nil {
t . Fatal ( "Expected failure" )
}
2018-02-09 14:32:10 +01:00
match , _ := regexp . MatchString ( tt . expected , err . Error ( ) )
if ! match {
t . Fatalf ( "Validation failed: Expected '%s', got '%s'" , tt . expected , err . Error ( ) )
}
}
}
}
2018-03-14 22:34:46 +01:00
func TestDefinitionSetValue ( t * testing . T ) {
d := Definition {
Image : DefinitionImage {
Distribution : "ubuntu" ,
Release : "artful" ,
} ,
Source : DefinitionSource {
Downloader : "debootstrap" ,
URL : "https://ubuntu.com" ,
Keys : [ ] string { "0xCODE" } ,
} ,
Packages : DefinitionPackages {
Manager : "apt" ,
} ,
Actions : [ ] DefinitionAction {
{
Trigger : "post-update" ,
Action : "/bin/true" ,
} ,
{
Trigger : "post-packages" ,
Action : "/bin/false" ,
} ,
} ,
}
err := d . SetValue ( "image.release" , "bionic" )
if err != nil {
t . Fatalf ( "Unexpected error: %s" , err )
}
if d . Image . Release != "bionic" {
t . Fatalf ( "Expected '%s', got '%s'" , "bionic" , d . Image . Release )
}
err = d . SetValue ( "actions.0.trigger" , "post-files" )
if err != nil {
t . Fatalf ( "Unexpected error: %s" , err )
}
if d . Actions [ 0 ] . Trigger != "post-files" {
t . Fatalf ( "Expected '%s', got '%s'" , "post-files" , d . Actions [ 0 ] . Trigger )
}
// Index out of bounds
err = d . SetValue ( "actions.3.trigger" , "post-files" )
if err == nil || err . Error ( ) != "Index out of range" {
t . Fatal ( "Expected index out of range" )
}
// Nonsense
err = d . SetValue ( "image" , "[foo: bar]" )
if err == nil || err . Error ( ) != "Cannot assign string value to struct" {
t . Fatal ( "Expected unsupported assignment" )
}
}