fix linting errors
Some checks failed
Lint / golangci-lint (push) Failing after 10m15s
Declarative Tests / test (push) Successful in 1m12s

This commit is contained in:
Matthew Rich 2024-04-03 12:27:16 -07:00
parent f2e451e668
commit 08e8ca1aa7
13 changed files with 89 additions and 36 deletions

View File

@ -24,7 +24,7 @@ make build
# Update Resource state
`cli -resource-file decl-runner.yaml`
`decl -resource-file decl-runner.yaml`
# Read resource state
@ -36,6 +36,8 @@ Read the state of an existing resource (URI) and generate a YAML representation
Resources:
* [file](examples/file.yaml)
* [user](examples/user.yaml)
* [container](examples/container.yaml)
* [file](examples/file.yaml) [schema](internal/resource/file.jsonschema)
* [user](examples/user.yaml) [schema](internal/resource/user.jsonschema)
* [package](examples/package.yaml) [schema](internal/resource/package.jsonschema)
* [container](examples/container.yaml) [schema](internal/resource/container.jsonschema)
* [network_route](examples/network_route.yaml) [schema](internal/resource/network_route.jsonschema)

View File

@ -60,9 +60,13 @@ func main() {
}
}
if *resourceUri != "" {
slog.Info("importing resource: %s\n", *resourceUri)
d.AddResource(*resourceUri)
slog.Info("importing resource", "resource", *resourceUri)
if addResourceErr := d.AddResource(*resourceUri); addResourceErr != nil {
log.Fatal(addResourceErr)
}
}
d.Generate(os.Stdout)
if documentGenerateErr := d.Generate(os.Stdout); documentGenerateErr != nil {
log.Fatal(documentGenerateErr)
}
}

View File

