Skip to content

Commit bed2f2e

Browse files
committed
Check all managed tags when one is missing
1 parent 1dffd8d commit bed2f2e

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

internal/kibana/savedobjects.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ func (c *Client) SetManagedSavedObject(ctx context.Context, savedObjectType stri
150150
}
151151

152152
type ExportSavedObjectsRequest struct {
153+
Type string `json:"type,omitempty"`
153154
ExcludeExportDetails bool `json:"excludeExportDetails"`
154155
IncludeReferencesDeep bool `json:"includeReferencesDeep"`
155-
Objects []ExportSavedObjectsRequestObject `json:"objects"`
156+
Objects []ExportSavedObjectsRequestObject `json:"objects,omitempty"`
156157
}
157158

158159
type ExportSavedObjectsRequestObject struct {

internal/packages/assets.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"os"
1212
"path/filepath"
13+
"strings"
1314

1415
"gopkg.in/yaml.v3"
1516

@@ -142,13 +143,18 @@ func loadKibanaTags(pkgRootPath string) ([]Asset, error) {
142143

143144
assets := make([]Asset, len(tags))
144145
for i, tag := range tags {
145-
assets[i].ID = tag.Text
146+
assets[i].ID = sharedTagID(tag.Text)
146147
assets[i].Type = AssetTypeKibanaTag.typeName
147148
assets[i].SourcePath = tagsFilePath
148149
}
149150
return assets, nil
150151
}
151152

153+
// sharedTagID tries to mimick tags created by fleet for tags defined in tags.yml.
154+
func sharedTagID(text string) string {
155+
return strings.Join(append(strings.Split(strings.ToLower(text), " "), "default"), "-")
156+
}
157+
152158
func loadElasticsearchAssets(pkgRootPath string) ([]Asset, error) {
153159
packageManifestPath := filepath.Join(pkgRootPath, PackageManifestFile)
154160
pkgManifest, err := ReadPackageManifest(packageManifestPath)

internal/testrunner/runners/asset/tester.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"strings"
1212

13+
"github.com/elastic/elastic-package/internal/common"
1314
"github.com/elastic/elastic-package/internal/kibana"
1415
"github.com/elastic/elastic-package/internal/logger"
1516
"github.com/elastic/elastic-package/internal/packages"
@@ -130,6 +131,11 @@ func (r *tester) run(ctx context.Context) ([]testrunner.TestResult, error) {
130131
}
131132
installedAssets := installedPackage.Assets()
132133

134+
installedTags, err := r.kibanaClient.ExportSavedObjects(ctx, kibana.ExportSavedObjectsRequest{Type: "tag"})
135+
if err != nil {
136+
return result.WithError(fmt.Errorf("cannot get installed tags: %w", err))
137+
}
138+
133139
// No Elasticsearch asset is created when an Input package is installed through the API.
134140
// This would require to create a Agent policy and add that input package to the Agent policy.
135141
// As those input packages could have some required fields, it would also require to add
@@ -151,14 +157,12 @@ func (r *tester) run(ctx context.Context) ([]testrunner.TestResult, error) {
151157
TestType: TestType,
152158
})
153159

154-
var tr []testrunner.TestResult
155-
if !findActualAsset(installedAssets, e) {
160+
tr, _ := rc.WithSuccess()
161+
if !findActualAsset(installedAssets, installedTags, e) {
156162
tr, _ = rc.WithError(testrunner.ErrTestCaseFailed{
157163
Reason: "could not find expected asset",
158164
Details: fmt.Sprintf("could not find %s asset \"%s\". Assets loaded:\n%s", e.Type, e.ID, formatAssetsAsString(installedAssets)),
159165
})
160-
} else {
161-
tr, _ = rc.WithSuccess()
162166
}
163167
result := tr[0]
164168
if r.withCoverage && e.SourcePath != "" {
@@ -191,13 +195,28 @@ func (r *tester) TearDown(ctx context.Context) error {
191195
return nil
192196
}
193197

194-
func findActualAsset(actualAssets []packages.Asset, expectedAsset packages.Asset) bool {
198+
func findActualAsset(actualAssets []packages.Asset, installedTags []common.MapStr, expectedAsset packages.Asset) bool {
195199
for _, a := range actualAssets {
196200
if a.Type == expectedAsset.Type && a.ID == expectedAsset.ID {
197201
return true
198202
}
199203
}
200204

205+
if expectedAsset.Type == "tag" {
206+
// If we haven't found the asset, and it is a tag, it could be some of the shared tags defined in tags.yml.
207+
for _, tag := range installedTags {
208+
managed, _ := tag.GetValue("managed")
209+
if managed, ok := managed.(bool); !ok || !managed {
210+
continue
211+
}
212+
213+
id, _ := tag.GetValue("id")
214+
if id, ok := id.(string); ok && id == expectedAsset.ID {
215+
return true
216+
}
217+
}
218+
}
219+
201220
return false
202221
}
203222

0 commit comments

Comments
 (0)