Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions x-pack/heartbeat/Jenkinsfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ stages:
- "macosTest"
tags: true ## for all the tags
stage: extended
<<<<<<< HEAD
# TODO: there are windows test failures already reported
# https://github.com/elastic/beats/issues/23957 and https://github.com/elastic/beats/issues/23958
# waiting for being fixed.
Expand Down Expand Up @@ -75,6 +76,38 @@ stages:
# platforms: ## override default labels in this specific stage.
# - "windows-7-32-bit"
# stage: extended_win
=======
windows-2022:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-2022"
stage: mandatory
windows-2019:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-2019"
stage: extended_win
windows-2016:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-2016"
stage: mandatory
windows-2012:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-2012-r2"
stage: extended_win
windows-10:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-10"
stage: extended_win
windows-8:
mage: "mage build test"
platforms: ## override default labels in this specific stage.
- "windows-8"
stage: extended_win
>>>>>>> 8552e34a6a (ci: enable windows for testing heartbeat (#32937))
packaging-linux:
packaging-linux: "mage package"
e2e:
Expand Down
2 changes: 2 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/inline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.
//go:build linux || darwin
// +build linux darwin

package source

Expand Down
2 changes: 2 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/inline_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.
//go:build linux || darwin
// +build linux darwin

package source

Expand Down
3 changes: 3 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
3 changes: 3 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
2 changes: 2 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/offline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.
//go:build linux || darwin
// +build linux darwin

package source

Expand Down
132 changes: 132 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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.
//go:build linux || darwin
// +build linux darwin

package source

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/elastic/elastic-agent-libs/mapstr"
)

type ProjectSource struct {
Content string `config:"content" json:"content"`
TargetDirectory string
}

var ErrNoContent = fmt.Errorf("no 'content' value specified for project monitor source")

func (p *ProjectSource) Validate() error {
if !regexp.MustCompile(`\S`).MatchString(p.Content) {
return ErrNoContent
}

return nil
}

func (p *ProjectSource) Fetch() error {
decodedBytes, err := base64.StdEncoding.DecodeString(p.Content)
if err != nil {
return err
}

tf, err := ioutil.TempFile(os.TempDir(), "elastic-synthetics-zip-")
if err != nil {
return fmt.Errorf("could not create tmpfile for project monitor source: %w", err)
}
defer os.Remove(tf.Name())

// copy the encoded contents in to a temp file for unzipping later
_, err = io.Copy(tf, bytes.NewReader(decodedBytes))
if err != nil {
return err
}

p.TargetDirectory, err = ioutil.TempDir(os.TempDir(), "elastic-synthetics-unzip-")
if err != nil {
return fmt.Errorf("could not make temp dir for unzipping project source: %w", err)
}

err = unzip(tf, p.Workdir(), "")
if err != nil {
p.Close()
return err
}

// Offline is not required for project resources as we are only linking
// to the globally installed agent, but useful for testing purposes
if !Offline() {
// set up npm project and ensure synthetics is installed
err = setupProjectDir(p.Workdir())
if err != nil {
return fmt.Errorf("setting up project dir failed: %w", err)
}
}

return nil
}

type PackageJSON struct {
Name string `json:"name"`
Private bool `json:"private"`
Dependencies mapstr.M `json:"dependencies"`
}

// setupProjectDir sets ups the required package.json file and
// links the synthetics dependency to the globally installed one that is
// baked in to the Heartbeat image to maintain compatibility and
// allows us to control the synthetics agent version
func setupProjectDir(workdir string) error {
fname, err := exec.LookPath("elastic-synthetics")
if err == nil {
fname, err = filepath.Abs(fname)
}
if err != nil {
return fmt.Errorf("cannot resolve global synthetics library: %w", err)
}

globalPath := strings.Replace(fname, "bin/elastic-synthetics", "lib/node_modules/@elastic/synthetics", 1)
symlinkPath := fmt.Sprintf("file:%s", globalPath)
pkgJson := PackageJSON{
Name: "project-journey",
Private: true,
Dependencies: mapstr.M{
"@elastic/synthetics": symlinkPath,
},
}
pkgJsonContent, err := json.MarshalIndent(pkgJson, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(workdir, "package.json"), pkgJsonContent, 0755)
if err != nil {
return err
}

// setup the project linking to the global synthetics library
return runSimpleCommand(exec.Command("npm", "install"), workdir)
}

func (p *ProjectSource) Workdir() string {
return p.TargetDirectory
}

func (p *ProjectSource) Close() error {
if p.TargetDirectory != "" {
return os.RemoveAll(p.TargetDirectory)
}
return nil
}
100 changes: 100 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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.
//go:build linux || darwin
// +build linux darwin

package source

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"

"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/mapstr"
)

