Skip to content

Commit dcc3edc

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

File tree

2 files changed

+129
-33
lines changed

2 files changed

+129
-33
lines changed

libenv/sysinfo.c

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,7 +3786,6 @@ static void SysOsVersionMajor(EvalContext *ctx)
37863786

37873787
static const char *OSReleaseGet(EvalContext *ctx, const char *field)
37883788
{
3789-
#ifndef __MINGW32__
37903789
DataType type_out;
37913790
const JsonElement *os_rel = EvalContextVariableGetSpecial(
37923791
ctx, SPECIAL_SCOPE_SYS, "os_release", &type_out);
@@ -3802,52 +3801,75 @@ static const char *OSReleaseGet(EvalContext *ctx, const char *field)
38023801
}
38033802
const char *result = JsonPrimitiveGetAsString(child);
38043803
return result;
3804+
}
38053805

3806-
#else
3807-
3808-
Log(LOG_LEVEL_ERR, "os-release is not defined on Windows")
3809-
return NULL;
3810-
3811-
#endif
3806+
static void OSVersionMinorPut(EvalContext *ctx, const char *minor)
3807+
{
3808+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3809+
"os_version_minor",
3810+
(minor == NULL) ? "Unknown" : minor,
3811+
CF_DATA_TYPE_STRING,
3812+
"source=agent,derived-from=os_release");
38123813
}
38133814

38143815
static void SysOSVersionMinor(EvalContext *ctx)
38153816
{
3817+
#ifndef __MINGW32__
3818+
38163819
const char *version_id = OSReleaseGet(ctx, "VERSION_ID");
38173820
const char *name = OSReleaseGet(ctx, "NAME");
3818-
3819-
char *minor;
3820-
if (version_id != NULL)
3821+
if (version_id == NULL)
38213822
{
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);
3823+
return OSVersionMinorPut(ctx, NULL);
38363824
}
3837-
3838-
if (NULL_OR_EMPTY(minor))
3825+
if (name == NULL)
38393826
{
3840-
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3841-
"os_version_minor", "Unknown",
3842-
CF_DATA_TYPE_STRING, "source=agent");
3827+
return OSVersionMinorPut(ctx, NULL);
38433828
}
3844-
else
3829+
Item *version_tuple = SplitString(version_id, '.');
3830+
if (version_tuple == NULL)
3831+
{
3832+
return OSVersionMinorPut(ctx, NULL);
3833+
}
3834+
if (name != NULL && (StringStartsWith(name, "solaris") || StringStartsWith(name, "sunos")))
38453835
{
3836+
OSVersionMinorPut(ctx, version_tuple->name);
3837+
return DeleteItemList(version_tuple);
3838+
}
3839+
if (version_tuple->next == NULL)
3840+
{
3841+
OSVersionMinorPut(ctx, NULL);
3842+
return DeleteItemList(version_tuple);
3843+
}
3844+
OSVersionMinorPut(ctx, version_tuple->next->name);
3845+
return DeleteItemList(version_tuple);
3846+
3847+
#else
3848+
3849+
char *release = SafeStringDuplicate(VSYSNAME.release);
3850+
char *major = NULL;
3851+
char *minor = NULL;
3852+
char *rel;
3853+
rel = FindNextInteger(release, &major);
3854+
if (rel == NULL)
3855+
{
3856+
free(release);
38463857
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3847-
"os_version_minor", minor,
3848-
CF_DATA_TYPE_STRING,
3849-
"source=agent,derived-from=os_release");
3858+
"os_version_minor",
3859+
"Unknown",
3860+
CF_DATA_TYPE_STRING,
3861+
"source=agent")
3862+
return;
38503863
}
3864+
rel = FindNextInteger(rel, &minor);
3865+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS,
3866+
"os_version_minor",
3867+
(minor == NULL) ? "Unknown" : minor,
3868+
CF_DATA_TYPE_STRING,
3869+
"source=agent");
3870+
free(release);
3871+
3872+
#endif
38513873
}
38523874

38533875

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)