Path: blob/main/components/content-service/pkg/initializer/download_test.go
2499 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package initializer56import (7"bytes"8"context"9"io"10"net/http"11"os"12"testing"1314"github.com/gitpod-io/gitpod/content-service/api"15"github.com/opencontainers/go-digest"16)1718// RoundTripFunc .19type RoundTripFunc func(req *http.Request) *http.Response2021// RoundTrip .22func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {23return f(req), nil24}2526func TestFileDownloadInitializer(t *testing.T) {27const defaultContent = "hello world"2829type serverSideFile struct {30Path string31Content string32}33tests := []struct {34Name string35Files []fileInfo36ServerSide []serverSideFile37ExpectedError string38}{39{40Name: "happy path",41Files: []fileInfo{42{43URL: "/file1",44Path: "/level/file1",45Digest: digest.FromString(defaultContent),46},47// duplication is intentional48{49URL: "/file1",50Path: "/level/file1",51Digest: digest.FromString(defaultContent),52},53{54URL: "/file2",55Path: "/level/file2",56Digest: digest.FromString(defaultContent),57},58},59ServerSide: []serverSideFile{60{Path: "/file1", Content: defaultContent},61{Path: "/file2", Content: defaultContent},62},63},64{65Name: "digest mismatch",66Files: []fileInfo{67{68URL: "/file1",69Path: "/level/file1",70Digest: digest.FromString(defaultContent + "foobar"),71},72},73ServerSide: []serverSideFile{74{Path: "/file1", Content: defaultContent},75},76ExpectedError: "digest mismatch",77},78{79Name: "file not found",80Files: []fileInfo{81{82URL: "/file1",83Path: "/level/file1",84Digest: digest.FromString(defaultContent + "foobar"),85},86},87ExpectedError: "non-OK download response: Not Found",88},89}9091for _, test := range tests {92t.Run(test.Name, func(t *testing.T) {93tmpdir, err := os.MkdirTemp("", "TestFileDownloadInitializer*")94if err != nil {95t.Fatal("cannot create tempdir", err)96}97defer os.RemoveAll(tmpdir)9899client := &http.Client{100Transport: RoundTripFunc(func(req *http.Request) *http.Response {101for _, f := range test.ServerSide {102if f.Path != req.URL.Path {103continue104}105106return &http.Response{107StatusCode: http.StatusOK,108Body: io.NopCloser(bytes.NewReader([]byte(f.Content))),109Header: make(http.Header),110}111}112113return &http.Response{114Status: http.StatusText(http.StatusNotFound),115StatusCode: http.StatusNotFound,116Header: make(http.Header),117}118}),119}120121req := &api.FileDownloadInitializer{}122for _, f := range test.Files {123req.Files = append(req.Files, &api.FileDownloadInitializer_FileInfo{124Url: "http://foobar" + f.URL,125FilePath: f.Path,126Digest: string(f.Digest),127})128}129130initializer, err := newFileDownloadInitializer(tmpdir, req)131if err != nil {132t.Fatal(err)133}134initializer.HTTPClient = client135initializer.RetryTimeout = 0136137src, _, err := initializer.Run(context.Background(), nil)138if err == nil && src != api.WorkspaceInitFromOther {139t.Errorf("initializer returned wrong content init source. expected %v, got %v", api.WorkspaceInitFromOther, src)140}141if err != nil && err.Error() != test.ExpectedError {142t.Fatalf("unexpected error: %v", err)143}144})145}146}147148149