Skip to content

Commit a816551

Browse files
authored
Add some tests for EPR_PROXY_TO override in profile (#3224)
1 parent 55e84c3 commit a816551

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

‎internal/configuration/locations/locations_internal_test.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,18 @@ func Test_configurationDirError(t *testing.T) {
3636
default:
3737
env = "HOME"
3838
}
39-
homeEnv := os.Getenv(env)
40-
os.Unsetenv(env)
39+
t.Setenv(env, "")
4140

4241
_, err := configurationDir()
4342
assert.Error(t, err)
44-
45-
os.Setenv(env, homeEnv)
4643
}
4744

4845
func Test_configurationDirOverride(t *testing.T) {
4946
expected := "/tmp/foobar"
50-
os.Setenv(elasticPackageDataHome, expected)
47+
t.Setenv(elasticPackageDataHome, expected)
5148

5249
actual, err := configurationDir()
5350
assert.Nil(t, err)
5451

5552
assert.Equal(t, expected, actual)
56-
os.Setenv(elasticPackageDataHome, "")
5753
}

‎internal/stack/resources.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const (
2929
// ComposeFile is the docker compose file.
3030
ComposeFile = "docker-compose.yml"
3131

32+
// DockerfilePackageRegistryFile is the dockerfile for package-registry container.
33+
DockerfilePackageRegistryFile = "Dockerfile.package-registry"
34+
3235
// ElasticsearchConfigFile is the elasticsearch config file.
3336
ElasticsearchConfigFile = "elasticsearch.yml"
3437

@@ -77,7 +80,7 @@ var (
7780
staticSource = resource.NewSourceFS(static).WithTemplateFuncs(templateFuncs)
7881
stackResources = []resource.Resource{
7982
&resource.File{
80-
Path: "Dockerfile.package-registry",
83+
Path: DockerfilePackageRegistryFile,
8184
Content: staticSource.Template("_static/Dockerfile.package-registry.tmpl"),
8285
},
8386
&resource.File{

‎internal/stack/resources_test.go‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestApplyResourcesWithCustomGeoipDir(t *testing.T) {
2424
elasticPackagePath := t.TempDir()
2525
profilesPath := filepath.Join(elasticPackagePath, "profiles")
2626

27-
os.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath)
27+
t.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath)
2828

2929
// Create profile.
3030
err := profile.CreateProfile(profile.Options{
@@ -70,6 +70,82 @@ func TestApplyResourcesWithCustomGeoipDir(t *testing.T) {
7070
assert.Contains(t, volumes, expectedVolume)
7171
}
7272

73+
func TestApplyResourcesWithDefaultPackageRegistryURL(t *testing.T) {
74+
const profileName = "custom_package_registry"
75+
const expectedEPR = "https://epr.elastic.co"
76+
77+
elasticPackagePath := t.TempDir()
78+
profilesPath := filepath.Join(elasticPackagePath, "profiles")
79+
80+
t.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath)
81+
82+
// Create profile.
83+
err := profile.CreateProfile(profile.Options{
84+
ProfilesDirPath: profilesPath,
85+
Name: profileName,
86+
})
87+
require.NoError(t, err)
88+
89+
p, err := profile.LoadProfile(profileName)
90+
require.NoError(t, err)
91+
t.Logf("Profile name: %s, path: %s", p.ProfileName, p.ProfilePath)
92+
93+
// Ensure that the profile has no specific configuration for EPR proxy.
94+
v := p.Config(configElasticEPRProxyTo, "")
95+
require.Equal(t, "", v)
96+
97+
// Now, apply resources and check that the variable has been used.
98+
stackVersion := "8.6.1"
99+
err = applyResources(p, stackVersion, stackVersion)
100+
require.NoError(t, err)
101+
102+
d, err := os.ReadFile(p.Path(ProfileStackPath, DockerfilePackageRegistryFile))
103+
require.NoError(t, err)
104+
105+
assert.Contains(t, string(d), fmt.Sprintf("ENV EPR_PROXY_TO=%s", expectedEPR))
106+
}
107+
108+
func TestApplyResourcesWithCustomPackageRegistryURL(t *testing.T) {
109+
const profileName = "custom_package_registry"
110+
const expectedEPR = "http://localhost"
111+
112+
elasticPackagePath := t.TempDir()
113+
profilesPath := filepath.Join(elasticPackagePath, "profiles")
114+
115+
t.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath)
116+
117+
// Create profile.
118+
err := profile.CreateProfile(profile.Options{
119+
ProfilesDirPath: profilesPath,
120+
Name: profileName,
121+
})
122+
require.NoError(t, err)
123+
124+
// Write configuration to the profile.
125+
configPath := filepath.Join(profilesPath, profileName, profile.PackageProfileConfigFile)
126+
config := fmt.Sprintf("%s: %q", configElasticEPRProxyTo, expectedEPR)
127+
err = os.WriteFile(configPath, []byte(config), 0644)
128+
require.NoError(t, err)
129+
130+
p, err := profile.LoadProfile(profileName)
131+
require.NoError(t, err)
132+
t.Logf("Profile name: %s, path: %s", p.ProfileName, p.ProfilePath)
133+
134+
// Smoke test to check that we are actually loading the profile we want and it has the setting.
135+
v := p.Config(configElasticEPRProxyTo, "")
136+
require.Equal(t, expectedEPR, v)
137+
138+
// Now, apply resources and check that the variable has been used.
139+
stackVersion := "8.6.1"
140+
err = applyResources(p, stackVersion, stackVersion)
141+
require.NoError(t, err)
142+
143+
d, err := os.ReadFile(p.Path(ProfileStackPath, DockerfilePackageRegistryFile))
144+
require.NoError(t, err)
145+
146+
assert.Contains(t, string(d), fmt.Sprintf("ENV EPR_PROXY_TO=%s", expectedEPR))
147+
}
148+
73149
func TestSemverLessThan(t *testing.T) {
74150
b, err := semverLessThan("8.9.0", "8.10.0-SNAPSHOT")
75151
require.NoError(t, err)

0 commit comments

Comments
 (0)