Skip to content

Commit

Permalink
break: move from "pull-requests" to "commits" GCP bucket (elastic#866)
Browse files Browse the repository at this point in the history
* fix: use fallback version when using Git SHA commits

* break: move from "pull-requests" to "commits" GCP bucket

* chore: typos
  • Loading branch information
mdelapenya committed Mar 11, 2021
1 parent e5b4f7a commit cad6581
Show file tree
Hide file tree
Showing 9 changed files with 19,173 additions and 19,390 deletions.
4 changes: 0 additions & 4 deletions e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ fleet-fleet:
fleet-fleet-ci-snapshots:
SUITE="fleet" TAGS="fleet_mode_agent" TIMEOUT_FACTOR=3 LOG_LEVEL=TRACE BEATS_USE_CI_SNAPSHOTS=true DEVELOPER_MODE=true GITHUB_CHECK_SHA1=a1962c8864016010adcde9f35bd8378debb4fbf7 $(MAKE) functional-test

.PHONY: fleet-fleet-pr-ci-snapshots
fleet-fleet-pr-ci-snapshots:
SUITE="fleet" TAGS="fleet_mode_agent" TIMEOUT_FACTOR=3 LOG_LEVEL=TRACE BEATS_USE_CI_SNAPSHOTS=true DEVELOPER_MODE=true BEAT_VERSION=pr-14954 $(MAKE) functional-test

.PHONY: fleet-nightly
fleet-nightly:
SUITE="fleet" TAGS="fleet_mode_agent && nightly" TIMEOUT_FACTOR=3 LOG_LEVEL=TRACE DEVELOPER_MODE=true $(MAKE) functional-test
Expand Down
2 changes: 1 addition & 1 deletion e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ To do so:
1. Login as a user
1. Select the base branch for the test code: 7.10.x, 7.11.x, 7.x or master.
1. In the left menu, click on `Buid with Parameters`.
1. In the input parameters form, set the Beat version **for Fleet and Metricbeat** with the ID of your pull request, using `pr-` as prefix. I.e. `pr-23456`.
1. In the input parameters form, keep the Beat version (for Fleet and Metricbeat) as is, to use each branch's default version.
1. In the input parameters form, keep the stack version (for Fleet and Metricbeat) as is, to use each branch's default version.
1. In the input parameters form, set the `GITHUB_CHECK_NAME` to `E2E Tests`. This value will appear as the label for the Github check for the E2E tests.
1. In the input parameters form, set the `GITHUB_CHECK_REPO` to `beats`.
Expand Down
4 changes: 1 addition & 3 deletions e2e/_suites/fleet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ This is an example of the optional configuration:
# (Fleet mode) This environment variable will use the snapshots produced by Beats CI. If the above variable
# is set, this variable will take no effect
export BEATS_USE_CI_SNAPSHOTS="true"
# (Stand-Alone mode) This environment variable will use the its value as the Docker tag produced by Beats CI (Please look up here: https://container-library.elastic.co/r/observability-ci/elastic-agent). Here you have two examples for tags:
export BEAT_VERSION="pr-20356"
# or
# (Stand-Alone mode) This environment variable will use the its value as the Docker tag produced by Beats CI (Please look up Google Cloud Storage CI bucket).
export BEAT_VERSION="78a762c76080aafa34c52386341b590dac24e2df"
```

Expand Down
3 changes: 1 addition & 2 deletions e2e/_suites/fleet/installers.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ func (i *DockerPackage) Preinstall() error {
// wait for tagging to ensure the loaded image is present
e2e.Sleep(3 * time.Second)

// we need to tag the loaded image because its tag relates to the target branch,
// and we want it to use the 'pr-12345' format.
// we need to tag the loaded image because its tag relates to the target branch
return docker.TagImage(
"docker.elastic.co/beats/"+i.artifact+":"+agentVersionBase,
"docker.elastic.co/observability-ci/"+i.artifact+":"+i.originalVersion+"-amd64",
Expand Down
2 changes: 1 addition & 1 deletion e2e/_suites/metricbeat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This is an example of the optional configuration:
# or
# This environment variable will use the snapshots produced by Beats CI
export BEATS_USE_CI_SNAPSHOTS="true"
export BEAT_VERSION="pr-20356"
export GITHUB_CHECK_SHA1="01234567890"
```

3. Define the proper Docker images to be used in tests (Optional).
Expand Down
19,141 changes: 19,141 additions & 0 deletions e2e/_testresources/gcp/commits.json

Large diffs are not rendered by default.

19,268 changes: 0 additions & 19,268 deletions e2e/_testresources/gcp/pull-requests.json

This file was deleted.

28 changes: 8 additions & 20 deletions e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ func BuildArtifactName(artifact string, version string, fallbackVersion string,
return artifactName
}

// CheckPRVersion returns a fallback version if the version comes from a Pull Request (PR)
// CheckPRVersion returns a fallback version if the version comes from a commit
func CheckPRVersion(version string, fallbackVersion string) string {
if strings.HasPrefix(strings.ToLower(version), "pr-") {
commitSHA := shell.GetEnv("GITHUB_CHECK_SHA1", "")
if commitSHA != "" {
return fallbackVersion
}

Expand Down Expand Up @@ -157,21 +158,13 @@ func getGCPBucketCoordinates(fileName string, artifact string, version string, f
// the commit SHA will identify univocally the artifact in the GCP storage bucket
commitSHA := shell.GetEnv("GITHUB_CHECK_SHA1", "")
if commitSHA != "" {
prefix = fmt.Sprintf("commits/%s", commitSHA)
object = artifact + "/" + fileName
}

// we are setting a version from a pull request: the version of the artifact will be kept as the base one
// i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-x86_64.rpm
// i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-amd64.deb
// i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-linux-x86_64.tar.gz
if strings.HasPrefix(strings.ToLower(version), "pr-") {
log.WithFields(log.Fields{
"version": fallbackVersion,
"commit": commitSHA,
"PR": version,
}).Debug("Using CI snapshots for a pull request")
prefix = fmt.Sprintf("pull-requests/%s", version)
object = fmt.Sprintf("%s/%s", artifact, fileName)
"version": fallbackVersion,
}).Debug("Using CI snapshots for a commit")
prefix = fmt.Sprintf("commits/%s", commitSHA)
object = artifact + "/" + fileName
}

return bucket, prefix, object
Expand Down Expand Up @@ -201,12 +194,7 @@ func GetExponentialBackOff(elapsedTime time.Duration) *backoff.ExponentialBackOf
// 1. Elastic's artifact repository, building the JSON path query based
// If the version is a PR, then it will return the version without checking the artifacts API
// i.e. GetElasticArtifactVersion("$VERSION")
// i.e. GetElasticArtifactVersion("pr-22000")
func GetElasticArtifactVersion(version string) string {
if strings.HasPrefix(strings.ToLower(version), "pr-") {
return version
}

exp := GetExponentialBackOff(time.Minute)

retryCount := 1
Expand Down
111 changes: 20 additions & 91 deletions e2e/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
var testVersion = "8.0.0-SNAPSHOT"

const bucket = "beats-ci-artifacts"
const pullRequests = "pull-requests"
const commits = "commits"
const snapshots = "snapshots"

var nextTokenParamJSON *gabs.Container
var pullRequestsJSON *gabs.Container
var commitsJSON *gabs.Container
var snapshotsJSON *gabs.Container

func init() {
Expand All @@ -27,11 +27,11 @@ func init() {
}
nextTokenParamJSON, _ = gabs.ParseJSON([]byte(nextTokenParamContent))

pullRequestsContent, err := ioutil.ReadFile("_testresources/gcp/pull-requests.json")
commitsContent, err := ioutil.ReadFile("_testresources/gcp/commits.json")
if err != nil {
os.Exit(1)
}
pullRequestsJSON, _ = gabs.ParseJSON([]byte(pullRequestsContent))
commitsJSON, _ = gabs.ParseJSON([]byte(commitsContent))

snapshotsContent, err := ioutil.ReadFile("_testresources/gcp/snapshots.json")
if err != nil {
Expand Down Expand Up @@ -178,8 +178,8 @@ func TestBuildArtifactName(t *testing.T) {
})

t.Run("For Docker for a Pull Request", func(t *testing.T) {
defer os.Unsetenv("BEAT_VERSION")
os.Setenv("BEAT_VERSION", "pr-12345")
defer os.Unsetenv("GITHUB_CHECK_SHA1")
os.Setenv("GITHUB_CHECK_SHA1", "0123456789")

artifact = "elastic-agent"
arch := "amd64"
Expand All @@ -202,9 +202,11 @@ func TestCheckPRVersion(t *testing.T) {
assert.Equal(t, testVersion, v)
})

t.Run("A PR should return base version", func(t *testing.T) {
prVersion := "pr-12345"
v := CheckPRVersion(prVersion, testVersion)
t.Run("A Commit-based version should return base version", func(t *testing.T) {
defer os.Unsetenv("GITHUB_CHECK_SHA1")
os.Setenv("GITHUB_CHECK_SHA1", "0123456789")

v := CheckPRVersion(testVersion, testVersion)

assert.Equal(t, testVersion, v)
})
Expand All @@ -219,7 +221,7 @@ func TestGetBucketSearchNextPageParam_HasMorePages(t *testing.T) {

func TestGetBucketSearchNextPageParam_HasNoMorePages(t *testing.T) {
// this JSON file does not contain the tokken field
param := getBucketSearchNextPageParam(pullRequestsJSON)
param := getBucketSearchNextPageParam(commitsJSON)
assert.True(t, param == "")
}

Expand Down Expand Up @@ -264,79 +266,6 @@ func TestGetGCPBucketCoordinates_Commits(t *testing.T) {
})
}

func TestGetGCPBucketCoordinates_CommitsForAPullRequest(t *testing.T) {
artifact := "elastic-agent"
version := "pr-23456"

t.Run("Fetching pull request bucket for RPM package", func(t *testing.T) {
defer os.Unsetenv("GITHUB_CHECK_SHA1")
os.Setenv("GITHUB_CHECK_SHA1", "0123456789")

fileName := "elastic-agent-" + testVersion + "-x86_64.rpm"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm")
})

t.Run("Fetching pull request bucket for DEB package", func(t *testing.T) {
defer os.Unsetenv("GITHUB_CHECK_SHA1")
os.Setenv("GITHUB_CHECK_SHA1", "0123456789")

fileName := "elastic-agent-" + testVersion + "-amd64.deb"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb")
})

t.Run("Fetching pull request bucket for TAR package adds OS to fileName and object", func(t *testing.T) {
defer os.Unsetenv("GITHUB_CHECK_SHA1")
os.Setenv("GITHUB_CHECK_SHA1", "0123456789")

fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz")
})
}

func TestGetGCPBucketCoordinates_PullRequests(t *testing.T) {
artifact := "elastic-agent"
version := "pr-23456"

t.Run("Fetching pull request bucket for RPM package", func(t *testing.T) {
fileName := "elastic-agent-" + testVersion + "-x86_64.rpm"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm")
})

t.Run("Fetching pull request bucket for DEB package", func(t *testing.T) {
fileName := "elastic-agent-" + testVersion + "-amd64.deb"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb")
})

t.Run("Fetching pull request bucket for TAR package adds OS to fileName and object", func(t *testing.T) {
fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz"

bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion)
assert.Equal(t, bucket, "beats-ci-artifacts")
assert.Equal(t, prefix, "pull-requests/pr-23456")
assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz")
})
}

func TestGetGCPBucketCoordinates_Snapshots(t *testing.T) {
artifact := "elastic-agent"
version := testVersion
Expand Down Expand Up @@ -369,19 +298,19 @@ func TestGetGCPBucketCoordinates_Snapshots(t *testing.T) {
})
}

func TestProcessBucketSearchPage_PullRequestsFound(t *testing.T) {
// retrieving last element in pull-requests.json
object := "pr-22495/filebeat/filebeat-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz"
func TestProcessBucketSearchPage_CommitFound(t *testing.T) {
// retrieving last element in commits.json
object := "024b732844d40bdb2bf806480af2b03fcb8fbdbe/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-darwin-x86_64.tar.gz"

mediaLink, err := processBucketSearchPage(pullRequestsJSON, 1, bucket, pullRequests, object)
mediaLink, err := processBucketSearchPage(commitsJSON, 1, bucket, commits, object)
assert.Nil(t, err)
assert.True(t, mediaLink == "https://storage.googleapis.com/download/storage/v1/b/beats-ci-artifacts/o/pull-requests%2Fpr-22495%2Ffilebeat%2Ffilebeat-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz?generation=1610634841693588&alt=media")
assert.True(t, mediaLink == "https://storage.googleapis.com/download/storage/v1/b/beats-ci-artifacts/o/commits%2F024b732844d40bdb2bf806480af2b03fcb8fbdbe%2Felastic-agent%2Felastic-agent-8.0.0-SNAPSHOT-darwin-x86_64.tar.gz?generation=1612983859986704&alt=media")
}

func TestProcessBucketSearchPage_PullRequestsNotFound(t *testing.T) {
object := "pr-fooo/filebeat/filebeat-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz"
func TestProcessBucketSearchPage_CommitsNotFound(t *testing.T) {
object := "foo/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz"

mediaLink, err := processBucketSearchPage(pullRequestsJSON, 1, bucket, pullRequests, object)
mediaLink, err := processBucketSearchPage(commitsJSON, 1, bucket, commits, object)
assert.NotNil(t, err)
assert.True(t, mediaLink == "")
}
Expand Down

0 comments on commit cad6581

Please sign in to comment.