|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package stack |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "gopkg.in/yaml.v3" |
| 16 | + |
| 17 | + "github.com/elastic/elastic-package/internal/profile" |
| 18 | +) |
| 19 | + |
| 20 | +func TestApplyResourcesWithCustomGeoipDir(t *testing.T) { |
| 21 | + const expectedGeoipPath = "/some/path/ingest-geoip" |
| 22 | + const profileName = "custom_geoip" |
| 23 | + |
| 24 | + elasticPackagePath := t.TempDir() |
| 25 | + profilesPath := filepath.Join(elasticPackagePath, "profiles") |
| 26 | + |
| 27 | + os.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath) |
| 28 | + |
| 29 | + // Create profile. |
| 30 | + err := profile.CreateProfile(profile.Options{ |
| 31 | + // PackagePath is actually the profiles path, what is a bit counterintuitive. |
| 32 | + PackagePath: profilesPath, |
| 33 | + Name: profileName, |
| 34 | + }) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + // Write configuration to the profile. |
| 38 | + configPath := filepath.Join(profilesPath, profileName, profile.PackageProfileConfigFile) |
| 39 | + config := fmt.Sprintf("stack.geoip_dir: %q", expectedGeoipPath) |
| 40 | + err = os.WriteFile(configPath, []byte(config), 0644) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + p, err := profile.LoadProfile(profileName) |
| 44 | + require.NoError(t, err) |
| 45 | + t.Logf("Profile name: %s, path: %s", p.ProfileName, p.ProfilePath) |
| 46 | + |
| 47 | + // Smoke test to check that we are actually loading the profile we want and it has the setting. |
| 48 | + v := p.Config("stack.geoip_dir", "") |
| 49 | + require.Equal(t, expectedGeoipPath, v) |
| 50 | + |
| 51 | + // Now, apply resources and check that the variable has been used. |
| 52 | + err = applyResources(p, "8.6.1") |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + d, err := os.ReadFile(p.Path(profileStackPath, SnapshotFile)) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + var composeFile struct { |
| 59 | + Services struct { |
| 60 | + Elasticsearch struct { |
| 61 | + Volumes []string `yaml:"volumes"` |
| 62 | + } `yaml:"elasticsearch"` |
| 63 | + } `yaml:"services"` |
| 64 | + } |
| 65 | + err = yaml.Unmarshal(d, &composeFile) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + volumes := composeFile.Services.Elasticsearch.Volumes |
| 69 | + expectedVolume := fmt.Sprintf("%s:/usr/share/elasticsearch/config/ingest-geoip", expectedGeoipPath) |
| 70 | + assert.Contains(t, volumes, expectedVolume) |
| 71 | +} |
0 commit comments