def test_download_handler_validate_extra_arguments(): # "images.key" with compression, image_arg, overlay, ... action = DownloadHandler( "key", "/path/to/save", urlparse("http://example.com/resource.img.gz") ) action.job = Job(1234, {}, None) action.parameters = { "images": { "key": { "url": "http://example.com/resource.img.gz", "compression": "gz", "image_arg": "something", "overlay": True, } }, "namespace": "common", } action.params = action.parameters["images"]["key"] action.validate() assert action.data == { "common": { "download-action": { "key": { "file": "/path/to/save/key/resource.img", "image_arg": "something", "compression": "gz", "overlay": True, } } } } # "key" with compression, image_arg, overlay, ... action = DownloadHandler( "key", "/path/to/save", urlparse("http://example.com/resource.img.gz") ) action.job = Job(1234, {}, None) action.parameters = { "key": { "url": "http://example.com/resource.img.gz", "compression": "gz", "image_arg": "something", "overlay": True, }, "namespace": "common", } action.params = action.parameters["key"] action.validate() assert action.data == { "common": { "download-action": { "key": { "file": "/path/to/save/key/resource.img", "compression": "gz", "image_arg": "something", "overlay": True, } } } }
def test_download_handler_validate_kernel(): # "images.key" for kernel # In this case, the "kernel.type" is not taken into account action = DownloadHandler("kernel", "/path/to/save", urlparse("http://example.com/kernel")) action.job = Job(1234, {}, None) action.parameters = { "images": { "kernel": { "url": "http://example.com/kernel", "type": "zimage" } }, "namespace": "common", } action.params = action.parameters["images"]["kernel"] action.validate() assert action.data == { "common": { "download-action": { "kernel": { "file": "/path/to/save/kernel/kernel", "compression": None } } } } # "key" for kernel action = DownloadHandler("kernel", "/path/to/save", urlparse("http://example.com/kernel")) action.job = Job(1234, {}, None) action.parameters = { "kernel": { "url": "http://example.com/kernel", "type": "zimage" }, "namespace": "common", } action.params = action.parameters["kernel"] action.validate() assert action.data == { "common": { "download-action": { "kernel": { "file": "/path/to/save/kernel/kernel", "compression": None }, "type": { "kernel": "zimage" }, } } }
def test_download_handler_validate_simple(): # "images.key" without extra parameters action = DownloadHandler("key", "/path/to/save", urlparse("http://example.com/resource.img")) action.job = Job(1234, {}, None) action.parameters = { "images": { "key": { "url": "http://example.com/resource.img" } }, "namespace": "common", } action.params = action.parameters["images"]["key"] action.validate() assert action.data == { "common": { "download-action": { "key": { "file": "/path/to/save/key/resource.img", "compression": None } } } } # "key" without extra parameters action = DownloadHandler("key", "/path/to/save", urlparse("http://example.com/resource.img")) action.job = Job(1234, {}, None) action.parameters = { "key": { "url": "http://example.com/resource.img" }, "namespace": "common", } action.params = action.parameters["key"] action.validate() assert action.data == { "common": { "download-action": { "key": { "file": "/path/to/save/key/resource.img", "compression": None } } } }
def test_download_handler_errors(): # "key" downloading a directory # TODO: is this a good idea to keep this feature? action = DownloadHandler("key", "/path/to/save", urlparse("http://example.com/resource/")) action.section = "deploy" action.job = Job(1234, {}, None) action.parameters = { "key": { "url": "http://example.com/resource/" }, "namespace": "common", } action.params = action.parameters["key"] with pytest.raises(JobError) as exc: action.validate() assert str(exc.value) == "Cannot download a directory for key" # Uknown compression format action = DownloadHandler("key", "/path/to/save", urlparse("http://example.com/resource.img")) action.section = "deploy" action.job = Job(1234, {}, None) action.parameters = { "key": { "url": "http://example.com/resource.img", "compression": "something" }, "namespace": "common", } action.params = action.parameters["key"] action.validate() assert action.errors == ["Unknown 'compression' format 'something'"] # Uknown archive format action = DownloadHandler("key", "/path/to/save", urlparse("http://example.com/resource.img")) action.section = "deploy" action.job = Job(1234, {}, None) action.parameters = { "key": { "url": "http://example.com/resource.img", "archive": "cpio" }, "namespace": "common", } action.params = action.parameters["key"] action.validate() assert action.errors == ["Unknown 'archive' format 'cpio'"]