jx/internal/resource/declaration_test.go

112 lines
2.5 KiB
Go
Raw Normal View History

2024-03-20 19:25:25 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
"encoding/json"
2024-03-25 20:31:06 +00:00
"fmt"
"github.com/stretchr/testify/assert"
_ "log"
_ "os"
"path/filepath"
"testing"
2024-03-20 19:25:25 +00:00
)
2024-04-21 18:06:53 +00:00
/*
2024-03-20 19:25:25 +00:00
func TestYamlLoadDecl(t *testing.T) {
2024-03-25 20:31:06 +00:00
file := filepath.Join(TempDir, "fooread.txt")
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
resourceAttributes := make(map[string]any)
decl := fmt.Sprintf(`
2024-03-20 19:25:25 +00:00
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0600"
content: |-
test line 1
test line 2
`, file)
2024-03-25 20:31:06 +00:00
e := YamlLoadDecl(decl, &resourceAttributes)
assert.Equal(t, nil, e)
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
assert.Equal(t, "nobody", resourceAttributes["group"])
2024-03-20 19:25:25 +00:00
}
2024-04-21 18:06:53 +00:00
*/
2024-03-20 19:25:25 +00:00
func TestNewResourceDeclaration(t *testing.T) {
2024-03-25 20:31:06 +00:00
resourceDeclaration := NewDeclaration()
assert.NotEqual(t, nil, resourceDeclaration)
2024-03-20 19:25:25 +00:00
}
func TestNewResourceDeclarationType(t *testing.T) {
2024-03-25 20:31:06 +00:00
file := filepath.Join(TempDir, "fooread.txt")
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
decl := fmt.Sprintf(`
2024-03-20 19:25:25 +00:00
type: file
attributes:
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0600"
content: |-
test line 1
test line 2
`, file)
2024-03-25 20:31:06 +00:00
resourceDeclaration := NewDeclaration()
assert.NotEqual(t, nil, resourceDeclaration)
2024-04-03 19:27:16 +00:00
e := resourceDeclaration.LoadDecl(decl)
assert.Nil(t, e)
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
2024-03-25 20:31:06 +00:00
assert.NotEqual(t, nil, resourceDeclaration.Attributes)
2024-03-20 19:25:25 +00:00
}
func TestDeclarationNewResource(t *testing.T) {
2024-03-25 20:31:06 +00:00
resourceDeclaration := NewDeclaration()
assert.NotNil(t, resourceDeclaration)
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
errNewUnknownResource := resourceDeclaration.NewResource()
assert.ErrorIs(t, errNewUnknownResource, ErrUnknownResourceType)
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
resourceDeclaration.Type = "file"
errNewFileResource := resourceDeclaration.NewResource()
assert.Nil(t, errNewFileResource)
2024-03-20 19:25:25 +00:00
2024-03-25 20:31:06 +00:00
assert.NotNil(t, resourceDeclaration.Attributes)
2024-03-20 19:25:25 +00:00
}
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)
}