163 lines
3.8 KiB
Go
163 lines
3.8 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package client
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"fmt"
|
|
"context"
|
|
"decl/internal/folio"
|
|
"decl/internal/resource"
|
|
"decl/internal/codec"
|
|
"log/slog"
|
|
"os"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
var containerDoc string = `
|
|
imports:
|
|
- %s
|
|
resources:
|
|
- type: container
|
|
transition: create
|
|
attributes:
|
|
image: rosskeenhouse/build-golang:1.22.6-alpine
|
|
name: jx-client-resources-test
|
|
hostconfig:
|
|
autoremove: false
|
|
mounts:
|
|
- type: "bind"
|
|
source: "%s"
|
|
target: "/src"
|
|
- type: "bind"
|
|
source: "%s"
|
|
target: "%s"
|
|
workingdir: "/src"
|
|
entrypoint:
|
|
- "/src/jx"
|
|
cmd:
|
|
- apply
|
|
- %s
|
|
wait: true
|
|
---
|
|
resources:
|
|
- type: container
|
|
transition: delete
|
|
attributes:
|
|
name: jx-client-resources-test
|
|
`
|
|
|
|
// create a container
|
|
// run a test inside the container
|
|
func TestUserResource(t *testing.T) {
|
|
ctx := context.Background()
|
|
c := NewClient()
|
|
assert.NotNil(t, c)
|
|
|
|
TempDir.Mkdir("testresources", 0700)
|
|
tmpresourcespath := TempDir.FilePath("testresources")
|
|
configurations := fmt.Sprintf(`
|
|
configurations:
|
|
- name: tmpdir
|
|
values:
|
|
prefix: %s
|
|
`, tmpresourcespath)
|
|
|
|
assert.Nil(t, TempDir.CreateFile("config.jx.yaml", configurations))
|
|
|
|
configURI := TempDir.URIPath("config.jx.yaml")
|
|
//assert.Nil(t, c.Import([]string{configURI}))
|
|
|
|
testUserFile := fmt.Sprintf(`
|
|
imports:
|
|
- %s
|
|
resources:
|
|
- type: file
|
|
config: tmpdir
|
|
transition: update
|
|
attributes:
|
|
path: testdir
|
|
mode: 0600
|
|
state: present
|
|
- type: group
|
|
transition: update
|
|
attributes:
|
|
name: testuser
|
|
- type: group
|
|
transition: update
|
|
attributes:
|
|
name: testgroup
|
|
- type: user
|
|
transition: update
|
|
attributes:
|
|
name: testuser
|
|
gecos: "my test account"
|
|
home: "/home/testuser"
|
|
createhome: true
|
|
group: testuser
|
|
groups:
|
|
- testgroup
|
|
- testuser
|
|
appendgroups: true
|
|
state: present
|
|
`, configURI)
|
|
|
|
assert.Nil(t, TempDir.CreateFile("test_userfile.jx.yaml", testUserFile))
|
|
|
|
for _, resourceTestDoc := range []string{
|
|
TempDir.FilePath("test_userfile.jx.yaml"),
|
|
} {
|
|
content := fmt.Sprintf(containerDoc, configURI, os.Getenv("WORKSPACE_PATH"), TempDir, TempDir, resourceTestDoc)
|
|
assert.Nil(t, TempDir.CreateFile("run-tests.jx.yaml", content))
|
|
|
|
runTestsDocument := TempDir.URIPath("run-tests.jx.yaml")
|
|
assert.Nil(t, c.Import([]string{runTestsDocument}))
|
|
assert.Nil(t, c.LoadDocumentImports())
|
|
|
|
assert.Nil(t, c.Apply(ctx, false))
|
|
|
|
applied, ok := folio.DocumentRegistry.GetDocument(folio.URI(runTestsDocument))
|
|
assert.True(t, ok)
|
|
|
|
cont := applied.ResourceDeclarations[0].Resource().(*resource.Container)
|
|
|
|
slog.Info("TestUserResources", "stdout", cont.Stdout, "stderr", cont.Stderr)
|
|
slog.Info("TestUserResources", "doc", applied, "container", applied.ResourceDeclarations[0])
|
|
assert.Equal(t, 0, len(applied.Errors))
|
|
assert.Greater(t, len(cont.Stdout), 0)
|
|
|
|
resultReader := io.NopCloser(strings.NewReader(cont.Stdout))
|
|
decoder := codec.NewDecoder(resultReader, codec.FormatYaml)
|
|
|
|
result := folio.NewDocument(nil)
|
|
assert.Nil(t, decoder.Decode(folio.NewDocument(nil)))
|
|
assert.Nil(t, decoder.Decode(result))
|
|
|
|
uri := fmt.Sprintf("file://%s", resourceTestDoc)
|
|
//testDoc := folio.DocumentRegistry.NewDocument(folio.URI(uri))
|
|
docs, loadErr := folio.DocumentRegistry.Load(folio.URI(uri))
|
|
assert.Nil(t, loadErr)
|
|
testDoc := docs[0]
|
|
|
|
var added int = 0
|
|
diffs, diffsErr := testDoc.(*folio.Document).Diff(result, nil)
|
|
assert.Nil(t, diffsErr)
|
|
assert.Greater(t, len(diffs), 1)
|
|
for _, line := range strings.Split(diffs, "\n") {
|
|
if len(line) > 0 {
|
|
switch line[0] {
|
|
case '+':
|
|
slog.Info("TestUserResources Diff", "line", line, "added", added)
|
|
added++
|
|
case '-':
|
|
assert.Fail(t, "resource attribute missing", line)
|
|
}
|
|
}
|
|
}
|
|
assert.Equal(t, 4, added)
|
|
}
|
|
|
|
}
|