Skip to content
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

chartsvc: Unit and integration tests #329

Merged
merged 7 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions cmd/chartsvc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,39 @@ func listRepoCharts(w http.ResponseWriter, req *http.Request, params Params) {
func getChart(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var chart *models.Chart
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reasoning behind these pointer to value changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having trouble getting the mock interface match the expectation description. Simplest was to fix this was to remove the double reference.

mock: Unexpected Method Call
-----------------------------

One(**models.Chart)
		0: (**models.Chart)(0xc4200b41a8)

The closest call I have is: 

One(*models.Chart)
		0: &models.Chart{ID:"", Name:"", Repo:models.Repo{Name:"", URL:""}, Description:"", Home:"", Keywords:[]string(nil), Maintainers:[]models.Maintainer(nil), Sources:[]string(nil), Icon:"", RawIcon:[]uint8(nil), ChartVersions:[]models.ChartVersion(nil)}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm okay, I think the tests in monocular had a similar thing though, but it's not a big problem either way.

var chart models.Chart
chartID := fmt.Sprintf("%s/%s", params["repo"], params["chartName"])
if err := db.C(chartCollection).FindId(chartID).One(&chart); err != nil {
log.WithError(err).Errorf("could not find chart with id %s", chartID)
response.NewErrorResponse(http.StatusNotFound, "could not find chart").Write(w)
return
}

cr := newChartResponse(chart)
cr := newChartResponse(&chart)
response.NewDataResponse(cr).Write(w)
}

// listChartVersions returns a list of chart versions for the given chart
func listChartVersions(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var chart *models.Chart
var chart models.Chart
chartID := fmt.Sprintf("%s/%s", params["repo"], params["chartName"])
if err := db.C(chartCollection).FindId(chartID).One(&chart); err != nil {
log.WithError(err).Errorf("could not find chart with id %s", chartID)
response.NewErrorResponse(http.StatusNotFound, "could not find chart").Write(w)
return
}

cvl := newChartVersionListResponse(chart)
cvl := newChartVersionListResponse(&chart)
response.NewDataResponse(cvl).Write(w)
}

// getChartVersion returns the given chart version
func getChartVersion(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var chart *models.Chart
var chart models.Chart
chartID := fmt.Sprintf("%s/%s", params["repo"], params["chartName"])
if err := db.C(chartCollection).Find(bson.M{
"_id": chartID,
Expand All @@ -141,15 +141,15 @@ func getChartVersion(w http.ResponseWriter, req *http.Request, params Params) {
return
}

cvr := newChartVersionResponse(chart, chart.ChartVersions[0])
cvr := newChartVersionResponse(&chart, chart.ChartVersions[0])
response.NewDataResponse(cvr).Write(w)
}

// getChartIcon returns the icon for a given chart
func getChartIcon(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var chart *models.Chart
var chart models.Chart
chartID := fmt.Sprintf("%s/%s", params["repo"], params["chartName"])
if err := db.C(chartCollection).FindId(chartID).One(&chart); err != nil {
log.WithError(err).Errorf("could not find chart with id %s", chartID)
Expand All @@ -165,11 +165,11 @@ func getChartIcon(w http.ResponseWriter, req *http.Request, params Params) {
w.Write(chart.RawIcon)
}

// getChartVersionReadme returns the README and values.yaml for a given chart
// getChartVersionReadme returns the README for a given chart
func getChartVersionReadme(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var files *models.ChartFiles
var files models.ChartFiles
fileID := fmt.Sprintf("%s/%s-%s", params["repo"], params["chartName"], params["version"])
if err := db.C(filesCollection).FindId(fileID).One(&files); err != nil {
log.WithError(err).Errorf("could not find files with id %s", fileID)
Expand All @@ -184,7 +184,7 @@ func getChartVersionReadme(w http.ResponseWriter, req *http.Request, params Para
func getChartVersionValues(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()
var files *models.ChartFiles
var files models.ChartFiles
fileID := fmt.Sprintf("%s/%s-%s", params["repo"], params["chartName"], params["version"])
if err := db.C(filesCollection).FindId(fileID).One(&files); err != nil {
log.WithError(err).Errorf("could not find values.yaml with id %s", fileID)
Expand All @@ -201,11 +201,11 @@ func newChartResponse(c *models.Chart) *apiResponse {
Type: "chart",
ID: c.ID,
Attributes: chartAttributes(*c),
Links: selfLink{"/v1/charts/" + c.ID},
Links: selfLink{pathPrefix + "/charts/" + c.ID},
Relationships: relMap{
"latestChartVersion": rel{
Data: chartVersionAttributes(c.ID, latestCV),
Links: selfLink{"/v1/charts/" + c.ID + "/versions/" + latestCV.Version},
Links: selfLink{pathPrefix + "/charts/" + c.ID + "/versions/" + latestCV.Version},
},
},
}
Expand All @@ -221,14 +221,14 @@ func newChartListResponse(charts []*models.Chart) apiListResponse {
}

func chartVersionAttributes(cid string, cv models.ChartVersion) models.ChartVersion {
cv.Readme = "/v1/assets/" + cid + "/versions/" + cv.Version + "/README.md"
cv.Values = "/v1/assets/" + cid + "/versions/" + cv.Version + "/values.yaml"
cv.Readme = pathPrefix + "/assets/" + cid + "/versions/" + cv.Version + "/README.md"
cv.Values = pathPrefix + "/assets/" + cid + "/versions/" + cv.Version + "/values.yaml"
return cv
}

func chartAttributes(c models.Chart) models.Chart {
if c.RawIcon != nil {
c.Icon = "/v1/assets/" + c.ID + "/logo-160x160-fit.png"
c.Icon = pathPrefix + "/assets/" + c.ID + "/logo-160x160-fit.png"
} else {
// If the icon wasn't processed, it is either not set or invalid
c.Icon = ""
Expand All @@ -241,11 +241,11 @@ func newChartVersionResponse(c *models.Chart, cv models.ChartVersion) *apiRespon
Type: "chartVersion",
ID: fmt.Sprintf("%s-%s", c.ID, cv.Version),
Attributes: chartVersionAttributes(c.ID, cv),
Links: selfLink{"/v1/charts/" + c.ID + "/versions/" + cv.Version},
Links: selfLink{pathPrefix + "/charts/" + c.ID + "/versions/" + cv.Version},
Relationships: relMap{
"chart": rel{
Data: chartAttributes(*c),
Links: selfLink{"/v1/charts/" + c.ID},
Links: selfLink{pathPrefix + "/charts/" + c.ID},
},
},
}
Expand Down
Loading