jx/internal/resource/document_test.go

187 lines
3.8 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-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"
owner: "nobody"
group: "nobody"
mode: "0600"
content: |-
test line 1
test line 2
state: present
- type: user
attributes:
name: "testuser"
uid: 10022
group: "10022"
2024-03-20 16:15:27 +00:00
home: "/home/testuser"
state: present
`, file)
2024-03-25 20:31:06 +00:00
d := NewDocument()
assert.NotEqual(t, nil, 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-03-25 20:31:06 +00:00
expected := fmt.Sprintf(`
2024-03-20 19:23:31 +00:00
resources:
- type: file
attributes:
path: %s
owner: "root"
group: "root"
mode: "0644"
content: |
%s
atime: %s
ctime: %s
mtime: %s
2024-03-20 19:23:31 +00:00
filetype: "regular"
state: present
`, file, 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"
uid: 10022
group: "10022"
home: "/home/testuser"
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"
uid: 10022
group: "10022"
home: "/home/testuser"
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))
}