100 lines
1.7 KiB
Go
100 lines
1.7 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
package resource
|
|
|
|
import (
|
|
"context"
|
|
_ "encoding/json"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
_ "io"
|
|
_ "net/http"
|
|
_ "net/http/httptest"
|
|
_ "net/url"
|
|
_ "os"
|
|
_ "strings"
|
|
"testing"
|
|
"decl/internal/data"
|
|
)
|
|
|
|
func TestNewUserResource(t *testing.T) {
|
|
u := NewUser()
|
|
assert.NotEqual(t, nil, u)
|
|
}
|
|
|
|
func TestReadUser(t *testing.T) {
|
|
ctx := context.Background()
|
|
decl := `
|
|
name: "nobody"
|
|
`
|
|
|
|
u := NewUser()
|
|
e := u.LoadDecl(decl)
|
|
assert.Nil(t, e)
|
|
assert.Equal(t, "nobody", u.Name)
|
|
|
|
fmt.Printf("%#v\n", u)
|
|
_, readErr := u.Read(ctx)
|
|
assert.Nil(t, readErr)
|
|
|
|
fmt.Printf("%#v\n", u)
|
|
assert.Equal(t, "65534", u.UID)
|
|
|
|
}
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
|
|
decl := `
|
|
name: "testuser"
|
|
uid: 12001
|
|
gid: 12001
|
|
home: "/home/testuser"
|
|
state: present
|
|
`
|
|
|
|
u := NewUser()
|
|
e := u.LoadDecl(decl)
|
|
assert.Equal(t, nil, e)
|
|
assert.Equal(t, "testuser", u.Name)
|
|
|
|
u.CreateCommand.Executor = func(value any) ([]byte, error) {
|
|
return []byte(``), nil
|
|
}
|
|
|
|
u.ReadCommand.Extractor = func(out []byte, target any) error {
|
|
return nil
|
|
}
|
|
|
|
u.DeleteCommand.Executor = func(value any) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
applyErr := u.Apply()
|
|
assert.Nil(t, applyErr)
|
|
|
|
assert.Equal(t, "12001", u.UID)
|
|
|
|
u.State = "absent"
|
|
|
|
applyDeleteErr := u.Apply()
|
|
assert.Equal(t, nil, applyDeleteErr)
|
|
}
|
|
|
|
func TestSystemUser(t *testing.T) {
|
|
u := NewUser()
|
|
|
|
u.UseConfig(MockConfigValueGetter(func(key string) (any, error) {
|
|
switch key {
|
|
case "user":
|
|
return "bar", nil
|
|
case "group":
|
|
return "foo", nil
|
|
case "uid":
|
|
return "", nil
|
|
}
|
|
return nil, fmt.Errorf("%w: %s", data.ErrUnknownConfigurationKey, key)
|
|
}))
|
|
|
|
u.ResolveId(context.Background())
|
|
assert.Equal(t, "bar", u.Name)
|
|
}
|