jx/internal/resource/file_test.go

182 lines
3.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-20 19:23:31 +00:00
"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"
"testing"
"time"
"syscall"
2024-03-20 16:15:27 +00:00
)
func TestNewFileResource(t *testing.T) {
2024-03-20 19:23:31 +00:00
f := NewFile()
assert.NotEqual(t, nil, f)
2024-03-20 16:15:27 +00:00
}
func TestApplyResourceTransformation(t *testing.T) {
2024-03-20 19:23:31 +00:00
f := NewFile()
assert.NotEqual(t, nil, f)
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
//e := f.Apply()
//assert.Equal(t, nil, e)
2024-03-20 16:15:27 +00:00
}
func TestReadFile(t *testing.T) {
2024-03-20 19:23:31 +00:00
ctx := context.Background()
file, _ := filepath.Abs(filepath.Join(TempDir, "fooread.txt"))
2024-03-20 16:15:27 +00:00
declarationAttributes := `
2024-03-20 16:15:27 +00:00
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0600"
atime: 2001-12-15T01:01:01.000000001Z
ctime: %s
mtime: 2001-12-15T01:01:01.000000001Z
2024-03-20 16:15:27 +00:00
content: |-
test line 1
test line 2
2024-03-20 19:23:31 +00:00
filetype: "regular"
2024-03-20 16:15:27 +00:00
state: present
`
decl := fmt.Sprintf(declarationAttributes, file, "2001-12-15T01:01:01.000000001Z")
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
testFile := NewFile()
e := testFile.LoadDecl(decl)
assert.Equal(t, nil, e)
testFile.Apply()
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
f := NewFile()
assert.NotEqual(t, nil, f)
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
f.Path = file
r, e := f.Read(ctx)
assert.Equal(t, nil, e)
assert.Equal(t, "nobody", 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, cTime.Format(time.RFC3339Nano))
assert.YAMLEq(t, expected, string(r))
2024-03-20 16:15:27 +00:00
}
2024-03-25 05:06:08 +00:00
func TestReadFileError(t *testing.T) {
ctx := context.Background()
file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt"))
f := NewFile()
assert.NotEqual(t, nil, f)
f.Path = file
_, e := f.Read(ctx)
assert.True(t, os.IsNotExist(e))
assert.Equal(t, "absent", f.State)
}
2024-03-20 16:15:27 +00:00
func TestCreateFile(t *testing.T) {
2024-03-20 19:23:31 +00:00
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt"))
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
decl := fmt.Sprintf(`
2024-03-20 16:15:27 +00:00
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0600"
content: |-
test line 1
test line 2
state: present
`, file)
2024-03-20 19:23:31 +00:00
f := NewFile()
e := f.LoadDecl(decl)
assert.Equal(t, nil, e)
assert.Equal(t, "nobody", f.Owner)
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
applyErr := f.Apply()
assert.Equal(t, nil, applyErr)
assert.FileExists(t, file, nil)
s, e := os.Stat(file)
assert.Equal(t, nil, e)
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
assert.Greater(t, s.Size(), int64(0))
2024-03-20 16:15:27 +00:00
2024-03-20 19:23:31 +00:00
f.State = "absent"
assert.Equal(t, nil, f.Apply())
assert.NoFileExists(t, file, nil)
}
func TestFileType(t *testing.T) {
fileType := []byte(`
filetype: "directory"
`)
var testFile File
err := yaml.Unmarshal(fileType, &testFile)
assert.Nil(t, err)
}
func TestFileDirectory(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testdir"))
decl := fmt.Sprintf(`
path: "%s"
owner: "nobody"
group: "nobody"
mode: "0700"
filetype: "directory"
state: present
`, file)
f := NewFile()
e := f.LoadDecl(decl)
assert.Equal(t, nil, e)
assert.Equal(t, "nobody", f.Owner)
applyErr := f.Apply()
assert.Equal(t, nil, applyErr)
assert.DirExists(t, file)
f.State = "absent"
deleteErr := f.Apply()
assert.Nil(t, deleteErr)
assert.NoDirExists(t, file)
2024-03-20 16:15:27 +00:00
}
func TestFileTimes(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testtimes.txt"))
decl := fmt.Sprintf(`
path: "%s"
owner: "nobody"
group: "nobody"
mtime: 2001-12-15T01:01:01.1Z
mode: "0600"
filtetype: "regular"
state: "present"
`, file)
expectedTime, timeErr := time.Parse(time.RFC3339, "2001-12-15T01:01:01.1Z")
assert.Nil(t, timeErr)
f := NewFile()
e := f.LoadDecl(decl)
assert.Nil(t, e)
assert.Equal(t, "nobody", f.Owner)
assert.True(t, f.Mtime.Equal(expectedTime))
}