diff --git a/internal/resource/declaration_test.go b/internal/resource/declaration_test.go new file mode 100644 index 0000000..3e9e2e6 --- /dev/null +++ b/internal/resource/declaration_test.go @@ -0,0 +1,75 @@ +// Copyright 2024 Matthew Rich . All rights reserved. +package resource + +import ( +_ "os" + "path/filepath" + "fmt" +_ "log" + "testing" + "github.com/stretchr/testify/assert" +) + +func TestYamlLoadDecl(t *testing.T) { + + file := filepath.Join(TempDir, "fooread.txt") + + resourceAttributes := make(map[string]any) + decl := fmt.Sprintf(` + path: "%s" + owner: "nobody" + group: "nobody" + mode: "0600" + content: |- + test line 1 + test line 2 +`, file) + + e := YamlLoadDecl(decl, &resourceAttributes) + assert.Equal(t, nil, e) + + assert.Equal(t, "nobody", resourceAttributes["group"]) +} + +func TestNewResourceDeclaration(t *testing.T) { + resourceDeclaration := NewDeclaration() + assert.NotEqual(t, nil, resourceDeclaration) +} + +func TestNewResourceDeclarationType(t *testing.T) { + file := filepath.Join(TempDir, "fooread.txt") + + decl := fmt.Sprintf(` + type: file + attributes: + path: "%s" + owner: "nobody" + group: "nobody" + mode: "0600" + content: |- + test line 1 + test line 2 +`, file) + + resourceDeclaration := NewDeclaration() + assert.NotEqual(t, nil, resourceDeclaration) + + resourceDeclaration.LoadDecl(decl) + assert.Equal(t, "file", resourceDeclaration.Type) + assert.NotEqual(t, nil, resourceDeclaration.Attributes) +} + +func TestDeclarationNewResource(t *testing.T) { + resourceDeclaration := NewDeclaration() + assert.NotNil(t, resourceDeclaration) + + errNewUnknownResource := resourceDeclaration.NewResource() + assert.ErrorIs(t, errNewUnknownResource, ErrUnknownResourceType) + + resourceDeclaration.Type = "file" + errNewFileResource := resourceDeclaration.NewResource() + assert.Nil(t, errNewFileResource) + + //assert.NotNil(t, resourceDeclaration.Implementation) + assert.NotNil(t, resourceDeclaration.Attributes) +} diff --git a/internal/resource/resource_test.go b/internal/resource/resource_test.go index 1782d49..953fabd 100644 --- a/internal/resource/resource_test.go +++ b/internal/resource/resource_test.go @@ -1,9 +1,10 @@ +// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "os" - "path/filepath" - "fmt" +_ "path/filepath" +_ "fmt" "log" "testing" "github.com/stretchr/testify/assert" @@ -17,58 +18,17 @@ func TestMain(m *testing.M) { if err != nil || TempDir == "" { log.Fatal(err) } - defer os.RemoveAll(TempDir) rc := m.Run() + os.RemoveAll(TempDir) os.Exit(rc) } -func TestYamlLoadDecl(t *testing.T) { +func TestNewResource(t *testing.T) { + resourceUri := "file://foo" + testFile := NewResource(resourceUri) + assert.NotNil(t, testFile) - file := filepath.Join(TempDir, "fooread.txt") - - resourceAttributes := make(map[string]any) - decl := fmt.Sprintf(` - path: "%s" - owner: "nobody" - group: "nobody" - mode: "0600" - content: |- - test line 1 - test line 2 -`, file) - - e := YamlLoadDecl(decl, &resourceAttributes) - assert.Equal(t, nil, e) - - assert.Equal(t, "nobody", resourceAttributes["group"]) -} - -func TestNewResourceDeclaration(t *testing.T) { - resourceDeclaration := NewDeclaration() - assert.NotEqual(t, nil, resourceDeclaration) -} - -func TestNewResourceDeclarationType(t *testing.T) { - file := filepath.Join(TempDir, "fooread.txt") - - decl := fmt.Sprintf(` - type: file - attributes: - path: "%s" - owner: "nobody" - group: "nobody" - mode: "0600" - content: |- - test line 1 - test line 2 -`, file) - - resourceDeclaration := NewDeclaration() - assert.NotEqual(t, nil, resourceDeclaration) - - resourceDeclaration.LoadDecl(decl) - assert.Equal(t, "file", resourceDeclaration.Type) - assert.NotEqual(t, nil, resourceDeclaration.Attributes) + assert.Equal(t, "foo", testFile.(*File).Path) } diff --git a/internal/resource/user.go b/internal/resource/user.go index d08704f..b769436 100644 --- a/internal/resource/user.go +++ b/internal/resource/user.go @@ -1,6 +1,8 @@ +// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( + "context" "fmt" _ "os" _ "gopkg.in/yaml.v3" @@ -27,6 +29,14 @@ func NewUser() *User { return &User{ loader: YamlLoadDecl } } +func (u *User) URI() string { + return fmt.Sprintf("user://%s", u.Name) +} + +func (u *User) ResolveId(ctx context.Context) string { + return LookupUIDString(u.Name) +} + func (u *User) Apply() error { switch u.State { case "present": @@ -100,3 +110,5 @@ func (u *User) UserAddCommand(args *[]string) error { } return nil } + +func (u *User) Type() string { return "user" } diff --git a/internal/resource/user_test.go b/internal/resource/user_test.go index 35073e7..c6e10ae 100644 --- a/internal/resource/user_test.go +++ b/internal/resource/user_test.go @@ -1,3 +1,4 @@ +// Copyright 2024 Matthew Rich . All rights reserved. package resource import (