jx/internal/resource/declaration_test.go
Matthew Rich bcf4e768ff
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run
Fix an issue with the package resource where a missing package would cause a fatal error
WIP: add support container image build using local filesytem contexts or contextes generated from resource definitions
WIP: added support for the create command in the exec resource
Fix a type matching error in `types` package use of generics
2024-07-22 15:03:22 -07:00

137 lines
3.1 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
_ "log"
_ "os"
"path/filepath"
"decl/internal/types"
"testing"
)
/*
func TestYamlLoadDecl(t *testing.T) {
file := filepath.Join(TempDir, "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 := filepath.Join(TempDir, "fooread.txt")
decl := fmt.Sprintf(`
type: file
attributes:
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0600"
content: |-
test line 1
test line 2
`, file)
resourceDeclaration := NewDeclaration()
assert.NotNil(t, resourceDeclaration)
e := resourceDeclaration.LoadDecl(decl)
assert.Nil(t, e)
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
assert.NotNil(t, resourceDeclaration.Attributes)
}
func TestDeclarationNewResource(t *testing.T) {
resourceDeclaration := NewDeclaration()
assert.NotNil(t, resourceDeclaration)
errNewUnknownResource := resourceDeclaration.NewResource()
assert.ErrorIs(t, errNewUnknownResource, types.ErrUnknownType)
resourceDeclaration.Type = "file"
errNewFileResource := resourceDeclaration.NewResource()
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)
}