-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang: funcs: Testing some simple functions and mcl
- Loading branch information
Showing
9 changed files
with
398 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import "fmt" | ||
import "os" | ||
import "strings" | ||
|
||
$s = os.readfile("/etc/hosts") | ||
$substr = "127.0.0.1" | ||
$b = strings.contains($s, $substr) | ||
|
||
print "print1" { | ||
msg => $b, | ||
} | ||
|
||
file "/tmp/mgmt/checklist" { | ||
state => $const.res.file.state.exists, | ||
content => fmt.printf("/etc/hosts %s: %s", $substr, $b), | ||
} |
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,12 @@ | ||
import "net" | ||
|
||
$ipport = net.ip_port("192.168.1.1", "433") | ||
|
||
print "ipport" { | ||
msg => $ipport, | ||
} | ||
|
||
file "/tmp/mgmt/ipport" { | ||
state => $const.res.file.state.exists, | ||
content => $ipport, | ||
} |
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,14 @@ | ||
import "fmt" | ||
import "strings" | ||
import "sys" | ||
import "os" | ||
|
||
$home = sys.getenv("HOME") | ||
$file = "${home}/.bashrc" | ||
$s = os.readfile($file) | ||
$substr = "" | ||
$lenoffile = strings.count($s, $substr) | ||
|
||
print "print0" { | ||
msg => fmt.printf("char length of file: %s: %s", $file, $lenoffileb, | ||
} |
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,58 @@ | ||
// Mgmt | ||
// Copyright (C) 2013-2021+ James Shubin and the project contributors | ||
// Written by James Shubin <[email protected]> and the project contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package corenet | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/purpleidea/mgmt/lang/funcs/simple" | ||
"github.com/purpleidea/mgmt/lang/types" | ||
) | ||
|
||
func init() { | ||
simple.ModuleRegister(ModuleName, "ip_port", &types.FuncValue{ | ||
T: types.NewType("func(ip str, port str) str"), | ||
V: IPPort, | ||
}) | ||
} | ||
|
||
// IPPort returns the combind IPv4/IPv6:(input[0]) and Port:(input[1]). Returns | ||
// error if IPv4/IPv6 string is incorrect format or Port not in range 0-65536. | ||
func IPPort(input []types.Value) (types.Value, error) { | ||
ip := net.ParseIP(input[0].Str()) // Is nil if incorrect format. | ||
ipStr := input[0].Str() | ||
port := input[1].Str() | ||
portInt, err := strconv.Atoi(port) | ||
|
||
if ip == nil { | ||
return &types.StrValue{V: ""}, errors.New("incorrect ip format") | ||
} | ||
if err != nil { | ||
return &types.StrValue{V: ""}, fmt.Errorf("err converting str to int %v", err) | ||
} | ||
if portInt < 0 || portInt > 65536 { | ||
return &types.StrValue{V: ""}, errors.New("port not in range 0-65536") | ||
} | ||
|
||
return &types.StrValue{ | ||
V: ipStr + ":" + port, | ||
}, nil | ||
} |
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,76 @@ | ||
// Mgmt | ||
// Copyright (C) 2013-2021+ James Shubin and the project contributors | ||
// Written by James Shubin <[email protected]> and the project contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package corenet | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/purpleidea/mgmt/lang/types" | ||
) | ||
|
||
func TestIPPort(t *testing.T) { | ||
ipporttests := []struct { | ||
name string | ||
ip string | ||
port string | ||
expected string | ||
err error | ||
}{ | ||
// Success | ||
{"correct ipv4 and port", "192.168.1.1", "80", "192.168.1.1:80", nil}, | ||
{"correct ipv6 and port", "2345:0425:2CA1:0000:0000:0567:5673:23b5", "8080", "2345:0425:2CA1:0000:0000:0567:5673:23b5:8080", nil}, | ||
{"correct ipv4 and port - allow port 0", "192.168.1.1", "0", "192.168.1.1:0", nil}, | ||
{"correct ipv6 and port - allows port 65536", "2345:0425:2CA1::0567:5673:23b5", "65536", "2345:0425:2CA1::0567:5673:23b5:65536", nil}, | ||
// Fail | ||
{"incorrect ipv4 - octet over 255", "392.868.11.79", "80", "", errors.New("incorrect ip format")}, | ||
{"incorrect ipv4 - CIDR format", "10.10.10.100/8", "23", "", errors.New("incorrect ip format")}, | ||
{"incorrect ipv4 - dots...", "172.16.10..254", "23", "", errors.New("incorrect ip format")}, | ||
{"incorrect ipv6 - double double colon", "5678:A425:2CA1::0567::5673:23b5", "80", "", errors.New("incorrect ip format")}, | ||
{"incorrect ipv6 - non hex chars", "M678:Z425:2CA1::05X7:T673:23b5", "1234", "", errors.New("incorrect ip format")}, | ||
{"incorrect port - outside of range", "192.168.1.1", "65537", "", errors.New("port not in range 0-65536")}, | ||
{"incorrect port - negative port number", "192.168.1.1", "-9483670", "", errors.New("port not in range 0-65536")}, | ||
} | ||
|
||
for _, test := range ipporttests { | ||
t.Run(test.name, func(t *testing.T) { | ||
output, err := IPPort([]types.Value{ | ||
&types.StrValue{V: test.ip}, | ||
&types.StrValue{V: test.port}, | ||
}) | ||
expectedStr := &types.StrValue{V: test.expected} | ||
|
||
if test.err != nil && err.Error() != test.err.Error() { | ||
t.Errorf("ip: %s, port %s, expected error: %q, got %q", test.ip, test.port, test.err, err) | ||
return | ||
} else if test.err != nil && err == nil { | ||
t.Errorf("ip: %s, port: %s, expected error: %v, but got nil", test.ip, test.port, test.err) | ||
return | ||
} else if test.err == nil && err != nil { | ||
t.Errorf("ip: %s, port %s, did not expect error but got: %#v", test.ip, test.port, err) | ||
return | ||
} | ||
if err1 := output.Cmp(expectedStr); err1 != nil { | ||
t.Errorf("ip: %s, port: %s, expected: %s, got: %s", test.ip, test.port, expectedStr, output) | ||
return | ||
} | ||
|
||
}) | ||
} | ||
|
||
} |
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,39 @@ | ||
// Mgmt | ||
// Copyright (C) 2013-2021+ James Shubin and the project contributors | ||
// Written by James Shubin <[email protected]> and the project contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package corestrings | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/purpleidea/mgmt/lang/funcs/simple" | ||
"github.com/purpleidea/mgmt/lang/types" | ||
) | ||
|
||
func init() { | ||
simple.ModuleRegister(ModuleName, "contains", &types.FuncValue{ | ||
T: types.NewType("func(s str, substr str) str"), | ||
V: Contains, | ||
}) | ||
} | ||
|
||
// Contains reports whether substr is within s. | ||
func Contains(input []types.Value) (types.Value, error) { | ||
s, substr := input[0].Str(), input[1].Str() | ||
return &types.StrValue{V: strconv.FormatBool(strings.Contains(s, substr))}, nil | ||
} |
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,65 @@ | ||
// Mgmt | ||
// Copyright (C) 2013-2021+ James Shubin and the project contributors | ||
// Written by James Shubin <[email protected]> and the project contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package corestrings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/purpleidea/mgmt/lang/types" | ||
) | ||
|
||
func TestContains(t *testing.T) { | ||
containsTests := []struct { | ||
name string | ||
s string | ||
substr string | ||
expected string | ||
err error | ||
}{ | ||
// Success | ||
{"does contain", "tomorrow", "row", "true", nil}, | ||
{"does not contain", "fighter", "light", "false", nil}, | ||
} | ||
|
||
for _, test := range containsTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
output, err := Contains([]types.Value{ | ||
&types.StrValue{V: test.s}, | ||
&types.StrValue{V: test.substr}, | ||
}) | ||
expectedStr := &types.StrValue{V: test.expected} | ||
|
||
if test.err != nil && err.Error() != test.err.Error() { | ||
t.Errorf("s: %s, substr %s, expected error: %q, got %q", test.s, test.substr, test.err, err) | ||
return | ||
} else if test.err != nil && err == nil { | ||
t.Errorf("s: %s, substr: %s, expected error: %v, but got nil", test.s, test.substr, test.err) | ||
return | ||
} else if test.err == nil && err != nil { | ||
t.Errorf("s: %s, substr %s, did not expect error but got: %#v", test.s, test.substr, err) | ||
return | ||
} | ||
if err1 := output.Cmp(expectedStr); err1 != nil { | ||
t.Errorf("s: %s, substr: %s, expected: %s, got: %s", test.s, test.substr, expectedStr, output) | ||
return | ||
} | ||
|
||
}) | ||
} | ||
|
||
} |
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,49 @@ | ||
// Mgmt | ||
// Copyright (C) 2013-2021+ James Shubin and the project contributors | ||
// Written by James Shubin <[email protected]> and the project contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package corestrings | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/purpleidea/mgmt/lang/funcs/simple" | ||
"github.com/purpleidea/mgmt/lang/types" | ||
) | ||
|
||
func init() { | ||
simple.ModuleRegister(ModuleName, "count", &types.FuncValue{ | ||
T: types.NewType("func(s str, substr str) str"), | ||
V: Count, | ||
}) | ||
} | ||
|
||
// Count counts the number of non-overlapping instances of substr in s. If | ||
// substr is an empty string, Count returns 0 if empty string and len(string) if | ||
// substr is empty | ||
func Count(input []types.Value) (types.Value, error) { | ||
s, substr := input[0].Str(), input[1].Str() | ||
if s == "" { | ||
return &types.StrValue{V: "0"}, nil | ||
} | ||
|
||
count := strings.Count(s, substr) | ||
if substr == "" { | ||
count-- | ||
} | ||
return &types.StrValue{V: fmt.Sprint(count)}, nil | ||
} |
Oops, something went wrong.