@ -62,8 +62,8 @@ func (d *Declaration) SetURI(uri string) error {
panic("unknown resource")
}
d.Type = TypeName(d.Attributes.Type())
d.Attributes.Read(context.Background()) // fix context
return nil
_,e := d.Attributes.Read(context.Background()) // fix context
return e
}
func (d *Declaration) UnmarshalYAML(value *yaml.Node) error {

View File

@ -55,7 +55,8 @@ func TestNewResourceDeclarationType(t *testing.T) {
resourceDeclaration := NewDeclaration()
assert.NotEqual(t, nil, resourceDeclaration)
resourceDeclaration.LoadDecl(decl)
e := resourceDeclaration.LoadDecl(decl)
assert.Nil(t, e)
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
assert.NotEqual(t, nil, resourceDeclaration.Attributes)
}

View File

@ -66,8 +66,12 @@ func (d *Document) Apply() error {
func (d *Document) Generate(w io.Writer) error {
e := NewYAMLEncoder(w)
e.Encode(d)
return e.Close()
err := e.Encode(d);
if err == nil {
return e.Close()
}
e.Close()
return err
}
func (d *Document) AddResourceDeclaration(resourceType string, resourceDeclaration Resource) {
@ -81,7 +85,10 @@ func (d *Document) AddResource(uri string) error {
//parsedResourceURI, e := url.Parse(uri)
//if e == nil {
decl := NewDeclaration()
decl.SetURI(uri)
if e := decl.SetURI(uri); e != nil {
return e
}
d.ResourceDecls = append(d.ResourceDecls, *decl)
//}
return nil

View File

@ -107,7 +107,8 @@ resources:
assert.NotNil(t, f)
f.(*File).Path = filepath.Join(TempDir, "foo.txt")
f.(*File).Read(ctx)
_,readErr := f.(*File).Read(ctx)
assert.Nil(t, readErr)
d.AddResourceDeclaration("file", f)
ey := d.Generate(&documentYaml)
assert.Equal(t, nil, ey)
@ -123,11 +124,12 @@ func TestDocumentAddResource(t *testing.T) {
d := NewDocument()
assert.NotNil(t, d)
d.AddResource(fmt.Sprintf("file://%s", file))
e := d.AddResource(fmt.Sprintf("file://%s", file))
assert.Nil(t, e)
}
func TestDocumentJSON(t *testing.T) {
document := fmt.Sprintf(`
document := `
---
resources:
- type: user
@ -137,7 +139,7 @@ resources:
group: "10022"
home: "/home/testuser"
state: present
`)
`
d := NewDocument()
assert.NotNil(t, d)
docReader := strings.NewReader(document)
@ -158,7 +160,7 @@ func TestDocumentJSONSchema(t *testing.T) {
}
func TestDocumentYAML(t *testing.T) {
document := fmt.Sprintf(`
document := `
---
resources:
- type: user
@ -168,7 +170,7 @@ resources:
group: "10022"
home: "/home/testuser"
state: present
`)
`
d := NewDocument()
assert.NotNil(t, d)
docReader := strings.NewReader(document)

View File

@ -43,7 +43,8 @@ func TestCreateExec(t *testing.T) {
func TestExecSetURI(t *testing.T) {
x := NewExec()
assert.NotNil(t, x)
x.SetURI("exec://" + "12345_key")
e := x.SetURI("exec://" + "12345_key")
assert.Nil(t, e)
assert.Equal(t, "exec", x.Type())
assert.Equal(t, "12345_key", x.Id)
}

View File

@ -69,10 +69,12 @@ func (f *File) URI() string {
func (f *File) SetURI(uri string) error {
resourceUri, e := url.Parse(uri)
if resourceUri.Scheme == "file" {
f.Path, e = filepath.Abs(filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()))
} else {
e = fmt.Errorf("%w: %s is not a file", ErrInvalidResourceURI, uri)
if e == nil {
if resourceUri.Scheme == "file" {
f.Path, e = filepath.Abs(filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()))
} else {
e = fmt.Errorf("%w: %s is not a file", ErrInvalidResourceURI, uri)
}
}
return e
}
@ -110,7 +112,9 @@ func (f *File) Apply() error {
return linkErr
}
case DirectoryFile:
os.MkdirAll(f.Path, os.FileMode(mode))
if mkdirErr := os.MkdirAll(f.Path, os.FileMode(mode)); mkdirErr != nil {
return mkdirErr
}
default:
fallthrough
case RegularFile:
@ -201,8 +205,10 @@ func (f *File) ReadStat() error {
}
func (f *File) Read(ctx context.Context) ([]byte, error) {
f.NormalizePath()
if normalizePathErr := f.NormalizePath(); normalizePathErr != nil {
return nil, normalizePathErr
}
statErr := f.ReadStat()
if statErr != nil {
return nil, statErr

View File

@ -57,7 +57,8 @@ func TestReadFile(t *testing.T) {
testFile := NewFile()
e := testFile.LoadDecl(decl)
assert.Equal(t, nil, e)
testFile.Apply()
applyErr := testFile.Apply()
assert.Nil(t, applyErr)
f := NewFile()
assert.NotEqual(t, nil, f)
@ -184,7 +185,8 @@ func TestFileSetURI(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testuri.txt"))
f := NewFile()
assert.NotNil(t, f)
f.SetURI("file://" + file)
e := f.SetURI("file://" + file)
assert.Nil(t, e)
assert.Equal(t, "file", f.Type())
assert.Equal(t, file, f.Path)
}
@ -235,11 +237,14 @@ func TestFileReadStat(t *testing.T) {
l.Target = linkTargetFile
l.State = "present"
l.Apply()
l.ReadStat()
applyErr := l.Apply()
assert.Nil(t, applyErr)
readStatErr := l.ReadStat()
assert.Nil(t, readStatErr)
testRead := NewFile()
testRead.Path = link
testRead.Read(ctx)
_,testReadErr := testRead.Read(ctx)
assert.Nil(t, testReadErr)
assert.Equal(t, linkTargetFile, testRead.Target)
}

View File

@ -50,7 +50,8 @@ func TestReadNetworkRoute(t *testing.T) {
testRoute := NewNetworkRoute()
e := testRoute.LoadDecl(declarationAttributes)
assert.Equal(t, nil, e)
testRoute.Apply()
testRouteErr := testRoute.Apply()
assert.Nil(t, testRouteErr)
r, e := testRoute.Read(ctx)
assert.Nil(t, e)

View File

@ -53,7 +53,8 @@ func TestSchemaValidate(t *testing.T) {
testFile := NewFile()
e := testFile.LoadDecl(decl)
assert.Equal(t, nil, e)
testFile.Apply()
fileApplyErr := testFile.Apply()
assert.Nil(t, fileApplyErr)
jsonDoc, jsonErr := json.Marshal(testFile)
assert.Nil(t, jsonErr)

View File

@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "package",
"type": "object",
"required": [ "name", "type" ],
"properties": {
"name": {
"type": "string"
},
"version": {
"type": "string"
},
"type": {
"type": "string",
"description": "package type",
"enum": [ "rpm", "deb", "yum", "dnf", "apt", "pip", "go" ]
}
}
}

View File

@ -64,10 +64,14 @@ func (u *User) Apply() error {
if _, pathErr := exec.LookPath("useradd"); pathErr != nil {
if _, addUserPathErr := exec.LookPath("adduser"); addUserPathErr == nil {
userCommandName = "adduser"
u.AddUserCommand(&args)
if addUserCommandErr := u.AddUserCommand(&args); addUserCommandErr != nil {
return addUserCommandErr
}
}
} else {
u.UserAddCommand(&args)
if userAddCommandErr := u.UserAddCommand(&args); userAddCommandErr != nil {
return userAddCommandErr
}
}
args = append(args, u.Name)
cmd := exec.Command(userCommandName, args...)