Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tests #413

Merged
merged 17 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion core/clusterip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ func (t L) Len() int {
}

func (t L) Less(i, j int) bool {
return t[i].Path.String() < t[j].Path.String()
switch {
case t[i].Path.String() != t[j].Path.String():
return t[i].Path.String() < t[j].Path.String()
case t[i].Node != t[j].Node:
return t[i].Node < t[j].Node
case t[i].RID != t[j].RID:
return t[i].RID < t[j].RID
case !t[i].IP.Equal(t[j].IP):
return t[i].IP.String() < t[j].IP.String()
default:
return false
}
}

func (t L) Swap(i, j int) {
Expand Down
48 changes: 48 additions & 0 deletions core/clusterip/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package clusterip

import (
"github.com/opensvc/om3/core/path"
"github.com/stretchr/testify/assert"
"net"
"sort"
"testing"
)

func TestSort(t *testing.T) {
sortedList := func() L {
return L{
{IP: net.IP{10, 8, 0, 8}, Node: "test", Path: path.T{Name: "a", Namespace: "A", Kind: path.KindCcfg}, RID: "ip#10"},
{IP: net.IP{10, 200, 3, 1}, Node: "test", Path: path.T{Name: "b", Namespace: "A", Kind: path.KindSvc}, RID: "ip#3"},
{IP: net.IP{10, 6, 6, 9}, Node: "a", Path: path.T{Name: "z", Namespace: "C", Kind: path.KindVol}, RID: "ip#99"},
{IP: net.IP{10, 0, 0, 0}, Node: "b", Path: path.T{Name: "z", Namespace: "C", Kind: path.KindVol}, RID: "ip#70"},
{IP: net.IP{10, 20, 1, 1}, Node: "b", Path: path.T{Name: "z", Namespace: "D", Kind: path.KindSvc}, RID: "ip#10"},
{IP: net.IP{10, 30, 0, 1}, Node: "b", Path: path.T{Name: "z", Namespace: "D", Kind: path.KindSvc}, RID: "ip#10"},
{IP: net.IP{10, 99, 99, 1}, Node: "b", Path: path.T{Name: "z", Namespace: "D", Kind: path.KindUsr}, RID: "ip#10"},
{IP: net.IP{10, 0, 99, 1}, Node: "b", Path: path.T{Name: "z", Namespace: "D", Kind: path.KindUsr}, RID: "ip#8"},
}
}
unsortedList := func(order []int) L {
list := L{}
ori := sortedList()
for _, v := range order {
list = append(list, ori[v])
}
return list
}
listToBeSorted := unsortedList([]int{1, 2, 0, 3, 6, 4, 5, 7})
sort.Sort(listToBeSorted)
assert.Equal(t, sortedList(), listToBeSorted)

listToBeSorted = unsortedList([]int{4, 5, 6, 1, 3, 2, 0, 7})
sort.Sort(listToBeSorted)
assert.Equal(t, sortedList(), listToBeSorted)

listToBeSorted = unsortedList([]int{4, 0, 5, 2, 1, 6, 7, 3})
sort.Sort(listToBeSorted)
assert.Equal(t, sortedList(), listToBeSorted)

listToBeSorted = unsortedList([]int{6, 7, 3, 4, 5, 2, 1, 0})
sort.Sort(listToBeSorted)
assert.Equal(t, sortedList(), listToBeSorted)

}
95 changes: 79 additions & 16 deletions util/compobj/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,39 @@ type (
}
CompFile struct {
Path string `json:"path"`
Mode int `json:"mode"`
Mode *int `json:"mode"`
UID interface{} `json:"uid"`
GID interface{} `json:"gid"`
Fmt string `json:"fmt"`
Fmt *string `json:"fmt"`
Ref string `json:"ref"`
}
)

var compFilesInfo = ObjInfo{
DefaultPrefix: "OSVC_COMP_FILE_",
ExampleValue: CompFile{
Path: "/some/path/to/file",
Fmt: "[email protected] %%HOSTNAME%%@corp.com",
UID: 500,
GID: 500,
},
Description: `* Verify and install file content.
var (
collectorSafeGetMetaFunc = collectorSafeGetMeta

stringFmt = "[email protected] %%HOSTNAME%%@corp.com"

compFilesInfo = ObjInfo{
DefaultPrefix: "OSVC_COMP_FILE_",
ExampleValue: CompFile{
Path: "/some/path/to/file",
Fmt: &stringFmt,
UID: 500,
GID: 500,
},
Description: `* Verify and install file content.
* Verify and set file or directory ownership and permission
* Directory mode is triggered if the path ends with /
* Only for the fmt field : if the newline character is not present at the end of the text, one is automatically added

Special wildcards::

%%ENV:VARNAME%% Any environment variable value
%%HOSTNAME%% Hostname
%%SHORT_HOSTNAME%% Short hostname
`,
FormDefinition: `Desc: |
FormDefinition: `Desc: |
A file rule, fed to the 'files' compliance object to create a directory or a file and set its ownership and permissions. For files, a reference content can be specified or pointed through an URL.
Css: comp48

Expand Down Expand Up @@ -114,7 +120,8 @@ Inputs:
Help: A reference content for the file. The text can embed substitution variables specified with %%ENV:VAR%%.
Type: text
`,
}
}
)

