From 9a5199e843ddfe2a4337822a92ec05029adb5bee Mon Sep 17 00:00:00 2001 From: Edvin N Date: Tue, 22 Nov 2022 14:25:48 +0100 Subject: [PATCH] Fix fmt (#17) --- plugins.py | 8 +-- plugins_test.py | 136 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 108 insertions(+), 36 deletions(-) diff --git a/plugins.py b/plugins.py index 6f3a159..5c4fadd 100755 --- a/plugins.py +++ b/plugins.py @@ -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" @@ -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) diff --git a/plugins_test.py b/plugins_test.py index c3ed94f..76ca9c1 100644 --- a/plugins_test.py +++ b/plugins_test.py @@ -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()