94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package resource
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
_ "gopkg.in/yaml.v3"
|
|
_ "io"
|
|
_ "log"
|
|
_ "net/http"
|
|
_ "net/http/httptest"
|
|
_ "net/url"
|
|
"os"
|
|
"path/filepath"
|
|
_ "strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewSchema(t *testing.T) {
|
|
s := NewSchema("document")
|
|
assert.NotEqual(t, nil, s)
|
|
}
|
|
|
|
func TestSchemaValidateJSON(t *testing.T) {
|
|
ctx := context.Background()
|
|
s := NewSchema("file")
|
|
assert.NotEqual(t, nil, s)
|
|
|
|
file, _ := filepath.Abs(TempDir.FilePath("fooread.txt"))
|
|
|
|
expectedAtime, atimeErr := time.Parse(time.RFC3339Nano, "2001-12-15T01:01:01.000000001Z")
|
|
assert.Nil(t, atimeErr)
|
|
expectedTime := expectedAtime.Local().Format(time.RFC3339Nano)
|
|
declarationAttributes := `
|
|
path: "%s"
|
|
owner: "%s"
|
|
group: "%s"
|
|
mode: "0600"
|
|
atime: %s
|
|
ctime: %s
|
|
mtime: %s
|
|
content: |-
|
|
test line 1
|
|
test line 2
|
|
sha256: f2082f984f1bf1a7886e2af32ccc9ca474fbff3553d131204b070c438114dd51
|
|
size: 23
|
|
filetype: "regular"
|
|
state: present
|
|
`
|
|
|
|
decl := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, expectedTime, expectedTime)
|
|
|
|
testFile := NewFile()
|
|
e := testFile.LoadDecl(decl)
|
|
assert.Nil(t, e)
|
|
fileApplyErr := testFile.Apply()
|
|
assert.Nil(t, fileApplyErr)
|
|
|
|
jsonDoc, jsonErr := json.Marshal(testFile)
|
|
assert.Nil(t, jsonErr)
|
|
|
|
schemaErr := s.Validate(string(jsonDoc))
|
|
assert.Nil(t, schemaErr)
|
|
|
|
f := NewFile()
|
|
assert.NotNil(t, f)
|
|
|
|
f.Path = file
|
|
r, e := f.Read(ctx)
|
|
assert.Nil(t, e)
|
|
assert.Equal(t, ProcessTestUserName, f.Owner)
|
|
|
|
info, statErr := os.Stat(file)
|
|
assert.Nil(t, statErr)
|
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
|
assert.True(t, ok)
|
|
cTime := time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
|
|
|
|
expected := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, cTime.Local().Format(time.RFC3339Nano), expectedTime)
|
|
assert.YAMLEq(t, expected, string(r))
|
|
}
|
|
|
|
func TestSchemaValidateSchema(t *testing.T) {
|
|
s := NewSchema("document")
|
|
assert.NotNil(t, s)
|
|
|
|
assert.Nil(t, s.ValidateSchema())
|
|
}
|