165 lines
4.0 KiB
Go
165 lines
4.0 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
package folio
|
|
|
|
import (
|
|
_ "encoding/json"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
_ "log"
|
|
_ "os"
|
|
_ "decl/internal/types"
|
|
"decl/internal/codec"
|
|
"testing"
|
|
)
|
|
|
|
/*
|
|
func TestYamlLoadDecl(t *testing.T) {
|
|
|
|
file := TempDir.FilePath("fooread.txt")
|
|
|
|
resourceAttributes := make(map[string]any)
|
|
decl := fmt.Sprintf(`
|
|
path: "%s"
|
|
owner: "nobody"
|
|
group: "nobody"
|
|
mode: "0600"
|
|
content: |-
|
|
test line 1
|
|
test line 2
|
|
`, file)
|
|
|
|
e := YamlLoadDecl(decl, &resourceAttributes)
|
|
assert.Equal(t, nil, e)
|
|
|
|
assert.Equal(t, "nobody", resourceAttributes["group"])
|
|
}
|
|
*/
|
|
|
|
func TestNewResourceDeclaration(t *testing.T) {
|
|
resourceDeclaration := NewDeclaration()
|
|
assert.NotEqual(t, nil, resourceDeclaration)
|
|
}
|
|
|
|
func TestNewResourceDeclarationType(t *testing.T) {
|
|
file := TempDir.FilePath("fooread.txt")
|
|
|
|
decl := fmt.Sprintf(`
|
|
type: foo
|
|
attributes:
|
|
name: "%s"
|
|
`, file)
|
|
|
|
resourceDeclaration := NewDeclaration()
|
|
resourceDeclaration.ResourceTypes = TestResourceTypes
|
|
assert.NotNil(t, resourceDeclaration)
|
|
|
|
e := resourceDeclaration.LoadString(decl, codec.FormatYaml)
|
|
assert.Nil(t, e)
|
|
|
|
assert.Equal(t, TypeName("foo"), resourceDeclaration.Type)
|
|
assert.NotNil(t, resourceDeclaration.Attributes)
|
|
}
|
|
|
|
/* XXX
|
|
func TestEventTypeLoad(t *testing.T) {
|
|
file := TempDir.FilePath("fooread.txt")
|
|
|
|
fooTemplate := `
|
|
type: foo
|
|
on:
|
|
load: |
|
|
%s
|
|
attributes:
|
|
name: "%s"
|
|
`
|
|
|
|
for _, v := range []struct{ decl string; expected string } {
|
|
{ decl: fmt.Sprintf(fooTemplate, "print('Hello world!')", file), expected: "" },
|
|
{ decl: fmt.Sprintf(fooTemplate, "print('Hello world!') fail", file), expected: "Lua error: luaL_loadstring(): failed loading, [string \"print('Hello world!') fail...\"]:2: '=' expected near '<eof>', return code: 3" },
|
|
} {
|
|
resourceDeclaration := NewDeclaration()
|
|
resourceDeclaration.ResourceTypes = TestResourceTypes
|
|
assert.NotNil(t, resourceDeclaration)
|
|
|
|
e := resourceDeclaration.LoadString(v.decl, codec.FormatYaml)
|
|
assert.Nil(t, e)
|
|
assert.Equal(t, v.expected, resourceDeclaration.Error)
|
|
}
|
|
}
|
|
*/
|
|
|
|
/*
|
|
func TestDeclarationNewResource(t *testing.T) {
|
|
resourceDeclaration := NewDeclaration()
|
|
resourceDeclaration.ResourceTypes = TestResourceTypes
|
|
assert.NotNil(t, resourceDeclaration)
|
|
|
|
errNewUnknownResource := resourceDeclaration.NewResource(nil)
|
|
assert.ErrorIs(t, errNewUnknownResource, types.ErrUnknownType)
|
|
|
|
resourceDeclaration.Type = "foo"
|
|
errNewFileResource := resourceDeclaration.NewResource(nil)
|
|
assert.Nil(t, errNewFileResource)
|
|
|
|
assert.NotNil(t, resourceDeclaration.Attributes)
|
|
}
|
|
|
|
func TestDeclarationJson(t *testing.T) {
|
|
fileDeclJson := `
|
|
{
|
|
"type": "file",
|
|
"attributes": {
|
|
"path": "foo"
|
|
}
|
|
}
|
|
`
|
|
resourceDeclaration := NewDeclaration()
|
|
e := json.Unmarshal([]byte(fileDeclJson), resourceDeclaration)
|
|
assert.Nil(t, e)
|
|
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
|
|
|
|
//assert.Equal(t, "foo", resourceDeclaration.Attributes.(*File).Path)
|
|
|
|
userDeclJson := `
|
|
{
|
|
"type": "user",
|
|
"attributes": {
|
|
"name": "testuser",
|
|
"uid": "10012"
|
|
}
|
|
}
|
|
`
|
|
userResourceDeclaration := NewDeclaration()
|
|
ue := json.Unmarshal([]byte(userDeclJson), userResourceDeclaration)
|
|
assert.Nil(t, ue)
|
|
assert.Equal(t, TypeName("user"), userResourceDeclaration.Type)
|
|
//assert.Equal(t, "testuser", userResourceDeclaration.Attributes.(*User).Name)
|
|
//assert.Equal(t, "10012", userResourceDeclaration.Attributes.(*User).UID)
|
|
|
|
}
|
|
|
|
func TestDeclarationTransition(t *testing.T) {
|
|
fileName := filepath.Join(TempDir, "testdecl.txt")
|
|
fileDeclJson := fmt.Sprintf(`
|
|
{
|
|
"type": "file",
|
|
"transition": "present",
|
|
"attributes": {
|
|
"path": "%s"
|
|
}
|
|
}
|
|
`, fileName)
|
|
|
|
resourceDeclaration := NewDeclaration()
|
|
e := json.Unmarshal([]byte(fileDeclJson), resourceDeclaration)
|
|
assert.Nil(t, e)
|
|
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
|
|
//assert.Equal(t, fileName, resourceDeclaration.Attributes.(*File).Path)
|
|
err := resourceDeclaration.Apply()
|
|
assert.Nil(t, err)
|
|
assert.FileExists(t, fileName)
|
|
}
|
|
*/
|