Skip to content

Commit

Permalink
fix: bug unable to decode GetJobSpecificationsResponse for jobs with …
Browse files Browse the repository at this point in the history
…hook (#478)

* fix: bug unable to decode GetJobSpecificationsResponse for jobs with hook
* refactor: unexport adapter models in optimus resourcemgr

Co-authored-by: Anwar Hidayat <anwarhidayat14@gmail.com>
  • Loading branch information
arinda-arif and irainia authored Jul 28, 2022
1 parent bc0a5eb commit 606b5d4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
14 changes: 7 additions & 7 deletions ext/resourcemgr/model.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package resourcemgr

type GetJobSpecificationsResponse struct {
JobSpecificationResponses []JobSpecificationResponse `json:"jobSpecificationResponses"`
type getJobSpecificationsResponse struct {
JobSpecificationResponses []jobSpecificationResponse `json:"jobSpecificationResponses"`
}

type JobSpecificationResponse struct {
type jobSpecificationResponse struct {
ProjectName string `json:"projectName"`
NamespaceName string `json:"namespaceName"`
Job JobSpecification `json:"job"`
Job jobSpecification `json:"job"`
}

type JobSpecification struct {
type jobSpecification struct {
Version int `json:"version"`
Name string `json:"name"`
Owner string `json:"owner"`
Expand Down Expand Up @@ -73,8 +73,8 @@ type behaviorRetry struct {
}

type jobSpecHook struct {
Name string `json:"name"`
Config jobConfigItem `json:"config"`
Name string `json:"name"`
Config []jobConfigItem `json:"config"`
}

type jobDependency struct {
Expand Down
6 changes: 3 additions & 3 deletions ext/resourcemgr/optimus.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (o *optimusResourceManager) GetOptimusDependencies(ctx context.Context, unr
return nil, fmt.Errorf("unexpected status response: %s", response.Status)
}

var jobSpecResponse GetJobSpecificationsResponse
var jobSpecResponse getJobSpecificationsResponse
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&jobSpecResponse); err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
Expand Down Expand Up @@ -97,15 +97,15 @@ func (o *optimusResourceManager) constructGetJobSpecificationsRequest(ctx contex
return request, nil
}

func (o *optimusResourceManager) toOptimusDependencies(responses []JobSpecificationResponse) []models.OptimusDependency {
func (o *optimusResourceManager) toOptimusDependencies(responses []jobSpecificationResponse) []models.OptimusDependency {
output := make([]models.OptimusDependency, len(responses))
for i, r := range responses {
output[i] = o.toOptimusDependency(r)
}
return output
}

func (o *optimusResourceManager) toOptimusDependency(response JobSpecificationResponse) models.OptimusDependency {
func (o *optimusResourceManager) toOptimusDependency(response jobSpecificationResponse) models.OptimusDependency {
return models.OptimusDependency{
Name: o.name,
Host: o.config.Host,
Expand Down
104 changes: 91 additions & 13 deletions ext/resourcemgr/optimus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resourcemgr_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -152,22 +151,101 @@ func (o *OptimusResourceManager) TestGetJobSpecifications() {
actualRawQuery := r.URL.RawQuery
o.EqualValues(expectedRawQuery, actualRawQuery)

getJobSpecificationsResonse := resourcemgr.GetJobSpecificationsResponse{
JobSpecificationResponses: []resourcemgr.JobSpecificationResponse{
{
ProjectName: "project",
NamespaceName: "namespace",
Job: resourcemgr.JobSpecification{
Name: "job",
},
},
getJobSpecificationResponse := `
{
"jobSpecificationResponses": [
{
"projectName": "project",
"namespaceName": "namespace",
"job": {
"version": 0,
"name": "job"
}
}
]
}`
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(getJobSpecificationResponse))
})

ctx := context.Background()
unresolvedDependency := models.UnresolvedJobDependency{
ProjectName: "project",
JobName: "job",
ResourceDestination: "resource",
}

expectedJobSpecifications := []models.OptimusDependency{
{
Name: "other-optimus",
Host: server.URL,
Headers: map[string]string{
"key": "value",
},
ProjectName: "project",
NamespaceName: "namespace",
JobName: "job",
},
}

actualOptimusDependencies, actualError := manager.GetOptimusDependencies(ctx, unresolvedDependency)

o.EqualValues(expectedJobSpecifications, actualOptimusDependencies)
o.NoError(actualError)
})

o.Run("should return job specifications with hook and nil if no error is encountered", func() {
router := mux.NewRouter()
server := httptest.NewServer(router)
defer server.Close()

conf := config.ResourceManager{
Name: "other-optimus",
Config: config.ResourceManagerConfigOptimus{
Host: server.URL,
Headers: map[string]string{
"key": "value",
},
}
content, _ := json.Marshal(getJobSpecificationsResonse)
},
}
manager, err := resourcemgr.NewOptimusResourceManager(conf)
if err != nil {
panic(err)
}

router.HandleFunc(apiPath, func(w http.ResponseWriter, r *http.Request) {
expectedHeaderValue := "value"
actualHeaderValue := r.Header.Get("key")
o.EqualValues(expectedHeaderValue, actualHeaderValue)

expectedRawQuery := "job_name=job&project_name=project&resource_destination=resource"
actualRawQuery := r.URL.RawQuery
o.EqualValues(expectedRawQuery, actualRawQuery)

getJobSpecificationResponse := `
{
"jobSpecificationResponses": [
{
"projectName": "project",
"namespaceName": "namespace",
"job": {
"version": 0,
"name": "job",
"hooks": [{
"name": "hook-1",
"config": [{
"name": "hook-1-config-1-key",
"value": "hook-1-config-1-value"
}]
}]
}
}
]
}`
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(content)
w.Write([]byte(getJobSpecificationResponse))
})

ctx := context.Background()
Expand Down

0 comments on commit 606b5d4

Please sign in to comment.