update user resource
This commit is contained in:
parent
b58d6d249b
commit
1bea3894ca
@ -89,7 +89,3 @@ func TestCreateContainer(t *testing.T) {
|
|||||||
applyDeleteErr := c.Apply()
|
applyDeleteErr := c.Apply()
|
||||||
assert.Equal(t, nil, applyDeleteErr)
|
assert.Equal(t, nil, applyDeleteErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerResolveId(t *testing.T) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -132,6 +132,7 @@ func (f *File) ResolveId(ctx context.Context) string {
|
|||||||
if fileAbsErr != nil {
|
if fileAbsErr != nil {
|
||||||
panic(fileAbsErr)
|
panic(fileAbsErr)
|
||||||
}
|
}
|
||||||
|
f.Path = filePath
|
||||||
return filePath
|
return filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
_ "path/filepath"
|
"path/filepath"
|
||||||
_ "fmt"
|
_ "fmt"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
@ -32,3 +33,14 @@ func TestNewResource(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "foo", testFile.(*File).Path)
|
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)
|
||||||
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
_ "context"
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"decl/tests/mocks"
|
"decl/tests/mocks"
|
||||||
@ -15,12 +15,7 @@ func TestNewResourceTypes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewResourceTypesRegister(t *testing.T) {
|
func TestNewResourceTypesRegister(t *testing.T) {
|
||||||
m := &mocks.MockResource {
|
m := mocks.NewFooResource()
|
||||||
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 },
|
|
||||||
}
|
|
||||||
|
|
||||||
resourceTypes := NewTypes()
|
resourceTypes := NewTypes()
|
||||||
assert.NotEqual(t, nil, resourceTypes)
|
assert.NotEqual(t, nil, resourceTypes)
|
||||||
@ -33,12 +28,7 @@ func TestNewResourceTypesRegister(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceTypesFromURI(t *testing.T) {
|
func TestResourceTypesFromURI(t *testing.T) {
|
||||||
m := &mocks.MockResource {
|
m := mocks.NewFooResource()
|
||||||
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 },
|
|
||||||
}
|
|
||||||
|
|
||||||
resourceTypes := NewTypes()
|
resourceTypes := NewTypes()
|
||||||
assert.NotEqual(t, nil, resourceTypes)
|
assert.NotEqual(t, nil, resourceTypes)
|
||||||
|
@ -5,10 +5,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "os"
|
_ "os"
|
||||||
_ "gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"os/user"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@ -29,6 +32,15 @@ func NewUser() *User {
|
|||||||
return &User{ loader: YamlLoadDecl }
|
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 {
|
func (u *User) URI() string {
|
||||||
return fmt.Sprintf("user://%s", u.Name)
|
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) 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MockContainerClient struct {
|
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)
|
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)
|
InjectContainerList func(context.Context, types.ContainerListOptions) ([]types.Container, error)
|
||||||
InjectContainerInspect func(context.Context, string) (types.ContainerJSON, 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)
|
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 {
|
if m.InjectContainerStart == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ _ "gopkg.in/yaml.v3"
|
|||||||
func NewFooResource() *MockResource {
|
func NewFooResource() *MockResource {
|
||||||
return &MockResource {
|
return &MockResource {
|
||||||
InjectType: func() string { return "foo" },
|
InjectType: func() string { return "foo" },
|
||||||
|
InjectResolveId: func(ctx context.Context) string { return "bar" },
|
||||||
InjectRead: func(ctx context.Context) ([]byte, error) { return nil,nil },
|
InjectRead: func(ctx context.Context) ([]byte, error) { return nil,nil },
|
||||||
InjectLoadDecl: func(string) error { return nil },
|
InjectLoadDecl: func(string) error { return nil },
|
||||||
InjectApply: func() error { return nil },
|
InjectApply: func() error { return nil },
|
||||||
|
@ -9,6 +9,7 @@ _ "gopkg.in/yaml.v3"
|
|||||||
type MockResource struct {
|
type MockResource struct {
|
||||||
InjectURI func() string
|
InjectURI func() string
|
||||||
InjectType func() string
|
InjectType func() string
|
||||||
|
InjectResolveId func(ctx context.Context) string
|
||||||
InjectLoadDecl func(string) error
|
InjectLoadDecl func(string) error
|
||||||
InjectApply func() error
|
InjectApply func() error
|
||||||
InjectRead func(context.Context) ([]byte, error)
|
InjectRead func(context.Context) ([]byte, error)
|
||||||
@ -18,6 +19,10 @@ func (m *MockResource) URI() string {
|
|||||||
return m.InjectURI()
|
return m.InjectURI()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MockResource) ResolveId(ctx context.Context) string {
|
||||||
|
return m.ResolveId(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MockResource) LoadDecl(yamlResourceDeclaration string) error {
|
func (m *MockResource) LoadDecl(yamlResourceDeclaration string) error {
|
||||||
return m.InjectLoadDecl(yamlResourceDeclaration)
|
return m.InjectLoadDecl(yamlResourceDeclaration)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user