func setUpTests() func() {
GoOffline()
return func() {
GoOnline()
}
}

func TestProjectSource(t *testing.T) {
teardown := setUpTests()
defer teardown()

type testCase struct {
name string
cfg mapstr.M
wantErr bool
}
testCases := []testCase{
{
"decode project content",
mapstr.M{
"content": "UEsDBBQACAAIAJ27qVQAAAAAAAAAAAAAAAAiAAAAZXhhbXBsZXMvdG9kb3MvYWR2YW5jZWQuam91cm5leS50c5VRPW/CMBDd+RWnLA0Sigt0KqJqpbZTN+iEGKzkIC6JbfkuiBTx3+uEEAGlgi7Rnf38viIESCLkR/FJ6Eis1VIjpanATBKrWFCpOUU/kcCNzG2GJNgkhoRM1lLHmERfpnAay4ipo3JrHMMWmjPYwcKZHILn33zBqIV3ADIjkxdrJ4y251eZJFNJq3b1Hh1XJx+KeKK+8XATpxiv3o07RidI7Ex5OOocTEQixcz6mF66MRgGXkmxMhqkTiA2VcJ6NQsgpZcZAnueoAfhFqxcYs9/ncwJdl0YP9XeY6OJgb3qFDcMYwhejb5jsAUDyYxBaSi9HmCJlfZJ2vCYNCpc1h2d5m8AB/r99cU+GmS/hpwXc4nmrKh/K917yK57VqZe1lU6zM26WvIiY2WbHunWIiusb3I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AAIAlwAAAP4DAAAAAA==",
},
false,
},
{
"bad encoded content",
mapstr.M{
"content": "12312edasd",
},
true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
psrc, err := dummyPSource(tc.cfg)
if tc.wantErr {
err = psrc.Fetch()
require.Error(t, err)
return
}
require.NoError(t, err)
fetchAndValidate(t, psrc)
})
}
}

func validateFileContents(t *testing.T, dir string) {
expected := []string{
"examples/todos/helpers.ts",
"examples/todos/advanced.journey.ts",
}
for _, file := range expected {
_, err := os.Stat(path.Join(dir, file))
assert.NoError(t, err)
}
}

func fetchAndValidate(t *testing.T, psrc *ProjectSource) {
err := psrc.Fetch()
require.NoError(t, err)

validateFileContents(t, psrc.Workdir())
// check if the working directory is deleted
require.NoError(t, psrc.Close())
_, err = os.Stat(psrc.TargetDirectory)
require.True(t, os.IsNotExist(err), "TargetDirectory %s should have been deleted", psrc.TargetDirectory)
}

func dummyPSource(conf map[string]interface{}) (*ProjectSource, error) {
psrc := &ProjectSource{}
y, _ := yaml.Marshal(conf)
c, err := config.NewConfigWithYAML(y, string(y))
if err != nil {
return nil, err
}
err = c.Unpack(psrc)
return psrc, err
}
6 changes: 6 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// 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.
<<<<<<< HEAD
=======

//go:build linux || darwin
// +build linux darwin
>>>>>>> 8552e34a6a (ci: enable windows for testing heartbeat (#32937))

package source

Expand Down
3 changes: 3 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/validatepackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
3 changes: 3 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/zipurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
3 changes: 3 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/zipurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux || darwin
// +build linux darwin

package source

import (
Expand Down
Loading