From 43b332c14985267ca951bd13cdfbc935b9f6cb8f Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 16 Jun 2025 22:23:02 -0700 Subject: [PATCH] Bump Go to 1.24.4, fixed code smells --- .devcontainer/Dockerfile | 10 ++-- .github/workflows/master.yml | 4 +- .github/workflows/re-release.yml | 4 +- .github/workflows/release.yml | 4 +- Dockerfile | 2 +- go.mod | 18 +++---- go.sum | 32 +++++------ sonar-project.properties | 2 +- src/core/comparator.go | 15 +++--- src/model/snapshot.go | 18 +++---- src/model/snapshot_test.go | 91 ++++++++++++++++++++++++++++++++ src/repository/account.go | 6 +-- 12 files changed, 150 insertions(+), 56 deletions(-) create mode 100644 src/model/snapshot_test.go diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 828af5a..d4f973d 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,7 +1,11 @@ -ARG GO_VERSION=1.24.3 -ARG ALPINE_VERSION=3.21 +ARG GO_VERSION=1.24.4 +ARG ALPINE_VERSION=3.22 FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} # Install packages -RUN apk add -q --update --progress --no-cache git make openssh-client sudo \ No newline at end of file +RUN apk add -q --update --progress --no-cache \ + git \ + make \ + openssh-client \ + sudo \ No newline at end of file diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 4fc510c..19c2beb 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -17,10 +17,10 @@ jobs: with: fetch-depth: 0 - - name: Set up Go 1.24.3 + - name: Set up Go 1.24.4 uses: actions/setup-go@v5 with: - go-version: '1.24.3' + go-version: '1.24.4' - name: Start MongoDB uses: supercharge/mongodb-github-action@1.12.0 diff --git a/.github/workflows/re-release.yml b/.github/workflows/re-release.yml index eef2790..cef4b07 100644 --- a/.github/workflows/re-release.yml +++ b/.github/workflows/re-release.yml @@ -20,10 +20,10 @@ jobs: fetch-depth: 0 ref: ${{ github.event.inputs.tag }} - - name: Set up Go 1.24.3 + - name: Set up Go 1.24.4 uses: actions/setup-go@v5 with: - go-version: '1.24.3' + go-version: '1.24.4' - name: Start MongoDB uses: supercharge/mongodb-github-action@1.12.0 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5418504..b3cabbf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,10 +15,10 @@ jobs: with: fetch-depth: 0 - - name: Set up Go 1.24.3 + - name: Set up Go 1.24.4 uses: actions/setup-go@v5 with: - go-version: '1.24.3' + go-version: '1.24.4' - name: Start MongoDB uses: supercharge/mongodb-github-action@1.12.0 diff --git a/Dockerfile b/Dockerfile index 9c2e746..42a7c74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.24.3-alpine AS builder +FROM golang:1.24.4-alpine AS builder WORKDIR /app COPY . . diff --git a/go.mod b/go.mod index d3435d2..4d0dd01 100644 --- a/go.mod +++ b/go.mod @@ -1,21 +1,21 @@ module github.com/switcherapi/switcher-gitops -go 1.24.3 +go 1.24.4 require ( github.com/go-git/go-billy/v5 v5.6.2 - github.com/go-git/go-git/v5 v5.16.0 + github.com/go-git/go-git/v5 v5.16.2 github.com/golang-jwt/jwt/v5 v5.2.2 github.com/gorilla/mux v1.8.1 github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.10.0 - go.mongodb.org/mongo-driver v1.17.3 + go.mongodb.org/mongo-driver v1.17.4 ) require ( dario.cat/mergo v1.0.2 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect - github.com/ProtonMail/go-crypto v1.2.0 // indirect + github.com/ProtonMail/go-crypto v1.3.0 // indirect github.com/cloudflare/circl v1.6.1 // indirect github.com/cyphar/filepath-securejoin v0.4.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -29,18 +29,18 @@ require ( github.com/montanaflynn/stats v0.7.1 // indirect github.com/pjbgf/sha1cd v0.3.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/sergi/go-diff v1.4.0 // indirect github.com/skeema/knownhosts v1.3.1 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect - golang.org/x/crypto v0.38.0 // indirect - golang.org/x/net v0.40.0 // indirect - golang.org/x/sync v0.14.0 // indirect + golang.org/x/crypto v0.39.0 // indirect + golang.org/x/net v0.41.0 // indirect + golang.org/x/sync v0.15.0 // indirect golang.org/x/sys v0.33.0 // indirect - golang.org/x/text v0.25.0 // indirect + golang.org/x/text v0.26.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1334670..6ad9ba1 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= -github.com/ProtonMail/go-crypto v1.2.0 h1:+PhXXn4SPGd+qk76TlEePBfOfivE0zkWFenhGhFLzWs= -github.com/ProtonMail/go-crypto v1.2.0/go.mod h1:9whxjD8Rbs29b4XWbB8irEcE8KHMqaR2e7GWU1R+/PE= +github.com/ProtonMail/go-crypto v1.3.0 h1:ILq8+Sf5If5DCpHQp4PbZdS1J7HDFRXz/+xKBiRGFrw= +github.com/ProtonMail/go-crypto v1.3.0/go.mod h1:9whxjD8Rbs29b4XWbB8irEcE8KHMqaR2e7GWU1R+/PE= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -28,8 +28,8 @@ github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UN github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= -github.com/go-git/go-git/v5 v5.16.0 h1:k3kuOEpkc0DeY7xlL6NaaNg39xdgQbtH5mwCafHO9AQ= -github.com/go-git/go-git/v5 v5.16.0/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8= +github.com/go-git/go-git/v5 v5.16.2 h1:fT6ZIOjE5iEnkzKyxTHK1W4HGAsPhqEqiSAssSO77hM= +github.com/go-git/go-git/v5 v5.16.2/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8= github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8= github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ= @@ -67,8 +67,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= -github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= -github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= +github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= +github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8= github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY= @@ -88,13 +88,13 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeHxQ= -go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ= +go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw= +go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= -golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= +golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM= +golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -102,12 +102,12 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= -golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= +golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= -golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= +golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -128,8 +128,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= -golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M= +golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/sonar-project.properties b/sonar-project.properties index d68944d..58e5d5b 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -8,7 +8,7 @@ sonar.sources=src sonar.exclusions=**/*_test.go sonar.tests=src sonar.test.inclusions=**/*_test.go -sonar.coverage.exclusions=src/server/**/*,src/db/**/*,src/cmd/**/*,src/repository/**/* +sonar.coverage.exclusions=src/server/**/*,src/db/**/*,src/cmd/**/* sonar.language=go sonar.go.coverage.reportPaths=coverage.out \ No newline at end of file diff --git a/src/core/comparator.go b/src/core/comparator.go index db5c6ad..46cc69f 100644 --- a/src/core/comparator.go +++ b/src/core/comparator.go @@ -70,9 +70,10 @@ func (c *ComparatorService) RemoveDeleted(diffResult model.DiffResult) model.Dif func checkGroupDiff(left model.Snapshot, right model.Snapshot, diffType DiffType, diffResult model.DiffResult) model.DiffResult { for _, leftGroup := range left.Domain.Group { if !slices.Contains(model.GroupNames(right.Domain.Group), leftGroup.Name) { - if diffType == NEW { + switch diffType { + case NEW: appendDiffResults(string(diffType), string(GROUP), []string{}, leftGroup, &diffResult) - } else if diffType == DELETED { + case DELETED: appendDiffResults(string(diffType), string(GROUP), []string{leftGroup.Name}, nil, &diffResult) } } else { @@ -103,9 +104,10 @@ func checkConfigDiff(leftGroup model.Group, rightGroup model.Group, diffResult * for _, leftConfig := range leftGroup.Config { if !slices.Contains(model.ConfigKeys(rightGroup.Config), leftConfig.Key) { - if diffType == NEW { + switch diffType { + case NEW: appendDiffResults(string(diffType), string(CONFIG), []string{leftGroup.Name}, leftConfig, diffResult) - } else if diffType == DELETED { + case DELETED: appendDiffResults(string(diffType), string(CONFIG), []string{leftGroup.Name, leftConfig.Key}, nil, diffResult) } } else { @@ -139,10 +141,11 @@ func checkStrategyDiff(leftConfig model.Config, rightConfig model.Config, leftGr for _, leftStrategy := range leftConfig.Strategies { if !slices.Contains(model.StrategyNames(rightConfig.Strategies), leftStrategy.Strategy) { - if diffType == NEW { + switch diffType { + case NEW: appendDiffResults(string(diffType), string(STRATEGY), []string{leftGroup.Name, leftConfig.Key}, leftStrategy, diffResult) - } else if diffType == DELETED { + case DELETED: appendDiffResults(string(diffType), string(STRATEGY), []string{leftGroup.Name, leftConfig.Key, leftStrategy.Strategy}, nil, diffResult) } diff --git a/src/model/snapshot.go b/src/model/snapshot.go index 6467259..e0bf0dd 100644 --- a/src/model/snapshot.go +++ b/src/model/snapshot.go @@ -69,27 +69,27 @@ func StrategyNames(strategies []Strategy) []string { } func GetStrategyByName(strategies []Strategy, name string) Strategy { - for _, s := range strategies { - if s.Strategy == name { - return s + for i := range strategies { + if strategies[i].Strategy == name { + return strategies[i] } } return Strategy{} } func GetConfigByKey(configs []Config, key string) Config { - for _, c := range configs { - if c.Key == key { - return c + for i := range configs { + if configs[i].Key == key { + return configs[i] } } return Config{} } func GetGroupByName(groups []Group, name string) Group { - for _, g := range groups { - if g.Name == name { - return g + for i := range groups { + if groups[i].Name == name { + return groups[i] } } return Group{} diff --git a/src/model/snapshot_test.go b/src/model/snapshot_test.go new file mode 100644 index 0000000..31dbd75 --- /dev/null +++ b/src/model/snapshot_test.go @@ -0,0 +1,91 @@ +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGroups(t *testing.T) { + groups := []Group{ + {Name: "group1"}, + {Name: "group2"}, + {Name: "group3"}, + } + + t.Run("Should return group names", func(t *testing.T) { + result := GroupNames(groups) + + expected := []string{"group1", "group2", "group3"} + assert.Equal(t, expected, result, "Group names do not match") + }) + + t.Run("Should return Group by name", func(t *testing.T) { + result := GetGroupByName(groups, "group2") + + expected := Group{Name: "group2"} + assert.Equal(t, expected, result, "Expected group not found") + }) + + t.Run("Should return empty Group if not found", func(t *testing.T) { + result := GetGroupByName(groups, "group4") + + assert.Empty(t, result.Name, "Expected empty group when not found") + }) +} + +func TestConfigs(t *testing.T) { + configs := []Config{ + {Key: "config1"}, + {Key: "config2"}, + {Key: "config3"}, + } + + t.Run("Should return config keys", func(t *testing.T) { + result := ConfigKeys(configs) + + expected := []string{"config1", "config2", "config3"} + assert.Equal(t, expected, result, "Config keys do not match") + }) + + t.Run("Should return Config by key", func(t *testing.T) { + result := GetConfigByKey(configs, "config2") + + expected := Config{Key: "config2"} + assert.Equal(t, expected, result, "Expected config not found") + }) + + t.Run("Should return empty Config if not found", func(t *testing.T) { + result := GetConfigByKey(configs, "config4") + + assert.Empty(t, result.Key, "Expected empty config when not found") + }) +} + +func TestStrategies(t *testing.T) { + strategies := []Strategy{ + {Strategy: "strategy1"}, + {Strategy: "strategy2"}, + {Strategy: "strategy3"}, + } + + t.Run("Should return strategy names", func(t *testing.T) { + result := StrategyNames(strategies) + + expected := []string{"strategy1", "strategy2", "strategy3"} + assert.Equal(t, expected, result, "Strategy names do not match") + }) + + t.Run("Should return Strategy by name", func(t *testing.T) { + result := GetStrategyByName(strategies, "strategy2") + + expected := Strategy{Strategy: "strategy2"} + assert.Equal(t, expected, result, "Expected strategy not found") + }) + + t.Run("Should return empty Strategy if not found", func(t *testing.T) { + result := GetStrategyByName(strategies, "strategy4") + + assert.Empty(t, result.Strategy, "Expected empty strategy when not found") + }) +} diff --git a/src/repository/account.go b/src/repository/account.go index 6bc369d..c7b1179 100644 --- a/src/repository/account.go +++ b/src/repository/account.go @@ -6,7 +6,6 @@ import ( "time" "github.com/switcherapi/switcher-gitops/src/model" - "github.com/switcherapi/switcher-gitops/src/utils" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -160,10 +159,7 @@ func registerAccountRepositoryValidators(db *mongo.Database) { Options: indexOptions, } - _, err := collection.Indexes().CreateOne(context.Background(), indexModel) - if err != nil { - utils.LogError("Error creating index for account (environment, domain.id): %s", err.Error()) - } + collection.Indexes().CreateOne(context.Background(), indexModel) } func getUpdateFields(account *model.Account) bson.M {