Skip to content

Commit 57a45b8

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

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

libenv/sysinfo.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,15 +3814,11 @@ static void OSVersionMinorPut(EvalContext *ctx, const char *minor)
38143814

38153815
static void SysOSVersionMinor(EvalContext *ctx)
38163816
{
3817-
#ifndef __MINGW32__
3817+
#ifndef __MINGW32__
38183818

38193819
const char *version_id = OSReleaseGet(ctx, "VERSION_ID");
38203820
const char *name = OSReleaseGet(ctx, "NAME");
3821-
if (version_id == NULL)
3822-
{
3823-
return OSVersionMinorPut(ctx, NULL);
3824-
}
3825-
if (name == NULL)
3821+
if ((version_id == NULL) || (name == NULL))
38263822
{
38273823
return OSVersionMinorPut(ctx, NULL);
38283824
}
@@ -3831,7 +3827,7 @@ static void SysOSVersionMinor(EvalContext *ctx)
38313827
{
38323828
return OSVersionMinorPut(ctx, NULL);
38333829
}
3834-
if (name != NULL && (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos")))
3830+
if (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos"))
38353831
{
38363832
OSVersionMinorPut(ctx, version_tuple->name);
38373833
return DeleteItemList(version_tuple);
@@ -3844,7 +3840,7 @@ static void SysOSVersionMinor(EvalContext *ctx)
38443840
OSVersionMinorPut(ctx, version_tuple->next->name);
38453841
return DeleteItemList(version_tuple);
38463842

3847-
#else
3843+
#else
38483844

38493845
char *release = SafeStringDuplicate(VSYSNAME.release);
38503846
char *major = NULL;
@@ -3869,7 +3865,7 @@ static void SysOSVersionMinor(EvalContext *ctx)
38693865
"source=agent");
38703866
free(release);
38713867

3872-
#endif
3868+
#endif
38733869
}
38743870

38753871

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 minor version number from OS-class
27+
# Edge cases:
28+
# - On Solaris/SunOS minor 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 minor 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)