Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Datree
Commits
494449ab
Commit
494449ab
authored
3 years ago
by
myishay
Browse files
Options
Download
Email Patches
Plain Diff
fix: split publish errors to separate lines
parent
42fb4cbc
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
cmd/publish/main.go
+34
-9
cmd/publish/main.go
cmd/publish/main_test.go
+29
-9
cmd/publish/main_test.go
pkg/cliClient/cliClient_test.go
+4
-3
pkg/cliClient/cliClient_test.go
pkg/cliClient/publish.go
+5
-4
pkg/cliClient/publish.go
pkg/httpClient/client.go
+9
-0
pkg/httpClient/client.go
with
81 additions
and
25 deletions
+81
-25
cmd/publish/main.go
+
34
-
9
View file @
494449ab
package
publish
import
(
"encoding/json"
"fmt"
"github.com/datreeio/datree/bl/files"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/httpClient"
"github.com/datreeio/datree/pkg/localConfig"
"github.com/spf13/cobra"
)
...
...
@@ -22,7 +25,7 @@ type LocalConfig interface {
}
type
CliClient
interface
{
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
error
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
(
httpClient
.
Response
,
error
)
}
type
PublishCommandContext
struct
{
...
...
@@ -56,9 +59,15 @@ func New(ctx *PublishCommandContext) *cobra.Command {
}
}()
err
:=
publish
(
ctx
,
args
[
0
])
if
err
!=
nil
{
ctx
.
Printer
.
PrintMessage
(
"Publish failed:
\n
"
+
err
.
Error
()
+
"
\n
"
,
"error"
)
publishFailedResponse
,
err
:=
publish
(
ctx
,
args
[
0
])
if
len
(
publishFailedResponse
.
Payload
)
>
0
{
for
_
,
message
:=
range
publishFailedResponse
.
Payload
{
ctx
.
Printer
.
PrintMessage
(
"Publish failed:
\n
"
,
"error"
)
ctx
.
Printer
.
PrintMessage
(
"
\t
"
+
message
+
"
\n
"
,
"error"
)
}
}
else
if
err
!=
nil
{
ctx
.
Printer
.
PrintMessage
(
"Publish failed: "
+
err
.
Error
()
+
"
\n
"
,
"error"
)
}
else
{
ctx
.
Printer
.
PrintMessage
(
"Published successfully
\n
"
,
"green"
)
}
...
...
@@ -80,17 +89,33 @@ type MessagesContext struct {
CliClient
*
cliClient
.
CliClient
}
func
publish
(
ctx
*
PublishCommandContext
,
path
string
)
error
{
type
PublishFailedResponse
struct
{
Code
string
`json:"code"`
Message
string
`json:"message"`
Payload
[]
string
`json:"payload"`
}
func
publish
(
ctx
*
PublishCommandContext
,
path
string
)
(
PublishFailedResponse
,
error
)
{
localConfigContent
,
err
:=
ctx
.
LocalConfig
.
GetLocalConfiguration
()
if
err
!=
nil
{
return
err
return
PublishFailedResponse
{},
err
}
policiesConfiguration
,
err
:=
files
.
ExtractYamlFileToUnknownStruct
(
path
)
if
err
!=
nil
{
return
err
return
PublishFailedResponse
{},
err
}
err
=
ctx
.
PublishCliClient
.
PublishPolicies
(
policiesConfiguration
,
localConfigContent
.
CliId
)
return
err
res
,
publishErr
:=
ctx
.
PublishCliClient
.
PublishPolicies
(
policiesConfiguration
,
localConfigContent
.
CliId
)
if
publishErr
!=
nil
{
if
res
.
StatusCode
!=
0
{
publishFailedResponse
:=
PublishFailedResponse
{}
err
=
json
.
Unmarshal
(
res
.
Body
,
&
publishFailedResponse
)
if
err
!=
nil
{
return
PublishFailedResponse
{},
err
}
return
publishFailedResponse
,
publishErr
}
}
return
PublishFailedResponse
{},
nil
}
This diff is collapsed.
Click to expand it.
cmd/publish/main_test.go
+
29
-
9
View file @
494449ab
package
publish
import
(
"encoding/json"
"errors"
"strings"
"testing"
"github.com/datreeio/datree/bl/files"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/pkg/httpClient"
"github.com/datreeio/datree/pkg/localConfig"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type
LocalConfigMock
struct
{
...
...
@@ -47,8 +51,9 @@ type PublishClientMock struct {
mock
.
Mock
}
func
(
p
*
PublishClientMock
)
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
error
{
return
p
.
Called
(
policiesConfiguration
,
cliId
)
.
Error
(
0
)
func
(
p
*
PublishClientMock
)
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
(
httpClient
.
Response
,
error
)
{
args
:=
p
.
Called
(
policiesConfiguration
,
cliId
)
return
args
.
Get
(
0
)
.
(
httpClient
.
Response
),
args
.
Error
(
1
)
}
func
TestPublishCommand
(
t
*
testing
.
T
)
{
...
...
@@ -77,20 +82,35 @@ func TestPublishCommand(t *testing.T) {
}
func
testPublishCommandSuccess
(
t
*
testing
.
T
,
ctx
*
PublishCommandContext
,
publishClientMock
*
PublishClientMock
)
{
publishClientMock
.
On
(
"PublishPolicies"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
nil
)
.
Once
()
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/valid-schema.yaml"
)
publishClientMock
.
On
(
"PublishPolicies"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
httpClient
.
Response
{},
nil
)
.
Once
()
_
,
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/valid-schema.yaml"
)
assert
.
Equal
(
t
,
nil
,
err
)
}
func
testPublishCommandFailedYaml
(
t
*
testing
.
T
,
ctx
*
PublishCommandContext
)
{
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/invalid-yaml.yaml"
)
_
,
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/invalid-yaml.yaml"
)
assert
.
NotEqual
(
t
,
nil
,
err
)
assert
.
Equal
(
t
,
"yaml: line 2: did not find expected key"
,
err
.
Error
())
}
func
testPublishCommandFailedSchema
(
t
*
testing
.
T
,
ctx
*
PublishCommandContext
,
publishClientMock
*
PublishClientMock
)
{
publishClientMock
.
On
(
"PublishPolicies"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
errors
.
New
(
"some error"
))
.
Once
()
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/invalid-schemas/duplicate-rule-id.yaml"
)
publishFailedPayloadMock
:=
[]
string
{
"first error"
,
"second error"
}
errMessage
:=
strings
.
Join
(
publishFailedPayloadMock
,
","
)
publishFailedResponseMock
:=
PublishFailedResponse
{
Code
:
"mocked code"
,
Message
:
errMessage
,
Payload
:
publishFailedPayloadMock
,
}
mockedResponseBody
,
_
:=
json
.
Marshal
(
publishFailedResponseMock
)
mockedResponse
:=
httpClient
.
Response
{
StatusCode
:
400
,
Body
:
mockedResponseBody
,
}
publishClientMock
.
On
(
"PublishPolicies"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
mockedResponse
,
errors
.
New
(
errMessage
))
.
Once
()
publishFailedRes
,
err
:=
publish
(
ctx
,
"../../internal/fixtures/policyAsCode/invalid-schemas/duplicate-rule-id.yaml"
)
assert
.
NotEqual
(
t
,
nil
,
err
)
assert
.
Equal
(
t
,
"some error"
,
err
.
Error
())
assert
.
Equal
(
t
,
errMessage
,
err
.
Error
())
assert
.
Equal
(
t
,
publishFailedResponseMock
,
publishFailedRes
)
}
This diff is collapsed.
Click to expand it.
pkg/cliClient/cliClient_test.go
+
4
-
3
View file @
494449ab
...
...
@@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/datreeio/datree/bl/files"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"github.com/datreeio/datree/bl/files"
"gopkg.in/yaml.v3"
"github.com/datreeio/datree/pkg/extractor"
...
...
@@ -219,9 +220,9 @@ func TestPublishPolicies(t *testing.T) {
httpClient
:
&
httpClientMock
,
}
actualResponse
:=
client
.
PublishPolicies
(
tt
.
args
.
policiesConfiguration
,
tt
.
args
.
cliId
)
_
,
err
:=
client
.
PublishPolicies
(
tt
.
args
.
policiesConfiguration
,
tt
.
args
.
cliId
)
httpClientMock
.
AssertCalled
(
t
,
"Request"
,
tt
.
expected
.
request
.
method
,
tt
.
expected
.
request
.
uri
,
tt
.
expected
.
request
.
body
,
tt
.
expected
.
request
.
headers
)
assert
.
Equal
(
t
,
tt
.
expected
.
response
,
actualResponse
)
assert
.
Equal
(
t
,
tt
.
expected
.
response
,
err
)
})
}
}
...
...
This diff is collapsed.
Click to expand it.
pkg/cliClient/publish.go
+
5
-
4
View file @
494449ab
package
cliClient
import
(
"github.com/datreeio/datree/bl/files"
"net/http"
"github.com/datreeio/datree/bl/files"
"github.com/datreeio/datree/pkg/httpClient"
)
func
(
c
*
CliClient
)
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
error
{
func
(
c
*
CliClient
)
PublishPolicies
(
policiesConfiguration
files
.
UnknownStruct
,
cliId
string
)
(
httpClient
.
Response
,
error
)
{
headers
:=
map
[
string
]
string
{
"x-cli-id"
:
cliId
}
_
,
err
:=
c
.
httpClient
.
Request
(
http
.
MethodPut
,
"/cli/policy/publish"
,
policiesConfiguration
,
headers
)
return
err
return
c
.
httpClient
.
Request
(
http
.
MethodPut
,
"/cli/policy/publish"
,
policiesConfiguration
,
headers
)
}
This diff is collapsed.
Click to expand it.
pkg/httpClient/client.go
+
9
-
0
View file @
494449ab
...
...
@@ -79,9 +79,18 @@ func (c *Client) Request(method string, resourceURI string, body interface{}, he
return
responseBody
,
err
}
responseBody
=
Response
{
StatusCode
:
response
.
StatusCode
,
Body
:
b
,
}
return
responseBody
,
fmt
.
Errorf
(
fmt
.
Sprintf
(
"%v"
,
errorJson
[
"message"
]))
}
if
err
!=
nil
{
return
responseBody
,
err
}
responseBody
=
Response
{
StatusCode
:
response
.
StatusCode
,
Body
:
b
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment