Skip to content

Commit

Permalink
Fix linting errors (#576)
Browse files Browse the repository at this point in the history
* Handle return errors in tests
* remove staticcheck from linter
* ignore linting on deprecated statements

---------

Co-authored-by: Richard Hagen <richard.hagen@bouvet.no>
  • Loading branch information
Richard87 and Richard87 authored Dec 27, 2023
1 parent 5484dab commit 24bad64
Show file tree
Hide file tree
Showing 20 changed files with 803 additions and 444 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ jobs:
run: go mod download
- name: Install GolangCI Lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
- name: Install StaticCheck
run: go install honnef.co/go/tools/cmd/staticcheck@2023.1.6

- name: golangci-lint
run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions --new
run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions

# Add legacy checks since golangci-lint only tests changed files for now
- name: StaticCheck
run: staticcheck -f text `go list ./... | grep -v "pkg/client"`
- name: Go Vet
run: go vet `go list ./... | grep -v "pkg/client"`
test:
name: Unit Test
runs-on: ubuntu-latest
Expand Down
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ test:

.PHONY: lint
lint: bootstrap
golangci-lint run --max-same-issues 0 --new
staticcheck ./...
golangci-lint run --max-same-issues 0

build-kaniko:
docker run --rm -it -v $(CURRENT_FOLDER):/workspace gcr.io/kaniko-project/executor:latest --destination=$(DOCKER_REGISTRY)/radix-api-server:3hv6o --snapshotMode=time --cache=true
Expand Down Expand Up @@ -67,7 +66,6 @@ docker-push: $(addsuffix -push,$(IMAGES))
docker push $(DOCKER_REGISTRY)/$*-server:$(IMAGE_TAG)

HAS_SWAGGER := $(shell command -v swagger;)
HAS_STATICCHECK := $(shell command -v staticcheck;)
HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;)
HAS_MOCKGEN := $(shell command -v mockgen;)

