2023-04-26 11:51:11 +00:00
|
|
|
package artifact_test
|
2020-04-04 17:16:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-06-05 17:36:34 +00:00
|
|
|
"os"
|
2020-04-04 17:16:36 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
"gitlab.com/slon/shad-go/distbuild/pkg/artifact"
|
|
|
|
"gitlab.com/slon/shad-go/distbuild/pkg/build"
|
2020-04-04 17:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestArtifactTransfer(t *testing.T) {
|
|
|
|
remoteCache := newTestCache(t)
|
|
|
|
localCache := newTestCache(t)
|
|
|
|
|
|
|
|
id := build.ID{0x01}
|
|
|
|
|
|
|
|
dir, commit, _, err := remoteCache.Create(id)
|
|
|
|
require.NoError(t, err)
|
2024-06-05 17:36:34 +00:00
|
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "a.txt"), []byte("foobar"), 0777))
|
2020-04-04 17:16:36 +00:00
|
|
|
require.NoError(t, commit())
|
|
|
|
|
|
|
|
l := zaptest.NewLogger(t)
|
|
|
|
|
|
|
|
h := artifact.NewHandler(l, remoteCache.Cache)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
h.Register(mux)
|
|
|
|
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
require.NoError(t, artifact.Download(ctx, server.URL, localCache.Cache, id))
|
|
|
|
|
|
|
|
dir, unlock, err := localCache.Get(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer unlock()
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
content, err := os.ReadFile(filepath.Join(dir, "a.txt"))
|
2020-04-04 17:16:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("foobar"), content)
|
|
|
|
|
|
|
|
err = artifact.Download(ctx, server.URL, localCache.Cache, build.ID{0x02})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|