func init() {
m["file"] = NewCompFiles
Expand All @@ -137,7 +144,7 @@ func (t *CompFiles) Add(s string) error {

func (t CompFile) Content() ([]byte, error) {
if t.Ref == "" {
b := []byte(t.Fmt)
b := []byte(*t.Fmt)
if !bytes.HasSuffix(b, []byte("\n")) {
b = append(b, []byte("\n")...)
}
Expand Down Expand Up @@ -173,16 +180,37 @@ func (t CompFile) ParseGID() int {
}

func (t CompFile) FileMode() (os.FileMode, error) {
s := fmt.Sprintf("0%d", t.Mode)
s := fmt.Sprintf("0%d", *t.Mode)
i, err := strconv.ParseInt(s, 8, 32)
if err != nil {
return os.FileMode(0), err
}
return os.FileMode(i), nil
}

func (t CompFiles) checkPathExistance(rule CompFile) ExitCode {
_, err := os.Lstat(rule.Path)
m, _ := file.Mode(rule.Path)
t.VerboseErrorf(m.String())
if err != nil {
if os.IsNotExist(err) {
t.VerboseErrorf("the file %s does not exist\n", rule.Path)
return ExitNok
}
t.VerboseErrorf("can't check if the file %s exist: %s", rule.Path, err)
return ExitNok
}
return ExitOk
}

func (t CompFiles) checkMode(rule CompFile) ExitCode {
if rule.Mode == nil {
return ExitNotApplicable
}
target, err := rule.FileMode()
if strings.HasSuffix(rule.Path, "/") {
target = target | os.ModeDir
}
if err != nil {
t.VerboseErrorf("file %s parse target mode: %s\n", rule.Path, err)
return ExitNok
Expand All @@ -202,6 +230,9 @@ func (t CompFiles) checkMode(rule CompFile) ExitCode {

func (t CompFiles) fixMode(rule CompFile) ExitCode {
target, err := rule.FileMode()
if strings.HasSuffix(rule.Path, "/") {
target = target | os.ModeDir
}
if err != nil {
t.Errorf("file %s parse target mode: %s\n", rule.Path, err)
return ExitNok
Expand Down Expand Up @@ -273,7 +304,7 @@ func (t CompFile) isSafeRef() bool {
}

func (t CompFiles) checkSafeRef(rule CompFile) ExitCode {
meta, err := collectorSafeGetMeta(rule.Ref)
meta, err := collectorSafeGetMetaFunc(rule.Ref)
currentMD5, err := file.MD5(rule.Path)
if err != nil {
t.VerboseErrorf("file %s md5sum: %s\n", rule.Path, err)
Expand All @@ -288,6 +319,9 @@ func (t CompFiles) checkSafeRef(rule CompFile) ExitCode {
}

func (t CompFiles) checkContent(rule CompFile) ExitCode {
if rule.Ref == "" && rule.Fmt == nil {
return ExitNotApplicable
}
if rule.isSafeRef() {
return t.checkSafeRef(rule)
}
Expand Down Expand Up @@ -342,8 +376,33 @@ func (t CompFiles) fixContent(rule CompFile) ExitCode {
t.Infof("file %s rewritten\n", rule.Path)
return ExitOk
}
func (t CompFiles) fixPathExistance(rule CompFile) ExitCode {
if strings.HasPrefix(rule.Path, "/") {
err := os.Mkdir(rule.Path, 0666)
if err != nil {
t.VerboseErrorf("can't create the file :%s", rule.Path)
return ExitNok
}
return ExitOk
}
f, err := os.Create(rule.Path)
if err != nil {
t.VerboseErrorf("can't create the file :%s", rule.Path)
return ExitNok
}
if err = f.Close(); err != nil {
t.VerboseErrorf("can't close the file :%s", rule.Path)
return ExitNok
}
return ExitOk
}

func (t CompFiles) FixRule(rule CompFile) ExitCode {
if e := t.checkPathExistance(rule); e == ExitNok {
if e := t.fixPathExistance(rule); e == ExitNok {
return ExitNok
}
}
if e := t.checkContent(rule); e == ExitNok {
if e := t.fixContent(rule); e == ExitNok {
return e
Expand All @@ -364,6 +423,10 @@ func (t CompFiles) FixRule(rule CompFile) ExitCode {

func (t CompFiles) CheckRule(rule CompFile) ExitCode {
var e, o ExitCode
if o = t.checkPathExistance(rule); o == ExitNok {
return ExitNok
}
e = e.Merge(o)
o = t.checkContent(rule)
e = e.Merge(o)
o = t.checkOwnership(rule)
Expand Down
Loading