Skip to content
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

features list command #433

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.prospero.it.cli;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.Stream;
import org.wildfly.prospero.actions.InstallationHistoryAction;
import org.wildfly.prospero.api.SavedState;
import org.wildfly.prospero.cli.ReturnCodes;
import org.wildfly.prospero.cli.commands.CliConstants;
import org.wildfly.prospero.it.ExecutionUtils;
import org.wildfly.prospero.test.MetadataTestUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

public class FeaturesAddTest {

protected static final String DATASOURCE_GALLEON_FPL = "org.wildfly:wildfly-datasources-galleon-pack";
protected static final String DATASOURCES_FP_VERSION = "4.0.0.Final";
protected static final String MYSQL_CONNECTOR_VERSION = "8.0.32";
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();

private File targetDir;

@Before
public void setUp() throws IOException {
targetDir = tempDir.newFolder("wildfly");
}

@Test
public void addFeaturePack() throws Exception {
Path channelsFile = MetadataTestUtils.prepareChannel("manifests/wildfly-27.0.1.Final-channel.yaml");

System.out.println("Installing wildfly");
ExecutionUtils.prosperoExecution(CliConstants.Commands.INSTALL,
CliConstants.CHANNELS, channelsFile.toString(),
CliConstants.PROFILE, "wildfly",
CliConstants.DIR, targetDir.getAbsolutePath())
.withTimeLimit(10, TimeUnit.MINUTES)
.execute()
.assertReturnCode(ReturnCodes.SUCCESS);

final ChannelManifest manifest = new ChannelManifest(null, null, null, List.of(
new Stream("org.wildfly", "wildfly-datasources-galleon-pack", DATASOURCES_FP_VERSION),
new Stream("com.mysql", "mysql-connector-j", MYSQL_CONNECTOR_VERSION))
);
final Path manifestPath = tempDir.newFile("datasources-manifest.yaml").toPath();
Files.writeString(manifestPath, ChannelManifestMapper.toYaml(manifest));

System.out.println("Adding datasources channel");
ExecutionUtils.prosperoExecution(CliConstants.Commands.CHANNEL, CliConstants.Commands.ADD,
CliConstants.CHANNEL_NAME, "datasources",
CliConstants.REPOSITORIES, "central::https://repo1.maven.org/maven2",
CliConstants.CHANNEL_MANIFEST, manifestPath.toUri().toURL().toExternalForm(),
CliConstants.DIR, targetDir.getAbsolutePath())
.withTimeLimit(10, TimeUnit.MINUTES)
.execute()
.assertReturnCode(ReturnCodes.SUCCESS);

// install the datasource FP
System.out.println("Installing datasources feature pack");
ExecutionUtils.prosperoExecution(CliConstants.Commands.FEATURES, CliConstants.Commands.ADD,
CliConstants.FPL, DATASOURCE_GALLEON_FPL,
CliConstants.LAYERS, "datasources-web-server,mysql-datasource",
CliConstants.YES,
CliConstants.DIR, targetDir.getAbsolutePath())
.withTimeLimit(10, TimeUnit.MINUTES)
.execute()
.assertReturnCode(ReturnCodes.SUCCESS);

// verify the datasources feature pack was installed successfully
final Path modulePath = Path.of("modules", "com", "mysql", "jdbc");
assertThat(targetDir.toPath().resolve(modulePath))
.exists()
.isDirectory();

assertThat(targetDir.toPath().resolve(modulePath).resolve("main").resolve("mysql-connector-j-8.0.32.jar"))
.exists()
.isNotEmptyFile();

// rollback the datasources feature pack

System.out.println("Getting the previous state from history");
final SavedState savedState = new InstallationHistoryAction(targetDir.toPath(), null).getRevisions().get(1);
System.out.println("Rolling back the datasources feature pack");
ExecutionUtils.prosperoExecution(CliConstants.Commands.REVERT, CliConstants.Commands.PERFORM,
CliConstants.REVISION, savedState.getName(),
CliConstants.YES,
CliConstants.DIR, targetDir.getAbsolutePath())
.withTimeLimit(10, TimeUnit.MINUTES)
.execute()
.assertReturnCode(ReturnCodes.SUCCESS);

assertThat(targetDir.toPath().resolve(modulePath))
.doesNotExist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.aether.artifact.DefaultArtifact;
import org.jboss.galleon.Constants;
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.util.PathsUtils;
import org.junit.Test;
import org.wildfly.channel.Channel;
import org.wildfly.channel.Repository;
Expand Down Expand Up @@ -83,6 +84,10 @@ public void installWildflyCore() throws Exception {
assertThat(record.get().getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getUrl)
.containsExactly(MetadataTestUtils.class.getClassLoader().getResource(CHANNEL_BASE_CORE_19).toExternalForm());

// verify the provisioning.xml was recorded in the .installation folder
assertThat(outputPath.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.PROVISIONING_RECORD_XML))
.hasSameTextualContentAs(PathsUtils.getProvisioningXml(outputPath));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.galleon.ProvisioningException;
import org.wildfly.channel.Repository;
import org.wildfly.prospero.actions.ApplyCandidateAction;
import org.wildfly.prospero.actions.FeaturesAddAction;
import org.wildfly.prospero.api.Console;
import org.wildfly.prospero.actions.InstallationExportAction;
import org.wildfly.prospero.actions.InstallationHistoryAction;
Expand Down Expand Up @@ -74,4 +75,9 @@ public InstallationExportAction exportAction(Path targetPath) {
public InstallationRestoreAction restoreAction(Path targetPath, MavenOptions mavenOptions, Console console) throws ProvisioningException {
return new InstallationRestoreAction(targetPath, mavenOptions, console);
}

public FeaturesAddAction featuresAddAction(Path installationDir, MavenOptions mavenOptions, List<Repository> repositories, Console console)
throws ProvisioningException, MetadataException {
return new FeaturesAddAction(mavenOptions, installationDir, repositories, console);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.wildfly.prospero.cli.commands.CliConstants;
import org.wildfly.prospero.cli.commands.CloneCommand;
import org.wildfly.prospero.cli.commands.CompletionCommand;
import org.wildfly.prospero.cli.commands.FeaturesCommand;
import org.wildfly.prospero.cli.commands.HistoryCommand;
import org.wildfly.prospero.cli.commands.InstallCommand;
import org.wildfly.prospero.cli.commands.MainCommand;
Expand Down Expand Up @@ -91,6 +92,10 @@ public static CommandLine createCommandLine(CliConsole console, ActionFactory ac
commandLine.addSubcommand(cloneCommand);
cloneCommand.addSubCommands(commandLine);

final FeaturesCommand featuresCommand = new FeaturesCommand(console, actionFactory);
commandLine.addSubcommand(featuresCommand);
featuresCommand.addSubCommands(commandLine);

commandLine.setUsageHelpAutoWidth(true);
commandLine.setExecutionExceptionHandler(new ExecutionExceptionHandler(console));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.wildfly.prospero.cli;

import org.apache.commons.lang3.StringUtils;
import org.jboss.galleon.Constants;
import org.wildfly.prospero.actions.ApplyCandidateAction;
import org.wildfly.prospero.cli.commands.CliConstants;
Expand All @@ -26,6 +27,7 @@
import java.nio.file.Path;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;

import static java.lang.String.format;

Expand Down Expand Up @@ -524,4 +526,71 @@ default ArgumentParsingException missingRequiresResource(String resource) {
default String parsingError(String path) {
return format(bundle.getString("prospero.general.error.galleon.parse"), path);
}

default String featurePackNotFound(String featurePackName) {
return format(bundle.getString("prospero.general.error.feature_pack.not_found"), featurePackName);
}

default ArgumentParsingException featurePackNameNotMavenCoordinate() {
return new ArgumentParsingException(format(bundle.getString("prospero.features.add.validation.fpl_name")));
}

default String layerNotSupported(String fpl, String layerName, Set<String> supportedLayers) {
return format(bundle.getString("prospero.features.add.validation.layer.not_supported"),
fpl, layerName, StringUtils.join(supportedLayers, ", "));
}

default String layerNotSupported(String fpl) {
return format(bundle.getString("prospero.features.add.validation.layer.no_layers"), fpl);
}

default String modelNotSupported(String fpl, String model, Set<String> supportedModels) {
return format(bundle.getString("prospero.features.add.validation.model.not_supported"),
fpl, model, StringUtils.join(supportedModels, ", "));
}

default String featuresAddHeader(String fpl, Path dir) {
return format(bundle.getString("prospero.features.add.header"), fpl, dir);
}

default String featuresAddPrompt() {
return bundle.getString("prospero.features.add.prompt");
}

default String featuresAddPromptAccepted() {
return bundle.getString("prospero.features.add.prompt.yes");
}

default String featuresAddPromptCancelled() {
return bundle.getString("prospero.features.add.prompt.no");
}

default String featurePackTitle() {
return bundle.getString("prospero.history.feature_pack.title");
}

default String configurationModel() {
return bundle.getString("prospero.history.configuration_model.title");
}

default String diffFeaturesChanges() {
return bundle.getString("prospero.changes.diff.features_changes");
}

default String featuresListHeader(Path dir) {
return format(bundle.getString("prospero.features.list.header"), dir);
}

default String featuresIncludedLayers() {
return bundle.getString("prospero.features.list.included_layers");
}

default String featuresModel() {
return bundle.getString("prospero.features.list.model");
}

default String featuresLayer() {
return bundle.getString("prospero.features.list.layer");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.wildfly.prospero.api.ArtifactChange;
import org.wildfly.prospero.api.ChannelChange;
import org.wildfly.prospero.api.Diff;
import org.wildfly.prospero.api.FeatureChange;

public class DiffPrinter {

Expand All @@ -38,23 +39,29 @@ private void print(Diff diff, String tab, boolean nested) {


if (diff.getChildren().isEmpty()) {
String nameText = diff.getName().map(s -> s + ":\t\t").orElse("");
if (nested) {
System.out.printf("%s%s ==> %s%n", nameText, diff.getOldValue().orElse("[]"), diff.getNewValue().orElse("[]"));
if (diff.hasValues()) {
String nameText = diff.getName().map(s -> s + ":\t\t").orElse("");
print(diff, nested, "%s%s ==> %s%n", nameText, diff.getOldValue().orElse("[]"), diff.getNewValue().orElse("[]"));
} else {
System.out.printf("[%s] %s%s ==> %s%n", getStatus(diff), nameText, diff.getOldValue().orElse("[]"), diff.getNewValue().orElse("[]"));
String nameText = diff.getName().map(s -> s + "\t\t").orElse("");
print(diff, nested, "%s%n", nameText);
}
} else {
String nameText = diff.getName().orElse("");
if (nested) {
System.out.printf("%s: %n", nameText);
} else {
System.out.printf("[%s] %s:%n", getStatus(diff), nameText);
}
print(diff, nested, "%s: %n", nameText);
diff.getChildren().forEach(c -> print(c, tab + " ", true));
}
}

private static void print(Diff diff, boolean nested, String text, String... args) {
if (!nested) {
text = String.format("[%s] ", getStatus(diff)) + String.format(text, (String[]) args);
} else {
text = String.format(text, (String[]) args);
}
System.out.print(text);
}

private static String getStatus(Diff diff) {
Diff.Status status = diff.getStatus();
String statusText;
Expand All @@ -77,6 +84,18 @@ private static String getStatus(Diff diff) {
diffType = CliMessages.MESSAGES.artifactChangeType();
} else if (diff instanceof ChannelChange) {
diffType = CliMessages.MESSAGES.channelChangeType();
} else if (diff instanceof FeatureChange) {
final FeatureChange.Type subType = ((FeatureChange) diff).getType();
switch (subType) {
case FEATURE:
diffType = CliMessages.MESSAGES.featurePackTitle();
break;
case CONFIG:
diffType = CliMessages.MESSAGES.configurationModel();
break;
default:
diffType = subType.name();
}
} else {
diffType = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ private CliConstants() {
*/
public static final class Commands {


private Commands() {
}

public static final String MAIN_COMMAND = "prospero";
public static final String FEATURES = "features";
public static final String INSTALL = "install";
public static final String UPDATE = "update";
public static final String HISTORY = "history";
Expand Down Expand Up @@ -96,4 +98,8 @@ private Commands() {
public static final String ARG_PATH = "--path";

public static final String ACCEPT_AGREEMENTS = "--accept-license-agreements";

public static final String LAYERS = "--layers";
public static final String MODEL = "--model";
public static final String CONFIG = "--config";
}
Loading