jx/internal/resource/document_test.go

230 lines
4.7 KiB
Go
Raw Normal View History

2024-03-20 19:23:31 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-20 16:15:27 +00:00
package resource
import (
2024-03-25 20:31:06 +00:00
"context"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
2024-05-24 05:11:51 +00:00
"os/user"
2024-03-20 16:15:27 +00:00
)
func TestNewDocumentLoader(t *testing.T) {
2024-03-25 20:31:06 +00:00
d := NewDocument()
assert.NotEqual(t, nil, d)
2024-03-20 16:15:27 +00:00
}
func TestDocumentLoader(t *testing.T) {
2024-03-25 20:31:06 +00:00
dir, err := os.MkdirTemp("", "testdocumentloader")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
file, _ := filepath.Abs(filepath.Join(dir, "foo.txt"))
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
document := fmt.Sprintf(`
2024-03-20 16:15:27 +00:00
---
resources:
- type: file
attributes:
path: "%s"
2024-05-24 05:11:51 +00:00
owner: "%s"
group: "%s"
2024-03-20 16:15:27 +00:00
mode: "0600"
content: |-
test line 1
test line 2
state: present
- type: user
attributes:
name: "testuser"
2024-05-06 00:48:54 +00:00
uid: "10022"
group: "10022"
2024-03-20 16:15:27 +00:00
home: "/home/testuser"
2024-05-06 00:48:54 +00:00
createhome: true
2024-03-20 16:15:27 +00:00
state: present
2024-05-24 05:11:51 +00:00
`, file, ProcessTestUserName, ProcessTestGroupName)
2024-03-25 20:31:06 +00:00
d := NewDocument()
2024-05-24 05:11:51 +00:00
assert.NotNil(t, d)
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
docReader := strings.NewReader(document)
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
e := d.Load(docReader)
assert.Nil(t, e)
2024-03-25 20:31:06 +00:00
resources := d.Resources()
assert.Equal(t, 2, len(resources))
2024-03-20 16:15:27 +00:00
}
2024-03-20 19:23:31 +00:00
func TestDocumentGenerator(t *testing.T) {
2024-03-25 20:31:06 +00:00
ctx := context.Background()
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
fileContent := `// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-20 19:23:31 +00:00
`
2024-03-25 20:31:06 +00:00
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt"))
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
err := os.WriteFile(file, []byte(fileContent), 0644)
assert.Nil(t, err)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
info, statErr := os.Stat(file)
assert.Nil(t, statErr)
mTime := info.ModTime()
stat, ok := info.Sys().(*syscall.Stat_t)
assert.True(t, ok)
2024-03-25 20:31:06 +00:00
aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
cTime := time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
2024-05-24 05:11:51 +00:00
processUser, userErr := user.Current()
assert.Nil(t, userErr)
processGroup, groupErr := user.LookupGroupId(processUser.Gid)
assert.Nil(t, groupErr)
2024-03-25 20:31:06 +00:00
expected := fmt.Sprintf(`
2024-03-20 19:23:31 +00:00
resources:
- type: file
attributes:
path: %s
2024-05-24 05:11:51 +00:00
owner: "%s"
group: "%s"
2024-03-20 19:23:31 +00:00
mode: "0644"
content: |
%s
atime: %s
ctime: %s
mtime: %s
2024-04-21 18:06:53 +00:00
sha256: ea33e2082ca777f82dc9571b08df95d81925eed04e1bdbac7cdc6dc52d330eca
2024-04-23 22:35:08 +00:00
size: 82
2024-03-20 19:23:31 +00:00
filetype: "regular"
state: present
2024-05-24 05:11:51 +00:00
`, file, processUser.Username, processGroup.Name, fileContent, aTime.Format(time.RFC3339Nano), cTime.Format(time.RFC3339Nano), mTime.Format(time.RFC3339Nano))
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
var documentYaml strings.Builder
d := NewDocument()
assert.NotEqual(t, nil, d)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
f, e := ResourceTypes.New("file://")
assert.Nil(t, e)
assert.NotNil(t, f)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
f.(*File).Path = filepath.Join(TempDir, "foo.txt")
2024-04-03 19:27:16 +00:00
_,readErr := f.(*File).Read(ctx)
assert.Nil(t, readErr)
2024-03-25 20:31:06 +00:00
d.AddResourceDeclaration("file", f)
ey := d.Generate(&documentYaml)
assert.Equal(t, nil, ey)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
assert.Greater(t, documentYaml.Len(), 0)
assert.YAMLEq(t, expected, documentYaml.String())
2024-03-20 19:23:31 +00:00
}
func TestDocumentAddResource(t *testing.T) {
2024-03-25 20:31:06 +00:00
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt"))
err := os.WriteFile(file, []byte(""), 0644)
assert.Nil(t, err)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
d := NewDocument()
assert.NotNil(t, d)
2024-04-03 19:27:16 +00:00
e := d.AddResource(fmt.Sprintf("file://%s", file))
assert.Nil(t, e)
2024-03-20 19:23:31 +00:00
}
func TestDocumentJSON(t *testing.T) {
2024-04-03 19:27:16 +00:00
document := `
---
resources:
- type: user
attributes:
name: "testuser"
2024-05-06 00:48:54 +00:00
uid: "10022"
group: "10022"
home: "/home/testuser"
2024-05-06 00:48:54 +00:00
createhome: true
state: present
2024-04-03 19:27:16 +00:00
`
d := NewDocument()
assert.NotNil(t, d)
docReader := strings.NewReader(document)
e := d.Load(docReader)
assert.Nil(t, e)
marshalledJSON, jsonErr := d.JSON()
assert.Nil(t, jsonErr)
assert.Greater(t, len(marshalledJSON), 0)
}
func TestDocumentJSONSchema(t *testing.T) {
document := NewDocument()
document.ResourceDecls = []Declaration{}
e := document.Validate()
assert.Nil(t, e)
}
func TestDocumentYAML(t *testing.T) {
2024-04-03 19:27:16 +00:00
document := `
---
resources:
- type: user
attributes:
name: "testuser"
2024-05-06 00:48:54 +00:00
uid: "10022"
group: "10022"
home: "/home/testuser"
2024-05-06 00:48:54 +00:00
createhome: true
state: present
2024-04-03 19:27:16 +00:00
`
d := NewDocument()
assert.NotNil(t, d)
docReader := strings.NewReader(document)
e := d.Load(docReader)
assert.Nil(t, e)
marshalledYAML, yamlErr := d.YAML()
assert.Nil(t, yamlErr)
assert.YAMLEq(t, string(document), string(marshalledYAML))
}
2024-04-23 22:35:08 +00:00
func TestDocumentResourceFilter(t *testing.T) {
document := `
---
resources:
- type: user
attributes:
name: "testuser"
2024-05-06 00:48:54 +00:00
uid: "10022"
2024-04-23 22:35:08 +00:00
home: "/home/testuser"
state: present
- type: file
attributes:
path: "foo.txt"
state: present
- type: file
attributes:
path: "bar.txt"
state: present
`
d := NewDocument()
assert.NotNil(t, d)
docReader := strings.NewReader(document)
e := d.Load(docReader)
assert.Nil(t, e)
resources := d.Filter(func(d *Declaration) bool {
2024-05-06 04:40:34 +00:00
return d.Type == "file"
2024-04-23 22:35:08 +00:00
})
assert.Equal(t, 2, len(resources))
}