Skip to content

Commit

Permalink
test: add a unit test when podman is not present (redhat-developer#886)
Browse files Browse the repository at this point in the history
* test: add a unit test when podman is not present

Signed-off-by: Stephane Bouchet <[email protected]>

* fix after reviews

Signed-off-by: Stephane Bouchet <[email protected]>

* fix after reviews

Signed-off-by: Stephane Bouchet <[email protected]>

* fix after reviews

Signed-off-by: Stephane Bouchet <[email protected]>

* fix after reviews

Signed-off-by: Stephane Bouchet <[email protected]>

---------

Signed-off-by: Stephane Bouchet <[email protected]>
  • Loading branch information
sbouchet authored Sep 23, 2024
1 parent c124498 commit 8232bf8
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@RunWith(Parameterized.class)
public class OdoCliComponentTest extends OdoCliTest {
protected static final String COMPONENT_PATH = "src/it/projects/go";
private static final String COMPONENT_PATH = "src/it/projects/go";
private final ComponentFeature feature;
private String project;
private String component;
Expand All @@ -50,21 +50,26 @@ public static Iterable<Object[]> data() {
}

@Before
public void initTestEnv() {
public void initTestEnv() throws IOException {
project = PROJECT_PREFIX + random.nextInt();
component = COMPONENT_PREFIX + random.nextInt();
service = SERVICE_PREFIX + random.nextInt();
odo.createDevfileRegistry(REGISTRY_NAME, REGISTRY_URL, null);
}

@After
public void cleanUp() throws IOException {
if (odo.isStarted(COMPONENT_PATH, feature)) {
odo.stop(COMPONENT_PATH, component, feature);
if (odo.isStarted(component, feature)) {
odo.stop(projectPath, component, feature);
}
if (!odo.discover(projectPath).isEmpty()) {
odo.deleteComponent(project, projectPath, component, ComponentKind.DEVFILE);
}
if (project.equals(odo.getCurrentNamespace())) {
odo.deleteProject(project);
}
cleanLocalProjectDirectory(projectPath);
odo.deleteDevfileRegistry(REGISTRY_NAME);
}

protected void startComponent(String component, ComponentFeature feature) throws IOException {
Expand All @@ -87,7 +92,7 @@ public void checkCreateComponent() throws IOException, ExecutionException, Inter
@Test
public void checkCreateAndDiscoverComponent() throws IOException, ExecutionException, InterruptedException {
createComponent(project, component, projectPath);
List<ComponentDescriptor> components = odo.discover(COMPONENT_PATH);
List<ComponentDescriptor> components = odo.discover(projectPath);
assertNotNull(components);
assertEquals(1, components.size());
assertEquals(projectPath, components.get(0).getPath());
Expand All @@ -111,43 +116,68 @@ public void checkCreateComponentAndLinkService() throws IOException, ExecutionEx
createComponent(project, component, projectPath);
ServiceTemplate serviceTemplate = getServiceTemplate();
OperatorCRD crd = getOperatorCRD(serviceTemplate);
assertNotNull(crd);
odo.createService(project, serviceTemplate, crd, service, null, true);
List<Service> deployedServices = odo.getServices(project);
assertNotNull(deployedServices);
assertEquals(1, deployedServices.size());
Service deployedService = deployedServices.get(0);
assertNotNull(deployedService);
String target = deployedService.getName() + '/' + deployedService.getKind() + "." + deployedService.getApiVersion();
Binding binding = odo.link(COMPONENT_PATH, target);
Binding binding = odo.link(projectPath, target);
assertNotNull(binding);
List<Binding> bindings = odo.listBindings(projectPath);
assertEquals(1, bindings.size());
//cleanup
odo.deleteBinding(projectPath, binding.getName());
bindings = odo.listBindings(projectPath);
assertEquals(0, bindings.size());
odo.deleteService(project, deployedService);
deployedServices = odo.getServices(project);
assertEquals(0, deployedServices.size());
}

@Test
public void checkCreateComponentAndListURLs() throws IOException, ExecutionException, InterruptedException {
createComponent(project, component, projectPath);
List<URL> urls = odo.listURLs(COMPONENT_PATH);
List<URL> urls = odo.listURLs(projectPath);
assertEquals(0, urls.size());
startComponent(component, feature);
urls = odo.listURLs(COMPONENT_PATH);
urls = odo.listURLs(projectPath);
assertEquals(1, urls.size());
//cleanup
odo.stop(projectPath, component, feature);
}

@Test
public void checkCreateComponentAndDebug() throws IOException, ExecutionException, InterruptedException {
createComponent(project, component, projectPath);
startComponent(component, feature);
DebugComponentFeature debugComponentFeature = new DebugComponentFeature(feature);
startComponent(component, debugComponentFeature);
assertTrue(odo.isStarted(component, debugComponentFeature));
int debugPort;
try (ServerSocket serverSocket = new ServerSocket(0)) {
debugPort = serverSocket.getLocalPort();
}
ExecHelper.submit(() -> {
try {
odo.debug(projectPath, debugPort);
assertTrue(odo.isStarted(component, feature));
} catch (IOException e) {
fail("Should not raise Exception");
fail("Should not raise Exception: " + e.getMessage());
}
});
List<Component> components = odo.getComponents(project);
assertNotNull(components);
assertEquals(1, components.size());
Component comp = components.get(0);
assertNotNull(comp);
ComponentFeatures features = comp.getLiveFeatures();
assertNotNull(features);
assertFalse(features.isEmpty());
assertEquals(1, features.size());
// assertTrue(features.isDebug()); odo issue, see #redhat-developer/odo/issues/7197
assertTrue(features.isDev());
//cleanup
odo.stop(projectPath, component, feature);
}

Expand All @@ -163,6 +193,25 @@ public void checkCreateComponentStarter() throws IOException, ExecutionException
public void checkCreateComponentAndStartDev() throws IOException, ExecutionException, InterruptedException {
createComponent(project, component, projectPath);
startComponent(component, feature);
with().pollDelay(10, TimeUnit.SECONDS).await().atMost(10, TimeUnit.MINUTES).until(() -> odo.isStarted(component, feature));
assertTrue(odo.isStarted(component, feature));
ComponentInfo info = odo.getComponentInfo(project, component, projectPath, ComponentKind.DEVFILE);
assertNotNull(info.getSupportedFeatures());
assertEquals(2, info.getSupportedFeatures().size());
List<ComponentFeature.Mode> supportedFeatures = info.getSupportedFeatures();
assertTrue(supportedFeatures.contains(ComponentFeature.Mode.DEV_MODE));
assertTrue(supportedFeatures.contains(ComponentFeature.Mode.DEBUG_MODE));
List<Component> components = odo.getComponents(project);
assertNotNull(components);
assertEquals(1, components.size());
Component comp = components.get(0);
assertNotNull(comp);
ComponentFeatures features = comp.getLiveFeatures();
assertNotNull(features);
assertFalse(features.isEmpty());
assertEquals(1, features.size());
assertTrue(features.isDev());
//cleanup
odo.stop(projectPath, component, feature);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@

public class OdoCliRegistryTest extends OdoCliTest {

public void testCheckCreateRegistry() throws IOException {
String registryName = REGISTRY_PREFIX + random.nextInt();
try {
odo.createDevfileRegistry(registryName, "https://registry.devfile.io", null);
} finally {
odo.deleteDevfileRegistry(registryName);
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
odo.createDevfileRegistry(REGISTRY_NAME, REGISTRY_URL, null);
}

@Override
protected void tearDown() throws Exception {
odo.deleteDevfileRegistry(REGISTRY_NAME);
super.tearDown();
}

public void testCheckListRegistries() throws IOException {
List<DevfileRegistry> registries = odo.listDevfileRegistries();
assertFalse(registries.isEmpty());
public void testCheckCreateRegistry() throws IOException {
String registryName = REGISTRY_PREFIX + random.nextInt();
try {
odo.createDevfileRegistry(registryName, "https://registry.devfile.io", null);
} finally {
odo.deleteDevfileRegistry(registryName);
}
}

public void testCheckListRegistries() throws IOException {
List<DevfileRegistry> registries = odo.listDevfileRegistries();
assertFalse(registries.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void createService(String project, String service) throws IOException, E
createProject(project);
ServiceTemplate serviceTemplate = getServiceTemplate();
OperatorCRD crd = getOperatorCRD(serviceTemplate);
assertNotNull(crd);
createService(project, serviceTemplate, crd, service, projectPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ protected void setUp() throws Exception {
}
});
odo = getOdo().get();
if (odo.listDevfileRegistries().stream().noneMatch(c -> c.getName().equals(REGISTRY_NAME)))
odo.createDevfileRegistry(REGISTRY_NAME, REGISTRY_URL, null);
}

@Override
protected void tearDown() throws Exception {
MessagesHelper.setTestDialog(previousTestDialog);
odo.deleteDevfileRegistry(REGISTRY_NAME);
super.tearDown();
}

Expand Down Expand Up @@ -110,9 +107,7 @@ protected void cleanLocalProjectDirectory(String projectPath) throws IOException
}

protected OperatorCRD getOperatorCRD(ServiceTemplate serviceTemplate) {
OperatorCRD crd = serviceTemplate.getCRDs().stream().filter(c -> c.getName().equals(SERVICE_CRD)).findFirst().orElse(null);
assertNotNull(crd);
return crd;
return serviceTemplate.getCRDs().stream().filter(c -> c.getName().equals(SERVICE_CRD)).findFirst().orElse(null);
}

protected ServiceTemplate getServiceTemplate() throws IOException {
Expand Down
5 changes: 2 additions & 3 deletions src/it/projects/go/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.odo/env
.odo/odo-file-index.json
.odo/
main
main.exe
main.exe
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public void update(AnActionEvent e) {
super.update(e);
if (e.getPresentation().isVisible()) {
Object node = adjust(getSelected(getTree(e)));
if (!(node instanceof ComponentNode)) {
if (!(node instanceof ComponentNode componentNode)) {
return;
}
ComponentNode componentNode = (ComponentNode) node;
e.getPresentation().setEnabled(componentNode.getComponent().getInfo().isLocalPodmanPresent());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,65 @@

public class ComponentFeatures {

private final Set<ComponentFeature> features = new HashSet<>();

public ComponentFeatures(ComponentFeature feature) {
addFeature(feature);
}

public ComponentFeatures() {}

public void addFeature(ComponentFeature feature) {
features.add(feature);
}

public void removeFeature(ComponentFeature feature) {
features.remove(feature);
}

public boolean is(ComponentFeature feature) {
return features.contains(feature);
}

public boolean isDev() {
return is(ComponentFeature.DEV) || is(ComponentFeature.DEV_ON_PODMAN);
}

public boolean isDeploy() {
return is(ComponentFeature.DEPLOY);
}

public boolean isDebug() {
return features.stream().anyMatch(DebugComponentFeature.class::isInstance);
}

@Override
public String toString() {
return features.stream().map(f -> f.getMode().getLabel()).collect(Collectors.joining(", "));
}

public boolean isOnCluster() {
return isDev() || isDebug() || isDeploy();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComponentFeatures that = (ComponentFeatures) o;
return Objects.equals(features, that.features);
}

@Override
public int hashCode() {
return Objects.hash(features);
}
private final Set<ComponentFeature> features = new HashSet<>();

public ComponentFeatures(ComponentFeature feature) {
addFeature(feature);
}

public ComponentFeatures() {}

public void addFeature(ComponentFeature feature) {
features.add(feature);
}

public void removeFeature(ComponentFeature feature) {
features.remove(feature);
}

public boolean is(ComponentFeature feature) {
return features.contains(feature);
}

public boolean isDev() {
return is(ComponentFeature.DEV) || is(ComponentFeature.DEV_ON_PODMAN);
}

public boolean isDeploy() {
return is(ComponentFeature.DEPLOY);
}

public boolean isDebug() {
return features.stream().anyMatch(DebugComponentFeature.class::isInstance);
}

@Override
public String toString() {
return features.stream().map(f -> f.getMode().getLabel()).collect(Collectors.joining(", "));
}

public boolean isOnCluster() {
return isDev() || isDebug() || isDeploy();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComponentFeatures that = (ComponentFeatures) o;
return Objects.equals(features, that.features);
}

@Override
public int hashCode() {
return Objects.hash(features);
}

public boolean isEmpty() {
return features.isEmpty();
}

public int size() {
return features.size();
}
}
Loading

0 comments on commit 8232bf8

Please sign in to comment.