jx/internal/resource/declaration_test.go

136 lines
3.1 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.
2024-05-06 00:48:54 +00:00
2024-03-20 19:25:25 +00:00
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",
2024-05-06 00:48:54 +00:00
"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)
2024-05-06 00:48:54 +00:00
assert.Equal(t, "10012", userResourceDeclaration.Attributes.(*User).UID)
}
2024-05-09 07:39:45 +00:00
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)
2024-05-13 16:42:25 +00:00
err := resourceDeclaration.Apply()
2024-05-13 05:41:12 +00:00
assert.Nil(t, err)
2024-05-09 07:39:45 +00:00
assert.FileExists(t, fileName)
}