jx/internal/resource/declaration_test.go

76 lines
1.7 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 (
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
)
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
}
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)
resourceDeclaration.LoadDecl(decl)
assert.Equal(t, "file", resourceDeclaration.Type)
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.Implementation)
assert.NotNil(t, resourceDeclaration.Attributes)
2024-03-20 19:25:25 +00:00
}