Yum { public Optional<YumPackageName> queryInstalled(TaskContext context, String packageName) { CommandResult commandResult = terminal.newCommandLine(context) .add("rpm", "-q", packageName, "--queryformat", RPM_QUERYFORMAT) .ignoreExitCode() .executeSilently(); if (commandResult.getExitCode() != 0) return Optional.empty(); YumPackageName.Builder builder = new YumPackageName.Builder(); List<Function<String, YumPackageName.Builder>> builders = PACKAGE_NAME_BUILDERS_GENERATOR.apply(builder); List<Optional<String>> lines = commandResult.mapEachLine(line -> Optional.of(line).filter(s -> !"(none)".equals(s))); if (lines.size() != builders.size()) throw new IllegalStateException(String.format( "Unexpected response from rpm, expected %d lines, got %s", builders.size(), commandResult.getOutput())); IntStream.range(0, builders.size()).forEach(i -> lines.get(i).ifPresent(builders.get(i)::apply)); return Optional.of(builder.build()); } Yum(Terminal terminal); Optional<YumPackageName> queryInstalled(TaskContext context, String packageName); InstallFixedYumCommand installFixedVersion(YumPackageName yumPackage); GenericYumCommand install(YumPackageName... packages); GenericYumCommand install(String package1, String... packages); GenericYumCommand install(List<String> packages); GenericYumCommand upgrade(YumPackageName... packages); GenericYumCommand upgrade(String package1, String... packages); GenericYumCommand upgrade(List<String> packages); GenericYumCommand remove(YumPackageName... packages); GenericYumCommand remove(String package1, String... packages); GenericYumCommand remove(List<String> packages);  }
@Test public void testQueryInstalledNevra() { terminal.expectCommand( "rpm -q docker --queryformat \"%{NAME}\\\\n%{EPOCH}\\\\n%{VERSION}\\\\n%{RELEASE}\\\\n%{ARCH}\" 2>&1", 0, "docker\n2\n1.13.1\n74.git6e3bb8e.el7.centos\nx86_64"); Optional<YumPackageName> installed = yum.queryInstalled(taskContext, "docker"); assertTrue(installed.isPresent()); assertEquals("docker", installed.get().getName()); assertEquals("2", installed.get().getEpoch().get()); assertEquals("1.13.1", installed.get().getVersion().get()); assertEquals("74.git6e3bb8e.el7.centos", installed.get().getRelease().get()); assertEquals("x86_64", installed.get().getArchitecture().get()); }
@Test public void testQueryInstalledPartial() { terminal.expectCommand( "rpm -q vespa-node-admin --queryformat \"%{NAME}\\\\n%{EPOCH}\\\\n%{VERSION}\\\\n%{RELEASE}\\\\n%{ARCH}\" 2>&1", 0, "vespa-node-admin\n(none)\n6.283.62\n1.el7\nnoarch"); Optional<YumPackageName> installed = yum.queryInstalled(taskContext, "vespa-node-admin"); assertTrue(installed.isPresent()); assertEquals("vespa-node-admin", installed.get().getName()); assertFalse(installed.get().getEpoch().isPresent()); assertEquals("6.283.62", installed.get().getVersion().get()); assertEquals("1.el7", installed.get().getRelease().get()); assertEquals("noarch", installed.get().getArchitecture().get()); }
@Test public void testQueryNotInstalled() { terminal.expectCommand( "rpm -q fake-package --queryformat \"%{NAME}\\\\n%{EPOCH}\\\\n%{VERSION}\\\\n%{RELEASE}\\\\n%{ARCH}\" 2>&1", 1, "package fake-package is not installed"); Optional<YumPackageName> installed = yum.queryInstalled(taskContext, "fake-package"); assertFalse(installed.isPresent()); }