Skip to content

Commit

Permalink
refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Oct 13, 2023
1 parent 6d0c9e3 commit d543bfa
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package http

import (
"bytes"
"context"
"crypto/sha512"
"fmt"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
"github.com/elastic/elastic-agent/testing/pgptest"
)

const (
version = "7.5.1"
sourcePattern = "/downloads/beats/filebeat/"
source = "http://artifacts.elastic.co/downloads/"
)

var (
beatSpec = artifact.Artifact{
Name: "filebeat",
Cmd: "filebeat",
Artifact: "beats/filebeat",
}
)

type testCase struct {
system string
arch string
}

func getTestCases() []testCase {
// always test random package to save time
return []testCase{
{"linux", "32"},
{"linux", "64"},
{"linux", "arm64"},
{"darwin", "32"},
{"darwin", "64"},
{"windows", "32"},
{"windows", "64"},
}
}

func getElasticCoServer(t *testing.T) (*httptest.Server, []byte) {
correctValues := map[string]struct{}{
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "i386.deb"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "amd64.deb"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "i686.rpm"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "x86_64.rpm"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "linux-x86.tar.gz"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "linux-arm64.tar.gz"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "linux-x86_64.tar.gz"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "windows-x86.zip"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "windows-x86_64.zip"): {},
fmt.Sprintf("%s-%s-%s", beatSpec.Cmd, version, "darwin-x86_64.tar.gz"): {},
}
var resp []byte
content := []byte("anything will do")
hash := sha512.Sum512(content)
pub, sig := pgptest.Sing(t, bytes.NewReader(content))

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
packageName := r.URL.Path[len(sourcePattern):]

ext := filepath.Ext(packageName)
if ext == ".gz" {
ext = ".tar.gz"
}
packageName = strings.TrimSuffix(packageName, ext)

switch ext {
case ".sha512":
resp = []byte(fmt.Sprintf("%x %s", hash, packageName))
case ".asc":
resp = sig
case ".tar.gz", ".zip", ".deb", ".rpm":
packageName += ext
resp = content
default:
w.WriteHeader(http.StatusNotFound)
t.Errorf("mock elastic.co server: unknown file extension: %q", ext)
return
}

if _, ok := correctValues[packageName]; !ok {
t.Errorf("mock elastic.co server: invalid package name: %q", packageName)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte{})
return
}

_, err := w.Write(resp)
assert.NoErrorf(t, err, "mock elastic.co server: failes writing reponse")
})

return httptest.NewServer(handler), pub
}

func getElasticCoClient(server *httptest.Server) http.Client {
return http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, s string) (net.Conn, error) {
_ = s
return net.Dial(network, server.Listener.Addr().String())
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package http

import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
Expand All @@ -17,6 +18,7 @@ import (
"time"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
"github.com/elastic/elastic-agent/pkg/core/logger"

"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
Expand All @@ -25,6 +27,48 @@ import (
"github.com/elastic/elastic-agent-libs/transport/httpcommon"
)

func TestDownload(t *testing.T) {
targetDir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}

log, _ := logger.New("", false)
timeout := 30 * time.Second
testCases := getTestCases()
server, _ := getElasticCoServer(t)
elasticClient := getElasticCoClient(server)

config := &artifact.Config{
SourceURI: source,
TargetDirectory: targetDir,
HTTPTransportSettings: httpcommon.HTTPTransportSettings{
Timeout: timeout,
},
}

for _, testCase := range testCases {
testName := fmt.Sprintf("%s-binary-%s", testCase.system, testCase.arch)
t.Run(testName, func(t *testing.T) {
config.OperatingSystem = testCase.system
config.Architecture = testCase.arch

testClient := NewDownloaderWithClient(log, config, elasticClient)
artifactPath, err := testClient.Download(context.Background(), beatSpec, version)
if err != nil {
t.Fatal(err)
}

_, err = os.Stat(artifactPath)
if err != nil {
t.Fatal(err)
}

os.Remove(artifactPath)
})
}
}

func TestDownloadBodyError(t *testing.T) {
// This tests the scenario where the download encounters a network error
// part way through the download, while copying the response body.
Expand Down

This file was deleted.

Loading

0 comments on commit d543bfa

Please sign in to comment.