-
Notifications
You must be signed in to change notification settings - Fork 127
Conditional subobjects based on package version #2911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
teresaromero
merged 13 commits into
elastic:main
from
teresaromero:2832-mappings-subobjects
Sep 18, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
95d84fa
add support for version-based question prompts in data stream creation
teresaromero 092a91c
Update cmd/create_data_stream.go
teresaromero 6401c45
extract survey question logic into a separate function and add tests
teresaromero 4a0836c
add tests for createDataStreamDescriptorFromAnswers handling of Subob…
teresaromero b2d9ecc
add copyright notice to create_data_stream_test.go
teresaromero 4bcc828
Apply suggestion from @mrodm
teresaromero e4891f6
Apply suggestion from @mrodm
teresaromero 3e68e2e
Apply suggestion from @mrodm
teresaromero a9a124f
Apply suggestion from @mrodm
teresaromero 4621f9a
Apply suggestion from @mrodm
teresaromero 21a3eb4
Apply suggestion from @mrodm
teresaromero 2682da9
Add missing require import in create_data_stream_test.go
teresaromero b9ec1f0
Fix assertion for subobjects true case
teresaromero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/Masterminds/semver/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetSurveyQuestionsForVersion_BelowSemver3_2_0(t *testing.T) { | ||
version := semver.MustParse("3.1.9") | ||
questions := getInitialSurveyQuestionsForVersion(version) | ||
|
||
require.Len(t, questions, 3, "should return 3 questions for spec version < 3.2.0") | ||
|
||
assert.Equal(t, "name", questions[0].Name) | ||
assert.IsType(t, &survey.Input{}, questions[0].Prompt) | ||
assert.Equal(t, "title", questions[1].Name) | ||
assert.IsType(t, &survey.Input{}, questions[1].Prompt) | ||
assert.Equal(t, "type", questions[2].Name) | ||
assert.IsType(t, &survey.Select{}, questions[2].Prompt) | ||
} | ||
|
||
func TestGetSurveyQuestionsForVersion_EqualSemver3_2_0(t *testing.T) { | ||
version := semver.MustParse("3.2.0") | ||
questions := getInitialSurveyQuestionsForVersion(version) | ||
|
||
require.Len(t, questions, 4, "should return 4 questions for spec version >= 3.2.0") | ||
|
||
assert.Equal(t, "subobjects", questions[3].Name) | ||
assert.IsType(t, &survey.Confirm{}, questions[3].Prompt) | ||
} | ||
|
||
func TestGetSurveyQuestionsForVersion_AboveSemver3_2_0(t *testing.T) { | ||
version := semver.MustParse("3.3.0") | ||
questions := getInitialSurveyQuestionsForVersion(version) | ||
|
||
require.Len(t, questions, 4, "should return 4 questions for spec version > 3.2.0") | ||
|
||
assert.Equal(t, "subobjects", questions[3].Name) | ||
assert.IsType(t, &survey.Confirm{}, questions[3].Prompt) | ||
} | ||
|
||
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsFalseForSpecVersionBelow3_2_0(t *testing.T) { | ||
specVersion := semver.MustParse("3.1.0") | ||
answers := newDataStreamAnswers{ | ||
Name: "test_stream", | ||
Title: "Test Stream", | ||
Type: "logs", | ||
Subobjects: false, | ||
} | ||
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion) | ||
|
||
assert.Equal(t, "test_stream", descriptor.Manifest.Name) | ||
assert.Equal(t, "Test Stream", descriptor.Manifest.Title) | ||
assert.Equal(t, "logs", descriptor.Manifest.Type) | ||
assert.Equal(t, "/tmp/package", descriptor.PackageRoot) | ||
assert.Nil(t, descriptor.Manifest.Elasticsearch) | ||
} | ||
|
||
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsFalseForSpecVersionGTE3_2_0(t *testing.T) { | ||
specVersion := semver.MustParse("3.2.0") | ||
answers := newDataStreamAnswers{ | ||
Name: "test_stream", | ||
Title: "Test Stream", | ||
Type: "logs", | ||
Subobjects: false, | ||
} | ||
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion) | ||
|
||
require.NotNil(t, descriptor.Manifest.Elasticsearch) | ||
require.NotNil(t, descriptor.Manifest.Elasticsearch.IndexTemplate) | ||
require.NotNil(t, descriptor.Manifest.Elasticsearch.IndexTemplate.Mappings) | ||
assert.False(t, descriptor.Manifest.Elasticsearch.IndexTemplate.Mappings.Subobjects) | ||
} | ||
|
||
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsTrueForSpecVersionGTE3_2_0(t *testing.T) { | ||
specVersion := semver.MustParse("3.2.0") | ||
answers := newDataStreamAnswers{ | ||
Name: "test_stream", | ||
Title: "Test Stream", | ||
Type: "logs", | ||
Subobjects: true, | ||
} | ||
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion) | ||
|
||
require.Nil(t, descriptor.Manifest.Elasticsearch) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could unit test this function, checking the manifest it generates for given answers.
Further testing could be added to
internal/packages/archetype/data_stream_test.go
, but we wouldn't be really testing if the behaviour changes for the format version used in the package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some unit testing as suggested, also for the survey questions i've encapsulated this logic to at least check that the question is added or not based on the version.