jx/internal/resource/schema_test.go

98 lines
2.2 KiB
Go
Raw Normal View History

// 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) {
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-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)
declarationAttributes := `
path: "%s"
2024-05-24 05:11:51 +00:00
owner: "%s"
group: "%s"
mode: "0600"
2024-05-24 05:11:51 +00:00
atime: %s
ctime: %s
2024-05-24 05:11:51 +00:00
mtime: %s
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
filetype: "regular"
state: present
`
2024-05-24 05:11:51 +00:00
decl := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, expectedTime, expectedTime)
testFile := NewFile()
e := testFile.LoadDecl(decl)
2024-05-24 05:11:51 +00:00
assert.Nil(t, e)
assert.Equal(t, file, testFile.ResolveId(ctx))
2024-04-03 19:27:16 +00:00
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()
2024-05-24 05:11:51 +00:00
assert.NotNil(t, f)
f.Path = file
assert.Nil(t, f.Init(nil))
r, e := f.Read(ctx)
2024-05-24 05:11:51 +00:00
assert.Nil(t, e)
2024-05-24 05:11:51 +00:00
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))
2024-05-24 05:11:51 +00:00
expected := fmt.Sprintf(declarationAttributes, file, ProcessTestUserName, ProcessTestGroupName, expectedTime, cTime.Local().Format(time.RFC3339Nano), expectedTime)
assert.YAMLEq(t, expected, string(r))
}
2024-04-09 19:30:05 +00:00
func TestSchemaValidateSchema(t *testing.T) {
s := NewSchema("document")
assert.NotNil(t, s)
2024-04-09 19:30:05 +00:00
assert.Nil(t, s.ValidateSchema())
}