jx/internal/resource/schema_test.go
Matthew Rich 8feb7b8d56
Some checks failed
Lint / golangci-lint (push) Failing after 10m1s
Declarative Tests / test (push) Failing after 14s
add support of import search paths [doublejynx/jx#7]
2024-10-16 10:26:42 -07:00

98 lines
2.2 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)
assert.Equal(t, file, testFile.ResolveId(ctx))
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
assert.Nil(t, f.Init(nil))
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())
}