Skip to content

Commit

Permalink
initial commit but compilation fails
Browse files Browse the repository at this point in the history
correct db setup

creation

upsert redefined

unit tests for db

passing unit test for db layer

all functions covered

in-memory covered
  • Loading branch information
jaroslaw-pieszka committed Mar 6, 2024
1 parent 355860e commit b6114c2
Show file tree
Hide file tree
Showing 17 changed files with 526 additions and 1 deletion.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ require (
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
Expand Down Expand Up @@ -126,17 +129,21 @@ require (
go.opentelemetry.io/otel/sdk v1.23.1 // indirect
go.opentelemetry.io/otel/trace v1.23.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gotest.tools/v3 v3.1.0 // indirect
k8s.io/component-base v0.26.3 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240221221325-2ac9dc51f3f1 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ github.com/envoyproxy/go-control-plane v0.10.3/go.mod h1:fJJn/j26vwOu972OllsvAgJ
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.6.7/go.mod h1:dyJXwwfPK2VSqiB9Klm1J6romD608Ba7Hij42vrOBCo=
github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0++PMirau2/yoOwVac3AbF2w=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls=
github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
Expand Down Expand Up @@ -929,6 +930,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
Expand Down Expand Up @@ -1176,6 +1178,8 @@ go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7e
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
Expand Down Expand Up @@ -1439,6 +1443,7 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
8 changes: 8 additions & 0 deletions internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,11 @@ func (c *ConfigForPlan) ContainsAdditionalComponent(componentName string) bool {
}
return false
}

type SubaccountState struct {
ID string `json:"id"`

BetaEnabled string `json:"betaEnabled"`
UsedForProduction string `json:"usedForProduction"`
ModifiedAt int64 `json:"modifiedAt"`
}
10 changes: 10 additions & 0 deletions internal/storage/dbmodel/subaccount_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dbmodel

type SubaccountStateDTO struct {
ID string `json:"id"`

BetaEnabled string `json:"beta_enabled"`
UsedForProduction string `json:"used_for_production"`

ModifiedAt int64 `json:"modified_at_at"`
}
14 changes: 14 additions & 0 deletions internal/storage/driver/memory/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ func NewInstance(operations *operations) *instances {
}
}

func (s *instances) GetDistinctSubAccounts() ([]string, error) {
//iterate over instances and return distinct subaccounts
collectedSubAccounts := make(map[string]struct{})
for _, v := range s.instances {
collectedSubAccounts[v.SubAccountID] = struct{}{}
}
//convert map keys to slice
var subAccounts []string
for k := range collectedSubAccounts {
subAccounts = append(subAccounts, k)
}
return subAccounts, nil
}

func (s *instances) InsertWithoutEncryption(instance internal.Instance) error {
return errors.New("not implemented")
}
Expand Down
50 changes: 50 additions & 0 deletions internal/storage/driver/memory/subaccount_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package memory

import (
"sync"

"github.com/kyma-project/kyma-environment-broker/internal"
)

type SubaccountStates struct {
mutex sync.Mutex

subaccountStates map[string]internal.SubaccountState
}

func NewSubaccountStates() *SubaccountStates {
return &SubaccountStates{
subaccountStates: make(map[string]internal.SubaccountState, 0),
}
}

func (s *SubaccountStates) DeleteState(subaccountID string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.subaccountStates, subaccountID)

return nil
}

func (s *SubaccountStates) UpsertState(subaccountState internal.SubaccountState) error {
s.mutex.Lock()
defer s.mutex.Unlock()

s.subaccountStates[subaccountState.ID] = subaccountState

return nil
}

func (s *SubaccountStates) ListStates() ([]internal.SubaccountState, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

var states []internal.SubaccountState

//convert map to slice
for _, state := range s.subaccountStates {
states = append(states, state)
}

return states, nil
}
92 changes: 92 additions & 0 deletions internal/storage/driver/memory/subaccount_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package memory

import (
"github.com/kyma-project/kyma-environment-broker/internal"

Check failure on line 4 in internal/storage/driver/memory/subaccount_state_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `goimports`-ed (goimports)

Check failure on line 4 in internal/storage/driver/memory/subaccount_state_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `goimports`-ed (goimports)
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
subaccountID1 = "subaccountID1"
subaccountID2 = "subaccountID2"
)

var (
givenSubaccount1State1 = internal.SubaccountState{
ID: subaccountID1,
BetaEnabled: "true",
UsedForProduction: "NOT_SET",
ModifiedAt: 10,
}

givenSubaccount1State2 = internal.SubaccountState{
ID: subaccountID1,
BetaEnabled: "false",
UsedForProduction: "NOT_SET",
ModifiedAt: 54,
}
givenSubaccount2State3 = internal.SubaccountState{
ID: subaccountID2,
BetaEnabled: "true",
UsedForProduction: "USED_FOR_PRODUCTION",
ModifiedAt: 108,
}
)

