Skip to content

Commit b01c5ad

Browse files
committed
Added test for os minor version
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent e57cbe7 commit b01c5ad

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

libenv/sysinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,7 +3816,7 @@ static void SysOSVersionMinor(EvalContext *ctx)
38163816
const char *version_id = OSReleaseGet(ctx, "VERSION_ID");
38173817
const char *name = OSReleaseGet(ctx, "NAME");
38183818

3819-
char *minor;
3819+
char *minor = NULL;
38203820
if (version_id != NULL)
38213821
{
38223822
Item *version_tuple = SplitString(version_id, '.');
@@ -3831,11 +3831,11 @@ static void SysOSVersionMinor(EvalContext *ctx)
38313831
{
38323832
minor = version_tuple->next->name;
38333833
}
3834+
free(version_tuple);
38343835
}
3835-
free(version_tuple);
38363836
}
38373837

3838-
if (NULL_OR_EMPTY(minor))
3838+
if (minor == NULL)
38393839
{
38403840
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
38413841
"os_version_minor", "Unknown",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
body common control
2+
{
3+
bundlesequence => { "test", "check" };
4+
}
5+
6+
bundle agent test
7+
{
8+
9+
vars:
10+
# Platforms to test
11+
any::
12+
"platforms"
13+
slist => { "debian", "ubuntu", "redhat", "rhel", "centos", "fedora",
14+
"aix", "hpux", "suse", "opensuse", "opensuse_leap", "sles",
15+
"solaris", "sunos", "windows", "freebsd", "macos" };
16+
17+
18+
# Regex matching current platforms OS-class with version numbers
19+
!solaris&!sunos::
20+
"class_regex"
21+
string => format("^(%s)_[0-9]+_[0-9]+$", join("|", "platforms"));
22+
solaris|sunos::
23+
"class_regex"
24+
string => format("^(%s)_[0-9]+$", join("|", "platforms"));
25+
26+
# Regex to extract major version number from OS-class
27+
# Edge cases:
28+
# - On Solaris/SunOS major version comes second
29+
# E.g. Solaris 11 has class "solaris_5_11"
30+
any::
31+
"extract_regex"
32+
string => ifelse("solaris|sunos", "^[a-z]+_([0-9]+)$",
33+
"opensuse_leap", "^[a-z_]+_[0-9]+_([0-9]+$)",
34+
"^[a-z]+_[0-9]+_([0-9]+$)");
35+
36+
# Find OS-class with version numbers using regex
37+
any::
38+
"os_class"
39+
string => nth(classesmatching("$(class_regex)"), "0");
40+
41+
# Get extracted major version number
42+
any::
43+
"expected"
44+
string => nth("version_number", "1");
45+
46+
classes:
47+
any::
48+
"regextract_success"
49+
expression => regextract("$(extract_regex)", "$(os_class)", "version_number");
50+
}
51+
52+
bundle agent check
53+
{
54+
vars:
55+
any::
56+
"defined_classes"
57+
slist => classesmatching(".*");
58+
59+
classes:
60+
any::
61+
"passed"
62+
expression => strcmp("$(test.expected)", "$(sys.os_version_minor)");
63+
64+
reports:
65+
DEBUG::
66+
"Version number extracted from class: $(test.os_class)";
67+
"Defined classes: $(defined_classes)";
68+
"$(this.promise_filename) Expected: $(test.expected)";
69+
"$(this.promise_filename) Found: $(sys.os_version_minor)";
70+
passed::
71+
"$(this.promise_filename) Pass";
72+
!passed::
73+
"$(this.promise_filename) FAIL";
74+
}

0 commit comments

Comments
 (0)