Skip to content

Commit

Permalink
fix(tests): unit tests on surrogate, add default provider name constant
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Jul 2, 2024
1 parent fdcafec commit 2598476
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 54 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/non-regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
workflow_dispatch:

env:
GO_VERSION: '1.21'
GO_VERSION: '1.22'

jobs:
lint-validation:
Expand Down Expand Up @@ -34,24 +34,7 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}
- name: Run unit static tests
run: go test -v -race $(go list ./... | grep -v pkg/storage)
unit-test-golang-with-services:
needs: lint-validation
name: Unit tests with external services
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: Build and run the docker stack
run: |
docker network create your_network || true
docker compose -f docker-compose.yml.test up -d --build --force-recreate --remove-orphans
- name: Run pkg storage tests
run: docker compose -f docker-compose.yml.test exec -T souin go test -v -race ./pkg/storage
run: go test -v
validate-prod-container-building:
needs: unit-test-golang-with-services
name: Validate that the container build for prod
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/plugins-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build current Souin as caddy module with referenced Souin core version when merge on master
run: cd plugins/caddy && xcaddy build --with github.com/${{ github.repository }}/plugins/caddy@$(git rev-parse --short "$GITHUB_SHA")
run: cd plugins/caddy && xcaddy build --with github.com/${{ github.repository }}/plugins/caddy@$(git rev-parse --short "$GITHUB_SHA") --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
4 changes: 2 additions & 2 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build Souin as caddy module
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../..
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
-
name: Run Caddy tests
run: cd plugins/caddy && go test -v ./...
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build Souin as caddy module for current commit
run: cd souin/plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../..
run: cd souin/plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
-
name: Run detached caddy
run: cd souin/plugins/caddy && ./caddy run --config ../../docs/cache-tests/cache-tests-caddyfile --adapter caddyfile &
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/workflow_plugins_generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build Souin as caddy module
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../..
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
-
name: Run Caddy tests
run: cd plugins/caddy && go test -v ./...
Expand Down Expand Up @@ -121,7 +121,7 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build Souin as caddy module for current commit
run: cd souin/plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../..
run: cd souin/plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
-
name: Run detached caddy
run: cd souin/plugins/caddy && ./caddy run --config ../../docs/cache-tests/cache-tests-caddyfile --adapter caddyfile &
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ build-caddy: ## Build caddy binary
cd plugins/caddy && \
go mod tidy && \
go mod download && \
XCADDY_RACE_DETECTOR=1 XCADDY_DEBUG=1 xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../..
XCADDY_RACE_DETECTOR=1 XCADDY_DEBUG=1 xcaddy build \
--with github.com/darkweak/souin/plugins/caddy=./ \
--with github.com/darkweak/souin=../.. \
--with github.com/darkweak/storages/badger/caddy \
--with github.com/darkweak/storages/etcd/caddy \
--with github.com/darkweak/storages/nuts/caddy \
--with github.com/darkweak/storages/olric/caddy \
--with github.com/darkweak/storages/otter/caddy \
--with github.com/darkweak/storages/redis/caddy

build-dev: env-dev ## Build containers with dev env vars
$(DC_BUILD) souin
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/defaultProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Factory(c configurationtypes.AbstractConfigurationInterface) (types.Storer,

// Name returns the storer name
func (provider *Default) Name() string {
return "DEFAULT"
return types.DefaultStorageName
}

// MapKeys method returns a map with the key and value
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/darkweak/storages/core"
)

const DefaultStorageName = "DEFAULT"