func TestSubaccountState(t *testing.T) {
subaccountStates := NewSubaccountStates()

t.Run("should insert and fetch state", func(t *testing.T) {

err := subaccountStates.UpsertState(givenSubaccount1State1)
require.NoError(t, err)

actualStates, err := subaccountStates.ListStates()
require.NoError(t, err)
assert.Len(t, actualStates, 1)
assert.Equal(t, "true", actualStates[0].BetaEnabled)
assert.Equal(t, "NOT_SET", actualStates[0].UsedForProduction)
})

t.Run("should update subaccount state and then fetch it", func(t *testing.T) {

err := subaccountStates.UpsertState(givenSubaccount1State2)

require.NoError(t, err)
actualStates, err := subaccountStates.ListStates()
require.NoError(t, err)
assert.Len(t, actualStates, 1)
assert.Equal(t, "false", actualStates[0].BetaEnabled)
assert.Equal(t, "NOT_SET", actualStates[0].UsedForProduction)
})

t.Run("insert second subaccount state and fetch two states", func(t *testing.T) {

err := subaccountStates.UpsertState(givenSubaccount2State3)

require.NoError(t, err)
actualStates, err := subaccountStates.ListStates()
require.NoError(t, err)
assert.Len(t, actualStates, 2)
expectedStates := []internal.SubaccountState{givenSubaccount1State2, givenSubaccount2State3}

for _, expectedState := range expectedStates {
assert.Contains(t, actualStates, expectedState)

}
})

t.Run("delete second subaccount state and fetch one state", func(t *testing.T) {

err := subaccountStates.DeleteState(subaccountID2)

require.NoError(t, err)
actualStates, err := subaccountStates.ListStates()
require.NoError(t, err)
assert.Equal(t, givenSubaccount1State2, actualStates[0])
assert.Len(t, actualStates, 1)
})

}
21 changes: 21 additions & 0 deletions internal/storage/driver/postsql/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ type Instance struct {
cipher Cipher
}

func (s *Instance) GetDistinctSubAccounts() ([]string, error) {
sess := s.NewReadSession()
var (
subAccounts []string
lastErr dberr.Error
)
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
subAccounts, lastErr = sess.GetDistinctSubAccounts()
if lastErr != nil {
log.Errorf("while fetching distinct subaccounts list: %v", lastErr)
return false, nil
}
return true, nil
})
if err != nil {
return nil, lastErr
}

return subAccounts, nil
}

func NewInstance(sess postsql.Factory, operations *operations, cipher Cipher) *Instance {
return &Instance{
Factory: sess,
Expand Down
34 changes: 34 additions & 0 deletions internal/storage/driver/postsql/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,39 @@ func TestInstance(t *testing.T) {
assert.Equal(t, 0, numberOfInstancesB)
})

t.Run("Should get distinct subaccounts from active instances", func(t *testing.T) {
storageCleanup, brokerStorage, err := GetStorageForDatabaseTests()
require.NoError(t, err)
require.NotNil(t, brokerStorage)
defer func() {
err := storageCleanup()
assert.NoError(t, err)
}()

// populate database with samples
fixInstances := []internal.Instance{
*fixInstance(instanceData{val: "A1", globalAccountID: "ga1", subAccountID: "sa1", runtimeID: "runtimeID1"}),
*fixInstance(instanceData{val: "A2", globalAccountID: "ga1", subAccountID: "sa1", runtimeID: "runtimeID2"}),
*fixInstance(instanceData{val: "A3", globalAccountID: "ga1", subAccountID: "sa2", runtimeID: "runtimeID3"}),
*fixInstance(instanceData{val: "A4", globalAccountID: "ga2", subAccountID: "sa3", runtimeID: "runtimeID4"}),
*fixInstance(instanceData{val: "A5", globalAccountID: "ga2", subAccountID: "sa4", runtimeID: "runtimeID5"}),
*fixInstance(instanceData{val: "A6", globalAccountID: "ga2", subAccountID: "sa5", runtimeID: ""}),
*fixInstance(instanceData{val: "A7", globalAccountID: "ga2", subAccountID: "sa6", runtimeID: "runtimeID7"}),
}

for _, i := range fixInstances {
err = brokerStorage.Instances().Insert(i)
require.NoError(t, err)
}

// when
subaccounts, err := brokerStorage.Instances().GetDistinctSubAccounts()
require.NoError(t, err)

// then
assert.Equal(t, 6, len(subaccounts))
})

t.Run("Should fetch instances along with their operations", func(t *testing.T) {
storageCleanup, brokerStorage, err := GetStorageForDatabaseTests()
require.NoError(t, err)
Expand Down Expand Up @@ -813,6 +846,7 @@ type instanceData struct {
val string
globalAccountID string
subAccountID string
runtimeID string
expired bool
trial bool
deletedAt time.Time
Expand Down
Loading

0 comments on commit b6114c2

Please sign in to comment.