Skip to content

Commit

Permalink
policy storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenderistDev committed Aug 16, 2024
1 parent 6cc912e commit ac5d08f
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 156 deletions.
143 changes: 0 additions & 143 deletions internal/clients/keenetic/policy/policy.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/clients/keenetic/policylist/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewPolicyList(host string, client client) *PolicyList {
}
}

func (l *PolicyList) GetPolicyList() (keeneticdto.PolicyResponse, error) {
func (l *PolicyList) GetPolicyList() (map[string]keeneticdto.Policy, error) {
req, err := http.NewRequest(http.MethodGet, l.host+"/rci/show/rc/ip/policy", nil)
if err != nil {
return nil, fmt.Errorf("build request error in GetPolicyList request: %w", err)
Expand All @@ -54,7 +54,7 @@ func (l *PolicyList) GetPolicyList() (keeneticdto.PolicyResponse, error) {
if err != nil {
return nil, fmt.Errorf("read response body error in GetPolicyList request: %w", err)
}
var res keeneticdto.PolicyResponse
var res map[string]keeneticdto.Policy

if err := json.Unmarshal(resBytes, &res); err != nil {
return nil, fmt.Errorf("unmarshal response error in setpolicy request: %w", err)
Expand Down
2 changes: 0 additions & 2 deletions internal/dto/keeneticdto/keeneticpolicy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package keeneticdto

type PolicyResponse map[string]Policy

type Policy struct {
Description string `json:"description"`
Permit []PolicyPermit `json:"permit"`
Expand Down
35 changes: 26 additions & 9 deletions internal/storages/policy/policy.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package policy

import (
"log/slog"
"time"

"keeneticToMqtt/internal/clients/keenetic/policylist"
"keeneticToMqtt/internal/dto/homeassistantdto"
"keeneticToMqtt/internal/dto/keeneticdto"
)

type Storage struct {
policyClient *policylist.PolicyList
refreshInterval time.Duration
policies []string
logger *slog.Logger
}
//go:generate mockgen -source=policy.go -destination=../../../test/mocks/gomock/storages/policy/policy.go

type (
policyClient interface {
GetPolicyList() (map[string]keeneticdto.Policy, error)
}
logger interface {
Error(msg string, args ...any)
Info(msg string, args ...any)
}

func NewStorage(policyClient *policylist.PolicyList, refreshInterval time.Duration, logger *slog.Logger) *Storage {
// Storage store policies in-memory.
Storage struct {
policyClient policyClient
refreshInterval time.Duration
policies []string
logger logger
}
)

// NewStorage creates Storage
func NewStorage(policyClient policyClient, refreshInterval time.Duration, logger logger) *Storage {
s := &Storage{
policyClient: policyClient,
refreshInterval: refreshInterval,
Expand All @@ -25,6 +38,7 @@ func NewStorage(policyClient *policylist.PolicyList, refreshInterval time.Durati
return s
}

// Run start storage updates.
func (s *Storage) Run() chan struct{} {
done := make(chan struct{})
ticker := time.NewTicker(s.refreshInterval)
Expand All @@ -43,6 +57,8 @@ func (s *Storage) Run() chan struct{} {

return done
}

// GetPolicyList returns policy list.
func (s *Storage) GetPolicyList() []string {
if len(s.policies) == 0 {
s.refresh()
Expand All @@ -64,4 +80,5 @@ func (s *Storage) refresh() {
}

s.policies = policies
s.logger.Info("update policies", "policies", policies)
}
118 changes: 118 additions & 0 deletions internal/storages/policy/policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package policy

import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"keeneticToMqtt/internal/dto/keeneticdto"
mock_policy "keeneticToMqtt/test/mocks/gomock/storages/policy"
)

func TestStorage_GetPolicyList(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

const (
name = "name"
)
someErr := errors.New("some err")
policies := []string{"none", name}

tests := []struct {
name string
policyClient func() policyClient
refreshInterval time.Duration
policies []string
logger func() logger
expectedRes []string
}{
{
name: "success get policy list with refresh",
policyClient: func() policyClient {
policyClient := mock_policy.NewMockpolicyClient(ctrl)
policyClient.EXPECT().GetPolicyList().Return(map[string]keeneticdto.Policy{
name: {},
}, nil)

return policyClient
},
logger: func() logger {
logger := mock_policy.NewMocklogger(ctrl)
logger.EXPECT().Info(gomock.Eq("update policies"), gomock.Eq("policies"), gomock.Eq(policies))
return logger
},
expectedRes: policies,
},
{
name: "success get policy list without refresh",
policies: policies,
policyClient: func() policyClient {
policyClient := mock_policy.NewMockpolicyClient(ctrl)
return policyClient
},
logger: func() logger {
logger := mock_policy.NewMocklogger(ctrl)
return logger
},
expectedRes: policies,
},
{
name: "get policy list with error while refresh",
policyClient: func() policyClient {
policyClient := mock_policy.NewMockpolicyClient(ctrl)
policyClient.EXPECT().GetPolicyList().Return(nil, someErr)
return policyClient
},
logger: func() logger {
logger := mock_policy.NewMocklogger(ctrl)
logger.EXPECT().Error(
gomock.Eq("error while refresh policies storage"),
gomock.Eq("error"),
gomock.Eq(someErr),
)
return logger
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := NewStorage(tt.policyClient(), tt.refreshInterval, tt.logger())
if len(tt.policies) > 0 {
policy.policies = tt.policies
}
res := policy.GetPolicyList()
assert.Equal(t, tt.expectedRes, res)
})
}
}

func TestStorage_Run(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

const (
name = "name"
)
policies := []string{"none", name}

policyClient := mock_policy.NewMockpolicyClient(ctrl)
policyClient.EXPECT().GetPolicyList().Return(map[string]keeneticdto.Policy{
name: {},
}, nil)

logger := mock_policy.NewMocklogger(ctrl)
logger.EXPECT().Info(gomock.Eq("update policies"), gomock.Eq("policies"), gomock.Eq(policies))
logger.EXPECT().Info("shutdown policy storage")
storage := NewStorage(policyClient, 10*time.Millisecond, logger)
done := storage.Run()

ticker := time.NewTicker(15 * time.Millisecond)
<-ticker.C
done <- struct{}{}
<-ticker.C
return
}
Loading

0 comments on commit ac5d08f

Please sign in to comment.