-
Notifications
You must be signed in to change notification settings - Fork 295
fix(OpenGraph): Do not raise exceptions during on-start migrations BED-7480 #2411
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
Open
sirisjo
wants to merge
4
commits into
main
Choose a base branch
from
BED-7480
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+253
−33
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
190 changes: 190 additions & 0 deletions
190
cmd/api/src/database/migration/extensions_integration_test.go
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,190 @@ | ||
| // Copyright 2026 Specter Ops, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build integration | ||
|
|
||
| package migration_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/peterldowns/pgtestdb" | ||
| "gorm.io/gorm" | ||
|
|
||
| "github.com/specterops/bloodhound/cmd/api/src/auth" | ||
| "github.com/specterops/bloodhound/cmd/api/src/database" | ||
| "github.com/specterops/bloodhound/cmd/api/src/model" | ||
| "github.com/specterops/bloodhound/cmd/api/src/test/integration/utils" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type IntegrationTestSuite struct { | ||
| Context context.Context | ||
| BHDatabase *database.BloodhoundDB | ||
| DB *gorm.DB | ||
| } | ||
|
|
||
| func setupIntegrationTestSuite(t *testing.T) IntegrationTestSuite { | ||
| t.Helper() | ||
|
|
||
| var ( | ||
| ctx = context.Background() | ||
| connConf = pgtestdb.Custom(t, getPostgresConfig(t), pgtestdb.NoopMigrator{}) | ||
| gormDB *gorm.DB | ||
| db *database.BloodhoundDB | ||
| err error | ||
| ) | ||
|
|
||
| // #region Setup for dbs | ||
|
|
||
| gormDB, err = database.OpenDatabase(connConf.URL()) | ||
| require.NoError(t, err) | ||
|
|
||
| db = database.NewBloodhoundDB(gormDB, auth.NewIdentityResolver()) | ||
|
|
||
| err = db.Migrate(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| err = db.PopulateExtensionData(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // #endregion | ||
|
|
||
| return IntegrationTestSuite{ | ||
| Context: ctx, | ||
| BHDatabase: db, | ||
| DB: gormDB, | ||
| } | ||
| } | ||
|
|
||
| // getPostgresConfig reads key/value pairs from the default integration | ||
| // config file and creates a pgtestdb configuration object. | ||
| func getPostgresConfig(t *testing.T) pgtestdb.Config { | ||
| t.Helper() | ||
|
|
||
| config, err := utils.LoadIntegrationTestConfig() | ||
| require.NoError(t, err) | ||
|
|
||
| environmentMap := make(map[string]string) | ||
| for _, entry := range strings.Fields(config.Database.Connection) { | ||
| if parts := strings.SplitN(entry, "=", 2); len(parts) == 2 { | ||
| environmentMap[parts[0]] = parts[1] | ||
| } | ||
| } | ||
|
|
||
| if strings.HasPrefix(environmentMap["host"], "/") { | ||
| return pgtestdb.Config{ | ||
| DriverName: "pgx", | ||
| User: environmentMap["user"], | ||
| Password: environmentMap["password"], | ||
| Database: environmentMap["dbname"], | ||
| Options: fmt.Sprintf("host=%s", url.PathEscape(environmentMap["host"])), | ||
| TestRole: &pgtestdb.Role{ | ||
| Username: environmentMap["user"], | ||
| Password: environmentMap["password"], | ||
| Capabilities: "NOSUPERUSER NOCREATEROLE", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return pgtestdb.Config{ | ||
| DriverName: "pgx", | ||
| Host: environmentMap["host"], | ||
| Port: environmentMap["port"], | ||
| User: environmentMap["user"], | ||
| Password: environmentMap["password"], | ||
| Database: environmentMap["dbname"], | ||
| Options: "sslmode=disable", | ||
| ForceTerminateConnections: true, | ||
| } | ||
| } | ||
|
|
||
| func (s *IntegrationTestSuite) teardownIntegrationTestSuite(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| if s.BHDatabase != nil { | ||
| s.BHDatabase.Close(s.Context) | ||
| } | ||
| } | ||
|
|
||
| func TestExtensions_GetOnStartExtensionData(t *testing.T) { | ||
| var ( | ||
| testSuite = setupIntegrationTestSuite(t) | ||
| ) | ||
|
|
||
| defer testSuite.teardownIntegrationTestSuite(t) | ||
|
|
||
| err := testSuite.BHDatabase.PopulateExtensionData(testSuite.Context) | ||
| require.NoError(t, err) | ||
|
|
||
| // Validate Both Schema Extensions Exist | ||
| extensions, totalRecords, err := testSuite.BHDatabase.GetGraphSchemaExtensions(testSuite.Context, model.Filters{}, model.Sort{}, 0, 0) | ||
|
|
||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, 2, totalRecords) | ||
|
|
||
| for _, extension := range extensions { | ||
| require.True(t, extension.IsBuiltin, "All extensions should be marked as built-in") | ||
| // Validate Schema Environments Exist | ||
| schemaEnvironments, err := testSuite.BHDatabase.GetEnvironmentsByExtensionId(testSuite.Context, extension.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // There should only be one schema environment per built-in extension | ||
| require.Len(t, schemaEnvironments, 1) | ||
| schemaEnvironment := schemaEnvironments[0] | ||
|
|
||
| // Validate Source Kinds Exist | ||
| sourceKind, err := testSuite.BHDatabase.GetSourceKindByID(testSuite.Context, int(schemaEnvironment.SourceKindId)) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, sourceKind) | ||
| validateSourceKind(t, extension.Name, sourceKind.Name.String()) | ||
|
|
||
| // Validate Environment Kinds Exist | ||
| environmentKind, err := testSuite.BHDatabase.GetKindById(testSuite.Context, schemaEnvironment.EnvironmentKindId) | ||
| require.NoError(t, err) | ||
| validateEnvironmentKind(t, extension.Name, environmentKind.Name) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func validateSourceKind(t *testing.T, extensionName, sourceKindName string) { | ||
| t.Helper() | ||
| switch extensionName { | ||
| case "AD": | ||
| require.Equal(t, "Base", sourceKindName) | ||
| case "AZ": | ||
| require.Equal(t, "AZBase", sourceKindName) | ||
| default: | ||
| t.Fatalf("Invalid extension name %s", extensionName) | ||
| } | ||
| } | ||
|
|
||
| func validateEnvironmentKind(t *testing.T, extensionName, environmentKindName string) { | ||
| t.Helper() | ||
| switch extensionName { | ||
| case "AD": | ||
| require.Equal(t, "Domain", environmentKindName) | ||
| case "AZ": | ||
| require.Equal(t, "AZTenant", environmentKindName) | ||
| default: | ||
| t.Fatalf("Invalid extension name %s", extensionName) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Assert the exact extension names (
AD,AZ), not only record count.The current checks verify count and per-row shape, but they don’t explicitly enforce the expected extension name set. Add a final name-set assertion to make the regression test stricter.
✅ Tighten expectations
func TestExtensions_GetOnStartExtensionData(t *testing.T) { var ( - testSuite = setupIntegrationTestSuite(t) + testSuite = setupIntegrationTestSuite(t) + extensionNames = make([]string, 0, 2) ) @@ require.NoError(t, err) require.Equal(t, 2, totalRecords) + require.Len(t, extensions, 2) for _, extension := range extensions { + extensionNames = append(extensionNames, extension.Name) require.True(t, extension.IsBuiltin, "All extensions should be marked as built-in") @@ validateEnvironmentKind(t, extension.Name, environmentKind.Name) } + + require.ElementsMatch(t, []string{"AD", "AZ"}, extensionNames) }Also applies to: 142-164
🤖 Prompt for AI Agents