Skip to content

Commit

Permalink
Upgrade depedencies
Browse files Browse the repository at this point in the history
github.com/go-co-op/gocron: v1 -> v2
  • Loading branch information
Danielius1922 authored and Daniel Adam committed Feb 3, 2024
1 parent ab7a168 commit b689d68
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 74 deletions.
2 changes: 2 additions & 0 deletions certificate-authority/service/cleanDatabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service_test
import (
"context"
"errors"
"fmt"
"io"
"testing"
"time"
Expand All @@ -26,6 +27,7 @@ func TestCertificateAuthorityServerCleanUpSigningRecords(t *testing.T) {
cfg := test.MakeConfig(t)
cfg.Clients.Storage.ExtendCronParserBySeconds = true
cfg.Clients.Storage.CleanUpRecords = "*/1 * * * * *"
fmt.Printf("%v\n\n", test.MakeConfig(t))

shutDown := testService.SetUpServices(context.Background(), t, testService.SetUpServicesCertificateAuthority|testService.SetUpServicesOAuth, testService.WithCAConfig(cfg))
defer shutDown()
Expand Down
24 changes: 13 additions & 11 deletions certificate-authority/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"
"time"

gocron "github.com/go-co-op/gocron"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
grpcService "github.com/plgd-dev/hub/v2/certificate-authority/service/grpc"
storeConfig "github.com/plgd-dev/hub/v2/certificate-authority/store/config"
Expand Down Expand Up @@ -89,20 +89,22 @@ func (c *StorageConfig) Validate() error {
if c.CleanUpRecords == "" {
return nil
}
s := gocron.NewScheduler(time.Local)
if c.ExtendCronParserBySeconds {
s = s.CronWithSeconds(c.CleanUpRecords)
} else {
s = s.Cron(c.CleanUpRecords)
s, err := gocron.NewScheduler(gocron.WithLocation(time.Local))
if err != nil {
return fmt.Errorf("cannot create cron job: %w", err)
}
_, err := s.Do(func() {
// do nothing
})
defer func() {
if errS := s.Shutdown(); errS != nil {
log.Errorf("failed to shutdown cron job: %w", errS)
}
}()
_, err = s.NewJob(gocron.CronJob(c.CleanUpRecords, c.ExtendCronParserBySeconds),
gocron.NewTask(func() {
// do nothing
}))
if err != nil {
return fmt.Errorf("cleanUpRecords('%v') - %w", c.CleanUpRecords, err)
}
s.Clear()
s.Stop()
return nil
}

Expand Down
203 changes: 203 additions & 0 deletions certificate-authority/service/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// ************************************************************************
// Copyright (C) 2022 plgd.dev, s.r.o.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ************************************************************************

package service_test

import (
"testing"

"github.com/plgd-dev/hub/v2/certificate-authority/service"
"github.com/plgd-dev/hub/v2/certificate-authority/test"
"github.com/stretchr/testify/require"
)

func TestConfigValidate(t *testing.T) {
type args struct {
cfg service.Config
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Clients.Storage.ExtendCronParserBySeconds = true
c.Clients.Storage.CleanUpRecords = "*/1 * * * * *"
return c
}(),
},
},
{
name: "invalid log config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Log.Level = 42
return c
}(),
},
wantErr: true,
},
{
name: "invalid grpc api config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.APIs.GRPC.Addr = "invalid"
return c
}(),
},
wantErr: true,
},
{
name: "invalid http api config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.APIs.HTTP.Addr = "invalid"
return c
}(),
},
wantErr: true,
},
{
name: "invalid signer config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Signer.CAPool = 42
return c
}(),
},
wantErr: true,
},
{
name: "invalid clients storage config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Clients.Storage.CleanUpRecords = "invalid"
return c
}(),
},
wantErr: true,
},
{
name: "invalid clients telemetry config",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Clients.OpenTelemetryCollector.GRPC.Enabled = true
c.Clients.OpenTelemetryCollector.GRPC.Connection.Addr = ""
return c
}(),
},
wantErr: true,
},
{
name: "invalid hubID",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.HubID = "invalid"
return c
}(),
},
wantErr: true,
},
{
name: "invalid signer",
args: args{
cfg: func() service.Config {
c := test.MakeConfig(t)
c.Signer.CertFile = "invalid"
return c
}(),
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cfg.Validate()
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

func TestStorageConfigValidate(t *testing.T) {
type args struct {
cfg service.StorageConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid - disabled",
args: args{
cfg: func() service.StorageConfig {
c := test.MakeStorageConfig()
c.CleanUpRecords = ""
return c
}(),
},
},
{
name: "invalid",
args: args{
cfg: func() service.StorageConfig {
c := test.MakeStorageConfig()
c.CleanUpRecords = "invalid"
return c
}(),
},
wantErr: true,
},
{
name: "invalid db",
args: args{
cfg: func() service.StorageConfig {
c := test.MakeStorageConfig()
c.Embedded.Use = "invalid"
return c
}(),
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cfg.Validate()
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
26 changes: 14 additions & 12 deletions certificate-authority/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"time"

gocron "github.com/go-co-op/gocron"
"github.com/go-co-op/gocron/v2"
grpcService "github.com/plgd-dev/hub/v2/certificate-authority/service/grpc"
httpService "github.com/plgd-dev/hub/v2/certificate-authority/service/http"
"github.com/plgd-dev/hub/v2/certificate-authority/store"
Expand Down Expand Up @@ -59,26 +59,28 @@ func newStore(ctx context.Context, config StorageConfig, fileWatcher *fsnotify.W
if config.CleanUpRecords == "" {
return db, fl.ToFunction(), nil
}
s := gocron.NewScheduler(time.Local)
if config.ExtendCronParserBySeconds {
s = s.CronWithSeconds(config.CleanUpRecords)
} else {
s = s.Cron(config.CleanUpRecords)
s, err := gocron.NewScheduler(gocron.WithLocation(time.Local))
if err != nil {
fl.Execute()
return nil, nil, fmt.Errorf("cannot create cron job: %w", err)
}
_, err = s.Do(func() {

_, err = s.NewJob(gocron.CronJob(config.CleanUpRecords, config.ExtendCronParserBySeconds), gocron.NewTask(func() {
_, errDel := db.DeleteNonDeviceExpiredRecords(ctx, time.Now())
if errDel != nil && !errors.Is(errDel, store.ErrNotSupported) {
log.Errorf("failed to delete expired signing records: %w", errDel)
}
})
}))
if err != nil {
fl.Execute()
return nil, nil, fmt.Errorf("cannot create cron job: %w", err)
}
fl.AddFunc(s.Clear)
fl.AddFunc(s.Stop)
s.StartAsync()

fl.AddFunc(func() {
if errS := s.Shutdown(); errS != nil {
log.Errorf("failed to shutdown cron job: %w", errS)
}
})
s.Start()
return db, fl.ToFunction(), nil
}

Expand Down
38 changes: 0 additions & 38 deletions certificate-authority/service/service_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/fullstorydev/grpchan v1.1.1
github.com/fxamacker/cbor/v2 v2.5.0
github.com/go-co-op/gocron v1.37.0
github.com/go-co-op/gocron/v2 v2.2.2
github.com/gocql/gocql v1.6.0
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/golang/snappy v0.0.4
Expand Down Expand Up @@ -88,6 +88,7 @@ require (
github.com/huandu/xstrings v1.0.0 // indirect
github.com/imdario/mergo v0.3.4 // indirect
github.com/jhump/protoreflect v1.15.6 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
Expand Down
Loading

0 comments on commit b689d68

Please sign in to comment.