File: test_identities.py

package info (click to toggle)
lavacli 0.9.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 604 kB
  • sloc: python: 9,619; sh: 105; makefile: 31
file content (213 lines) | stat: -rw-r--r-- 7,786 bytes parent folder | download | duplicates (3)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
# vim: set ts=4

# Copyright 2018 RĂ©mi Duraffort
# This file is part of lavacli.
#
# lavacli is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# lavacli is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with lavacli.  If not, see <http://www.gnu.org/licenses/>

import sys
import xmlrpc.client
import yaml

from lavacli import main


def test_identities_add(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "lavacli",
            "identities",
            "add",
            "v.l.o",
            "--uri",
            "https://validation.linaro.org/RPC2",
        ],
    )
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == ""  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert set(data.keys()) == set(["default", "v.l.o"])  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec
        assert data["v.l.o"] == {"uri": "https://validation.linaro.org/RPC2"}  # nosec


def test_identities_add_1(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "lavacli",
            "identities",
            "add",
            "v.l.o",
            "--uri",
            "https://validation.linaro.org/RPC2",
            "--proxy",
            "http://proxy:3128",
            "--username",
            "admin",
            "--token",
            "12345",
        ],
    )
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == ""  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert set(data.keys()) == set(["default", "v.l.o"])  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec
        assert data["v.l.o"] == {  # nosec
            "uri": "https://validation.linaro.org/RPC2",
            "proxy": "http://proxy:3128",
            "username": "admin",
            "token": "12345",
        }


def test_identities_add_empty_config(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "lavacli",
            "identities",
            "add",
            "v.l.o",
            "--uri",
            "https://validation.linaro.org/RPC2",
        ],
    )
    (tmpdir / "lavacli.yaml").remove()
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == ""  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert set(data.keys()) == set(["v.l.o"])  # nosec
        assert data["v.l.o"] == {"uri": "https://validation.linaro.org/RPC2"}  # nosec


def test_identities_delete(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "delete", "v.l.o"])
    with (tmpdir / "lavacli.yaml").open("w") as f_conf:
        f_conf.write(
            "default:\n  uri: https://lava.example.com/RPC2\nv.l.o:\n  uri: https://validation.linaro.org/RPC"
        )
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == ""  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert list(data.keys()) == ["default"]  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec


def test_identities_delete_empty_config(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "delete", "v.l.o"])
    (tmpdir / "lavacli.yaml").remove()
    assert main() == 1  # nosec
    assert capsys.readouterr()[0] == "Unknown identity 'v.l.o'\n"  # nosec
    assert not (tmpdir / "lavacli.yaml").exists()  # nosec


def test_identities_delete_missing_key(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "delete", "v.l.o"])
    assert main() == 1  # nosec
    assert capsys.readouterr()[0] == "Unknown identity 'v.l.o'\n"  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert list(data.keys()) == ["default"]  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec


def test_identities_list(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "list"])
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == "Identities:\n* default\n"  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert list(data.keys()) == ["default"]  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec


def test_identities_list_1(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "list"])
    with (tmpdir / "lavacli.yaml").open("w") as f_conf:
        f_conf.write(
            "default:\n  uri: https://lava.example.com/RPC2\nv.l.o:\n  uri: https://validation.linaro.org/RPC2"
        )
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == "Identities:\n* default\n* v.l.o\n"  # nosec

    with (tmpdir / "lavacli.yaml").open() as f_in:
        data = yaml.safe_load(f_in)
        assert set(data.keys()) == set(["default", "v.l.o"])  # nosec
        assert data["default"] == {"uri": "https://lava.example.com/RPC2"}  # nosec
        assert data["v.l.o"] == {"uri": "https://validation.linaro.org/RPC2"}  # nosec


def test_identities_list_2(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "list"])
    (tmpdir / "lavacli.yaml").remove()
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == "Identities:\n"  # nosec
    assert not (tmpdir / "lavacli.yaml").exists()  # nosec


def test_identities_show(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "show", "default"])
    assert main() == 0  # nosec
    assert capsys.readouterr()[0] == "uri: https://lava.example.com/RPC2\n"  # nosec


def test_identities_show_missing(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "show", "missing"])
    assert main() == 1  # nosec
    assert capsys.readouterr()[0] == "Unknown identity 'missing'\n"  # nosec


def test_identities_show_no_config(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "show", "default"])
    (tmpdir / "lavacli.yaml").remove()
    assert main() == 1  # nosec
    assert capsys.readouterr()[0] == "Unknown identity 'default'\n"  # nosec


def test_identities_show_invalid_config(setup, monkeypatch, capsys, tmpdir):
    version = "2018.4"
    monkeypatch.setattr(sys, "argv", ["lavacli", "identities", "show", "default"])
    with (tmpdir / "lavacli.yaml").open("w") as f_conf:
        f_conf.write("hello")
    assert main() == 1  # nosec
    assert capsys.readouterr()[0] == "Invalid configuration file\n"  # nosec