// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "encoding/json" "fmt" "github.com/stretchr/testify/assert" _ "log" _ "os" "path/filepath" "decl/internal/types" "testing" ) /* 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.NotNil(t, resourceDeclaration) e := resourceDeclaration.LoadDecl(decl) assert.Nil(t, e) assert.Equal(t, TypeName("file"), resourceDeclaration.Type) assert.NotNil(t, resourceDeclaration.Attributes) } func TestDeclarationNewResource(t *testing.T) { resourceDeclaration := NewDeclaration() assert.NotNil(t, resourceDeclaration) errNewUnknownResource := resourceDeclaration.NewResource() assert.ErrorIs(t, errNewUnknownResource, types.ErrUnknownType) resourceDeclaration.Type = "file" errNewFileResource := resourceDeclaration.NewResource() assert.Nil(t, errNewFileResource) assert.NotNil(t, resourceDeclaration.Attributes) } func TestDeclarationJson(t *testing.T) { fileDeclJson := ` { "type": "file", "attributes": { "path": "foo" } } ` resourceDeclaration := NewDeclaration() e := json.Unmarshal([]byte(fileDeclJson), resourceDeclaration) assert.Nil(t, e) assert.Equal(t, TypeName("file"), resourceDeclaration.Type) assert.Equal(t, "foo", resourceDeclaration.Attributes.(*File).Path) userDeclJson := ` { "type": "user", "attributes": { "name": "testuser", "uid": "10012" } } ` userResourceDeclaration := NewDeclaration() ue := json.Unmarshal([]byte(userDeclJson), userResourceDeclaration) assert.Nil(t, ue) assert.Equal(t, TypeName("user"), userResourceDeclaration.Type) assert.Equal(t, "testuser", userResourceDeclaration.Attributes.(*User).Name) assert.Equal(t, "10012", userResourceDeclaration.Attributes.(*User).UID) } func TestDeclarationTransition(t *testing.T) { fileName := filepath.Join(TempDir, "testdecl.txt") fileDeclJson := fmt.Sprintf(` { "type": "file", "transition": "present", "attributes": { "path": "%s" } } `, fileName) resourceDeclaration := NewDeclaration() e := json.Unmarshal([]byte(fileDeclJson), resourceDeclaration) assert.Nil(t, e) assert.Equal(t, TypeName("file"), resourceDeclaration.Type) assert.Equal(t, fileName, resourceDeclaration.Attributes.(*File).Path) err := resourceDeclaration.Apply() assert.Nil(t, err) assert.FileExists(t, fileName) }