From 1bea3894ca743c47fc93a2083a9439eb6e878e1e Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Thu, 21 Mar 2024 21:35:17 -0700 Subject: [PATCH] update user resource --- internal/resource/container_test.go | 4 --- internal/resource/file.go | 1 + internal/resource/resource_test.go | 14 +++++++++- internal/resource/types_test.go | 16 +++-------- internal/resource/user.go | 42 ++++++++++++++++++++++++++++- tests/mocks/container.go | 3 ++- tests/mocks/fooresource.go | 1 + tests/mocks/resource.go | 5 ++++ 8 files changed, 66 insertions(+), 20 deletions(-) diff --git a/internal/resource/container_test.go b/internal/resource/container_test.go index 2f1aa05..e46db82 100644 --- a/internal/resource/container_test.go +++ b/internal/resource/container_test.go @@ -89,7 +89,3 @@ func TestCreateContainer(t *testing.T) { applyDeleteErr := c.Apply() assert.Equal(t, nil, applyDeleteErr) } - -func TestContainerResolveId(t *testing.T) { - -} diff --git a/internal/resource/file.go b/internal/resource/file.go index 8f9e5ec..b92adfe 100644 --- a/internal/resource/file.go +++ b/internal/resource/file.go @@ -132,6 +132,7 @@ func (f *File) ResolveId(ctx context.Context) string { if fileAbsErr != nil { panic(fileAbsErr) } + f.Path = filePath return filePath } diff --git a/internal/resource/resource_test.go b/internal/resource/resource_test.go index 953fabd..1ad9dec 100644 --- a/internal/resource/resource_test.go +++ b/internal/resource/resource_test.go @@ -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) +} diff --git a/internal/resource/types_test.go b/internal/resource/types_test.go index fbca305..6920cab 100644 --- a/internal/resource/types_test.go +++ b/internal/resource/types_test.go @@ -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) diff --git a/internal/resource/user.go b/internal/resource/user.go index b769436..0b1700a 100644 --- a/internal/resource/user.go +++ b/internal/resource/user.go @@ -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) +} + diff --git a/tests/mocks/container.go b/tests/mocks/container.go index 6097101..fda6551 100644 --- a/tests/mocks/container.go +++ b/tests/mocks/container.go @@ -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 } diff --git a/tests/mocks/fooresource.go b/tests/mocks/fooresource.go index bd27b06..8f604ac 100644 --- a/tests/mocks/fooresource.go +++ b/tests/mocks/fooresource.go @@ -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 }, diff --git a/tests/mocks/resource.go b/tests/mocks/resource.go index 85de41d..69ac797 100644 --- a/tests/mocks/resource.go +++ b/tests/mocks/resource.go @@ -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) }