-
Notifications
You must be signed in to change notification settings - Fork 26
[ST] Add MustGather and publish logs from tests in AZP #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
im-konge
wants to merge
9
commits into
strimzi:main
Choose a base branch
from
im-konge:store-logs-correctly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+247
−4
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e87b244
Store logs from STs correctly in AZP
im-konge 1c92b21
add log collector
im-konge 9988894
debug
im-konge 9edabfe
fixup! debug
im-konge bcf3585
fixup! fixup! debug
im-konge f5c21bb
remove debug stuff
im-konge d7eb7ae
use mustgather
im-konge 40e1571
changes
im-konge 02e50e7
Maros' comments
im-konge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
systemtest/src/main/java/io/strimzi/kafka/access/log/MustGatherImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Copyright Strimzi authors. | ||
| * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
| */ | ||
| package io.strimzi.kafka.access.log; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.LabelSelectorBuilder; | ||
| import io.skodjob.testframe.LogCollector; | ||
| import io.skodjob.testframe.LogCollectorBuilder; | ||
| import io.skodjob.testframe.clients.KubeClient; | ||
| import io.skodjob.testframe.clients.cmdClient.Kubectl; | ||
| import io.skodjob.testframe.interfaces.MustGatherSupplier; | ||
| import io.strimzi.api.kafka.model.kafka.Kafka; | ||
| import io.strimzi.api.kafka.model.user.KafkaUser; | ||
| import io.strimzi.kafka.access.Environment; | ||
| import io.strimzi.kafka.access.TestConstants; | ||
| import io.strimzi.kafka.access.model.KafkaAccess; | ||
| import io.strimzi.kafka.access.utils.TestUtils; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Path; | ||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneId; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Implementation class for {@link io.skodjob.testframe.annotations.MustGather}, containing handling | ||
| * of the log collection in case of test failure. | ||
| */ | ||
| public final class MustGatherImpl implements MustGatherSupplier { | ||
| private final LogCollector defaultLogCollector = new LogCollectorBuilder() | ||
| .withKubeClient(new KubeClient()) | ||
| .withKubeCmdClient(new Kubectl()) | ||
| .withRootFolderPath(Environment.TEST_LOG_DIR) | ||
| .withNamespacedResources( | ||
| List.of( | ||
| TestConstants.SECRET.toLowerCase(Locale.ROOT), | ||
| TestConstants.DEPLOYMENT.toLowerCase(Locale.ROOT), | ||
| Kafka.RESOURCE_SINGULAR, | ||
| KafkaUser.RESOURCE_SINGULAR, | ||
| KafkaAccess.KIND | ||
| ).toArray(new String[0]) | ||
| ) | ||
| .withCollectPreviousLogs() | ||
| .build(); | ||
|
|
||
| private static final String CURRENT_DATE; | ||
|
|
||
| static { | ||
| // Get current date to create a unique folder | ||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"); | ||
| dateTimeFormatter = dateTimeFormatter.withZone(ZoneId.of("GMT")); | ||
| CURRENT_DATE = dateTimeFormatter.format(LocalDateTime.now()); | ||
| } | ||
|
|
||
| @Override | ||
| public void saveKubernetesState(ExtensionContext extensionContext) { | ||
| String testClass = extensionContext.getRequiredTestClass().getName(); | ||
| String testClassShortName = TestUtils.removePackageName(testClass); | ||
| String testCase = extensionContext.getRequiredTestClass() != null ? extensionContext.getRequiredTestClass().getSimpleName() : null; | ||
|
|
||
| LogCollector logCollector = new LogCollectorBuilder(defaultLogCollector) | ||
| .withRootFolderPath(buildFullPathToLogs(testClass, testCase).toString()) | ||
| .build(); | ||
|
|
||
| // firstly collect resources for class | ||
| logCollector.collectFromNamespacesWithLabels( | ||
| new LabelSelectorBuilder() | ||
| .withMatchLabels(Map.of(TestConstants.TEST_SUITE_NAME_LABEL, testClassShortName)) | ||
| .build() | ||
| ); | ||
|
|
||
| // then collect from Namespaces in test itself | ||
| if (testCase != null) { | ||
| logCollector.collectFromNamespacesWithLabels( | ||
| new LabelSelectorBuilder() | ||
| .withMatchLabels( | ||
| Map.of( | ||
| TestConstants.TEST_SUITE_NAME_LABEL, testClassShortName, | ||
| TestConstants.TEST_CASE_NAME_LABEL, testCase | ||
| ) | ||
| ) | ||
| .build() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method that checks existence of the folder on specified path. | ||
| * From there, if there are no sub-dirs created - for each of the test-case run/re-run - the method returns the | ||
| * full path containing the specified path and index (1). | ||
| * Otherwise, it lists all the directories, filtering all the folders that are indexes, takes the last one, and returns | ||
| * the full path containing specified path and index increased by one. | ||
| * | ||
| * @param rootPathToLogsForTestCase complete path for test-class/test-class and test-case logs | ||
| * | ||
| * @return full path to logs directory built from specified root path and index | ||
| */ | ||
| private Path checkPathAndReturnFullRootPathWithIndexFolder(Path rootPathToLogsForTestCase) { | ||
| File logsForTestCase = rootPathToLogsForTestCase.toFile(); | ||
| int index = 1; | ||
|
|
||
| if (logsForTestCase.exists()) { | ||
| String[] filesInLogsDir = logsForTestCase.list(); | ||
|
|
||
| if (filesInLogsDir != null && filesInLogsDir.length > 0) { | ||
| List<String> indexes = Arrays | ||
| .stream(filesInLogsDir) | ||
| .filter(file -> { | ||
| try { | ||
| Integer.parseInt(file); | ||
| return true; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| }) | ||
| .sorted() | ||
| .toList(); | ||
|
|
||
| // check if there is actually something in the list of folders | ||
| if (!indexes.isEmpty()) { | ||
| // take the highest index and increase it by one for a new directory | ||
| index = Integer.parseInt(indexes.get(indexes.size() - 1)) + 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return rootPathToLogsForTestCase.resolve(String.valueOf(index)); | ||
| } | ||
|
|
||
| /** | ||
| * Method for building the full path to logs for specified test-class and test-case. | ||
| * | ||
| * @param testClass name of the test-class | ||
| * @param testCase name of the test-case | ||
| * | ||
| * @return full path to the logs for test-class and test-case, together with index | ||
| */ | ||
| private Path buildFullPathToLogs(String testClass, String testCase) { | ||
| Path rootPathToLogsForTestCase = Path.of(Environment.TEST_LOG_DIR, CURRENT_DATE, testClass); | ||
|
|
||
| if (testCase != null) { | ||
| rootPathToLogsForTestCase = rootPathToLogsForTestCase.resolve(testCase); | ||
| } | ||
|
|
||
| return checkPathAndReturnFullRootPathWithIndexFolder(rootPathToLogsForTestCase); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
systemtest/src/main/java/io/strimzi/kafka/access/utils/TestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright Strimzi authors. | ||
| * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
| */ | ||
| package io.strimzi.kafka.access.utils; | ||
|
|
||
| public class TestUtils { | ||
im-konge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private TestUtils() {} | ||
|
|
||
| /** | ||
| * Method for cutting the length of the test case in case that it's too long for having it as label in the particular resource. | ||
| * | ||
| * @param testCaseName test case name that should be trimmed | ||
| * | ||
| * @return trimmed test case name if needed | ||
| */ | ||
| public static String trimTestCaseBaseOnItsLength(String testCaseName) { | ||
| // because label values `must be no more than 63 characters` | ||
| if (testCaseName.length() > 63) { | ||
| // we cut to 62 characters | ||
| return testCaseName.substring(0, 62); | ||
| } | ||
|
|
||
| return testCaseName; | ||
| } | ||
|
|
||
| public static String removePackageName(String testClassPath) { | ||
| return testClassPath.replace("io.strimzi.kafka.access.", ""); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.