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

Ensure PAYG refresh process does not call SCC #7699

Merged
merged 2 commits into from
Oct 17, 2023
Merged
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
Expand Up @@ -2488,7 +2488,7 @@ protected boolean accessibleUrl(String url, String user, String password) {
* @throws SCCClientException when access is not possible
* @return {@link SCCWebClient}
*/
private SCCClient getSCCClient(Credentials credentials)
protected SCCClient getSCCClient(Credentials credentials)
throws URISyntaxException, SCCClientException {
// check that URL is valid
URI url = new URI(Config.get().getString(ConfigDefaults.SCC_URL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


import static com.redhat.rhn.domain.channel.test.ChannelFactoryTest.createTestClonedChannel;
import static com.redhat.rhn.testing.RhnBaseTestCase.assertContains;
import static com.redhat.rhn.testing.RhnBaseTestCase.assertNotEmpty;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -67,14 +69,15 @@
import com.redhat.rhn.manager.content.ProductTreeEntry;
import com.redhat.rhn.manager.setup.MirrorCredentialsManager;
import com.redhat.rhn.manager.system.SystemManager;
import com.redhat.rhn.testing.BaseTestCaseWithUser;
import com.redhat.rhn.testing.JMockBaseTestCaseWithUser;
import com.redhat.rhn.testing.TestUtils;

import com.suse.cloud.CloudPaygManager;
import com.suse.manager.webui.services.pillar.MinionGeneralPillarGenerator;
import com.suse.manager.webui.services.pillar.MinionPillarManager;
import com.suse.mgrsync.MgrSyncStatus;
import com.suse.salt.netapi.parser.JsonParser;
import com.suse.scc.client.SCCClient;
import com.suse.scc.model.ChannelFamilyJson;
import com.suse.scc.model.SCCProductJson;
import com.suse.scc.model.SCCRepositoryJson;
Expand Down Expand Up @@ -111,7 +114,7 @@
/**
* Tests for {@link ContentSyncManager}.
*/
public class ContentSyncManagerTest extends BaseTestCaseWithUser {
public class ContentSyncManagerTest extends JMockBaseTestCaseWithUser {

// Files we read
private static final String JARPATH = "/com/redhat/rhn/manager/content/test/";
Expand Down Expand Up @@ -2380,6 +2383,30 @@ public void testBuildRepoFileUrl() throws Exception {
assertEquals(2, csm.buildRepoFileUrl(repourl, rpmrepo).size());
}

@Test
public void updateRepositoriesForPaygDoNotCallSCC() {
Credentials credentials = CredentialsFactory.createSCCCredentials();
credentials.setPassword("dummy");
credentials.setUrl("dummy");
credentials.setUsername("dummy");
credentials.setUser(user);
CredentialsFactory.storeCredentials(credentials);

SCCClient sccClient = mock(SCCClient.class);

checking(expectations -> {
expectations.never(sccClient).listRepositories();
});

ContentSyncManager csm = new ContentSyncManager() {
@Override
protected SCCClient getSCCClient(Credentials credentials) {
return sccClient;
}
};

csm.updateRepositoriesPayg();
}

/**
* {@inheritDoc}
Expand Down
14 changes: 8 additions & 6 deletions java/code/src/com/redhat/rhn/testing/MockObjectTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.redhat.rhn.testing;

import com.suse.utils.Exceptions;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.States;
Expand All @@ -24,8 +26,6 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.function.Consumer;

/**
* jMock boilerplate.
*/
Expand Down Expand Up @@ -59,10 +59,12 @@ public void checking(ExpectationBuilder expectations) {
/**
* @param expectationsConsumer consumer to build the expectations
*/
public void checking(Consumer<Expectations> expectationsConsumer) {
Expectations expectations = new Expectations();
expectationsConsumer.accept(expectations);
context.checking(expectations);
public void checking(Exceptions.ThrowingConsumer<Expectations, Exception> expectationsConsumer) {
Exceptions.handleByWrapping(() -> {
Expectations expectations = new Expectations();
expectationsConsumer.accept(expectations);
context.checking(expectations);
});
}

/**
Expand Down
44 changes: 32 additions & 12 deletions java/code/src/com/suse/utils/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,45 @@ public final class Exceptions {

/**
* Represents an operation that does not accept inputs and returns no result, but may throw an exception.
* @param <E> type of exception
*/
@FunctionalInterface
public interface ThrowingOperation {
public interface ThrowingRunnable<E extends Exception> {
/**
* Performs this operation.
* @throws Exception when an error occurs during the execution
* @throws E when an error occurs during the execution
*/
void execute() throws Exception;
void run() throws E;
}

/**
* Represents an operation that does not accept inputs and returns a value, but may throw an exception.
* @param <T> the type of the return value
* @param <E> type of exception
*/
@FunctionalInterface
public interface ThrowingSupplier<T> {
public interface ThrowingSupplier<T, E extends Exception> {
/**
* Performs this operation.
* @return the result of the operation
* @throws Exception when an error occurs during the execution
* @throws E when an error occurs during the execution
*/
T execute() throws Exception;
T get() throws E;
}

/**
* Represents an operation that accepts a single input argument and returns no result, but may throw an exception.
* @param <T> the type of the return value
* @param <E> type of exception
*/
@FunctionalInterface
public interface ThrowingConsumer<T, E extends Exception> {
/**
* Performs this operation.
* @param value the value to consume
* @throws E when an error occurs during the execution
*/
void accept(T value) throws E;
}

private Exceptions() {
Expand All @@ -51,11 +68,12 @@ private Exceptions() {
/**
* Executes an operation and returns the exception if occurred during the execution.
* @param operation the operation to perform
* @param <E> type of exception
* @return an optional wrapping the exception, or empty if the operation completes successfully.
*/
public static Optional<? extends Exception> handleByReturning(ThrowingOperation operation) {
public static <E extends Exception> Optional<Exception> handleByReturning(ThrowingRunnable<E> operation) {
try {
operation.execute();
operation.run();
return Optional.empty();
}
catch (Exception ex) {
Expand All @@ -66,11 +84,12 @@ public static Optional<? extends Exception> handleByReturning(ThrowingOperation
/**
* Executes an operation and wraps any exception into a runtime exception.
* @param operation the operation to perform
* @param <E> type of exception
* @throws RuntimeException if an exception occurs during the operation.
*/
public static void handleByWrapping(ThrowingOperation operation) {
public static <E extends Exception> void handleByWrapping(ThrowingRunnable<E> operation) {
handleByWrapping(() -> {
operation.execute();
operation.run();
return null;
});
}
Expand All @@ -79,12 +98,13 @@ public static void handleByWrapping(ThrowingOperation operation) {
* Executes an operation and wraps any exception into a runtime exception.
* @param operation the operation to perform
* @param <T> the type of the return value of the operation
* @param <E> type of exception
* @return the result value of the operation
* @throws RuntimeException if an exception occurs during the operation.
*/
public static <T> T handleByWrapping(ThrowingSupplier<T> operation) {
public static <T, E extends Exception> T handleByWrapping(ThrowingSupplier<T, E> operation) {
try {
return operation.execute();
return operation.get();
}
catch (Exception ex) {
throw new RuntimeException("Unable to execute operation", ex);
Expand Down
Loading