Skip to content

Commit

Permalink
Testing code-health workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
cveticm committed Dec 24, 2024
1 parent 7c7e163 commit 4099f9f
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/code-health.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
name: Code Health
on:
push:
branches:
- master
pull_request:
workflow_dispatch:

permissions:
pull-requests: write # For PR-specific operations
issues: write # For commenting functionality
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
with:
config: ${{ vars.PERMISSIONS_CONFIG }}
- uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: false # see https://github.com/golangci/golangci-lint-action/issues/807
- name: golangci-lint
uses: golangci/[email protected]
with:
version: v1.61.0
unit-tests:
env:
COVERAGE: coverage.out
TEST_CMD: gotestsum --junitfile unit-tests.xml --format standard-verbose --
UNIT_TAGS: unit
INTEGRATION_TAGS: integration
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
if: ${{ matrix.os=='ubuntu-latest' }}
with:
config: ${{ vars.PERMISSIONS_CONFIG }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- run: go install gotest.tools/gotestsum@latest
- run: make unit-test
- name: Test Summary
id: test_summary
uses: test-summary/[email protected]
with:
paths: unit-tests.xml
if: always() && matrix.os == 'ubuntu-latest'
- name: Upload coverage file
if: matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request'
uses: actions/upload-artifact@v4
with:
name: coverage-file
path: coverage.out
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ CLI_SOURCE_FILES?=./cmd/plugin
CLI_BINARY_NAME=binary
CLI_DESTINATION=./bin/$(CLI_BINARY_NAME)

TEST_CMD?=go test
UNIT_TAGS?=unit
COVERAGE?=coverage.out

.PHONY: build
build: ## Generate the binary in ./bin
@echo "==> Building $(CLI_BINARY_NAME) binary"
go build -o $(CLI_DESTINATION) $(CLI_SOURCE_FILES)

.PHONY: unit-test
unit-test: ## Run unit-tests
@echo "==> Running unit tests..."
$(TEST_CMD) --tags="$(UNIT_TAGS)" -race -cover -coverprofile $(COVERAGE) -count=1 ./...

.PHONY: fuzz-normalizer-test
fuzz-normalizer-test: ## Run fuzz test
@echo "==> Running fuzz test..."
$(TEST_CMD) -fuzz=Fuzz -fuzztime 50s --tags="$(UNIT_TAGS)" -race ./internal/kubernetes/operator/resources


.PHONY: help
.DEFAULT_GOAL := help
help:
Expand Down
31 changes: 31 additions & 0 deletions build/ci/check-licenses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Copyright 2024 MongoDB Inc
#
# 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.

set -Eeou pipefail

find_files() {
find . -not \( \
\( \
-wholename '*mock*' \
-o -wholename '*third_party*' \
\) -prune \
\) \
\( -name '*.go' -o -name '*.sh' \)
}

for FILE in $(find_files); do
addlicense -c "MongoDB Inc" -check "${FILE}"
done
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/go-github/v61 v61.0.0
github.com/google/uuid v1.6.0
github.com/mongodb/atlas-cli-core v0.0.0-20240904154034-c2296b7ce2bf
github.com/mongodb/mongodb-atlas-kubernetes/v2 v2.5.0
github.com/pelletier/go-toml v1.9.5
github.com/spf13/cobra v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKksrOoc=
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
github.com/mongodb/atlas-cli-core v0.0.0-20240904154034-c2296b7ce2bf h1:GIuU2Z8MzAOhZrKE5aRWazkxiYC+QOa7j+btOs55JxM=
github.com/mongodb/atlas-cli-core v0.0.0-20240904154034-c2296b7ce2bf/go.mod h1:zeBvF+hvzhmHwvjT9QuLeGrKQlsY1YlQo8v25wWzQug=
github.com/mongodb/mongodb-atlas-kubernetes/v2 v2.5.0 h1:2SzjXb9B3mVkYJfkCQXMMVjLULTNU7CXvLVKBpUe3MY=
github.com/mongodb/mongodb-atlas-kubernetes/v2 v2.5.0/go.mod h1:n4Ii1oia76D+x6Bn14KyfNxysMf6XAR6z8LfE93KHh0=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
Expand Down
33 changes: 33 additions & 0 deletions internal/cli/kubernetes/config/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 MongoDB Inc
//
// 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.

//go:build unit

package config

import (
"testing"

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

func TestGetOperatorMajorVersion(t *testing.T) {
t.Run("should return major when version has minor different of 0", func(t *testing.T) {
assert.Equal(t, "1.7.0", getOperatorMajorVersion("mongodb/mongodb-atlas-kubernetes-operator:1.7.1"))
})

t.Run("should return same major version when minor is 0", func(t *testing.T) {
assert.Equal(t, "1.7.0", getOperatorMajorVersion("mongodb/mongodb-atlas-kubernetes-operator:1.7.0"))
})
}
76 changes: 76 additions & 0 deletions internal/cli/kubernetes/config/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2022 MongoDB Inc
//
// 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.

//go:build unit

package config

import (
"testing"

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

func TestValidNamespace(t *testing.T) {
tests := []struct {
name string
targetNamespace string
expectedErr string
}{
{
name: "Valid Namespace",
targetNamespace: "valid-namespace",
expectedErr: "",
},
{
name: "Valid Empty Namespace",
targetNamespace: "",
expectedErr: "",
},
{
name: "Invalid Namespace with special characters",
targetNamespace: "invalid_namespace!",
expectedErr: "targetNamespace parameter is invalid: [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]",
},
{
name: "Invalid Namespace starting with non-alphanumeric",
targetNamespace: "-invalidnamespace",
expectedErr: "targetNamespace parameter is invalid: [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]",
},
{
name: "Invalid Namespace ending with non-alphanumeric",
targetNamespace: "invalidnamespace-",
expectedErr: "targetNamespace parameter is invalid: [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]",
},
{
name: "Invalid Namespace with uppercase letters",
targetNamespace: "InvalidNamespace",
expectedErr: "targetNamespace parameter is invalid: [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &GenerateOpts{
targetNamespace: tt.targetNamespace,
}
err := opts.ValidateTargetNamespace()

if tt.expectedErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedErr)
}
})
}
}

0 comments on commit 4099f9f

Please sign in to comment.