Skip to content

Commit

Permalink
feat: CoreDumpsRepository implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
srgchrksv committed Sep 18, 2022
1 parent e044620 commit ae80ef3
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 127 deletions.
1 change: 1 addition & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ lint:
run:
@export CRASHER_SERVER_ADDRESS=:8080
@go run cmd/crasher/main.go

13 changes: 11 additions & 2 deletions server/cmd/crasher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"server/internal/app/controllers"
"server/internal/app/repositories/mongodb_repository"
"server/internal/app/services"
"strconv"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -45,8 +46,16 @@ func main() {
}
}()

appsRepo := mongodb_repository.NewApplicationsRepository(dbClient, logger)
coreDumpsRepo := mongodb_repository.NewCoreDumpsRepository(dbClient, logger)
timeout, err := strconv.Atoi(app.CtxTimeout)
if err != nil {
logger.Fatal(
"failed to parse timeout env",
zap.Error(err),
)
}

appsRepo := mongodb_repository.NewApplicationsRepository(dbClient, logger, timeout)
coreDumpsRepo := mongodb_repository.NewCoreDumpsRepository(dbClient, logger,timeout)

appsService := services.NewApplicationsService(appsRepo, logger)
coreDumpsService := services.NewCoreDumpsService(coreDumpsRepo, logger)
Expand Down
2 changes: 1 addition & 1 deletion server/internal/app/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "os"

