Skip to content

Commit

Permalink
Prevent crash when the document kind is unknown (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexashley authored Aug 6, 2021
1 parent 29ac755 commit 8196e24
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion indexmanager/index_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (ir *indexRepository) CreateIndex(ctx context.Context, indexName, aliasName
return fmt.Errorf("unexpected status code after creating index: %d", res.StatusCode)
}

log.Info("index created", zap.String("index", indexName))
log.Info("index created")

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions indexmanager/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func NewMigrator(
}

func (m *migrator) GetMigrations(ctx context.Context) ([]*Migration, error) {
log := m.logger.Named("GetMigrations")
res, err := m.client.Indices.Get([]string{ElasticsearchAllIndices}, m.client.Indices.Get.WithContext(ctx))
if err := getErrorFromESResponse(res, err); err != nil {
return nil, err
Expand All @@ -79,6 +80,11 @@ func (m *migrator) GetMigrations(ctx context.Context) ([]*Migration, error) {
}

indexParts := m.registry.ParseIndexName(indexName)
if indexParts == nil {
log.Warn("Discovered index matching criteria, but wasn't able to determine document kind.", zap.String("index", indexName))
continue
}

currentVersion := m.registry.Version(indexParts.DocumentKind)

if indexParts.Version == currentVersion {
Expand Down
11 changes: 11 additions & 0 deletions indexmanager/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ var _ = Describe("Migrator", func() {
})
})

When("an index matches the criteria but isn't in the registry", func() {
BeforeEach(func() {
mockRegistry.ParseIndexNameReturns(nil)
})

It("should not return an error", func() {
Expect(actualMigrations).To(BeEmpty())
Expect(actualError).NotTo(HaveOccurred())
})
})

When("an error occurs fetching indices", func() {
BeforeEach(func() {
mockTransport.preparedHttpResponses[0] = &http.Response{
Expand Down
1 change: 1 addition & 0 deletions indexmanager/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type MappingsRegistry interface {
// Mapping returns the current versioned mapping for the document kind.
Mapping(documentKind string) *VersionedMapping
// ParseIndexName determines the version, document kind, and inner name of an index.
// If the document kind cannot be determined, nil is returned
ParseIndexName(indexName string) *IndexName
}

Expand Down
10 changes: 10 additions & 0 deletions indexmanager/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ var _ = Describe("MappingsRegistry", func() {
Expect(actualIndexName.Inner).To(Equal("long-inner-name"))
})
})

When("the document kind is unknown", func() {
BeforeEach(func() {
indexName = "rode-v1alpha1-foo"
})

It("should indicate that the document kind isn't in the registry", func() {
Expect(actualIndexName).To(BeNil())
})
})
})
})

Expand Down

0 comments on commit 8196e24

Please sign in to comment.