Expand All @@ -78,9 +76,6 @@ endif
ifndef HAS_GOLANGCI_LINT
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2
endif
ifndef HAS_STATICCHECK
go install honnef.co/go/tools/cmd/staticcheck@v0.4.6
endif
ifndef HAS_MOCKGEN
go install github.com/golang/mock/mockgen@v1.6.0
endif
52 changes: 35 additions & 17 deletions api/alerting/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
radixfake "github.com/equinor/radix-operator/pkg/client/clientset/versioned/fake"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -50,7 +51,8 @@ func (s *HandlerTestSuite) Test_GetAlertingConfig() {
s.Run("RadixAlert exist in namespace, incorrect appname label", func() {
incorrectLabelNs, incorrectLabelAppName := "incorrect-label-ns", "any-app"
incorrectlLabelRal := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "any-alert", Labels: map[string]string{kube.RadixAppLabel: incorrectLabelAppName}}}
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(incorrectLabelNs).Create(context.Background(), &incorrectlLabelRal, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(incorrectLabelNs).Create(context.Background(), &incorrectlLabelRal, metav1.CreateOptions{})
require.NoError(s.T(), err)

sut := handler{accounts: s.accounts, namespace: incorrectLabelNs, appName: "other-app"}
config, err := sut.GetAlertingConfig(context.Background())
Expand All @@ -63,8 +65,10 @@ func (s *HandlerTestSuite) Test_GetAlertingConfig() {
multipleAlertNs, multipleAlertApp := "multiple-alert-ns", "multiple-alert"
multipleRal1 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert1", Labels: map[string]string{kube.RadixAppLabel: multipleAlertApp}}}
multipleRal2 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert2", Labels: map[string]string{kube.RadixAppLabel: multipleAlertApp}}}
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(multipleAlertNs).Create(context.Background(), &multipleRal1, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(multipleAlertNs).Create(context.Background(), &multipleRal2, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(multipleAlertNs).Create(context.Background(), &multipleRal1, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(multipleAlertNs).Create(context.Background(), &multipleRal2, metav1.CreateOptions{})
require.NoError(s.T(), err)

sut := handler{accounts: s.accounts, namespace: multipleAlertNs, appName: multipleAlertApp}
config, err := sut.GetAlertingConfig(context.Background())
Expand All @@ -75,7 +79,8 @@ func (s *HandlerTestSuite) Test_GetAlertingConfig() {
s.Run("RadixAlert not reconciled", func() {
notReconciledNs, notReconciledApp := "not-reconciled-ns", "not-reconciled-app"
notReconciledRal := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert1", Labels: map[string]string{kube.RadixAppLabel: notReconciledApp}}}
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(notReconciledNs).Create(context.Background(), &notReconciledRal, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(notReconciledNs).Create(context.Background(), &notReconciledRal, metav1.CreateOptions{})
require.NoError(s.T(), err)

sut := handler{accounts: s.accounts, namespace: notReconciledNs, appName: notReconciledApp}
config, err := sut.GetAlertingConfig(context.Background())
Expand Down Expand Up @@ -104,14 +109,16 @@ func (s *HandlerTestSuite) Test_GetAlertingConfig() {
},
Status: radixv1.RadixAlertStatus{Reconciled: &metav1.Time{Time: time.Now()}},
}
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(appNs).Create(context.Background(), &ral, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(appNs).Create(context.Background(), &ral, metav1.CreateOptions{})
require.NoError(s.T(), err)
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: operatoralert.GetAlertSecretName(alertName)},
Data: map[string][]byte{
operatoralert.GetSlackConfigSecretKeyName("receiver2"): []byte("data"),
},
}
s.accounts.UserAccount.Client.CoreV1().Secrets(appNs).Create(context.Background(), &secret, metav1.CreateOptions{})
_, err = s.accounts.UserAccount.Client.CoreV1().Secrets(appNs).Create(context.Background(), &secret, metav1.CreateOptions{})
require.NoError(s.T(), err)

sut := handler{accounts: s.accounts, namespace: appNs, appName: appName, validAlertNames: alertNames}
config, err := sut.GetAlertingConfig(context.Background())
Expand Down Expand Up @@ -154,11 +161,16 @@ func (s *HandlerTestSuite) Test_DisableAlerting() {
ral3 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert3", Labels: map[string]string{kube.RadixAppLabel: "another-app"}}}
ral4 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert4"}}
ral5 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "alert5", Labels: map[string]string{kube.RadixAppLabel: appName}}}
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral1, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral2, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral3, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral4, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts("another-ns").Create(context.Background(), &ral5, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral1, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral2, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral3, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &ral4, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts("another-ns").Create(context.Background(), &ral5, metav1.CreateOptions{})
require.NoError(s.T(), err)

s.Run("disable alerting should delete expected RadixAlert resources", func() {
sut := handler{accounts: s.accounts, namespace: namespace, appName: appName}
Expand Down Expand Up @@ -190,11 +202,16 @@ func (s *HandlerTestSuite) Test_UpdateAlertingConfig() {
app3Alert1 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "app3-alert1", Labels: map[string]string{kube.RadixAppLabel: appName3}}}
app4Alert1 := radixv1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: "app4-alert1", Labels: map[string]string{kube.RadixAppLabel: appName4}}}

s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app1Alert1, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app1Alert2, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts("another-ns").Create(context.Background(), &app2Alert1, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app3Alert1, metav1.CreateOptions{})
s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app4Alert1, metav1.CreateOptions{})
_, err := s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app1Alert1, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app1Alert2, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts("another-ns").Create(context.Background(), &app2Alert1, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app3Alert1, metav1.CreateOptions{})
require.NoError(s.T(), err)
_, err = s.accounts.UserAccount.RadixClient.RadixV1().RadixAlerts(namespace).Create(context.Background(), &app4Alert1, metav1.CreateOptions{})
require.NoError(s.T(), err)

secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: operatoralert.GetAlertSecretName("app4-alert1")},
Expand All @@ -204,7 +221,8 @@ func (s *HandlerTestSuite) Test_UpdateAlertingConfig() {
operatoralert.GetSlackConfigSecretKeyName("receiver3"): []byte(aSecretValue),
},
}
s.accounts.UserAccount.Client.CoreV1().Secrets(namespace).Create(context.Background(), &secret, metav1.CreateOptions{})
_, err = s.accounts.UserAccount.Client.CoreV1().Secrets(namespace).Create(context.Background(), &secret, metav1.CreateOptions{})
require.NoError(s.T(), err)

s.Run("multiple RadixAlert resources returns error", func() {
sut := handler{accounts: s.accounts, namespace: namespace, appName: appName1}
Expand Down
Loading

0 comments on commit 24bad64

Please sign in to comment.