aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/yum/YumTesterTest.java
blob: aafa0fcfd72dd08fb32044c73cbdd732d8d7faf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.yum;

import com.yahoo.vespa.hosted.node.admin.component.TestTaskContext;
import com.yahoo.vespa.hosted.node.admin.task.util.process.TestTerminal;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author freva
 */
public class YumTesterTest {

    private static final String[] packages = {"pkg1", "pkg2"};
    private static final String[] repos = {"repo1", "repo2"};
    private static final String[] disablerepos = {"disablerepo1", "disablerepo2"};
    private static final YumPackageName minimalPackage = YumPackageName.fromString("pkg-1.13.1-0.el7");
    private static final YumPackageName fullPackage = YumPackageName.fromString("2:pkg-1.13.1-0.el7.x86_64");

    private final TestTerminal terminal = new TestTerminal();
    private final YumTester yum = new YumTester(terminal);
    private final TestTaskContext context = new TestTaskContext();

    @Test
    void generic_yum_methods() {
        assertYumMethod(yum -> yum.expectInstall(packages).withDisableRepo(disablerepos).withEnableRepo(repos),
                yum -> yum.install(List.of(packages)).disableRepo(disablerepos).enableRepo(repos).converge(context));

        assertYumMethod(yum -> yum.expectUpdate(packages).withDisableRepo(disablerepos).withEnableRepo(repos),
                yum -> yum.upgrade(List.of(packages)).disableRepo(disablerepos).enableRepo(repos).converge(context));

        assertYumMethod(yum -> yum.expectRemove(packages).withDisableRepo(disablerepos).withEnableRepo(repos),
                yum -> yum.remove(List.of(packages)).disableRepo(disablerepos).enableRepo(repos).converge(context));

        assertYumMethod(yum -> yum.expectInstallFixedVersion(minimalPackage.toName()).withDisableRepo(disablerepos).withEnableRepo(repos),
                yum -> yum.installFixedVersion(minimalPackage).disableRepo(disablerepos).enableRepo(repos).converge(context));

        // versionlock always returns success
        assertYumMethodAlwaysSuccess(yum -> yum.expectDeleteVersionLock(minimalPackage.toName()),
                yum -> yum.deleteVersionLock(minimalPackage).converge(context));

    }

    @Test
    void disable_other_repos() {
        assertYumMethod(yum -> yum.expectInstall(packages).withDisableRepo("*").withEnableRepo(repos),
                yum -> yum.install(List.of(packages)).disableRepo("*").enableRepo(repos).converge(context));
    }

    @Test
    void expect_query_installed() {
        yum.expectQueryInstalled(packages[0]).andReturn(fullPackage);
        assertEquals(Optional.of(fullPackage), yum.queryInstalled(context, packages[0]));
        terminal.verifyAllCommandsExecuted();
    }

    private void assertYumMethod(Function<YumTester, YumTester.GenericYumCommandExpectation> yumTesterExpectationFunction,
                                 Function<Yum, Boolean> yumFunction) {
        List.of(true, false).forEach(wantedReturnValue -> {
            yumTesterExpectationFunction.apply(yum).andReturn(wantedReturnValue);
            assertEquals(wantedReturnValue, yumFunction.apply(yum));
            terminal.verifyAllCommandsExecuted();
        });
    }

    private void assertYumMethodAlwaysSuccess(Function<YumTester, YumTester.GenericYumCommandExpectation> yumTesterExpectationFunction,
                                              Function<Yum, Boolean> yumFunction) {
        List.of(true, false).forEach(wantedReturnValue -> {
            yumTesterExpectationFunction.apply(yum).andReturn(wantedReturnValue);
            assertEquals(true, yumFunction.apply(yum));
            terminal.verifyAllCommandsExecuted();
        });
    }

}