gitea/modules/packages/conda/metadata_test.go
Jason Song 33cc5837a6
Support compression for Actions logs (#31761)
Support compression for Actions logs to save storage space and
bandwidth. Inspired by
https://github.com/go-gitea/gitea/issues/24256#issuecomment-1521153015

The biggest challenge is that the compression format should support
[seekable](https://github.com/facebook/zstd/blob/dev/contrib/seekable_format/zstd_seekable_compression_format.md).
So when users are viewing a part of the log lines, Gitea doesn't need to
download the whole compressed file and decompress it.

That means gzip cannot help here. And I did research, there aren't too
many choices, like bgzip and xz, but I think zstd is the most popular
one. It has an implementation in Golang with
[zstd](https://github.com/klauspost/compress/tree/master/zstd) and
[zstd-seekable-format-go](https://github.com/SaveTheRbtz/zstd-seekable-format-go),
and what is better is that it has good compatibility: a seekable format
zstd file can be read by a regular zstd reader.

This PR introduces a new package `zstd` to combine and wrap the two
packages, to provide a unified and easy-to-use API.

And a new setting `LOG_COMPRESSION` is added to the config, although I
don't see any reason why not to use compression, I think's it's a good
idea to keep the default with `none` to be consistent with old versions.

`LOG_COMPRESSION` takes effect for only new log files, it adds `.zst` as
an extension to the file name, so Gitea can determine if it needs
decompression according to the file name when reading. Old files will
keep the format since it's not worth converting them, as they will be
cleared after #31735.

<img width="541" alt="image"
src="https://github.com/user-attachments/assets/e9598764-a4e0-4b68-8c2b-f769265183c9">
2024-08-09 10:10:30 +08:00

152 lines
4.1 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package conda
import (
"archive/tar"
"archive/zip"
"bytes"
"io"
"testing"
"code.gitea.io/gitea/modules/zstd"
"github.com/dsnet/compress/bzip2"
"github.com/stretchr/testify/assert"
)
const (
packageName = "gitea"
packageVersion = "1.0.1"
description = "Package Description"
projectURL = "https://gitea.com"
repositoryURL = "https://gitea.com/gitea/gitea"
documentationURL = "https://docs.gitea.com"
)
func TestParsePackage(t *testing.T) {
createArchive := func(files map[string][]byte) *bytes.Buffer {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
for filename, content := range files {
hdr := &tar.Header{
Name: filename,
Mode: 0o600,
Size: int64(len(content)),
}
tw.WriteHeader(hdr)
tw.Write(content)
}
tw.Close()
return &buf
}
t.Run("MissingIndexFile", func(t *testing.T) {
buf := createArchive(map[string][]byte{"dummy.txt": {}})
p, err := parsePackageTar(buf)
assert.Nil(t, p)
assert.ErrorIs(t, err, ErrInvalidStructure)
})
t.Run("MissingAboutFile", func(t *testing.T) {
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"name","version":"1.0"}`)})
p, err := parsePackageTar(buf)
assert.NotNil(t, p)
assert.NoError(t, err)
assert.Equal(t, "name", p.Name)
assert.Equal(t, "1.0", p.Version)
assert.Empty(t, p.VersionMetadata.ProjectURL)
})
t.Run("InvalidName", func(t *testing.T) {
for _, name := range []string{"", "name!", "nAMe"} {
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"` + name + `","version":"1.0"}`)})
p, err := parsePackageTar(buf)
assert.Nil(t, p)
assert.ErrorIs(t, err, ErrInvalidName)
}
})
t.Run("InvalidVersion", func(t *testing.T) {
for _, version := range []string{"", "1.0-2"} {
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"name","version":"` + version + `"}`)})
p, err := parsePackageTar(buf)
assert.Nil(t, p)
assert.ErrorIs(t, err, ErrInvalidVersion)
}
})
t.Run("Valid", func(t *testing.T) {
buf := createArchive(map[string][]byte{
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `","subdir":"linux-64"}`),
"info/about.json": []byte(`{"description":"` + description + `","dev_url":"` + repositoryURL + `","doc_url":"` + documentationURL + `","home":"` + projectURL + `"}`),
})
p, err := parsePackageTar(buf)
assert.NotNil(t, p)
assert.NoError(t, err)
assert.Equal(t, packageName, p.Name)
assert.Equal(t, packageVersion, p.Version)
assert.Equal(t, "linux-64", p.Subdir)
assert.Equal(t, description, p.VersionMetadata.Description)
assert.Equal(t, projectURL, p.VersionMetadata.ProjectURL)
assert.Equal(t, repositoryURL, p.VersionMetadata.RepositoryURL)
assert.Equal(t, documentationURL, p.VersionMetadata.DocumentationURL)
})
t.Run(".tar.bz2", func(t *testing.T) {
tarArchive := createArchive(map[string][]byte{
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `"}`),
})
var buf bytes.Buffer
bw, _ := bzip2.NewWriter(&buf, nil)
io.Copy(bw, tarArchive)
bw.Close()
br := bytes.NewReader(buf.Bytes())
p, err := ParsePackageBZ2(br)
assert.NotNil(t, p)
assert.NoError(t, err)
assert.Equal(t, packageName, p.Name)
assert.Equal(t, packageVersion, p.Version)
assert.False(t, p.FileMetadata.IsCondaPackage)
})
t.Run(".conda", func(t *testing.T) {
tarArchive := createArchive(map[string][]byte{
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `"}`),
})
var infoBuf bytes.Buffer
zsw, _ := zstd.NewWriter(&infoBuf)
io.Copy(zsw, tarArchive)
zsw.Close()
var buf bytes.Buffer
zpw := zip.NewWriter(&buf)
w, _ := zpw.Create("info-x.tar.zst")
w.Write(infoBuf.Bytes())
zpw.Close()
br := bytes.NewReader(buf.Bytes())
p, err := ParsePackageConda(br, int64(br.Len()))
assert.NotNil(t, p)
assert.NoError(t, err)
assert.Equal(t, packageName, p.Name)
assert.Equal(t, packageVersion, p.Version)
assert.True(t, p.FileMetadata.IsCondaPackage)
})
}