From d03be2f4d75065076c4c117f279dcef32967b4fb Mon Sep 17 00:00:00 2001 From: Valerij Fredriksen Date: Mon, 20 Jan 2020 00:04:36 +0100 Subject: Produce smaller diff in FilterTableLineEditor --- .../maintenance/acl/FilterTableLineEditor.java | 36 ++++++++++++++++------ .../maintenance/acl/FilterTableLineEditorTest.java | 35 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 9 deletions(-) (limited to 'node-admin') diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditor.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditor.java index a9954200f8a..2d77cd32632 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditor.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditor.java @@ -6,7 +6,6 @@ import com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit; import com.yahoo.vespa.hosted.node.admin.task.util.file.LineEditor; import com.yahoo.vespa.hosted.node.admin.task.util.network.IPVersion; -import java.util.LinkedList; import java.util.List; /** @@ -16,10 +15,11 @@ import java.util.List; */ class FilterTableLineEditor implements LineEditor { - private final LinkedList wantedRules; + private final List wantedRules; + private int position = 0; private FilterTableLineEditor(List wantedRules) { - this.wantedRules = new LinkedList<>(wantedRules); + this.wantedRules = List.copyOf(wantedRules); } static FilterTableLineEditor from(Acl acl, IPVersion ipVersion) { @@ -29,15 +29,33 @@ class FilterTableLineEditor implements LineEditor { @Override public LineEdit edit(String line) { - // We have already added all the lines we wanted, remove the remainer - if (wantedRules.isEmpty()) return LineEdit.remove(); - - String wantedRule = wantedRules.pop(); - return wantedRule.equals(line) ? LineEdit.none() : LineEdit.replaceWith(wantedRule); + int index = indexOf(wantedRules, line, position); + // Unwanted rule, remove + if (index < 0) return LineEdit.remove(); + + // Wanted rule at the expected position, no diff + if (index == position) { + position++; + return LineEdit.none(); + } + + // Insert the rules between position and index before index + List toInsert = wantedRules.subList(position, index); + position = ++index; + return LineEdit.insertBefore(toInsert); } @Override public List onComplete() { - return this.wantedRules; + return position == wantedRules.size() ? List.of() : wantedRules.subList(position, wantedRules.size()); + } + + private static int indexOf(List list, T value, int startPos) { + for (int i = startPos; i < list.size(); i++) { + if (value.equals(list.get(i))) + return i; + } + + return -1; } } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditorTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditorTest.java index 192422492c8..495c1318b71 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditorTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditorTest.java @@ -6,6 +6,7 @@ import com.yahoo.vespa.hosted.node.admin.task.util.file.Editor; import com.yahoo.vespa.hosted.node.admin.task.util.network.IPVersion; import org.junit.Test; +import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; @@ -37,6 +38,25 @@ public class FilterTableLineEditorTest { "-A INPUT -j REJECT --reject-with icmp6-port-unreachable"); } + @Test + public void produces_minimal_diff_simple() { + assertFilterTableDiff(List.of(2, 5, 3, 6, 1, 4), List.of(2, 5, 6, 1, 4), + "Patching file table:\n" + + "--A INPUT -s 2001::3/128 -j ACCEPT\n"); + } + + @Test + public void produces_minimal_diff_complex() { + assertFilterTableDiff(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), List.of(5, 11, 6, 3, 10, 4, 8, 12), + "Patching file table:\n" + + "--A INPUT -s 2001::1/128 -j ACCEPT\n" + + "--A INPUT -s 2001::2/128 -j ACCEPT\n" + + "+-A INPUT -s 2001::11/128 -j ACCEPT\n" + + "+-A INPUT -s 2001::12/128 -j ACCEPT\n" + + "--A INPUT -s 2001::7/128 -j ACCEPT\n" + + "--A INPUT -s 2001::9/128 -j ACCEPT\n"); + } + private static void assertFilterTableLineEditorResult( Acl acl, IPVersion ipVersion, String currentFilterTable, String expectedRestoreFileContent) { FilterTableLineEditor filterLineEditor = FilterTableLineEditor.from(acl, ipVersion); @@ -47,4 +67,19 @@ public class FilterTableLineEditorTest { filterLineEditor); editor.edit(m -> {}); } + + private static void assertFilterTableDiff(List currentIpSuffix, List wantedIpSuffix, String diff) { + Acl.Builder currentAcl = new Acl.Builder(); + currentIpSuffix.forEach(i -> currentAcl.withTrustedNode("host" + i, "2001::" + i)); + List currentTable = new ArrayList<>(); + + Acl.Builder wantedAcl = new Acl.Builder(); + wantedIpSuffix.forEach(i -> wantedAcl.withTrustedNode("host" + i, "2001::" + i)); + + new Editor("table", List::of, currentTable::addAll, FilterTableLineEditor.from(currentAcl.build(), IPVersion.IPv6)) + .edit(log -> {}); + + new Editor("table", () -> currentTable, result -> {}, FilterTableLineEditor.from(wantedAcl.build(), IPVersion.IPv6)) + .edit(log -> assertEquals(diff, log)); + } } \ No newline at end of file -- cgit v1.2.3