jx/cli_test.go

55 lines
1.2 KiB
Go
Raw Normal View History

2024-04-03 18:47:55 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package main
import (
"github.com/stretchr/testify/assert"
"os"
"os/exec"
"testing"
"errors"
2024-04-19 07:52:10 +00:00
"log/slog"
"net/http"
"net/http/httptest"
"fmt"
2024-04-03 18:47:55 +00:00
)
func TestCli(t *testing.T) {
if _, e := os.Stat("./decl"); errors.Is(e, os.ErrNotExist) {
t.Skip("cli not built")
}
2024-04-21 18:06:53 +00:00
yaml, cliErr := exec.Command("./decl", "import", "--resource", "file://COPYRIGHT").Output()
2024-04-19 07:52:10 +00:00
slog.Info("TestCli", "err", cliErr)
2024-04-03 18:47:55 +00:00
assert.Nil(t, cliErr)
2024-04-19 07:52:10 +00:00
assert.NotEqual(t, "", string(yaml))
assert.Greater(t, len(yaml), 0)
}
func TestCliHTTPSource(t *testing.T) {
if _, e := os.Stat("./decl"); 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()
2024-04-21 18:06:53 +00:00
yaml, cliErr := exec.Command("./decl", "import", "--resource", ts.URL).Output()
2024-04-19 07:52:10 +00:00
slog.Info("TestCliHTTPSource", "err", cliErr)
assert.Nil(t, cliErr)
assert.NotEqual(t, "", string(yaml))
2024-04-03 18:47:55 +00:00
assert.Greater(t, len(yaml), 0)
}