Skip to content

Commit

Permalink
fix: bug external sensor created when internal sensor with same urn a…
Browse files Browse the repository at this point in the history
…lready resolved (#466)
  • Loading branch information
arinda-arif authored Jul 25, 2022
1 parent b49ae5a commit ea50e90
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
105 changes: 105 additions & 0 deletions job/dependency_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,7 @@ func TestDependencyResolver(t *testing.T) {
assert.Equal(t, unknownDependencies, actualUnknownDependencies)
assert.NoError(t, actualError)
})

t.Run("return job specs with their external dependencies info, unknown dependencies (external) and nil if no error is encountered", func(t *testing.T) {
externalDependencyResolver := new(mock.ExternalDependencyResolver)
externalDependencyResolver.AssertExpectations(t)
Expand Down Expand Up @@ -1618,5 +1619,109 @@ func TestDependencyResolver(t *testing.T) {
assert.Equal(t, unknownDependencies, actualUnknownDependencies)
assert.NoError(t, actualError)
})

t.Run("return job specs with only internal dependencies, nil unknown dependencies and nil error all internal dependencies are already resolved", func(t *testing.T) {
externalDependencyResolver := new(mock.ExternalDependencyResolver)
externalDependencyResolver.AssertExpectations(t)

jobSourceRepo := new(mock.JobSourceRepository)
jobSourceRepo.AssertExpectations(t)

basePlugin := &mock.BasePlugin{}
jobSpecRepo := mock.NewJobSpecRepository(t)
dependencyResolver := job.NewDependencyResolver(jobSpecRepo, jobSourceRepo, nil, nil, externalDependencyResolver)

ctx := context.Background()
projectID := models.ProjectID(uuid.New())

projectSpec := models.ProjectSpec{
ID: models.ProjectID(uuid.New()),
Name: "project",
}
namespaceSpec := models.NamespaceSpec{
ID: uuid.New(),
Name: "namespace",
ProjectSpec: projectSpec,
}
jobSpec := models.JobSpec{
ID: uuid.New(),
Name: "job",
Hooks: []models.JobSpecHook{
{
Unit: &models.Plugin{
Base: basePlugin,
},
},
},
NamespaceSpec: namespaceSpec,
}
basePlugin.On("PluginInfo").Return(&models.PluginInfoResponse{
Name: "plugin-c",
DependsOn: []string{"plugin-c"},
}, nil)

destination := "inferred-dependency"
inferredDependenciesPerJobID := map[uuid.UUID][]models.JobSpec{
jobSpec.ID: {
models.JobSpec{
Name: "dependency",
ResourceDestination: destination,
NamespaceSpec: namespaceSpec,
},
},
}

jobSpecRepo.On("GetAllByProjectID", ctx, projectID).Return([]models.JobSpec{jobSpec}, nil)
jobSpecRepo.On("GetStaticDependenciesPerJobID", ctx, projectID).Return(make(map[uuid.UUID][]models.JobSpec), nil)
jobSpecRepo.On("GetInferredDependenciesPerJobID", ctx, projectID).Return(inferredDependenciesPerJobID, nil)

staticExternalDependencies := make(map[string]models.ExternalDependency)
resourceURNsPerJobID := map[uuid.UUID][]string{
jobSpec.ID: {destination},
}
inferredExternalDependencies := make(map[string]models.ExternalDependency)

unresolvedStaticJobDependencyPerJobName := make(map[string][]models.UnresolvedJobDependency)
externalDependencyResolver.On("FetchStaticExternalDependenciesPerJobName", ctx, unresolvedStaticJobDependencyPerJobName).Return(staticExternalDependencies, []models.UnknownDependency{}, nil)

jobSourceRepo.On("GetResourceURNsPerJobID", ctx, projectID).Return(resourceURNsPerJobID, nil)

unresolvedInferredJobDependencyPerJobName := make(map[string][]models.UnresolvedJobDependency)
externalDependencyResolver.On("FetchInferredExternalDependenciesPerJobName", ctx, unresolvedInferredJobDependencyPerJobName).Return(inferredExternalDependencies, nil)

expectedJobSpecs := []models.JobSpec{
{
ID: jobSpec.ID,
Name: "job",
Hooks: []models.JobSpecHook{
{
Unit: &models.Plugin{
Base: basePlugin,
},
DependsOn: []*models.JobSpecHook{
{
Unit: &models.Plugin{
Base: basePlugin,
},
},
},
},
},
Dependencies: map[string]models.JobSpecDependency{
projectSpec.Name + "/" + inferredDependenciesPerJobID[jobSpec.ID][0].Name: {
Project: &projectSpec,
Job: &inferredDependenciesPerJobID[jobSpec.ID][0],
},
},
NamespaceSpec: namespaceSpec,
},
}

actualJobSpecs, actualUnknownDependencies, actualError := dependencyResolver.GetJobSpecsWithDependencies(ctx, projectID)

assert.EqualValues(t, expectedJobSpecs, actualJobSpecs)
assert.Empty(t, actualUnknownDependencies)
assert.NoError(t, actualError)
})
})
}
16 changes: 9 additions & 7 deletions store/postgres/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,10 @@ func (adapt JobSpecAdapter) ToJobRun(jr JobRun) (models.JobRun, models.Namespace
type jobDependency struct {
JobID uuid.UUID `json:"job_id"`

DependencyID uuid.UUID `json:"dependency_id"`
DependencyName string `json:"dependency_name"`
DependencyTaskName string `json:"dependency_task_name"`
DependencyID uuid.UUID `json:"dependency_id"`
DependencyName string `json:"dependency_name"`
DependencyTaskName string `json:"dependency_task_name"`
DependencyDestination string `json:"dependency_destination"`

DependencyProjectID uuid.UUID `json:"dependency_project_id"`
DependencyProject Project `gorm:"foreignKey:DependencyProjectID"`
Expand Down Expand Up @@ -567,10 +568,11 @@ func (adapt JobSpecAdapter) dependencyToJobSpec(conf jobDependency) (models.JobS
}

job := models.JobSpec{
ID: conf.DependencyID,
Name: conf.DependencyName,
Task: models.JobSpecTask{Unit: execUnit},
NamespaceSpec: namespaceSpec,
ID: conf.DependencyID,
Name: conf.DependencyName,
ResourceDestination: conf.DependencyDestination,
Task: models.JobSpecTask{Unit: execUnit},
NamespaceSpec: namespaceSpec,
}
return job, nil
}
1 change: 1 addition & 0 deletions store/postgres/job_spec_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (repo jobSpecRepository) GetInferredDependenciesPerJobID(ctx context.Contex
"j.id as dependency_id, "+
"j.name as dependency_name, "+
"j.task_name as dependency_task_name, "+
"j.destination as dependency_destination, "+
"j.namespace_id as dependency_namespace_id, "+
"j.project_id as dependency_project_id").
Joins("join job j on js.resource_urn = j.destination").
Expand Down
1 change: 1 addition & 0 deletions store/postgres/job_spec_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ func TestJobSpecRepository(t *testing.T) {
assert.EqualValues(t, []string{intraDependency.Name, interDependency.Name}, []string{checkModel[jobWithDependency.ID][0].Name, checkModel[jobWithDependency.ID][1].Name})
assert.EqualValues(t, []string{intraDependency.NamespaceSpec.Name, interDependency.NamespaceSpec.Name}, []string{checkModel[jobWithDependency.ID][0].NamespaceSpec.Name, checkModel[jobWithDependency.ID][1].NamespaceSpec.Name})
assert.EqualValues(t, []string{intraDependency.GetProjectSpec().Name, interDependency.GetProjectSpec().Name}, []string{checkModel[jobWithDependency.ID][0].GetProjectSpec().Name, checkModel[jobWithDependency.ID][1].GetProjectSpec().Name})
assert.EqualValues(t, []string{jobDestination1, jobDestination2}, []string{checkModel[jobWithDependency.ID][0].ResourceDestination, checkModel[jobWithDependency.ID][1].ResourceDestination})
assert.EqualValues(t, []string{gTask, gTask}, []string{checkModel[jobWithDependency.ID][0].Task.Unit.Info().Name, checkModel[jobWithDependency.ID][1].Task.Unit.Info().Name})
assert.Nil(t, err)
})
Expand Down

0 comments on commit ea50e90

Please sign in to comment.