-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from remimendes/clusterip_test
adding tests
- Loading branch information
Showing
18 changed files
with
1,695 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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")...) | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
Oops, something went wrong.