diff --git a/.travis.yml b/.travis.yml index adda71e..0fa6750 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,3 +26,8 @@ script: --deadline=4m ./spew | tee /dev/stderr)" - make test - make CI-Coverage +deploy: + provider: script + script: curl -X POST https://goreportcard.com/checks -F 'repo=github.com/ManageIQ-Exchange/manageiq-exchange-cli-go' + on: + branch: master diff --git a/src/manageiq-exchange/api/api.go b/src/manageiq-exchange/api/api.go index 35e4906..e06f018 100644 --- a/src/manageiq-exchange/api/api.go +++ b/src/manageiq-exchange/api/api.go @@ -1,7 +1,9 @@ package api import ( + "bufio" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -10,10 +12,8 @@ import ( user "manageiq-exchange/models/user" "net" "net/http" - "time" - "bufio" "strconv" - "errors" + "time" ) var netTransport = &http.Transport{ @@ -28,25 +28,25 @@ var netClient = &http.Client{ Transport: netTransport, } -type Api struct { +type API struct { Server string Port int Client *http.Client - Data DataApi + Data DataAPI } -type DataApi struct { +type DataAPI struct { Data interface{} `json:"data"` Meta meta.Metadata `json:"meta"` } -func (a *Api) Init(server string, port int) { +func (a *API) Init(server string, port int) { a.Server = server a.Port = port a.Client = netClient } -func (a *Api) CheckConnectionServer() bool { +func (a *API) CheckConnectionServer() bool { conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", a.Server, strconv.Itoa(a.Port))) if err != nil { fmt.Printf("Fatal error: %s\n", err.Error()) @@ -62,7 +62,7 @@ func (a *Api) CheckConnectionServer() bool { return true } -func (a *Api) URL() string { +func (a *API) URL() string { url := fmt.Sprintf("http://%s", a.Server) if a.Port > 0 { url += fmt.Sprintf(":%d", a.Port) @@ -70,14 +70,18 @@ func (a *Api) URL() string { return url } -func (a *Api) GetInfo() info.Info { - a.Request("GET", "", nil) +func (a *API) GetInfo() info.Info { + err := a.Request("GET", "", nil) + if err != nil { + fmt.Printf("%+v", err) + return info.Info{} + } var info info.Info info.Init(a.Data.Data.(map[string]interface{})) return info } -func (a *Api) GetUsers(expand bool) user.UserCollection { +func (a *API) GetUsers(expand bool) user.UserCollection { var path string if path = "/v1/users"; expand { path = "/v1/users?expand=resources" @@ -85,33 +89,33 @@ func (a *Api) GetUsers(expand bool) user.UserCollection { err := a.Request("GET", path, nil) if err != nil { fmt.Printf("%+v", err) + return user.UserCollection{} } var users user.UserCollection users.Init(a.Data.Data.([]interface{})) return users } -func (a *Api) Request(method string, path string, data io.Reader) error { +func (a *API) Request(method string, path string, data io.Reader) error { req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", a.URL(), path), data) if err != nil { return err } resp, err := a.Client.Do(req) - defer resp.Body.Close() if err != nil { return err } if resp.StatusCode != 200 { - return errors.New(strconv.Itoa(resp.StatusCode)) - } else { - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - jsonErr := json.Unmarshal(body, &a.Data) - if jsonErr != nil { - return jsonErr - } + return errors.New(fmt.Sprintf("Error status code: %v", resp.StatusCode)) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + jsonErr := json.Unmarshal(body, &a.Data) + if jsonErr != nil { + return jsonErr } return nil } diff --git a/src/manageiq-exchange/api/api_test.go b/src/manageiq-exchange/api/api_test.go index 4dd3bff..7145d99 100644 --- a/src/manageiq-exchange/api/api_test.go +++ b/src/manageiq-exchange/api/api_test.go @@ -1,16 +1,16 @@ package api import ( + "fmt" + "manageiq-exchange/models/info" + meta "manageiq-exchange/models/metadata" + user "manageiq-exchange/models/user" "net/http" "net/http/httptest" "net/url" "reflect" "strconv" "testing" - "fmt" - meta "manageiq-exchange/models/metadata" - user "manageiq-exchange/models/user" - "manageiq-exchange/models/info" ) var ( @@ -18,7 +18,7 @@ var ( mux *http.ServeMux // client is the client being tested. - client *Api + client *API // urlTest is the url test server urlTest *url.URL @@ -38,7 +38,7 @@ func setup() { // http client configured to use test server urlTest, _ = url.Parse(server.URL + "/") i, _ := strconv.Atoi(urlTest.Port()) - client = &Api{} + client = &API{} client.Init(urlTest.Hostname(), i) } @@ -48,7 +48,7 @@ func teardown() { } func TestApi_Init(t *testing.T) { - var server Api + var server API inputServer := "localhost" inputPort := 3000 wantServer := "localhost" @@ -91,7 +91,7 @@ func TestApi_URL(t *testing.T) { } for _, tt := range tests { t.Run(tt.inputServer, func(t *testing.T) { - var server Api + var server API server.Init(tt.inputServer, tt.inputPort) gotURL := server.URL() if !reflect.DeepEqual(gotURL, tt.wantURL) { @@ -122,19 +122,42 @@ func TestApi_Request(t *testing.T) { "A": "a", } wantMeta := meta.Metadata{ - CurrentPage:0, - TotalPages:0, - TotalCount:0, + CurrentPage: 0, + TotalPages: 0, + TotalCount: 0, } - if !reflect.DeepEqual(client.Data.Data, wantData){ + if !reflect.DeepEqual(client.Data.Data, wantData) { t.Errorf("client.Data.Data returned %+v want %+v", client.Data.Data, wantData) } - if !reflect.DeepEqual(client.Data.Meta, wantMeta){ + if !reflect.DeepEqual(client.Data.Meta, wantMeta) { t.Errorf("client.Data.Meta returned %+v want %+v", client.Data.Meta, wantMeta) } } +func TestApi_Request_badURL(t *testing.T) { + setup() + defer teardown() + + err := client.Request(http.MethodGet, "%zzzzz", nil) + + if err == nil { + t.Errorf("Expected error to be returned") + } +} + +func TestApi_Request_invalidDo(t *testing.T) { + setup() + defer teardown() + + client.Port = 0 + + err := client.Request(http.MethodGet, ":", nil) + if err == nil { + t.Errorf("Expected error to be returned") + } +} + func TestApi_Request_httpError(t *testing.T) { setup() defer teardown() @@ -168,15 +191,33 @@ func TestApi_GetInfo(t *testing.T) { Version: "1.0", Providers: map[string]info.Provider{ "github.com": info.Provider{ - ApplicationId: "abc", + ApplicationID: "abc", Server: "github.com", Version: "v3", }, }, } - if !reflect.DeepEqual(gotInfo, wantInfo){ - t.Errorf("Api.GetInfo() returned %+v want %+v",gotInfo , wantInfo) + if !reflect.DeepEqual(gotInfo, wantInfo) { + t.Errorf("Api.GetInfo() returned %+v want %+v", gotInfo, wantInfo) + } +} + +func TestApi_GetInfo_httpError(t *testing.T) { + setup() + defer teardown() + + pathURL := "/" + + mux.HandleFunc(pathURL, func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Bad Request", http.StatusBadRequest) + }) + + gotInfo := client.GetInfo() + wantInfo := info.Info{} + + if !reflect.DeepEqual(gotInfo, wantInfo) { + t.Errorf("Api.GetInfo() returned %+v want %+v", gotInfo, wantInfo) } } @@ -191,18 +232,20 @@ func TestApi_GetUsers_withoutExpandResources(t *testing.T) { fmt.Fprint(w, `{"data":[{"login":"aljesusg","name":"Alberto","github_id":1}]}`) }) - gotUsers := client.GetUsers(false) + expand := false + + gotUsers := client.GetUsers(expand) wantUser := user.User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } wantUsers := user.UserCollection{} wantUsers.Users = append(wantUsers.Users, wantUser) wantUsers.Total = len(wantUsers.Users) - if !reflect.DeepEqual(gotUsers, wantUsers){ - t.Errorf("Api.GetUsers(false) returned %+v want %+v",gotUsers , wantUsers) + if !reflect.DeepEqual(gotUsers, wantUsers) { + t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers) } } @@ -217,17 +260,38 @@ func TestApi_GetUsers_withExpandResources(t *testing.T) { fmt.Fprint(w, `{"data":[{"login":"aljesusg","name":"Alberto","github_id":1}]}`) }) - gotUsers := client.GetUsers(true) + expand := true + + gotUsers := client.GetUsers(expand) wantUser := user.User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } wantUsers := user.UserCollection{} wantUsers.Users = append(wantUsers.Users, wantUser) wantUsers.Total = len(wantUsers.Users) - if !reflect.DeepEqual(gotUsers, wantUsers){ - t.Errorf("Api.GetUsers(true) returned %+v want %+v",gotUsers , wantUsers) + if !reflect.DeepEqual(gotUsers, wantUsers) { + t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers) + } +} + +func TestApi_GetUsers_httpError(t *testing.T) { + setup() + defer teardown() + + pathURL := "/v1/users" + expand := true + + mux.HandleFunc(pathURL, func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Bad Request", http.StatusBadRequest) + }) + + gotUsers := client.GetUsers(expand) + wantUsers := user.UserCollection{} + + if !reflect.DeepEqual(gotUsers, wantUsers) { + t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers) } } diff --git a/src/manageiq-exchange/menu/menu.go b/src/manageiq-exchange/menu/menu.go index 4e45bdc..b87c297 100644 --- a/src/manageiq-exchange/menu/menu.go +++ b/src/manageiq-exchange/menu/menu.go @@ -31,19 +31,19 @@ func Menu() { if err != nil { fmt.Print(err) } - var miq_exchange api.Api - miq_exchange.Init(server, port) + var miqExchange api.API + miqExchange.Init(server, port) - statusConnection := miq_exchange.CheckConnectionServer() + statusConnection := miqExchange.CheckConnectionServer() - if version && statusConnection{ - info := miq_exchange.GetInfo() + if version && statusConnection { + info := miqExchange.GetInfo() fmt.Printf(info.Print()) } - if users && statusConnection{ - users := miq_exchange.GetUsers(expand) - fmt.Printf(users.Print(miq_exchange.Data.Meta.TotalCount)) + if users && statusConnection { + users := miqExchange.GetUsers(expand) + fmt.Printf(users.Print(miqExchange.Data.Meta.TotalCount)) } } diff --git a/src/manageiq-exchange/models/info/info.go b/src/manageiq-exchange/models/info/info.go index bf566ab..f89d065 100644 --- a/src/manageiq-exchange/models/info/info.go +++ b/src/manageiq-exchange/models/info/info.go @@ -13,7 +13,7 @@ type Info struct { type Provider struct { Type string `json:"type"` Enabled bool `json:"enabled"` - ApplicationId string `json:"id_application"` + ApplicationID string `json:"id_application"` Server string `json:"server"` Version string `json:"version"` Verify bool `json:"verify"` @@ -36,7 +36,7 @@ func (a *Info) Print() string { for k, v := range a.Providers { result += fmt.Sprintf(" %s: \n", k) result += fmt.Sprintf(" %s: %s\n", "Server", v.Server) - result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationId) + result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationID) result += fmt.Sprintf(" %s: %s\n", "Version", v.Version) } return result diff --git a/src/manageiq-exchange/models/info/info_test.go b/src/manageiq-exchange/models/info/info_test.go index 02950d8..829ecab 100644 --- a/src/manageiq-exchange/models/info/info_test.go +++ b/src/manageiq-exchange/models/info/info_test.go @@ -12,7 +12,7 @@ func TestInit(t *testing.T) { Version: "1.0", Providers: map[string]Provider{ "github.com": Provider{ - ApplicationId: "abc", + ApplicationID: "abc", Server: "github.com", Version: "v3", }, @@ -47,7 +47,7 @@ func TestPrint(t *testing.T) { Version: "1.0", Providers: map[string]Provider{ "github.com": Provider{ - ApplicationId: "abc", + ApplicationID: "abc", Server: "github.com", Version: "v3", }, diff --git a/src/manageiq-exchange/models/user/user.go b/src/manageiq-exchange/models/user/user.go index 93fe2d2..803f17f 100644 --- a/src/manageiq-exchange/models/user/user.go +++ b/src/manageiq-exchange/models/user/user.go @@ -6,9 +6,9 @@ import ( ) type User struct { - GithubId int `json:"github_id"` + GithubID int `json:"github_id"` Login string `json:"login"` - Url_profile string `json:"url_profile"` + URLProfile string `json:"url_profile"` Name string `json:"name"` Avatar string `json:"avatar"` Company string `json:"company"` @@ -40,8 +40,8 @@ func (u *User) Init(data map[string]interface{}) { func (u *User) Print() string { var result string - result = fmt.Sprintf("%s: %s (%d)\n\n", utils.PrintColor("User", "Red"), u.Login, u.GithubId) - result += utils.PrintValues(u, " ", []string{"Login", "GithubId"}) + result = fmt.Sprintf("%s: %s (%d)\n\n", utils.PrintColor("User", "Red"), u.Login, u.GithubID) + result += utils.PrintValues(u, " ", []string{"Login", "GithubID"}) return result } diff --git a/src/manageiq-exchange/models/user/user_test.go b/src/manageiq-exchange/models/user/user_test.go index b4ac9c0..441f0c8 100644 --- a/src/manageiq-exchange/models/user/user_test.go +++ b/src/manageiq-exchange/models/user/user_test.go @@ -12,7 +12,7 @@ func TestUserInit(t *testing.T) { want := User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } var data = map[string]interface{}{ "login": "aljesusg", @@ -33,7 +33,7 @@ func TestUserPrint(t *testing.T) { user := User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } if !reflect.DeepEqual(user.Print(), want) { t.Errorf("User Print returned -%+v-, want -%+v-", user.Print(), want) @@ -44,7 +44,7 @@ func TestUserCollectionInit(t *testing.T) { user := User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } users := UserCollection{} users.Users = append(users.Users, user) @@ -75,7 +75,7 @@ func TestUserCollectionPrint(t *testing.T) { user := User{ Login: "aljesusg", Name: "Alberto", - GithubId: 1, + GithubID: 1, } userCollection := UserCollection{} userCollection.Users = append(userCollection.Users, user) diff --git a/src/manageiq-exchange/models/utils/utils.go b/src/manageiq-exchange/models/utils/utils.go index f0a0eb8..749ec47 100644 --- a/src/manageiq-exchange/models/utils/utils.go +++ b/src/manageiq-exchange/models/utils/utils.go @@ -6,8 +6,10 @@ import ( "reflect" ) -const NC string = "\033[0m" // No Color +// NC is No Color +const NC string = "\033[0m" +// COLOR is color to print message with color var COLOR = map[string]string{ "Black": "0;30", "Dark Gray": "1;30", diff --git a/src/manageiq-exchange/models/utils/utils_test.go b/src/manageiq-exchange/models/utils/utils_test.go index 1e3434c..2e531eb 100644 --- a/src/manageiq-exchange/models/utils/utils_test.go +++ b/src/manageiq-exchange/models/utils/utils_test.go @@ -15,7 +15,7 @@ func TestPrintColor(t *testing.T) { type SampleInterface struct { Type string `json:"type"` Enabled bool `json:"enabled"` - ApplicationId string `json:"id_application"` + ApplicationID string `json:"id_application"` Server string `json:"server"` Version string `json:"version"` Verify bool `json:"verify"` @@ -25,7 +25,7 @@ func TestCreateFromMap(t *testing.T) { want := SampleInterface{ Server: "github.com", Version: "v3", - ApplicationId: "abc", + ApplicationID: "abc", } var data = map[string]interface{}{ "server": "github.com", @@ -64,22 +64,21 @@ func TestValueIsEmpty(t *testing.T) { } } -type DataFake struct { - Name string - GithubId int -} - func TestPrintValues(t *testing.T) { + type DataFake struct { + Name string + GithubID int + } + var data = &DataFake{ Name: "Alberto", - GithubId: 1, + GithubID: 1, } want := " Name : Alberto\n" - got := PrintValues(data, " ", []string{"GithubId"}) + got := PrintValues(data, " ", []string{"GithubID"}) if !reflect.DeepEqual(got, want) { t.Errorf("PrintValues() returned -%+v-, want -%+v-", got, want) } } -