update user resource

This commit is contained in:
Matthew Rich 2024-03-21 21:35:17 -07:00
parent b58d6d249b
commit 1bea3894ca
8 changed files with 66 additions and 20 deletions

View File

@ -89,7 +89,3 @@ func TestCreateContainer(t *testing.T) {
applyDeleteErr := c.Apply()
assert.Equal(t, nil, applyDeleteErr)
}
func TestContainerResolveId(t *testing.T) {
}

View File

@ -132,6 +132,7 @@ func (f *File) ResolveId(ctx context.Context) string {
if fileAbsErr != nil {
panic(fileAbsErr)
}
f.Path = filePath
return filePath
}

View File

@ -2,8 +2,9 @@
package resource
import (
"context"
"os"
_ "path/filepath"
"path/filepath"
_ "fmt"
"log"
"testing"
@ -32,3 +33,14 @@ func TestNewResource(t *testing.T) {
assert.Equal(t, "foo", testFile.(*File).Path)
}
func TestResolveId(t *testing.T) {
testFile := NewResource("file://../../README.md")
assert.NotNil(t, testFile)
absolutePath,e := filepath.Abs("../../README.md")
assert.Nil(t, e)
testFile.ResolveId(context.Background())
assert.Equal(t, absolutePath, testFile.(*File).Path)
}

View File

@ -2,7 +2,7 @@
package resource
import (
"context"
_ "context"
"testing"
"github.com/stretchr/testify/assert"
"decl/tests/mocks"
@ -15,12 +15,7 @@ func TestNewResourceTypes(t *testing.T) {
}
func TestNewResourceTypesRegister(t *testing.T) {
m := &mocks.MockResource {
InjectType: func() string { return "foo" },
InjectRead: func(ctx context.Context) ([]byte, error) { return nil,nil },
InjectLoadDecl: func(string) error { return nil },
InjectApply: func() error { return nil },
}
m := mocks.NewFooResource()
resourceTypes := NewTypes()
assert.NotEqual(t, nil, resourceTypes)
@ -33,12 +28,7 @@ func TestNewResourceTypesRegister(t *testing.T) {
}
func TestResourceTypesFromURI(t *testing.T) {
m := &mocks.MockResource {
InjectType: func() string { return "foo" },
InjectRead: func(ctx context.Context) ([]byte, error) { return nil,nil },
InjectLoadDecl: func(string) error { return nil },
InjectApply: func() error { return nil },
}
m := mocks.NewFooResource()
resourceTypes := NewTypes()
assert.NotEqual(t, nil, resourceTypes)

View File

@ -5,10 +5,13 @@ import (
"context"
"fmt"
_ "os"
_ "gopkg.in/yaml.v3"
"gopkg.in/yaml.v3"
"os/exec"
"strings"
"log"
"net/url"
"os/user"
"strconv"
)
type User struct {
@ -29,6 +32,15 @@ func NewUser() *User {
return &User{ loader: YamlLoadDecl }
}
func init() {
ResourceTypes.Register("user", func(u *url.URL) Resource {
user := NewUser()
user.Name = u.Path
user.UID, _ = LookupUID(u.Path)
return user
})
}
func (u *User) URI() string {
return fmt.Sprintf("user://%s", u.Name)
}
@ -112,3 +124,31 @@ func (u *User) UserAddCommand(args *[]string) error {
}
func (u *User) Type() string { return "user" }
func (u *User) Read(ctx context.Context) ([]byte, error) {
var readUser *user.User
var e error
if u.Name != "" {
readUser,e = user.Lookup(u.Name)
}
if u.UID >= 0 {
readUser,e = user.LookupId(strconv.Itoa(u.UID))
}
if e != nil {
panic(e)
}
u.Name = readUser.Username
u.UID,_ = strconv.Atoi(readUser.Uid)
if readGroup, groupErr := user.LookupGroupId(readUser.Gid); groupErr == nil {
u.Group = readGroup.Name
} else {
panic(groupErr)
}
u.Home = readUser.HomeDir
u.Gecos = readUser.Name
return yaml.Marshal(u)
}

View File

@ -10,6 +10,7 @@ import (
)
type MockContainerClient struct {
InjectContainerStart func(ctx context.Context, containerID string, options container.StartOptions) error
InjectContainerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
InjectContainerList func(context.Context, types.ContainerListOptions) ([]types.Container, error)
InjectContainerInspect func(context.Context, string) (types.ContainerJSON, error)
@ -21,7 +22,7 @@ func (m *MockContainerClient) ContainerCreate(ctx context.Context, config *conta
return m.InjectContainerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName)
}
func (m *MockContainerClient) (ctx context.Context, containerID string, options container.StartOptions) error {
func (m *MockContainerClient) ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error {
if m.InjectContainerStart == nil {
return nil
}

View File

@ -9,6 +9,7 @@ _ "gopkg.in/yaml.v3"
func NewFooResource() *MockResource {
return &MockResource {
InjectType: func() string { return "foo" },
InjectResolveId: func(ctx context.Context) string { return "bar" },
InjectRead: func(ctx context.Context) ([]byte, error) { return nil,nil },
InjectLoadDecl: func(string) error { return nil },
InjectApply: func() error { return nil },

View File

@ -9,6 +9,7 @@ _ "gopkg.in/yaml.v3"
type MockResource struct {
InjectURI func() string
InjectType func() string
InjectResolveId func(ctx context.Context) string
InjectLoadDecl func(string) error
InjectApply func() error
InjectRead func(context.Context) ([]byte, error)
@ -18,6 +19,10 @@ func (m *MockResource) URI() string {
return m.InjectURI()
}
func (m *MockResource) ResolveId(ctx context.Context) string {
return m.ResolveId(ctx)
}
func (m *MockResource) LoadDecl(yamlResourceDeclaration string) error {
return m.InjectLoadDecl(yamlResourceDeclaration)
}