Skip to content

Commit 658a7d7

Browse files
authored
[Integration Testing] Add support for FIPS-capable artifacts (elastic#8057)
* Add GetPackagePrefix function * Add WithArtifactFIPSOnly upgradeOpt * Incorporate fips- prefix into filename of artifact to be fetched * Remove fips- when determining name of extracted artifact directory
1 parent 82344c2 commit 658a7d7

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

pkg/testing/fetch_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,27 @@ package testing
77
import (
88
"errors"
99
gtesting "testing"
10+
11+
"github.com/stretchr/testify/require"
1012
)
1113

14+
func TestGetPackagePrefix(t *gtesting.T) {
15+
tests := map[string]struct {
16+
fipsOnly bool
17+
expected string
18+
}{
19+
"fips": {fipsOnly: true, expected: "fips-"},
20+
"no_fips": {fipsOnly: false, expected: ""},
21+
}
22+
23+
for name, tc := range tests {
24+
t.Run(name, func(t *gtesting.T) {
25+
actual := GetPackagePrefix(tc.fipsOnly)
26+
require.Equal(t, tc.expected, actual)
27+
})
28+
}
29+
}
30+
1231
func TestGetPackageSuffix(t *gtesting.T) {
1332
tests := map[string]struct {
1433
os string

pkg/testing/fetcher.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ var packageArchMap = map[string]string{
4040
"darwin-arm64-targz": "darwin-aarch64.tar.gz",
4141
}
4242

43+
// GetPackagePrefix returns the prefix right before the version component of the Elastic
44+
// Agent's artifact name, based on the given parameter values.
45+
func GetPackagePrefix(fipsOnly bool) string {
46+
if fipsOnly {
47+
return "fips-"
48+
}
49+
return ""
50+
}
51+
4352
// GetPackageSuffix returns the suffix ending for the builds of Elastic Agent based on the
4453
// operating system and architecture.
4554
func GetPackageSuffix(operatingSystem string, architecture string, packageFormat string) (string, error) {

pkg/testing/fetcher_artifact.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type httpDoer interface {
2626

2727
type artifactFetcher struct {
2828
snapshotOnly bool
29+
fipsOnly bool
2930

3031
doer httpDoer
3132
}
@@ -39,6 +40,13 @@ func WithArtifactSnapshotOnly() artifactFetcherOpt {
3940
}
4041
}
4142

43+
// WithArtifactFIPSOnly sets the ArtifactFetcher to only pull a FIPS-compliant build.
44+
func WithArtifactFIPSOnly() artifactFetcherOpt {
45+
return func(f *artifactFetcher) {
46+
f.fipsOnly = true
47+
}
48+
}
49+
4250
// ArtifactFetcher returns a fetcher that pulls the binary of the Elastic Agent from the Elastic artifacts API.
4351
//
4452
// It tries to pull the latest version and if it cannot find that version it tries to pull a SNAPSHOT build for
@@ -60,6 +68,7 @@ func (f *artifactFetcher) Name() string {
6068

6169
// Fetch fetches the Elastic Agent and places the resulting binary at the path.
6270
func (f *artifactFetcher) Fetch(ctx context.Context, operatingSystem string, architecture string, version string, packageFormat string) (FetcherResult, error) {
71+
prefix := GetPackagePrefix(f.fipsOnly)
6372
suffix, err := GetPackageSuffix(operatingSystem, architecture, packageFormat)
6473
if err != nil {
6574
return nil, err
@@ -84,7 +93,7 @@ func (f *artifactFetcher) Fetch(ctx context.Context, operatingSystem string, arc
8493
}
8594

8695
// this remote path cannot have the build metadata in it
87-
srcPath := fmt.Sprintf("elastic-agent-%s-%s", ver.VersionWithPrerelease(), suffix)
96+
srcPath := fmt.Sprintf("elastic-agent-%s%s-%s", prefix, ver.VersionWithPrerelease(), suffix)
8897
downloadSrc := fmt.Sprintf("%s%s", uri, srcPath)
8998

9099
return &artifactResult{

pkg/testing/fixture.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,19 @@ func (f *Fixture) Prepare(ctx context.Context, components ...UsableComponent) er
214214
}
215215
f.srcPackage = src
216216
filename := filepath.Base(src)
217+
218+
// Determine name of extracted Agent artifact directory from
219+
// the artifact filename.
217220
name, _, err := splitFileType(filename)
218221
if err != nil {
219222
return err
220223
}
224+
225+
// If the name has "-fips" in it, remove that part because
226+
// the extracted directory does not have that in it, even though
227+
// the artifact filename does.
228+
name = strings.Replace(name, "-fips", "", 1)
229+
221230
extractDir := createTempDir(f.t)
222231
finalDir := filepath.Join(extractDir, name)
223232
err = ExtractArtifact(f.t, src, extractDir)

0 commit comments

Comments
 (0)