2024-04-05 17:22:17 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved
package resource
import (
"context"
2024-07-22 22:03:22 +00:00
_ "encoding/json"
"fmt"
2024-04-05 17:22:17 +00:00
"github.com/stretchr/testify/assert"
2024-07-22 22:03:22 +00:00
_ "gopkg.in/yaml.v3"
_ "io"
2024-04-09 19:30:05 +00:00
"log/slog"
2024-07-22 22:03:22 +00:00
_ "net/http"
_ "net/http/httptest"
_ "net/url"
_ "os"
_ "strings"
2024-04-05 17:22:17 +00:00
"testing"
2024-07-01 21:54:18 +00:00
"decl/internal/command"
2024-04-05 17:22:17 +00:00
)
func TestNewPackageResource ( t * testing . T ) {
p := NewPackage ( )
assert . NotNil ( t , p )
}
func TestPackageApplyResourceTransformation ( t * testing . T ) {
p := NewPackage ( )
assert . NotNil ( t , p )
//e := f.Apply()
//assert.Equal(t, nil, e)
}
func TestReadPackage ( t * testing . T ) {
2024-04-09 19:30:05 +00:00
decl := `
2024-04-05 17:22:17 +00:00
name : vim
version : latest
type : apk
`
p := NewPackage ( )
assert . NotNil ( t , p )
2024-04-09 19:30:05 +00:00
m := & MockCommand {
2024-07-17 08:34:57 +00:00
CommandExists : func ( ) error { return nil } ,
2024-04-09 19:30:05 +00:00
Executor : func ( value any ) ( [ ] byte , error ) {
return nil , nil
} ,
Extractor : func ( output [ ] byte , target any ) error {
p . Name = "vim"
p . Version = "1.1.1"
p . State = "present"
return nil
} ,
}
2024-04-05 17:22:17 +00:00
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
2024-04-09 19:30:05 +00:00
assert . Equal ( t , "latest" , p . Version )
2024-07-01 21:54:18 +00:00
p . ReadCommand = ( * command . Command ) ( m )
2024-04-09 19:30:05 +00:00
yaml , readErr := p . Read ( context . Background ( ) )
assert . Nil ( t , readErr )
assert . Greater ( t , len ( yaml ) , 0 )
slog . Info ( "read()" , "yaml" , yaml )
assert . Equal ( t , "1.1.1" , p . Version )
slog . Info ( "resource: " , "package" , p )
assert . Nil ( t , p . Validate ( ) )
}
func TestReadAptPackage ( t * testing . T ) {
2024-09-19 08:11:57 +00:00
if ! PackageTypeApt . Exists ( ) {
t . Skip ( "Unsupported package type" )
}
2024-04-09 19:30:05 +00:00
decl := `
name : vim
required : ">1.1.1"
type : apt
`
p := NewPackage ( )
assert . NotNil ( t , p )
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
assert . Equal ( t , ">1.1.1" , p . Required )
p . ReadCommand = NewAptReadCommand ( )
p . ReadCommand . Executor = func ( value any ) ( [ ] byte , error ) {
return [ ] byte ( `
Package : vim
Version : 1.2 .2
` ) , nil
}
2024-04-05 17:22:17 +00:00
yaml , readErr := p . Read ( context . Background ( ) )
assert . Nil ( t , readErr )
assert . Greater ( t , len ( yaml ) , 0 )
2024-04-09 19:30:05 +00:00
slog . Info ( "read()" , "yaml" , yaml )
assert . Equal ( t , "1.2.2" , p . Version )
assert . Nil ( t , p . Validate ( ) )
2024-04-05 17:22:17 +00:00
}
func TestReadPackageError ( t * testing . T ) {
2024-07-22 22:03:22 +00:00
ctx := context . Background ( )
expected := fmt . Sprintf ( `
name : missing
state : absent
type : % s
` , SystemPackageType )
2024-09-19 08:11:57 +00:00
decl := fmt . Sprintf ( `
2024-07-22 22:03:22 +00:00
name : missing
2024-09-19 08:11:57 +00:00
type : % s
` , SystemPackageType )
2024-07-22 22:03:22 +00:00
p := NewPackage ( )
assert . NotNil ( t , p )
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
2024-09-19 08:11:57 +00:00
p . ReadCommand = SystemPackageType . NewReadCommand ( )
2024-07-22 22:03:22 +00:00
/ *
p . ReadCommand . Executor = func ( value any ) ( [ ] byte , error ) {
return [ ] byte ( ` ` ) , fmt . Errorf ( "exit status 1 dpkg-query: package 'makef' is not installed and no information is available\nUse dpkg --info (= dpkg-deb --info) to examine archive files.\n" )
}
* /
p . ResolveId ( ctx )
yaml , readErr := p . Read ( ctx )
assert . ErrorIs ( t , readErr , ErrResourceStateAbsent )
assert . YAMLEq ( t , expected , string ( yaml ) )
slog . Info ( "read()" , "yaml" , yaml )
assert . Equal ( t , "" , p . Version )
assert . Nil ( t , p . Validate ( ) )
2024-04-05 17:22:17 +00:00
}
func TestCreatePackage ( t * testing . T ) {
}
func TestPackageSetURI ( t * testing . T ) {
p := NewPackage ( )
assert . NotNil ( t , p )
2024-04-09 19:30:05 +00:00
e := p . SetURI ( "package://" + "12345_key?type=apk" )
2024-04-05 17:22:17 +00:00
assert . Nil ( t , e )
assert . Equal ( t , "package" , p . Type ( ) )
assert . Equal ( t , "12345_key" , p . Name )
}
2024-07-01 21:54:18 +00:00
func TestReadDebPackage ( t * testing . T ) {
2024-09-19 08:11:57 +00:00
if ! PackageTypeDeb . Exists ( ) {
t . Skip ( "Unsupported package type" )
}
2024-07-01 21:54:18 +00:00
decl := `
name : vim
source : vim - 8.2 .3995 - 1 ubuntu2 .17 . deb
type : deb
`
p := NewPackage ( )
assert . NotNil ( t , p )
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
p . ReadCommand = NewDebReadCommand ( )
p . ReadCommand . Executor = func ( value any ) ( [ ] byte , error ) {
return [ ] byte ( `
Package : vim
Version : 1.2 .2
` ) , nil
}
yaml , readErr := p . Read ( context . Background ( ) )
assert . Nil ( t , readErr )
slog . Info ( "Package.Read()" , "package" , p )
assert . Greater ( t , len ( yaml ) , 0 )
slog . Info ( "read()" , "yaml" , yaml )
assert . Equal ( t , "1.2.2" , p . Version )
assert . Nil ( t , p . Validate ( ) )
}
2024-07-17 08:34:57 +00:00
func TestPackageTypeErr ( t * testing . T ) {
decl := `
name : vim
source : vim - 8.2 .3995 - 1 ubuntu2 .17 . deb
type : deb
`
p := NewPackage ( )
assert . NotNil ( t , p )
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
p . ReadCommand = NewDebReadCommand ( )
p . ReadCommand . CommandExists = func ( ) error { return command . ErrUnknownCommand }
p . ReadCommand . Executor = func ( value any ) ( [ ] byte , error ) {
return [ ] byte ( `
Package : vim
Version : 1.2 .2
` ) , nil
}
_ , readErr := p . Read ( context . Background ( ) )
assert . ErrorIs ( t , readErr , ErrUnsupportedPackageType )
}
2024-09-19 08:11:57 +00:00
func TestPackageSourceRef ( t * testing . T ) {
decl := `
name : vim
sourceref : https : //localhost/vim-8.2.3995-1ubuntu2.17.deb
type : deb
`
p := NewPackage ( )
assert . NotNil ( t , p )
loadErr := p . LoadDecl ( decl )
assert . Nil ( t , loadErr )
p . ReadCommand = NewDebReadCommand ( )
p . ReadCommand . CommandExists = func ( ) error { return command . ErrUnknownCommand }
p . ReadCommand . Executor = func ( value any ) ( [ ] byte , error ) {
return [ ] byte ( `
Package : vim
Version : 1.2 .2
` ) , nil
}
_ , readErr := p . Read ( context . Background ( ) )
assert . ErrorIs ( t , readErr , ErrUnsupportedPackageType )
}