jx/internal/resource/document_test.go

130 lines
2.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-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"
gid: "10022"
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.Equal(t, nil, e)
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")
f.(*File).Read(ctx)
d.AddResourceDeclaration("file", f)
2024-03-20 19:23:31 +00:00
2024-03-25 20:31:06 +00:00
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)
d.AddResource(fmt.Sprintf("file://%s", file))
2024-03-20 19:23:31 +00:00
}