type Storer interface {
MapKeys(prefix string) map[string]string
ListKeys() []string
Expand Down
2 changes: 1 addition & 1 deletion pkg/surrogate/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf
if configuration, ok := config.GetSurrogateKeys()["_configuration"]; ok {
storer := core.GetRegisteredStorer(configuration.SurrogateConfiguration.Storer)
if storer == nil {
storer = core.GetRegisteredStorer("DEFAULT")
storer = core.GetRegisteredStorer(types.DefaultStorageName)
if storer == nil {
config.GetLogger().Sugar().Errorf("Impossible to retrieve the storers %s, nuts neither for the surrogate-keys", configuration.SurrogateConfiguration.Storer)
}
Expand Down
40 changes: 26 additions & 14 deletions pkg/surrogate/providers/common_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package providers

// const (
// baseHeaderValue = "test0, test1, test2, test3, test4"
// emptyHeaderValue = ""
// )

/*
func mockCommonProvider() *baseStorage /*, func() error {
instanciator, _ := storage.NewStorageFromName("nuts")
import (
"fmt"
"net/http"
"strings"
"sync"
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/storage"
"github.com/darkweak/souin/tests"
"github.com/darkweak/storages/core"
"go.uber.org/zap"
)

const (
baseHeaderValue = "test0, test1, test2, test3, test4"
emptyHeaderValue = ""
)

func mockCommonProvider() *baseStorage {
memoryStorer, _ := storage.Factory(mockConfiguration(tests.BaseConfiguration))
core.RegisterStorage(memoryStorer)
config := tests.MockConfiguration(tests.NutsConfiguration)
config.DefaultCache.Badger.Configuration = nil
storer, _ := instanciator(config)
sss := &SouinSurrogateStorage{
baseStorage: &baseStorage{
Storage: storer,
Storage: memoryStorer,
Keys: make(map[string]configurationtypes.SurrogateKeys),
keysRegexp: make(map[string]keysRegexpInner),
dynamic: true,
Expand All @@ -24,7 +37,7 @@ func mockCommonProvider() *baseStorage /*, func() error {

sss.baseStorage.parent = sss

return sss.baseStorage /*, storer.Close
return sss.baseStorage
}

func TestBaseStorage_ParseHeaders(t *testing.T) {
Expand Down Expand Up @@ -126,8 +139,8 @@ func TestBaseStorage_Store(t *testing.T) {
_ = bs.Store(&res, "/some")

storageSize := len(bs.Storage.MapKeys(surrogatePrefix))
if storageSize != 9 {
t.Errorf("The surrogate storage should contain 9 stored elements, %v given: %#v.\n", storageSize, bs.Storage.ListKeys())
if storageSize != 6 {
t.Errorf("The surrogate storage should contain 6 stored elements, %v given: %#v.\n", storageSize, bs.Storage.MapKeys(""))
}

value = bs.Storage.Get(surrogatePrefix + "something")
Expand Down Expand Up @@ -159,4 +172,3 @@ func TestBaseStorage_Store_Load(t *testing.T) {
t.Errorf("The surrogate storage should contain %d stored elements, %d given.", length+1, len(strings.Split(string(v), ",")))
}
}
*/
15 changes: 10 additions & 5 deletions pkg/surrogate/providers/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/storage"
"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/storages/core"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
yaml "gopkg.in/yaml.v3"
Expand Down Expand Up @@ -40,7 +43,7 @@ default_cache:
}

type testConfiguration struct {
defaultCache *configurationtypes.DefaultCache `yaml:"default_cache"`
DefaultCache *configurationtypes.DefaultCache `yaml:"default_cache"`
}

func (*testConfiguration) GetUrls() map[string]configurationtypes.URL {
Expand All @@ -50,7 +53,7 @@ func (*testConfiguration) GetPluginName() string {
return ""
}
func (t *testConfiguration) GetDefaultCache() configurationtypes.DefaultCacheInterface {
return t.defaultCache
return t.DefaultCache
}
func (*testConfiguration) GetAPI() configurationtypes.API {
return configurationtypes.API{}
Expand Down Expand Up @@ -106,10 +109,12 @@ func TestSurrogateFactory(t *testing.T) {
akamaiConfiguration := mockConfiguration(cdnConfigurationAkamai)
fastlyConfiguration := mockConfiguration(cdnConfigurationFastly)
souinConfiguration := mockConfiguration(cdnConfigurationSouin)
memoryStorer, _ := storage.Factory(souinConfiguration)
core.RegisterStorage(memoryStorer)

akamaiProvider := SurrogateFactory(akamaiConfiguration, "nuts")
fastlyProvider := SurrogateFactory(fastlyConfiguration, "nuts")
souinProvider := SurrogateFactory(souinConfiguration, "nuts")
akamaiProvider := SurrogateFactory(akamaiConfiguration, types.DefaultStorageName)
fastlyProvider := SurrogateFactory(fastlyConfiguration, types.DefaultStorageName)
souinProvider := SurrogateFactory(souinConfiguration, types.DefaultStorageName)

if akamaiProvider == nil {
t.Error("Impossible to create the Akamai surrogate provider instance")
Expand Down
Loading

0 comments on commit 2598476

Please sign in to comment.