jx/internal/resource/package_test.go
Matthew Rich 1460d2285b
Some checks failed
Declarative Tests / build-ubuntu-focal (push) Waiting to run
Lint / golangci-lint (push) Failing after 15s
Declarative Tests / test (push) Failing after 5s
Declarative Tests / build-fedora (push) Has been cancelled
add support for configuration documents
2024-07-01 14:54:18 -07:00

137 lines
2.8 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/slog"
_ "net/http"
_ "net/http/httptest"
_ "net/url"
_ "os"
_ "strings"
"testing"
"decl/internal/command"
)
func TestNewPackageResource(t *testing.T) {
p := NewPackage()
assert.NotNil(t, p)
}
func TestPackageApplyResourceTransformation(t *testing.T) {
p := NewPackage()
assert.NotNil(t, p)
//e := f.Apply()
//assert.Equal(t, nil, e)
}
func TestReadPackage(t *testing.T) {
decl := `
name: vim
version: latest
type: apk
`
p := NewPackage()
assert.NotNil(t, p)
m := &MockCommand{
Executor: func(value any) ([]byte, error) {
return nil, nil
},
Extractor: func(output []byte, target any) error {
p.Name = "vim"
p.Version = "1.1.1"
p.State = "present"
return nil
},
}
loadErr := p.LoadDecl(decl)
assert.Nil(t, loadErr)
assert.Equal(t, "latest", p.Version)
p.ReadCommand = (*command.Command)(m)
yaml, readErr := p.Read(context.Background())
assert.Nil(t, readErr)
assert.Greater(t, len(yaml), 0)
slog.Info("read()", "yaml", yaml)
assert.Equal(t, "1.1.1", p.Version)
slog.Info("resource: ", "package", p)
assert.Nil(t, p.Validate())
}
func TestReadAptPackage(t *testing.T) {
decl := `
name: vim
required: ">1.1.1"
type: apt
`
p := NewPackage()
assert.NotNil(t, p)
loadErr := p.LoadDecl(decl)
assert.Nil(t, loadErr)
assert.Equal(t, ">1.1.1", p.Required)
p.ReadCommand = NewAptReadCommand()
p.ReadCommand.Executor = func(value any) ([]byte, error) {
return []byte(`
Package: vim
Version: 1.2.2
`), nil
}
yaml, readErr := p.Read(context.Background())
assert.Nil(t, readErr)
assert.Greater(t, len(yaml), 0)
slog.Info("read()", "yaml", yaml)
assert.Equal(t, "1.2.2", p.Version)
assert.Nil(t, p.Validate())
}
func TestReadPackageError(t *testing.T) {
}
func TestCreatePackage(t *testing.T) {
}
func TestPackageSetURI(t *testing.T) {
p := NewPackage()
assert.NotNil(t, p)
e := p.SetURI("package://" + "12345_key?type=apk")
assert.Nil(t, e)
assert.Equal(t, "package", p.Type())
assert.Equal(t, "12345_key", p.Name)
}
func TestReadDebPackage(t *testing.T) {
decl := `
name: vim
source: vim-8.2.3995-1ubuntu2.17.deb
type: deb
`
p := NewPackage()
assert.NotNil(t, p)
loadErr := p.LoadDecl(decl)
assert.Nil(t, loadErr)
p.ReadCommand = NewDebReadCommand()
p.ReadCommand.Executor = func(value any) ([]byte, error) {
return []byte(`
Package: vim
Version: 1.2.2
`), nil
}
yaml, readErr := p.Read(context.Background())
assert.Nil(t, readErr)
slog.Info("Package.Read()", "package", p)
assert.Greater(t, len(yaml), 0)
slog.Info("read()", "yaml", yaml)
assert.Equal(t, "1.2.2", p.Version)
assert.Nil(t, p.Validate())
}