var (
ServerAddress = os.Getenv("CRASHER_SERVER_ADDRESS")
CtxTimeout = os.Getenv("CRASHER_DATABASE_TIMEOUTgit")
CtxTimeout = parseEnvOrDefaultValue("CRASHER_DATABASE_TIMEOUT", "5")
DatabaseAddress = os.Getenv("CRASHER_DATABASE_ADDRESS")
DatabaseUsername = os.Getenv("CRASHER_DATABASE_USERNAME")
DatabasePassword = os.Getenv("CRASHER_DATABASE_PASSWORD")
Expand Down
21 changes: 16 additions & 5 deletions server/internal/app/entities/core_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package entities

import (
"time"

"go.mongodb.org/mongo-driver/bson/primitive"
)

type CoreDumpStatus int
Expand All @@ -16,16 +14,16 @@ const (
)

type CoreDump struct {
ID primitive.ObjectID
ID string
OsInfo *OSInfo
AppInfo *AppInfo
Status CoreDumpStatus
Data string
Timestamp time.Time
Extensions []Extensions
Extensions []Extension
}

type Extensions struct {
type Extension struct {
Key string
Value string
}
Expand Down Expand Up @@ -54,6 +52,14 @@ func (c *CoreDump) GetTimestamp() time.Time {
return c.Timestamp
}

func (c *CoreDump) GetExtension(index int) *Extension {
return &c.Extensions[index]
}

func (c *CoreDump) GetExtensions() *[]Extension {
return &c.Extensions
}

func (c *CoreDump) SetOSInfo(info *OSInfo) {
c.OsInfo = info
}
Expand All @@ -73,3 +79,8 @@ func (c *CoreDump) SetData(data string) {
func (c *CoreDump) SetTimestamp(timestamp time.Time) {
c.Timestamp = timestamp
}

func (c *CoreDump) SetExtensions(key, value string) {
extension := &Extension{Key: key, Value: value}
c.Extensions = append(c.Extensions, *extension)
}
7 changes: 7 additions & 0 deletions server/internal/app/entities/core_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ func TestCoreDump(t *testing.T) {
timestamp := time.Now()
status := SolvedCoreDumpStatus
data := "server/internal/app/entities/core_dump_test.go@TestCoreDump:100"
extension := &Extension{
Key: "key",
Value: "value",
}

coreDump.SetOSInfo(osInfo)
coreDump.SetAppInfo(appInfo)
coreDump.SetTimestamp(timestamp)
coreDump.SetStatus(status)
coreDump.SetData(data)
coreDump.SetExtensions(extension.Key, extension.Value)

assert.Equal(t, osInfo, coreDump.GetOSInfo())
assert.Equal(t, appInfo, coreDump.GetAppInfo())
assert.Equal(t, timestamp, coreDump.GetTimestamp())
assert.Equal(t, status, coreDump.GetStatus())
assert.Equal(t, data, coreDump.GetData())
assert.Equal(t, extension, coreDump.GetExtension(0))
assert.Equal(t, true, len(*coreDump.GetExtensions()) > 0)
}
6 changes: 3 additions & 3 deletions server/internal/app/entities/os_info.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package entities

type OSInfo struct {
Name string
Version string
Architecture string
Name string
Version string
Architecture string
}

func NewOSInfo() *OSInfo {
Expand Down
18 changes: 0 additions & 18 deletions server/internal/app/entities/query_fields_mongo_test.go

This file was deleted.

11 changes: 11 additions & 0 deletions server/internal/app/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app

import "os"

func parseEnvOrDefaultValue(env, defaultValue string) string {
parsedEnv := os.Getenv(env)
if parsedEnv == "" {
return defaultValue
}
return parsedEnv
}
4 changes: 3 additions & 1 deletion server/internal/app/repositories/core_dumps_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

type CoreDumpsRepository interface {
GetCoreDumps(setters ...entities.OptionsMongo) ([]entities.CoreDump, error)
GetCoreDumps(parameters ...interface{}) ([]entities.CoreDump, error)
GetCoreDumpByID(id string) (entities.CoreDump, error)
AddCoreDump(coreDump entities.CoreDump) error
UpdateCoreDump(parameters interface{}) error
DeleteCoreDump(id string) error
DeleteAllCoreDumps() error
}
37 changes: 33 additions & 4 deletions server/internal/app/repositories/mock/core_dumps_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ type ApplicationsRepository struct {

var _ repositories.ApplicationsRepository = (*ApplicationsRepository)(nil)

func NewApplicationsRepository(db *mongo.Client, l *zap.Logger) *ApplicationsRepository {
func NewApplicationsRepository(db *mongo.Client, l *zap.Logger, timeout int) *ApplicationsRepository {
mongoDB := db.Database("crasher")
collection := mongoDB.Collection("coredumps")
timeout, err := ParseCtxTimeoutEnv()
if err != nil {
l.Fatal(
"failed to parse ctx timeout env",
zap.Error(err),
)
}

return &ApplicationsRepository{
dbClient: db,
collection: collection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@ import (
"errors"
mock_repositories "server/internal/app/repositories/mock"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)

func TestNewApplicationsRepository(t *testing.T) {
ctxTimeout := 5
applicationsRepository := NewApplicationsRepository(&mongo.Client{}, &zap.Logger{}, ctxTimeout)

require.Equal(t, "crasher", applicationsRepository.collection.Database().Name())
require.Equal(t, "coredumps", applicationsRepository.collection.Name())
require.Equal(t, &zap.Logger{}, applicationsRepository.logger)
require.Equal(t, ctxTimeout, applicationsRepository.timeout)
}
func TestGetApplicationNames(t *testing.T) {
many := []string{"app1", "app2", "app3"}
one := []string{"app1"}
empty := []string{}
t.Parallel()

slowResponse := time.Second * 6

c := gomock.NewController(t)
defer c.Finish()
r := mock_repositories.NewMockApplicationsRepository(c)

tests := []struct {
name string
Expand All @@ -24,41 +37,51 @@ func TestGetApplicationNames(t *testing.T) {
error error
}{
{
name: "ok - many elements",
name: "get many application names",
stubs: func(r *mock_repositories.MockApplicationsRepository, slice []string) {
r.EXPECT().GetApplicationNames().Return(slice, nil)
},
slice: many,
slice: []string{"app1", "app2", "app3"},
error: nil,
},
{
name: "ok - one app",
name: "get one application name",
stubs: func(r *mock_repositories.MockApplicationsRepository, slice []string) {
r.EXPECT().GetApplicationNames().Return(slice, nil)
},
slice: one,
slice: []string{"app1"},
error: nil,
},
{
name: "ok - empty slice",
name: "get empty application names",
stubs: func(r *mock_repositories.MockApplicationsRepository, slice []string) {
r.EXPECT().GetApplicationNames().Return(slice, nil)
},
slice: empty,
slice: nil,
error: nil,
},
{
name: "error",
name: "get error while getting application names",
stubs: func(r *mock_repositories.MockApplicationsRepository, slice []string) {
r.EXPECT().GetApplicationNames().Return(slice, errors.New("error"))
},
slice: nil,
error: errors.New("error"),
},
{
name: "get timeout error while getting application names",
stubs: func(r *mock_repositories.MockApplicationsRepository, slice []string) {
r.EXPECT().GetApplicationNames().Return(slice, errors.New("error getting application names"))
r.EXPECT().GetApplicationNames().DoAndReturn(func() ([]string, error) {
time.Sleep(slowResponse)
return slice, errors.New("error")
})
},
slice: empty,
error: errors.New("error getting application names"),
slice: nil,
error: errors.New("error"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := mock_repositories.NewMockApplicationsRepository(c)
test.stubs(r, test.slice)

result, err := r.GetApplicationNames()
Expand Down
Loading

0 comments on commit ae80ef3

Please sign in to comment.