Skip to content

Commit

Permalink
Fix fmt (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvin N authored Nov 22, 2022
1 parent 0ed8bef commit 9a5199e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 36 deletions.
8 changes: 4 additions & 4 deletions plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def getPlugins():
if pluginsKey in os.environ and not (not os.environ[pluginsKey]):
plugins = os.environ[pluginsKey].split(",")
for plugin in plugins:
parts = plugin.split(":",1)
parts = plugin.split(":", 1)
plugin_url = ""
name = ""
if len(parts) == 2:
if parts[0] == "url":
plugin_url= parts[1]
name = "/tmp/%s.zip" % plugin_url.rsplit('/', 1)[-1]
plugin_url = parts[1]
name = "/tmp/%s.zip" % plugin_url.rsplit("/", 1)[-1]
else:
plugin_url = f"https://grafana.com/api/plugins/{parts[0]}/versions/{parts[1]}/download?os={OS}&arch={ARCH}"
name = f"/tmp/{parts[0]}_{parts[1]}.zip"
Expand All @@ -53,7 +53,7 @@ def getPlugins():


def downloadPlugin(plugin):
url,file_name = plugin
url, file_name = plugin
print(f"downloading {url}")
with urllib.request.urlopen(url) as response, open(file_name, "wb") as out_file:
shutil.copyfileobj(response, out_file)
Expand Down
136 changes: 104 additions & 32 deletions plugins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,155 @@

OS = os.environ.get("OS", platform.system().lower())


class TestGetPlugins(unittest.TestCase):
def test_get_plugin_with_version(self):
os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource:1.3.5"
os.environ[
"GRAFANA_PLUGINS"
] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource:1.3.5"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'),
('https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/1.3.5/download?os='+OS+'&arch=x86_64', '/tmp/grafana-simple-json-datasource_1.3.5.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_1.0.1.zip",
),
(
"https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/1.3.5/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-simple-json-datasource_1.3.5.zip",
),
]
self.assertEqual(actual, expected)

def test_get_plugin_without_version(self):
os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource"
os.environ[
"GRAFANA_PLUGINS"
] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'),
('https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-simple-json-datasource_latest.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_1.0.1.zip",
),
(
"https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/latest/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-simple-json-datasource_latest.zip",
),
]
self.assertEqual(actual, expected)

def test_get_plugin_without_multiple(self):
os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'),
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_1.0.1.zip",
),
]
self.assertEqual(actual, expected)

def test_get_plugin_withurl(self):
os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel"
os.environ[
"GRAFANA_PLUGINS"
] = "url:https://grafana.com/api/plugins/grafana-clock-panel"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'),
(
"https://grafana.com/api/plugins/grafana-clock-panel",
"/tmp/grafana-clock-panel.zip",
),
]

self.assertEqual(actual, expected)

def test_get_plugin_withurl_multiple(self):
os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel:1.0.1"
os.environ[
"GRAFANA_PLUGINS"
] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel:1.0.1"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'),
('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel",
"/tmp/grafana-clock-panel.zip",
),
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_1.0.1.zip",
),
]

self.assertEqual(actual, expected)

def test_get_plugin_withurl_multiple_similar(self):
os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-simple-json-datasource"
os.environ[
"GRAFANA_PLUGINS"
] = "url:https://grafana.com/api/plugins/grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-simple-json-datasource"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'),
('https://grafana.com/api/plugins/grafana-simple-json-datasource', '/tmp/grafana-simple-json-datasource.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel",
"/tmp/grafana-clock-panel.zip",
),
(
"https://grafana.com/api/plugins/grafana-simple-json-datasource",
"/tmp/grafana-simple-json-datasource.zip",
),
]

self.assertEqual(actual, expected)

def test_get_plugin_withurl_multiple_withoutversion(self):
os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel"
os.environ[
"GRAFANA_PLUGINS"
] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'),
('https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_latest.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel",
"/tmp/grafana-clock-panel.zip",
),
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_latest.zip",
),
]

self.assertEqual(actual, expected)

def test_get_plugin_withurl_multiple_withoutversion_reverse(self):
os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-clock-panel"
os.environ[
"GRAFANA_PLUGINS"
] = "grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-clock-panel"
actual = getPlugins()
expected = [
('https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_latest.zip'),
('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip')
(
"https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os="
+ OS
+ "&arch=x86_64",
"/tmp/grafana-clock-panel_latest.zip",
),
(
"https://grafana.com/api/plugins/grafana-clock-panel",
"/tmp/grafana-clock-panel.zip",
),
]

self.assertEqual(actual, expected)

self.assertEqual(actual, expected)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit 9a5199e

Please sign in to comment.