102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package main
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"fmt"
|
|
)
|
|
|
|
var TempDir string
|
|
|
|
func TestMain(m *testing.M) {
|
|
var err error
|
|
TempDir, err = os.MkdirTemp("", "testcli")
|
|
if err != nil || TempDir == "" {
|
|
slog.Error("TestMain()", "error", err)
|
|
}
|
|
|
|
rc := m.Run()
|
|
|
|
os.RemoveAll(TempDir)
|
|
os.Exit(rc)
|
|
}
|
|
|
|
func TestCli(t *testing.T) {
|
|
if _, e := os.Stat("./jx"); errors.Is(e, os.ErrNotExist) {
|
|
t.Skip("cli not built")
|
|
}
|
|
yaml, cliErr := exec.Command("./jx", "import", "--resource", "file://COPYRIGHT").Output()
|
|
if cliErr != nil {
|
|
slog.Info("Debug CLI error", "error", cliErr, "stderr", cliErr.(*exec.ExitError).Stderr)
|
|
}
|
|
assert.Nil(t, cliErr)
|
|
assert.NotEqual(t, "", string(yaml))
|
|
assert.Greater(t, len(yaml), 0)
|
|
}
|
|
|
|
func TestCliHTTPSource(t *testing.T) {
|
|
if _, e := os.Stat("./jx"); errors.Is(e, os.ErrNotExist) {
|
|
t.Skip("cli not built")
|
|
}
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `
|
|
resources:
|
|
- type: file
|
|
attributes:
|
|
path: foo.txt
|
|
owner: nobody
|
|
group: nobody
|
|
mode: 0644
|
|
content: |
|
|
test file
|
|
content
|
|
state: present
|
|
`)
|
|
}))
|
|
defer ts.Close()
|
|
|
|
yaml, cliErr := exec.Command("./jx", "import", "--resource", ts.URL).Output()
|
|
if cliErr != nil {
|
|
slog.Info("Debug CLI error", "error", cliErr, "stderr", cliErr.(*exec.ExitError).Stderr)
|
|
}
|
|
assert.Nil(t, cliErr)
|
|
assert.NotEqual(t, "", string(yaml))
|
|
assert.Greater(t, len(yaml), 0)
|
|
}
|
|
|
|
func TestCliConfigSource(t *testing.T) {
|
|
if _, e := os.Stat("./jx"); errors.Is(e, os.ErrNotExist) {
|
|
t.Skip("cli not built")
|
|
}
|
|
|
|
configYaml := `
|
|
configurations:
|
|
- name: myhttpconnection
|
|
values:
|
|
http_user: foo
|
|
http_pass: bar
|
|
`
|
|
|
|
configPath := fmt.Sprintf("%s/testconfig.yaml", TempDir)
|
|
f, err := os.Create(configPath)
|
|
assert.Nil(t, err)
|
|
defer f.Close()
|
|
_, writeErr := f.Write([]byte(configYaml))
|
|
assert.Nil(t, writeErr)
|
|
|
|
yaml, cliErr := exec.Command("./jx", "import", "--config", configPath, "--resource", "file://COPYRIGHT").Output()
|
|
if cliErr != nil {
|
|
slog.Info("Debug CLI error", "error", cliErr, "stderr", cliErr.(*exec.ExitError).Stderr)
|
|
}
|
|
assert.Nil(t, cliErr)
|
|
slog.Info("TestConfigSource", "yaml", yaml)
|
|
}
|