Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
test: add user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
myishay committed Apr 20, 2021
1 parent 0baee9d commit 535060f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
6 changes: 3 additions & 3 deletions bl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Printer interface {
PrintSummaryTable(summary printer.Summary)
}
type CLIClient interface {
RequestEvaluation(pattern string, files []*propertiesExtractor.FileProperties, cliId string) (cliClient.EvaluationResponse, error)
RequestEvaluation(pattern string, files []*propertiesExtractor.FileProperties, cliId string, cliVersion string) (cliClient.EvaluationResponse, error)
}
type PropertiesExtractor interface {
ReadFilesFromPattern(pattern string, conc int) ([]*propertiesExtractor.FileProperties, []propertiesExtractor.FileError, []error)
Expand Down Expand Up @@ -43,7 +43,7 @@ type EvaluationResults struct {
}
}

func (e *Evaluator) Evaluate(pattern string, cliId string, evaluationConc int) (*EvaluationResults, []propertiesExtractor.FileError, error) {
func (e *Evaluator) Evaluate(pattern string, cliId string, evaluationConc int, cliVersion string) (*EvaluationResults, []propertiesExtractor.FileError, error) {
files, fileErrors, errors := e.propertiesExtractor.ReadFilesFromPattern(pattern, evaluationConc)
if len(errors) > 0 {
return nil, fileErrors, fmt.Errorf("failed evaluation with the following errors: %s", errors)
Expand All @@ -53,7 +53,7 @@ func (e *Evaluator) Evaluate(pattern string, cliId string, evaluationConc int) (
return nil, fileErrors, fmt.Errorf("no files detected")
}

res, err := e.cliClient.RequestEvaluation(pattern, files, cliId)
res, err := e.cliClient.RequestEvaluation(pattern, files, cliId, cliVersion)
if err != nil {
return nil, fileErrors, err
}
Expand Down
4 changes: 2 additions & 2 deletions bl/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type mockCliClient struct {
mock.Mock
}

func (m *mockCliClient) RequestEvaluation(pattern string, files []*propertiesExtractor.FileProperties, cliId string) (cliClient.EvaluationResponse, error) {
func (m *mockCliClient) RequestEvaluation(pattern string, files []*propertiesExtractor.FileProperties, cliId string, cliVersion string) (cliClient.EvaluationResponse, error) {
args := m.Called(pattern, files, cliId)
return args.Get(0).(cliClient.EvaluationResponse), args.Error(1)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestEvaluate(t *testing.T) {
printer: printer,
}

actualResponse, actualFilesErrs, actualErr := evaluator.Evaluate(tt.args.pattern, tt.args.cliId, tt.args.evaluationConc)
actualResponse, actualFilesErrs, actualErr := evaluator.Evaluate(tt.args.pattern, tt.args.cliId, tt.args.evaluationConc, "0.0.1")

propertiesExtractor.AssertCalled(t, "ReadFilesFromPattern", tt.args.pattern, tt.args.evaluationConc)
cliClient.AssertCalled(t, "RequestEvaluation", tt.args.pattern, tt.mock.propertiesExtractor.readFilesFromPattern.properties, tt.args.cliId)
Expand Down
4 changes: 2 additions & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LocalConfigManager interface {
type Evaluator interface {
PrintResults(results *bl.EvaluationResults, cliId string) error
PrintFileParsingErrors(errors []propertiesExtractor.FileError)
Evaluate(pattern string, cliId string, evaluationConc int) (*bl.EvaluationResults, []propertiesExtractor.FileError, error)
Evaluate(pattern string, cliId string, evaluationConc int, cliVersion string) (*bl.EvaluationResults, []propertiesExtractor.FileError, error)
}

type TestCommandContext struct {
Expand Down Expand Up @@ -60,7 +60,7 @@ func test(ctx *TestCommandContext, pattern string) error {
return err
}

evaluationResponse, fileParsingErrors, err := ctx.Evaluator.Evaluate(absolutePath, config.CliId, 50)
evaluationResponse, fileParsingErrors, err := ctx.Evaluator.Evaluate(absolutePath, config.CliId, 50, "0.0.1")
s.Stop()

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *mockEvaluator) PrintFileParsingErrors(errors []propertiesExtractor.File
m.Called(errors)
}

func (m *mockEvaluator) Evaluate(pattern string, cliId string, evaluationConc int) (*bl.EvaluationResults, []propertiesExtractor.FileError, error) {
func (m *mockEvaluator) Evaluate(pattern string, cliId string, evaluationConc int, cliVersion string) (*bl.EvaluationResults, []propertiesExtractor.FileError, error) {
args := m.Called(pattern, cliId, evaluationConc)
return args.Get(0).(*bl.EvaluationResults), args.Get(1).([]propertiesExtractor.FileError), args.Error(2)
}
Expand All @@ -45,7 +45,7 @@ func TestTestCommand(t *testing.T) {
FilesCount int
}{RulesCount: 1, TotalFailedRules: 0, FilesCount: 0},
}
evaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return(mockedEvaluateResponse, []propertiesExtractor.FileError{}, nil)
evaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockedEvaluateResponse, []propertiesExtractor.FileError{}, nil)
evaluator.On("PrintFileParsingErrors", mock.Anything).Return()
evaluator.On("PrintResults", mock.Anything, mock.Anything).Return()

Expand Down
28 changes: 15 additions & 13 deletions pkg/cliClient/cliClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import (

"github.com/datreeio/datree/pkg/httpClient"
extractor "github.com/datreeio/datree/pkg/propertiesExtractor"
"github.com/shirou/gopsutil/host"
)

type HTTPClient interface {
Request(method string, resourceURI string, body interface{}, headers map[string]string) (httpClient.Response, error)
}

type getUserAgentFn = func() (*UserAgent, error)
type CliClient struct {
baseUrl string
httpClient HTTPClient
baseUrl string
httpClient HTTPClient
getUserAgent getUserAgentFn
}

func NewCliClient(url string) *CliClient {
httpClient := httpClient.NewClient(url, nil)
return &CliClient{
baseUrl: url,
httpClient: httpClient,
baseUrl: url,
httpClient: httpClient,
getUserAgent: getUserAgent,
}
}

Expand Down Expand Up @@ -64,8 +66,8 @@ type EvaluationResponse struct {
Status string `json:"status"`
}

func (c *CliClient) RequestEvaluation(pattern string, files []*extractor.FileProperties, cliId string) (EvaluationResponse, error) {
evaluationRequest, err := c.createEvaluationRequest(pattern, files, cliId)
func (c *CliClient) RequestEvaluation(pattern string, files []*extractor.FileProperties, cliId string, cliVersion string) (EvaluationResponse, error) {
evaluationRequest, err := c.createEvaluationRequest(pattern, files, cliId, cliVersion)
if err != nil {
return EvaluationResponse{}, err
}
Expand All @@ -83,14 +85,14 @@ func (c *CliClient) RequestEvaluation(pattern string, files []*extractor.FilePro
return *evaluationResponse, nil
}

func (c *CliClient) createEvaluationRequest(pattern string, files []*extractor.FileProperties, cliId string) (EvaluationRequest, error) {
func (c *CliClient) createEvaluationRequest(pattern string, files []*extractor.FileProperties, cliId string, cliVersion string) (EvaluationRequest, error) {
var filesProperties []extractor.FileProperties

for _, file := range files {
filesProperties = append(filesProperties, *file)
}

osInfo, err := host.Info()
userAgent, err := c.getUserAgent()
if err != nil {
return EvaluationRequest{}, err
}
Expand All @@ -103,10 +105,10 @@ func (c *CliClient) createEvaluationRequest(pattern string, files []*extractor.F
PlatformVersion string "json:\"platformVersion\""
KernelVersion string "json:\"kernelVersion\""
}{
CliVersion: "0.0.1",
Os: osInfo.OS,
PlatformVersion: osInfo.PlatformVersion,
KernelVersion: osInfo.KernelVersion,
CliVersion: cliVersion,
Os: userAgent.OS,
PlatformVersion: userAgent.PlatformVersion,
KernelVersion: userAgent.KernelVersion,
},
Files: filesProperties,
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/cliClient/cliClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type RequestEvaluationTestCase struct {
status int
body *EvaluationResponse
}
getUserAgentFn func() (*UserAgent, error)
}
expected struct {
request struct {
Expand All @@ -61,11 +62,12 @@ func TestRequestEvaluation(t *testing.T) {
httpClientMock.On("Request", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockedHTTPResponse, nil)

client := &CliClient{
baseUrl: "http://cli-service.test.io",
httpClient: &httpClientMock,
baseUrl: "http://cli-service.test.io",
httpClient: &httpClientMock,
getUserAgent: tt.mock.getUserAgentFn,
}

res, _ := client.RequestEvaluation(tt.args.pattern, tt.args.properties, tt.args.cliId)
res, _ := client.RequestEvaluation(tt.args.pattern, tt.args.properties, tt.args.cliId, "0.0.1")

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, res)
Expand Down Expand Up @@ -130,6 +132,7 @@ func test_requestEvaluation_success() *RequestEvaluationTestCase {
status int
body *EvaluationResponse
}
getUserAgentFn func() (*UserAgent, error)
}{
response: struct {
status int
Expand All @@ -138,6 +141,12 @@ func test_requestEvaluation_success() *RequestEvaluationTestCase {
status: http.StatusOK,
body: &EvaluationResponse{},
},
getUserAgentFn: func() (*UserAgent, error) {
return &UserAgent{
OS: "darwin",
PlatformVersion: "1.2.3",
KernelVersion: "4.5.6"}, nil
},
},
expected: struct {
request struct {
Expand Down Expand Up @@ -168,8 +177,8 @@ func test_requestEvaluation_success() *RequestEvaluationTestCase {
}{
CliVersion: "0.0.1",
Os: "darwin",
PlatformVersion: "10.15.7",
KernelVersion: "19.6.0",
PlatformVersion: "1.2.3",
KernelVersion: "4.5.6",
},
},
headers: nil,
Expand Down

0 comments on commit 535060f

Please sign in to comment.