jx/internal/transport/transport_test.go
Matthew Rich bcf4e768ff
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run
Fix an issue with the package resource where a missing package would cause a fatal error
WIP: add support container image build using local filesytem contexts or contextes generated from resource definitions
WIP: added support for the create command in the exec resource
Fix a type matching error in `types` package use of generics
2024-07-22 15:03:22 -07:00

94 lines
1.9 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package transport
import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"
"fmt"
"os"
"log"
)
var TempDir string
var testFileResourceDoc string = `
resources:
- type: file
transition: read
attributes:
path: /tmp/foobar
`
func TestMain(m *testing.M) {
var err error
TempDir, err = os.MkdirTemp("", "testtransportfile")
if err != nil || TempDir == "" {
log.Fatal(err)
}
rc := m.Run()
os.RemoveAll(TempDir)
os.Exit(rc)
}
func TestNewTransportReader(t *testing.T) {
path := fmt.Sprintf("%s/foo", TempDir)
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
writeErr := os.WriteFile(path, []byte("test"), 0644)
assert.Nil(t, writeErr)
reader, err := NewReader(u)
assert.Nil(t, err)
assert.NotNil(t, reader)
}
func TestTransportReaderContentType(t *testing.T) {
path := fmt.Sprintf("%s/foo.jx.yaml", TempDir)
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
assert.False(t, Exists(u))
writeErr := os.WriteFile(path, []byte(testFileResourceDoc), 0644)
assert.Nil(t, writeErr)
reader, err := NewReader(u)
assert.Nil(t, err)
assert.True(t, reader.Exists())
assert.NotNil(t, reader)
assert.Equal(t, reader.ContentType(), "yaml")
}
func TestTransportReaderDir(t *testing.T) {
u, e := url.Parse(fmt.Sprintf("file://%s", TempDir))
assert.Nil(t, e)
reader, err := NewReader(u)
assert.ErrorContains(t, err, "is a directory")
assert.True(t, reader.Exists())
assert.NotNil(t, reader)
}
func TestTransportWriter(t *testing.T) {
path := fmt.Sprintf("%s/writefoo", TempDir)
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
assert.False(t, Exists(u))
writer, err := NewWriter(u)
assert.Nil(t, err)
defer writer.Close()
assert.NotNil(t, writer)
assert.True(t, writer.Exists())
_, writeErr := writer.Write([]byte("testdata"))
assert.Nil(t, writeErr)
assert.FileExists(t, path)
}