Skip to content

Commit d7df578

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

File tree

2 files changed

+107
-24
lines changed

2 files changed

+107
-24
lines changed

libenv/sysinfo.c

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,31 +3811,9 @@ static const char *OSReleaseGet(EvalContext *ctx, const char *field)
38113811
#endif
38123812
}
38133813

3814-
static void SysOSVersionMinor(EvalContext *ctx)
3814+
static void OSVersionMinorPut(EvalContext *ctx, char *minor)
38153815
{
3816-
const char *version_id = OSReleaseGet(ctx, "VERSION_ID");
3817-
const char *name = OSReleaseGet(ctx, "NAME");
3818-
3819-
char *minor;
3820-
if (version_id != NULL)
3821-
{
3822-
Item *version_tuple = SplitString(version_id, '.');
3823-
3824-
if (version_tuple != NULL)
3825-
{
3826-
if (name != NULL && (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos")))
3827-
{
3828-
minor = version_tuple->name;
3829-
}
3830-
else if (version_tuple->next != NULL)
3831-
{
3832-
minor = version_tuple->next->name;
3833-
}
3834-
}
3835-
free(version_tuple);
3836-
}
3837-
3838-
if (NULL_OR_EMPTY(minor))
3816+
if (minor == NULL)
38393817
{
38403818
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
38413819
"os_version_minor", "Unknown",
@@ -3850,6 +3828,37 @@ static void SysOSVersionMinor(EvalContext *ctx)
38503828
}
38513829
}
38523830

3831+
static void SysOSVersionMinor(EvalContext *ctx)
3832+
{
3833+
const char *version_id = OSReleaseGet(ctx, "VERSION_ID");
3834+
const char *name = OSReleaseGet(ctx, "NAME");
3835+
if (version_id == NULL)
3836+
{
3837+
return OSVersionMinorPut(ctx, NULL);
3838+
}
3839+
if (name == NULL)
3840+
{
3841+
return OSVersionMinorPut(ctx, NULL);
3842+
}
3843+
Item *version_tuple = SplitString(version_id, '.');
3844+
if (version_tuple == NULL)
3845+
{
3846+
return OSVersionMinorPut(ctx, NULL);
3847+
}
3848+
if (name != NULL && (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos")))
3849+
{
3850+
OSVersionMinorPut(ctx, version_tuple->name);
3851+
return DeleteItemList(version_tuple);
3852+
}
3853+
if (version_tuple->next == NULL)
3854+
{
3855+
OSVersionMinorPut(ctx, NULL);
3856+
return DeleteItemList(version_tuple);
3857+
}
3858+
OSVersionMinorPut(ctx, version_tuple->next->name);
3859+
return DeleteItemList(version_tuple);
3860+
}
3861+
38533862

38543863
/*****************************************************************************/
38553864

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)