2024-03-20 19:25:25 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
2024-03-20 16:15:27 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2024-05-06 00:48:54 +00:00
|
|
|
"context"
|
2024-03-25 20:31:06 +00:00
|
|
|
_ "encoding/json"
|
2024-05-06 00:48:54 +00:00
|
|
|
"fmt"
|
2024-03-25 20:31:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
_ "io"
|
|
|
|
_ "net/http"
|
|
|
|
_ "net/http/httptest"
|
|
|
|
_ "net/url"
|
|
|
|
_ "os"
|
|
|
|
_ "strings"
|
|
|
|
"testing"
|
2024-03-20 16:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewUserResource(t *testing.T) {
|
2024-03-25 20:31:06 +00:00
|
|
|
u := NewUser()
|
|
|
|
assert.NotEqual(t, nil, u)
|
2024-03-20 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-20 16:15:27 +00:00
|
|
|
func TestCreateUser(t *testing.T) {
|
2024-05-06 00:48:54 +00:00
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
decl := `
|
2024-03-20 16:15:27 +00:00
|
|
|
name: "testuser"
|
|
|
|
uid: 12001
|
|
|
|
gid: 12001
|
|
|
|
home: "/home/testuser"
|
|
|
|
state: present
|
|
|
|
`
|
2024-05-06 00:48:54 +00:00
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
u := NewUser()
|
|
|
|
e := u.LoadDecl(decl)
|
|
|
|
assert.Equal(t, nil, e)
|
|
|
|
assert.Equal(t, "testuser", u.Name)
|
2024-03-20 16:15:27 +00:00
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
applyErr := u.Apply()
|
2024-05-06 00:48:54 +00:00
|
|
|
assert.Nil(t, applyErr)
|
|
|
|
|
|
|
|
assert.Equal(t, "12001", u.UID)
|
2024-03-20 16:15:27 +00:00
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
u.State = "absent"
|
2024-03-20 16:15:27 +00:00
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
applyDeleteErr := u.Apply()
|
|
|
|
assert.Equal(t, nil, applyDeleteErr)
|
2024-03-20 16:15:27 +00:00
|
|
|
}
|