diff --git a/fetch-latest-releases.lua b/fetch-latest-releases.lua index ad019c5..43f7572 100755 --- a/fetch-latest-releases.lua +++ b/fetch-latest-releases.lua @@ -8,14 +8,16 @@ local cqueues = require("cqueues") local yaml = require("lyaml") local lfs = require("lfs") -local mirror = os.getenv("MIRROR") or "https://cz.alpinelinux.org/alpine" +local m = {} +m.mirror = os.getenv("MIRROR") or "https://cz.alpinelinux.org/alpine" -function fatal(...) - errormsg(...) + +function m.fatal(...) + m.errormsg(...) os.exit(1) end -function fetch(url) +function m.fetch(url) local headers, stream = request.new_from_uri(url):go() if not headers then fatal("Error: %s: %s", url, stream) @@ -24,13 +26,13 @@ function fetch(url) return headers:get(":status"), body end -function errormsg(...) +function m.errormsg(...) local msg = string.format(...) io.stderr:write(string.format("%s\n", msg)) return nil, msg end -function fetch_file(url, outfile) +function m.fetch_file(url, outfile) local headers, stream = request.new_from_uri(url):go() if not headers then fatal("Error: %s: %s", url, stream) @@ -52,7 +54,7 @@ function fetch_file(url, outfile) return os.rename(partfile, outfile) end -function mkdockerfile(dir, rootfsfile) +function m.mkdockerfile(dir, rootfsfile) local filename = string.format("%s/Dockerfile", dir) local f, err = io.open(filename, "w") if not f then @@ -62,35 +64,41 @@ function mkdockerfile(dir, rootfsfile) f:close() end -function get_minirootfs(images, destdir) +function m.minirootfs_image(images) for _,img in pairs(images) do if img.flavor == "alpine-minirootfs" then - if destdir then - local url = string.format("%s/%s/releases/%s/%s", - mirror, img.branch, img.arch, img.file) - local archdir = string.format("%s/%s", destdir, img.arch) - local ok, errmsg = lfs.mkdir(archdir) - fetch_file(url, string.format("%s/%s", archdir, img.file)) - mkdockerfile(archdir, img.file) - print(img.file) - end - return { version=img.version, file=img.file, sha512=img.sha512 } + return img end end + return nil +end + +function m.get_minirootfs(images, destdir) + local img = m.minirootfs_image(images) + if destdir then + local url = string.format("%s/%s/releases/%s/%s", + m.mirror, img.branch, img.arch, img.file) + local archdir = string.format("%s/%s", destdir, img.arch) + local ok, errmsg = lfs.mkdir(archdir) + m.fetch_file(url, string.format("%s/%s", archdir, img.file)) + m.mkdockerfile(archdir, img.file) + print(img.file) + end + return { version=img.version, file=img.file, sha512=img.sha512 } end -- get array of minirootsfs releases -- -function get_releases(branch, destdir) +function m.get_releases(branch, destdir) local arches = { "aarch64", "armhf", "armv7", "ppc64le" , "s390x", "x86", "x86_64" } local t = {} local loop = cqueues.new() for _, arch in pairs(arches) do loop:wrap(function() local url = string.format("%s/%s/releases/%s/latest-releases.yaml", - mirror, branch, arch) - local status, body = fetch(url) + m.mirror, branch, arch) + local status, body = m.fetch(url) if status == "200" then - t[arch] = get_minirootfs((yaml.load(body)), destdir) + t[arch] = m.get_minirootfs((yaml.load(body)), destdir) end end) end @@ -100,9 +108,7 @@ end -- return functions as module for unit testing if not string.match(arg[0], "fetch%-latest%-releases") then - return { - fatal = fatal, - } + return m end local branch = arg[1] or "edge" @@ -114,7 +120,7 @@ local version local releases = get_releases(branch, destdir) if next(releases) == nil then - fatal("No releases found on %s/%s/releases", mirror, branch) + fatal("No releases found on %s/%s/releases", m.mirror, branch) end local f = io.open(string.format("%s/checksums.sha512", destdir), "w") diff --git a/tests/fetch-latest-releases_spec.lua b/tests/fetch-latest-releases_spec.lua index 0842b47..f4aab7b 100644 --- a/tests/fetch-latest-releases_spec.lua +++ b/tests/fetch-latest-releases_spec.lua @@ -1,7 +1,61 @@ describe("test fetch-latest-releases functions", function() local f + + local function fetch(url) + data = {} + data["/v3.13/releases/aarch64/latest-releases.yaml"] = [[--- +- + title: "Mini root filesystem" + branch: v3.13 + arch: aarch64 + version: 3.13.4 + flavor: alpine-minirootfs + file: alpine-minirootfs-3.13.4-aarch64.tar.gz + iso: alpine-minirootfs-3.13.4-aarch64.tar.gz + date: 2021-04-14 + time: 10:28:29 + size: 2620916 + sha256: 5b359d72aa693d38945c0eb22a0f7a8071071af1914d04f59267230c9a2fe2b5 + sha512: 32d8832b81848566a5aecfbb21ea507e439bca123b611e586d27b02cadb94e5169e82e56f76c12c32d037aa4c0e8bec9bc4d98e5dd321d94876807d09e4c7f9c +]] + data["/v3.13/releases/x86/latest-releases.yaml"] = [[--- +- + title: "Mini root filesystem" + branch: v3.13 + arch: x86 + version: 3.13.5 + flavor: alpine-minirootfs + file: alpine-minirootfs-3.13.5-x86.tar.gz + iso: alpine-minirootfs-3.13.5-x86.tar.gz + date: 2021-04-14 + time: 10:25:51 + size: 2742455 + sha256: 7144c4b209ba7cf2e7c29eefefd1194a1150b5f34b6854104cf4a3d20b7e3053 + sha512: 843077403fc3ea031c8c4e2907d707b235890cc6ba7ea6489894ba5559cff04d988b3bad0cdbddfc4ebd5a54fed42a582823ea3f8b4bc2d22cf49c97aca9f70f +- + title: "Virtual" + branch: v3.13 + arch: x86 + version: 3.13.5 + flavor: alpine-virt + file: alpine-virt-3.13.5-x86.iso + iso: alpine-virt-3.13.5-x86.iso + date: 2021-04-14 + time: 10:30:25 + size: 38797312 + sha256: cf7ca3ae1459a2b8e973decd74ae939e70d09e14fdeb8edc6c064d7d27e4ea83 + sha512: 034954ae76d920067f56fc9275512d9b611887f9806a90040cd85ba6346a4503a9afa16ef8f6a2f950b637371be06ca5a280a6f7632f9d2ba658ea69746afd18 +]] + if data[url] then + return "200", data[url] + end + return "404", "" + end + setup(function() f = require("fetch-latest-releases") + f.fetch = fetch + f.mirror = "" end) teardown(function() @@ -15,4 +69,33 @@ describe("test fetch-latest-releases functions", function() assert.stub(os.exit).was_called_with(1) end) end) + + describe("test minirootfs_image", function() + it("should return flavor=alpine-minirootfs", function() + local img = f.minirootfs_image({ { flavor="alpine-rpi"}, { flavor="alpine-minirootfs"}}) + assert.are.same({flavor="alpine-minirootfs"}, img) + end) + end) + + describe("test get_minirootfs", function() + it("should call fetch_file", function() + res = f.get_minirootfs({ + { flavor="alpine-rpi", version="3.13.5"}, + { flavor="alpine-minirootfs", version="3.13.5"}, + }) + assert.are.same("3.13.5", res.version) + end) + end) + + describe("test get_releases", function() + it("should return 3.13.4 for aarch64", function() + rels = f.get_releases("v3.13") + assert.are.same("3.13.4", rels.aarch64.version) + end) + it("should return 3.13.5 for x86", function() + rels = f.get_releases("v3.13") + assert.are.same("3.13.5", rels.x86.version) + end) + end) + end)