summaryrefslogtreecommitdiff
path: root/tests/integration/api_plugin_test.py
blob: 38f9d12dada45d01e8c55fee737272ca320ae3b6 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os

import docker
import pytest

from .base import BaseAPIIntegrationTest
from ..helpers import requires_api_version

SSHFS = 'vieux/sshfs:latest'


@requires_api_version('1.25')
class PluginTest(BaseAPIIntegrationTest):
    @classmethod
    def teardown_class(cls):
        client = cls.get_client_instance()
        try:
            client.remove_plugin(SSHFS, force=True)
        except docker.errors.APIError:
            pass

    def teardown_method(self, method):
        client = self.get_client_instance()
        try:
            client.disable_plugin(SSHFS)
        except docker.errors.APIError:
            pass

        for p in self.tmp_plugins:
            try:
                client.remove_plugin(p, force=True)
            except docker.errors.APIError:
                pass

        client.close()

    def ensure_plugin_installed(self, plugin_name):
        try:
            return self.client.inspect_plugin(plugin_name)
        except docker.errors.NotFound:
            prv = self.client.plugin_privileges(plugin_name)
            for d in self.client.pull_plugin(plugin_name, prv):
                pass
        return self.client.inspect_plugin(plugin_name)

    def test_enable_plugin(self):
        pl_data = self.ensure_plugin_installed(SSHFS)
        assert pl_data['Enabled'] is False
        assert self.client.enable_plugin(SSHFS)
        pl_data = self.client.inspect_plugin(SSHFS)
        assert pl_data['Enabled'] is True
        with pytest.raises(docker.errors.APIError):
            self.client.enable_plugin(SSHFS)

    def test_disable_plugin(self):
        pl_data = self.ensure_plugin_installed(SSHFS)
        assert pl_data['Enabled'] is False
        assert self.client.enable_plugin(SSHFS)
        pl_data = self.client.inspect_plugin(SSHFS)
        assert pl_data['Enabled'] is True
        self.client.disable_plugin(SSHFS)
        pl_data = self.client.inspect_plugin(SSHFS)
        assert pl_data['Enabled'] is False
        with pytest.raises(docker.errors.APIError):
            self.client.disable_plugin(SSHFS)

    def test_inspect_plugin(self):
        self.ensure_plugin_installed(SSHFS)
        data = self.client.inspect_plugin(SSHFS)
        assert 'Config' in data
        assert 'Name' in data
        assert data['Name'] == SSHFS

    def test_plugin_privileges(self):
        prv = self.client.plugin_privileges(SSHFS)
        assert isinstance(prv, list)
        for item in prv:
            assert 'Name' in item
            assert 'Value' in item
            assert 'Description' in item

    def test_list_plugins(self):
        self.ensure_plugin_installed(SSHFS)
        data = self.client.plugins()
        assert len(data) > 0
        plugin = [p for p in data if p['Name'] == SSHFS][0]
        assert 'Config' in plugin

    def test_configure_plugin(self):
        pl_data = self.ensure_plugin_installed(SSHFS)
        assert pl_data['Enabled'] is False
        self.client.configure_plugin(SSHFS, {
            'DEBUG': '1'
        })
        pl_data = self.client.inspect_plugin(SSHFS)
        assert 'Env' in pl_data['Settings']
        assert 'DEBUG=1' in pl_data['Settings']['Env']

        self.client.configure_plugin(SSHFS, ['DEBUG=0'])
        pl_data = self.client.inspect_plugin(SSHFS)
        assert 'DEBUG=0' in pl_data['Settings']['Env']

    def test_remove_plugin(self):
        pl_data = self.ensure_plugin_installed(SSHFS)
        assert pl_data['Enabled'] is False
        assert self.client.remove_plugin(SSHFS) is True

    def test_force_remove_plugin(self):
        self.ensure_plugin_installed(SSHFS)
        self.client.enable_plugin(SSHFS)
        assert self.client.inspect_plugin(SSHFS)['Enabled'] is True
        assert self.client.remove_plugin(SSHFS, force=True) is True

    def test_install_plugin(self):
        try:
            self.client.remove_plugin(SSHFS, force=True)
        except docker.errors.APIError:
            pass

        prv = self.client.plugin_privileges(SSHFS)
        logs = [d for d in self.client.pull_plugin(SSHFS, prv)]
        assert filter(lambda x: x['status'] == 'Download complete', logs)
        assert self.client.inspect_plugin(SSHFS)
        assert self.client.enable_plugin(SSHFS)

    @requires_api_version('1.26')
    def test_upgrade_plugin(self):
        pl_data = self.ensure_plugin_installed(SSHFS)
        assert pl_data['Enabled'] is False
        prv = self.client.plugin_privileges(SSHFS)
        logs = [d for d in self.client.upgrade_plugin(SSHFS, SSHFS, prv)]
        assert filter(lambda x: x['status'] == 'Download complete', logs)
        assert self.client.inspect_plugin(SSHFS)
        assert self.client.enable_plugin(SSHFS)

    def test_create_plugin(self):
        plugin_data_dir = os.path.join(
            os.path.dirname(__file__), os.path.join('testdata', 'dummy-plugin')
        )
        assert self.client.create_plugin(
            'docker-sdk-py/dummy', plugin_data_dir
        )
        self.tmp_plugins.append('docker-sdk-py/dummy')
        data = self.client.inspect_plugin('docker-sdk-py/dummy')
        assert data['Config']['Entrypoint'] == ['/dummy']