Skip to content
This repository has been archived by the owner on Oct 11, 2019. It is now read-only.

Commit

Permalink
Removed CallSummary from generated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petemoore committed May 25, 2016
1 parent 8d0b9d8 commit 73f0ac7
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 115 deletions.
2 changes: 1 addition & 1 deletion codegenerator/model-data.txt
@@ -1,4 +1,4 @@
Generated: 1464163779
Generated: 1464177765
The following file is an auto-generated static dump of the API models at time of code generation.
It is provided here for reference purposes, but is not used by any code.

Expand Down
17 changes: 10 additions & 7 deletions codegenerator/model/api.go
Expand Up @@ -14,6 +14,7 @@ import (
//
//////////////////////////////////////////////////////////////////

// API represents the HTTP interface of a TaskCluster service
type API struct {
BaseURL string `json:"baseUrl"`
Description string `json:"description"`
Expand All @@ -26,7 +27,7 @@ type API struct {
}

func (api *API) String() string {
var result string = fmt.Sprintf(
result := fmt.Sprintf(
"Version = '%v'\n"+
"Schema = '%v'\n"+
"Title = '%v'\n"+
Expand Down Expand Up @@ -193,6 +194,8 @@ func (api *API) setAPIDefinition(apiDef *APIDefinition) {
api.apiDef = apiDef
}

// APIEntry represents an individual HTTP API call
// of a TaskCluster service
type APIEntry struct {
Args []string `json:"args"`
Description string `json:"description"`
Expand Down Expand Up @@ -302,21 +305,21 @@ func (entry *APIEntry) generateDirectMethod(apiName string) string {
}
}

responseType := "(*tcclient.CallSummary, error)"
responseType := "error"
if entry.Output != "" {
responseType = "(*" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + ", *tcclient.CallSummary, error)"
responseType = "(*" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + ", error)"
}

content := comment
content += "func (" + entry.Parent.apiDef.ExampleVarName + " *" + entry.Parent.apiDef.Name + ") " + entry.MethodName + "(" + inputParams + ") " + responseType + " {\n"
content += queryCode
content += "\tcd := tcclient.ConnectionData(*" + entry.Parent.apiDef.ExampleVarName + ")\n"
if entry.Output != "" {
content += "\tresponseObject, callSummary, err := (&cd).APICall(" + apiArgsPayload + ", \"" + strings.ToUpper(entry.Method) + "\", \"" + strings.Replace(strings.Replace(entry.Route, "<", "\" + url.QueryEscape(", -1), ">", ") + \"", -1) + "\", new(" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + "), " + queryExpr + ")\n"
content += "\treturn responseObject.(*" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + "), callSummary, err\n"
content += "\tresponseObject, _, err := (&cd).APICall(" + apiArgsPayload + ", \"" + strings.ToUpper(entry.Method) + "\", \"" + strings.Replace(strings.Replace(entry.Route, "<", "\" + url.QueryEscape(", -1), ">", ") + \"", -1) + "\", new(" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + "), " + queryExpr + ")\n"
content += "\treturn responseObject.(*" + entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName + "), err\n"
} else {
content += "\t_, callSummary, err := (&cd).APICall(" + apiArgsPayload + ", \"" + strings.ToUpper(entry.Method) + "\", \"" + strings.Replace(strings.Replace(entry.Route, "<", "\" + url.QueryEscape(", -1), ">", ") + \"", -1) + "\", nil, " + queryExpr + ")\n"
content += "\treturn callSummary, err\n"
content += "\t_, _, err := (&cd).APICall(" + apiArgsPayload + ", \"" + strings.ToUpper(entry.Method) + "\", \"" + strings.Replace(strings.Replace(entry.Route, "<", "\" + url.QueryEscape(", -1), ">", ") + \"", -1) + "\", nil, " + queryExpr + ")\n"
content += "\treturn err\n"
}
content += "}\n"
content += "\n"
Expand Down
7 changes: 5 additions & 2 deletions codegenerator/model/exchange.go
Expand Up @@ -12,6 +12,7 @@ import (
//
////////////////////////////////////////////////////////////////////////

// Exchange represents the set of AMQP interfaces for a TaskCluster service
type Exchange struct {
Description string `json:"description"`
Entries []ExchangeEntry `json:"entries"`
Expand All @@ -24,7 +25,7 @@ type Exchange struct {
}

func (exchange *Exchange) String() string {
var result string = fmt.Sprintf(
result := fmt.Sprintf(
"Version = '%v'\n"+
"Schema = '%v'\n"+
"Title = '%v'\n"+
Expand All @@ -50,6 +51,7 @@ func (exchange *Exchange) setAPIDefinition(apiDef *APIDefinition) {
exchange.apiDef = apiDef
}

// ExchangeEntry represents a single AMQP interface of a TaskCluster service
type ExchangeEntry struct {
Description string `json:"description"`
Exchange string `json:"exchange"`
Expand All @@ -67,7 +69,7 @@ func (entry *ExchangeEntry) postPopulate(apiDef *APIDefinition) {
}

func (entry *ExchangeEntry) String() string {
var result string = fmt.Sprintf(
result := fmt.Sprintf(
" Entry Type = '%v'\n"+
" Entry Exchange = '%v'\n"+
" Entry Name = '%v'\n"+
Expand All @@ -82,6 +84,7 @@ func (entry *ExchangeEntry) String() string {
return result
}

// RouteElement represents an element of am AMQP routing key
type RouteElement struct {
Constant string `json:"constant"`
MultipleWords bool `json:"multipleWords"`
Expand Down
34 changes: 19 additions & 15 deletions codegenerator/model/model.go
Expand Up @@ -29,13 +29,17 @@ var (
downloadedTime time.Time
)

// SortedAPIDefs is a sorted array of APIDefinitions
type SortedAPIDefs []APIDefinition

// needed so that SortedAPIDefs can implement sort.Interface
func (a SortedAPIDefs) Len() int { return len(a) }
func (a SortedAPIDefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortedAPIDefs) Less(i, j int) bool { return a[i].URL < a[j].URL }

// APIModel represents an abstract structured model of an API
// Currently there are two implementations - HTTP APIs and
// AMQP APIs.
type APIModel interface {
String() string
postPopulate(apiDef *APIDefinition)
Expand Down Expand Up @@ -65,11 +69,11 @@ func exitOnFail(err error) {
}
}

func (a *APIDefinition) generateAPICode() string {
return a.Data.generateAPICode(a.Name)
func (apiDef *APIDefinition) generateAPICode() string {
return apiDef.Data.generateAPICode(apiDef.Name)
}

func (apiDef *APIDefinition) loadJson(reader io.Reader) {
func (apiDef *APIDefinition) loadJSON(reader io.Reader) {
b := new(bytes.Buffer)
_, err := b.ReadFrom(reader)
exitOnFail(err)
Expand Down Expand Up @@ -105,10 +109,10 @@ func (apiDef *APIDefinition) loadJson(reader io.Reader) {
//
// When LoadAPIs returns, all json schemas and sub schemas should have been
// read and unmarhsalled into go objects.
func LoadAPIs(apiManifestUrl, supplementaryDataFile string) []APIDefinition {
resp, err := http.Get(apiManifestUrl)
func LoadAPIs(apiManifestURL, supplementaryDataFile string) []APIDefinition {
resp, err := http.Get(apiManifestURL)
if err != nil {
fmt.Printf("Could not download api manifest from url: '%v'!\n", apiManifestUrl)
fmt.Printf("Could not download api manifest from url: '%v'!\n", apiManifestURL)
}
exitOnFail(err)
supDataReader, err := os.Open(supplementaryDataFile)
Expand Down Expand Up @@ -137,15 +141,15 @@ func LoadAPIs(apiManifestUrl, supplementaryDataFile string) []APIDefinition {
} else {
fmt.Printf(
"\nFATAL: Manifest from url '%v' contains key '%v' with url '%v', but this url does not exist in supplementary data file '%v', therefore exiting...\n\n",
apiManifestUrl, i, apiMan[i], supplementaryDataFile)
apiManifestURL, i, apiMan[i], supplementaryDataFile)
os.Exit(64)
}
}
for i := range apiDefs {
if apiDefs[i].Name == "" {
fmt.Printf(
"\nFATAL: Manifest from url '%v' does not contain url '%v' which does exist in supplementary data file '%v', therefore exiting...\n\n",
apiManifestUrl, apiDefs[i].URL, supplementaryDataFile)
apiManifestURL, apiDefs[i].URL, supplementaryDataFile)
os.Exit(65)
}
}
Expand All @@ -155,23 +159,23 @@ func LoadAPIs(apiManifestUrl, supplementaryDataFile string) []APIDefinition {
resp, err = http.Get(apiDefs[i].URL)
exitOnFail(err)
defer resp.Body.Close()
apiDefs[i].loadJson(resp.Body)
apiDefs[i].loadJSON(resp.Body)

// check that the json schema is valid!
validateJson(apiDefs[i].SchemaURL, apiDefs[i].URL)
validateJSON(apiDefs[i].SchemaURL, apiDefs[i].URL)
}
return apiDefs
}

func validateJson(schemaUrl, docUrl string) {
schemaLoader := gojsonschema.NewReferenceLoader(schemaUrl)
docLoader := gojsonschema.NewReferenceLoader(docUrl)
func validateJSON(schemaURL, docURL string) {
schemaLoader := gojsonschema.NewReferenceLoader(schemaURL)
docLoader := gojsonschema.NewReferenceLoader(docURL)
result, err := gojsonschema.Validate(schemaLoader, docLoader)
exitOnFail(err)
if result.Valid() {
fmt.Printf("Document '%v' is valid against '%v'.\n", docUrl, schemaUrl)
fmt.Printf("Document '%v' is valid against '%v'.\n", docURL, schemaURL)
} else {
fmt.Printf("Document '%v' is INVALID against '%v'.\n", docUrl, schemaUrl)
fmt.Printf("Document '%v' is INVALID against '%v'.\n", docURL, schemaURL)
for _, desc := range result.Errors() {
fmt.Println("")
fmt.Printf("- %s\n", desc)
Expand Down
2 changes: 1 addition & 1 deletion integrationtest/doc.go
@@ -1,2 +1,2 @@
// This package stores all the integration tests that run against the taskcluster cluster client
// Package integrationtest stores all the integration tests that run against the taskcluster cluster client
package integrationtest
36 changes: 16 additions & 20 deletions integrationtest/integration_test.go
@@ -1,10 +1,9 @@
package integrationtest

import (
"bytes"
"encoding/json"
"net/url"
"os"
"regexp"
"testing"
"time"

Expand All @@ -25,12 +24,12 @@ func TestFindLatestBuildbotTask(t *testing.T) {
creds := &tcclient.Credentials{}
Index := index.New(creds)
Queue := queue.New(creds)
itr, _, err := Index.FindTask("buildbot.branches.mozilla-central.linux64.l10n")
itr, err := Index.FindTask("buildbot.branches.mozilla-central.linux64.l10n")
if err != nil {
t.Fatalf("%v\n", err)
}
taskID := itr.TaskID
td, _, err := Queue.Task(taskID)
td, err := Queue.Task(taskID)
if err != nil {
t.Fatalf("%v\n", err)
}
Expand All @@ -56,11 +55,11 @@ func TestFindLatestBuildbotTask(t *testing.T) {

func permaCreds(t *testing.T) *tcclient.Credentials {
permaCreds := &tcclient.Credentials{
ClientId: os.Getenv("TASKCLUSTER_CLIENT_ID"),
ClientID: os.Getenv("TASKCLUSTER_CLIENT_ID"),
AccessToken: os.Getenv("TASKCLUSTER_ACCESS_TOKEN"),
Certificate: os.Getenv("TASKCLUSTER_CERTIFICATE"),
}
if permaCreds.ClientId == "" || permaCreds.AccessToken == "" {
if permaCreds.ClientID == "" || permaCreds.AccessToken == "" {
t.Skip("Skipping test TestDefineTask since TASKCLUSTER_CLIENT_ID and/or TASKCLUSTER_ACCESS_TOKEN env vars not set")
}
return permaCreds
Expand Down Expand Up @@ -110,23 +109,21 @@ func TestDefineTask(t *testing.T) {
WorkerType: "win2008-worker",
}

tsr, cs, err := myQueue.DefineTask(taskID, td)
cd := tcclient.ConnectionData(*myQueue)
resp, cs, err := (&cd).APICall(td, "POST", "/task/"+url.QueryEscape(taskID)+"/define", new(queue.TaskStatusResponse), nil)
tsr := resp.(*queue.TaskStatusResponse)

//////////////////////////////////
// And now validate results.... //
//////////////////////////////////

if err != nil {
b := bytes.Buffer{}
cs.HttpRequest.Header.Write(&b)
headers := regexp.MustCompile(`(mac|nonce)="[^"]*"`).ReplaceAllString(b.String(), `$1="***********"`)
t.Logf("\n\nRequest sent:\n\nURL: %s\nMethod: %s\nHeaders:\n%v\nBody: %s", cs.HttpRequest.URL, cs.HttpRequest.Method, headers, cs.HttpRequestBody)
t.Fatalf("\n\nResponse received:\n\n%s", err)
t.Fatalf("%s", err)
}

t.Logf("Task https://queue.taskcluster.net/v1/task/%v created successfully", taskID)

if provisionerID := cs.HttpRequestObject.(*queue.TaskDefinitionRequest).ProvisionerID; provisionerID != "win-provisioner" {
if provisionerID := cs.HTTPRequestObject.(*queue.TaskDefinitionRequest).ProvisionerID; provisionerID != "win-provisioner" {
t.Errorf("provisionerId 'win-provisioner' expected but got %s", provisionerID)
}
if schedulerID := tsr.Status.SchedulerID; schedulerID != "go-test-test-scheduler" {
Expand All @@ -138,11 +135,11 @@ func TestDefineTask(t *testing.T) {
if state := tsr.Status.State; state != "unscheduled" {
t.Errorf("Expected 'state' to be 'unscheduled', but got %s", state)
}
submittedPayload := cs.HttpRequestBody
submittedPayload := cs.HTTPRequestBody

// only the contents is relevant below - the formatting and order of properties does not matter
// since a json comparison is done, not a string comparison...
expectedJson := []byte(`
expectedJSON := []byte(`
{
"created": "` + created.UTC().Format("2006-01-02T15:04:05.000Z") + `",
"deadline": "` + deadline.UTC().Format("2006-01-02T15:04:05.000Z") + `",
Expand Down Expand Up @@ -190,9 +187,9 @@ func TestDefineTask(t *testing.T) {
}
`)

jsonCorrect, formattedExpected, formattedActual, err := jsontest.JsonEqual(expectedJson, []byte(submittedPayload))
jsonCorrect, formattedExpected, formattedActual, err := jsontest.JsonEqual(expectedJSON, []byte(submittedPayload))
if err != nil {
t.Fatalf("Exception thrown formatting json data!\n%s\n\nStruggled to format either:\n%s\n\nor:\n\n%s", err, string(expectedJson), submittedPayload)
t.Fatalf("Exception thrown formatting json data!\n%s\n\nStruggled to format either:\n%s\n\nor:\n\n%s", err, string(expectedJSON), submittedPayload)
}

if !jsonCorrect {
Expand All @@ -208,9 +205,8 @@ func TestDefineTask(t *testing.T) {
t.Fatalf("Exception thrown generating temporary credentials!\n\n%s\n\n", err)
}
myQueue = queue.New(tempCreds)
_, cs, err = myQueue.CancelTask(taskID)
_, err = myQueue.CancelTask(taskID)
if err != nil {
t.Logf("Exception thrown cancelling task with temporary credentials!\n\n%s\n\n", err)
t.Fatalf("\n\n%s\n", cs.HttpRequest.Header)
t.Fatalf("Exception thrown cancelling task with temporary credentials!\n\n%s\n\n", err)
}
}

0 comments on commit 73f0ac7

Please sign in to comment.