2024-04-03 16:54:50 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:30:05 +00:00
|
|
|
func TestSchemaValidateJSON(t *testing.T) {
|
2024-04-03 16:54:50 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
s := NewSchema("file")
|
|
|
|
assert.NotEqual(t, nil, s)
|
|
|
|
|
2024-09-27 02:13:01 +00:00
|
|
|
file, _ := filepath.Abs(TempDir.FilePath("fooread.txt"))
|
2024-04-03 16:54:50 +00:00
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
expectedAtime, atimeErr := time.Parse(time.RFC3339Nano, "2001-12-15T01:01:01.000000001Z")
|
|
|
|
assert.Nil(t, atimeErr)
|
|
|
|
expectedTime := expectedAtime.Local().Format(time.RFC3339Nano)
|
2024-04-03 16:54:50 +00:00
|
|
|
declarationAttributes := `
|
|
|
|
path: "%s"
|
2024-05-24 05:11:51 +00:00
|
|
|
owner: "%s"
|
|
|
|
group: "%s"
|
2024-04-03 16:54:50 +00:00
|
|
|
mode: "0600"
|
2024-05-24 05:11:51 +00:00
|
|
|
atime: %s
|
2024-04-03 16:54:50 +00:00
|
|
|
ctime: %s
|
2024-05-24 05:11:51 +00:00
|
|
|
mtime: %s
|
2024-04-03 16:54:50 +00:00
|
|
|
content: |-
|
|
|
|
test line 1
|
|
|
|
test line 2
|
2024-04-21 18:06:53 +00:00
|
|
|
sha256: f2082f984f1bf1a7886e2af32ccc9ca474fbff3553d131204b070c438114dd51
|
2024-04-23 22:35:08 +00:00
|
|
|
size: 23
|
2024-04-03 16:54:50 +00:00
|
|
|
filetype: "regular"
|
|
|
|
state: present
|
|
|
|
`
|
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
decl := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, expectedTime, expectedTime)
|
2024-04-03 16:54:50 +00:00
|
|
|
|
|
|
|
testFile := NewFile()
|
|
|
|
e := testFile.LoadDecl(decl)
|
2024-05-24 05:11:51 +00:00
|
|
|
assert.Nil(t, e)
|
2024-10-16 17:26:42 +00:00
|
|
|
assert.Equal(t, file, testFile.ResolveId(ctx))
|
|
|
|
|
2024-04-03 19:27:16 +00:00
|
|
|
fileApplyErr := testFile.Apply()
|
|
|
|
assert.Nil(t, fileApplyErr)
|
2024-04-03 16:54:50 +00:00
|
|
|
|
|
|
|
jsonDoc, jsonErr := json.Marshal(testFile)
|
|
|
|
assert.Nil(t, jsonErr)
|
|
|
|
|
|
|
|
schemaErr := s.Validate(string(jsonDoc))
|
|
|
|
assert.Nil(t, schemaErr)
|
|
|
|
|
|
|
|
f := NewFile()
|
2024-05-24 05:11:51 +00:00
|
|
|
assert.NotNil(t, f)
|
2024-04-03 16:54:50 +00:00
|
|
|
|
|
|
|
f.Path = file
|
2024-10-16 17:26:42 +00:00
|
|
|
assert.Nil(t, f.Init(nil))
|
2024-04-03 16:54:50 +00:00
|
|
|
r, e := f.Read(ctx)
|
2024-05-24 05:11:51 +00:00
|
|
|
assert.Nil(t, e)
|
2024-10-16 17:26:42 +00:00
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
assert.Equal(t, ProcessTestUserName, f.Owner)
|
2024-04-03 16:54:50 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
expected := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, cTime.Local().Format(time.RFC3339Nano), expectedTime)
|
2024-04-03 16:54:50 +00:00
|
|
|
assert.YAMLEq(t, expected, string(r))
|
|
|
|
}
|
2024-04-09 19:30:05 +00:00
|
|
|
|
|
|
|
func TestSchemaValidateSchema(t *testing.T) {
|
2024-10-16 17:26:42 +00:00
|
|
|
s := NewSchema("document")
|
|
|
|
assert.NotNil(t, s)
|
2024-04-09 19:30:05 +00:00
|
|
|
|
|
|
|
assert.Nil(t, s.ValidateSchema())
|
|
|
|
}
|