Skip to content

Commit

Permalink
CE and DE better support (#1556)
Browse files Browse the repository at this point in the history
* Exclude temp test files

* Add credentials for LATEST CE and LTS CE

* Add tests for CE

* Fix generator for apps and portfolios in lower editions

* Fixes for CE and DE

* Fix for support of CE and DE config export

* Safety record login in to_json()

* Fix for CE

* Formatting

* CE support

* CE support

* Adapt tests to CE

* Avoid repeating soanrcloud tests

* Add latest-ce and lts-ce tests

* Adjust tests to CE

* Adjust tests to CE

* Adjusts tests to CE

* Fix check for edition

* Add more tests

* Fix for sonarcloud

* Fix for SonarCloud

* Fix for SonarCloud
  • Loading branch information
okorach authored Jan 9, 2025
1 parent 0f25f5d commit e31731a
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 193 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ tmp/
test/lts/
test/latest/
test/cloud/
test/latest-ce/
test/lts-ce/
test/latest-de/
test/lts-de/
18 changes: 10 additions & 8 deletions cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,20 @@ def export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> Non
if what_item not in what:
continue
ndx, func, _ = call_data
if not is_first:
print(",", file=fd)
is_first = False
worker = Thread(target=write_objects, args=(write_q, fd, ndx, export_settings))
worker.daemon = True
worker.name = f"Write{ndx[:1].upper()}{ndx[1:10]}"
worker.start()
try:
if not is_first:
print(",", file=fd)
is_first = False
worker = Thread(target=write_objects, args=(write_q, fd, ndx, export_settings))
worker.daemon = True
worker.name = f"Write{ndx[:1].upper()}{ndx[1:10]}"
worker.start()
func(endpoint, export_settings=export_settings, key_list=kwargs[options.KEYS], write_q=write_q)
write_q.join()
except exceptions.UnsupportedOperation as e:
log.warning(e.message)
if write_q:
write_q.put(utilities.WRITE_END)
write_q.join()
print("\n}", file=fd)
remove_empty = False if mode == "MIGRATION" else not kwargs.get(EXPORT_EMPTY, False)
utilities.normalize_json_file(file, remove_empty=remove_empty, remove_none=True)
Expand Down
8 changes: 7 additions & 1 deletion conf/prep_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"

cd "$ROOTDIR/test/unit" || exit 1

for target in lts latest
echo ""
echo "Generating edition / version specific tests"

for target in lts latest latest-ce lts-ce
do
rm -rf "$ROOTDIR/test/$target"
mkdir -p "$ROOTDIR/test/$target" 2>/dev/null
Expand All @@ -35,4 +38,7 @@ do
cp "credentials-$target.py" "$ROOTDIR/test/$target/credentials.py"
mv "$ROOTDIR/test/$target/conftest_${target}.py" "$ROOTDIR/test/$target/conftest.py"
mv "$ROOTDIR/test/$target/utilities_${target}.py" "$ROOTDIR/test/$target/utilities.py"
if [ "$target" != "latest" ]; then
rm "$ROOTDIR/test/$target/"test_sonarcloud*.py
fi
done
2 changes: 1 addition & 1 deletion conf/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo "Running tests"

export SONAR_HOST_URL=${1:-${SONAR_HOST_URL}}

for target in latest lts cloud
for target in latest lts latest-ce lts-ce cloud
do
if [ -d "$ROOTDIR/test/$target/" ]; then
coverage run --branch --source="$ROOTDIR" -m pytest "$ROOTDIR/test/$target/" --junit-xml="$buildDir/xunit-results-$target.xml"
Expand Down
2 changes: 1 addition & 1 deletion sonar/audit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_property(name: str, settings: Optional[types.ConfigSettings] = None) ->
"""Returns the value of a given property"""
if settings is None:
settings = _CONFIG_SETTINGS
return settings.get(name, "")
return "" if not settings else settings.get(name, "")


def configure() -> None:
Expand Down
3 changes: 3 additions & 0 deletions sonar/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ def _audit_logs(self, audit_settings: types.ConfigSettings) -> list[Problem]:
if not audit_settings.get("audit.logs", True):
log.info("Logs audit is disabled, skipping logs audit...")
return []
if self.is_sonarcloud():
log.info("Logs audit not available with SonarQube Cloud, skipping logs audit...")
return []
log_map = {"app": "sonar.log", "ce": "ce.log", "web": "web.log", "es": "es.log"}
if self.edition() == "datacenter":
log_map.pop("es")
Expand Down
1 change: 1 addition & 0 deletions sonar/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def to_json(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr
:rtype: dict
"""
json_data = self.sq_json.copy()
json_data["login"] = self.login
json_data["scmAccounts"] = self.scm_accounts
json_data["groups"] = self.groups().copy()
if export_settings.get("MODE", "") == "MIGRATION":
Expand Down
89 changes: 42 additions & 47 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

def create_test_object(a_class: type, key: str) -> any:
"""Creates a SonarQube test object of a given class"""
util.start_logging()
try:
o = a_class.get_object(endpoint=util.SQ, key=key)
except exceptions.ObjectNotFound:
Expand Down Expand Up @@ -70,55 +69,67 @@ def get_test_project() -> Generator[projects.Project]:
@pytest.fixture
def get_test_app() -> Generator[applications.Application]:
"""setup of tests"""
o = create_test_object(applications.Application, key=util.TEMP_KEY)
o = None
if util.SQ.edition() in ("developer", "enterprise", "datacenter"):
o = create_test_object(applications.Application, key=util.TEMP_KEY)
yield o
o.key = util.TEMP_KEY
try:
o.delete()
except exceptions.ObjectNotFound:
pass
if util.SQ.edition() in ("developer", "enterprise", "datacenter"):
o.key = util.TEMP_KEY
try:
o.delete()
except exceptions.ObjectNotFound:
pass


@pytest.fixture
def get_test_portfolio() -> Generator[portfolios.Portfolio]:
"""setup of tests"""
o = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY)
o = None
if util.SQ.edition() in ("enterprise", "datacenter"):
o = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY)
yield o
o.key = util.TEMP_KEY
try:
o.delete()
except exceptions.ObjectNotFound:
pass
if util.SQ.edition() in ("enterprise", "datacenter"):
o.key = util.TEMP_KEY
try:
o.delete()
except exceptions.ObjectNotFound:
pass


@pytest.fixture
def get_test_portfolio_2() -> Generator[portfolios.Portfolio]:
"""setup of tests"""
o = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY_2)
o = None
if util.SQ.edition() in ("enterprise", "datacenter"):
o = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY_2)
yield o
o.key = util.TEMP_KEY_2
try:
o.delete()
except exceptions.ObjectNotFound:
pass
if util.SQ.edition() in ("enterprise", "datacenter"):
o.key = util.TEMP_KEY_2
try:
o.delete()
except exceptions.ObjectNotFound:
pass


@pytest.fixture
def get_test_subportfolio() -> Generator[portfolios.Portfolio]:
"""setup of tests"""
parent = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY)
subp = parent.add_standard_subportfolio(key=util.TEMP_KEY_3, name=util.TEMP_KEY_3)
subp = None
if util.SQ.edition() in ("enterprise", "datacenter"):
parent = create_test_object(portfolios.Portfolio, key=util.TEMP_KEY)
subp = parent.add_standard_subportfolio(key=util.TEMP_KEY_3, name=util.TEMP_KEY_3)
yield subp
subp.key = util.TEMP_KEY_3
try:
subp.delete()
except exceptions.ObjectNotFound:
pass
parent.key = util.TEMP_KEY
try:
parent.delete()
except exceptions.ObjectNotFound:
pass
if util.SQ.edition() in ("enterprise", "datacenter"):
subp.key = util.TEMP_KEY_3
try:
subp.delete()
except exceptions.ObjectNotFound:
pass
parent.key = util.TEMP_KEY
try:
parent.delete()
except exceptions.ObjectNotFound:
pass


@pytest.fixture
Expand Down Expand Up @@ -216,22 +227,6 @@ def get_sarif_file() -> Generator[str]:
rm(file)


@pytest.fixture
def get_test_application() -> Generator[applications.Application]:
"""setup of tests"""
util.start_logging()
try:
o = applications.Application.get_object(endpoint=util.SQ, key=util.TEMP_KEY)
except exceptions.ObjectNotFound:
o = applications.Application.create(endpoint=util.SQ, key=util.TEMP_KEY, name=util.TEMP_KEY)
yield o
try:
o.key = util.TEMP_KEY
o.delete()
except exceptions.ObjectNotFound:
pass


@pytest.fixture
def get_test_quality_gate() -> Generator[qualitygates.QualityGate]:
"""setup of tests"""
Expand Down
25 changes: 25 additions & 0 deletions test/unit/credentials-latest-ce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
#
# sonar-tools tests
# Copyright (C) 2025 Olivier Korach
# mailto:olivier.korach AT gmail DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

from os import getenv

TARGET_PLATFORM = "http://localhost:10001"
TARGET_TOKEN = getenv("SONAR_TOKEN_LATEST_ADMIN_USER")
25 changes: 25 additions & 0 deletions test/unit/credentials-lts-ce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
#
# sonar-tools tests
# Copyright (C) 2025 Olivier Korach
# mailto:olivier.korach AT gmail DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

from os import getenv

TARGET_PLATFORM = "http://localhost:9001"
TARGET_TOKEN = getenv("SONAR_TOKEN_LTS_ADMIN_USER")
Loading

0 comments on commit e31731a

Please sign in to comment.