Skip to content

Commit

Permalink
Logging: fix crashing test
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Feb 6, 2023
1 parent fd024ae commit 24bb96b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
31 changes: 15 additions & 16 deletions iam/groups_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,16 @@ func (g *GroupsService) roleAction(ctx context.Context, group Group, role Role,
var assignRequest = groupRequest{
Roles: []string{role.ID},
}
req, err := g.client.newRequest(IDM, "POST", "authorize/identity/Group/"+group.ID+"/"+action, assignRequest, nil)
if err != nil {
return false, nil, err
}
req.Header.Set("api-version", groupAPIVersion)
req.Header.Set("Content-Type", "application/json")

var assignResponse interface{}
var resp *Response

err = internal.TryHTTPCall(ctx, 6, func() (*http.Response, error) {
err := internal.TryHTTPCall(ctx, 6, func() (*http.Response, error) {
req, err := g.client.newRequest(IDM, "POST", "authorize/identity/Group/"+group.ID+"/"+action, assignRequest, nil)
if err != nil {
return nil, err
}
req.Header.Set("api-version", groupAPIVersion)
req.Header.Set("Content-Type", "application/json")
resp, err = g.client.do(req, &assignResponse)
if resp == nil {
return nil, err
Expand Down Expand Up @@ -233,17 +232,17 @@ type groupRequest struct {
}

func (g *GroupsService) memberAction(ctx context.Context, group Group, action string, opt interface{}, options []OptionFunc) (map[string]interface{}, *Response, error) {
req, err := g.client.newRequest(IDM, "POST", "authorize/identity/Group/"+group.ID+"/"+action, opt, options)
if err != nil {
return nil, nil, err
}
req.Header.Set("api-version", groupAPIVersion)
req.Header.Set("Content-Type", "application/json")

var memberResponse map[string]interface{}
var resp *Response

err = internal.TryHTTPCall(ctx, 6, func() (*http.Response, error) {
err := internal.TryHTTPCall(ctx, 6, func() (*http.Response, error) {
req, err := g.client.newRequest(IDM, "POST", "authorize/identity/Group/"+group.ID+"/"+action, opt, options)
if err != nil {
return nil, err
}
req.Header.Set("api-version", groupAPIVersion)
req.Header.Set("Content-Type", "application/json")

resp, err = g.client.do(req, &memberResponse)
if resp == nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package internal

const (
LibraryVersion = "0.78.6"
LibraryVersion = "0.78.7"
)
7 changes: 7 additions & 0 deletions logging/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ type CustomIndexBody []struct {
Fieldtype string `json:"fieldtype"`
}

func (r *StoreResponse) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}

// NewClient returns an instance of the logger client with the given Config
func NewClient(httpClient *http.Client, config *Config) (*Client, error) {
var logger Client
Expand Down
16 changes: 8 additions & 8 deletions logging/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func TestStoreResources(t *testing.T) {
t.Errorf("Unexpected nil value for response")
return
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected HTTP 201, Got: %d", resp.StatusCode)
if resp.StatusCode() != http.StatusCreated {
t.Errorf("Expected HTTP 201, Got: %d", resp.StatusCode())
}
}

Expand Down Expand Up @@ -207,8 +207,8 @@ func TestStoreResourcesWithInvalidKey(t *testing.T) {
t.Errorf("Unexpected nil value for response")
return
}
if resp.StatusCode != http.StatusUnprocessableEntity {
t.Errorf("Expected HTTP %d, Got: %d", http.StatusUnprocessableEntity, resp.StatusCode)
if resp.StatusCode() != http.StatusUnprocessableEntity {
t.Errorf("Expected HTTP %d, Got: %d", http.StatusUnprocessableEntity, resp.StatusCode())
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ func TestStoreResourcesWithInvalidKeypair(t *testing.T) {
}
assert.NotNil(t, resp)
_ = err.Error() // Just to up coverage
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
assert.Equal(t, http.StatusForbidden, resp.StatusCode())
assert.NotNil(t, err)
}

Expand Down Expand Up @@ -346,7 +346,7 @@ func TestStoreResourcesWithBadResources(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode())
assert.Equal(t, ErrBatchErrors, err)
}

Expand Down Expand Up @@ -448,8 +448,8 @@ func TestStoreResourceWithBearerToken(t *testing.T) {
t.Errorf("Unexpected nil value for response")
return
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected HTTP 201, Got: %d", resp.StatusCode)
if resp.StatusCode() != http.StatusCreated {
t.Errorf("Expected HTTP 201, Got: %d", resp.StatusCode())
}
}

Expand Down
20 changes: 17 additions & 3 deletions logging/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func TestIntegration(t *testing.T) {
productKey := os.Getenv("INT_LOGGING_PRODUCT_KEY")
ingestorURL := os.Getenv("INT_LOGGING_INGESTOR_URL")

if !assert.NotEmpty(t, key) {
return
}
if !assert.NotEmpty(t, secret) {
return
}
if !assert.NotEmpty(t, productKey) {
return
}
if !assert.NotEmpty(t, ingestorURL) {
return
}

intClient, err := NewClient(nil, &Config{
SharedKey: key,
SharedSecret: secret,
Expand All @@ -53,7 +66,7 @@ func TestIntegration(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, http.StatusCreated, resp.StatusCode())

// Local validation test
resp, err = intClient.StoreResources([]Resource{
Expand All @@ -67,10 +80,11 @@ func TestIntegration(t *testing.T) {
if !assert.NotNil(t, err) {
return
}
assert.Equal(t, ErrBatchErrors, err)
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, ErrBatchErrors, err)

assert.Equal(t, http.StatusBadRequest, resp.StatusCode())
assert.Equal(t, 3, len(resp.Failed))
}

0 comments on commit 24bb96b

Please sign in to comment.