Skip to content

Commit

Permalink
Remove revive linter configuration to restore golint behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Jan 17, 2023
1 parent 5a36ac8 commit 4d2c26e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 57 deletions.
9 changes: 5 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ linters-settings:
# report about shadowed variables
check-shadowing: false

revive:
# confidence for issues, default is 0.8
confidence: 0.8

gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
Expand Down Expand Up @@ -102,6 +98,10 @@ linters-settings:
rangeValCopy:
sizeThreshold: 32

nolintlint:
require-explanation: true
require-specific: true

linters:
enable:
- megacheck
Expand All @@ -117,6 +117,7 @@ linters:
- unconvert
- misspell
- nakedret
- nolintlint

presets:
- bugs
Expand Down
1 change: 1 addition & 0 deletions cmd/uptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// main package for the uptest tooling.
package main

import (
Expand Down
26 changes: 22 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package config contains configuration options for configuring uptest runtime.
package config

import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

const (
AnnotationKeyTimeout = "uptest.upbound.io/timeout"
AnnotationKeyConditions = "uptest.upbound.io/conditions"
AnnotationKeyPreAssertHook = "uptest.upbound.io/pre-assert-hook"
// AnnotationKeyTimeout defines a test time for the annotated resource.
AnnotationKeyTimeout = "uptest.upbound.io/timeout"
// AnnotationKeyConditions defines the list of status conditions to
// assert on the tested resource.
AnnotationKeyConditions = "uptest.upbound.io/conditions"
// AnnotationKeyPreAssertHook defines the path to a pre-assert
// hook script to be executed before the resource is tested.
AnnotationKeyPreAssertHook = "uptest.upbound.io/pre-assert-hook"
// AnnotationKeyPostAssertHook defines the path to a post-assert
// hook script to be executed after the resource is tested.
AnnotationKeyPostAssertHook = "uptest.upbound.io/post-assert-hook"
AnnotationKeyPreDeleteHook = "uptest.upbound.io/pre-delete-hook"
// AnnotationKeyPreDeleteHook defines the path to a pre-delete
// hook script to be executed before the tested resource is deleted.
AnnotationKeyPreDeleteHook = "uptest.upbound.io/pre-delete-hook"
// AnnotationKeyPostDeleteHook defines the path to a post-delete
// hook script to be executed after the tested resource is deleted.
AnnotationKeyPostDeleteHook = "uptest.upbound.io/post-delete-hook"
)

// AutomatedTest represents an automated test of resource example
// manifests to be run with uptest.
type AutomatedTest struct {
Directory string

Expand All @@ -38,18 +52,22 @@ type AutomatedTest struct {
DefaultConditions []string
}

// Manifest represents a resource loaded from an example resource manifest file.
type Manifest struct {
FilePath string
Object *unstructured.Unstructured
YAML string
}

// TestCase represents a test-case to be run by kuttl.
type TestCase struct {
Timeout int
SetupScriptPath string
TeardownScriptPath string
}

// Resource represents a Kubernetes object to be tested and asserted
// by uptest.
type Resource struct {
Name string
Namespace string
Expand Down
68 changes: 40 additions & 28 deletions internal/crdschema/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package crdschema contains the implementation for crddiff, a utility
// for comparing two CRD schemas for detecting and reporting changes
// between those schemas.
package crdschema

import (
Expand Down Expand Up @@ -231,43 +234,52 @@ func filterNonBreaking(diffMap map[string]*diff.Diff) map[string]*diff.Diff {
return diffMap
}

func ignoreOptionalNewProperties(sd *diff.SchemaDiff) { // nolint:gocyclo
func ignoreOptionalNewProperties(sd *diff.SchemaDiff) {
if sd == nil || sd.Empty() {
return
}
if sd.PropertiesDiff != nil {
// optional new fields are non-breaking
filteredAddedProps := make(diff.StringList, 0, len(sd.PropertiesDiff.Added))
if sd.RequiredDiff != nil {
for _, f := range sd.PropertiesDiff.Added {
for _, r := range sd.RequiredDiff.Added {
if f == r {
filteredAddedProps = append(filteredAddedProps, f)
break
}
}
}
}
sd.PropertiesDiff.Added = filteredAddedProps
for n, csd := range sd.PropertiesDiff.Modified {
ignoreOptionalNewProperties(csd)
if csd != nil && empty(csd.PropertiesDiff) {
csd.PropertiesDiff = nil
}
if csd == nil || csd.Empty() {
delete(sd.PropertiesDiff.Modified, n)
}
}
if empty(sd.PropertiesDiff) {
sd.PropertiesDiff = nil
}
}
ignorePropertiesDiff(sd)
ignoreOptionalNewProperties(sd.ItemsDiff)
if sd.ItemsDiff != nil && sd.ItemsDiff.Empty() {
sd.ItemsDiff = nil
}
}

func ignorePropertiesDiff(sd *diff.SchemaDiff) {
if sd.PropertiesDiff == nil {
return
}
keepOptionalNewFieldsDiff(sd)
for n, csd := range sd.PropertiesDiff.Modified {
ignoreOptionalNewProperties(csd)
if csd != nil && empty(csd.PropertiesDiff) {
csd.PropertiesDiff = nil
}
if csd == nil || csd.Empty() {
delete(sd.PropertiesDiff.Modified, n)
}
}
if empty(sd.PropertiesDiff) {
sd.PropertiesDiff = nil
}
}

func keepOptionalNewFieldsDiff(sd *diff.SchemaDiff) {
// optional new fields are non-breaking
filteredAddedProps := make(diff.StringList, 0, len(sd.PropertiesDiff.Added))
if sd.RequiredDiff != nil {
for _, f := range sd.PropertiesDiff.Added {
for _, r := range sd.RequiredDiff.Added {
if f == r {
filteredAddedProps = append(filteredAddedProps, f)
break
}
}
}
}
sd.PropertiesDiff.Added = filteredAddedProps
}

func empty(sd *diff.SchemasDiff) bool {
if sd == nil || sd.Empty() {
return true
Expand Down
27 changes: 15 additions & 12 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package internal implements the uptest runtime for running
// automated tests using resource example manifests
// using kuttl.
package internal

import (
Expand Down Expand Up @@ -43,22 +46,22 @@ var (
caseDirectory = "case"
)

type PreparerOption func(*Preparer)
type preparerOption func(*preparer)

func WithDataSource(path string) PreparerOption {
return func(p *Preparer) {
func withDataSource(path string) preparerOption {
return func(p *preparer) {
p.dataSourcePath = path
}
}

func WithTestDirectory(path string) PreparerOption {
return func(p *Preparer) {
func withTestDirectory(path string) preparerOption {
return func(p *preparer) {
p.testDirectory = path
}
}

func NewPreparer(testFilePaths []string, opts ...PreparerOption) *Preparer {
p := &Preparer{
func newPreparer(testFilePaths []string, opts ...preparerOption) *preparer {
p := &preparer{
testFilePaths: testFilePaths,
testDirectory: os.TempDir(),
}
Expand All @@ -68,13 +71,13 @@ func NewPreparer(testFilePaths []string, opts ...PreparerOption) *Preparer {
return p
}

type Preparer struct {
type preparer struct {
testFilePaths []string
dataSourcePath string
testDirectory string
}

func (p *Preparer) PrepareManifests() ([]config.Manifest, error) {
func (p *preparer) prepareManifests() ([]config.Manifest, error) {
caseDirectory := filepath.Join(p.testDirectory, caseDirectory)
if err := os.MkdirAll(caseDirectory, os.ModePerm); err != nil {
return nil, errors.Wrapf(err, "cannot create directory %s", caseDirectory)
Expand Down Expand Up @@ -116,7 +119,7 @@ func (p *Preparer) PrepareManifests() ([]config.Manifest, error) {
return manifests, nil
}

func (p *Preparer) injectVariables() (map[string]string, error) {
func (p *preparer) injectVariables() (map[string]string, error) {
dataSourceMap := make(map[string]string)
if p.dataSourcePath != "" {
dataSource, err := os.ReadFile(p.dataSourcePath)
Expand All @@ -139,7 +142,7 @@ func (p *Preparer) injectVariables() (map[string]string, error) {
return inputs, nil
}

func (p *Preparer) injectValues(manifestData string, dataSourceMap map[string]string) string {
func (p *preparer) injectValues(manifestData string, dataSourceMap map[string]string) string {
// Inject data source values such as tenantID, objectID, accountID
dataSourceKeys := dataSourceRegex.FindAllStringSubmatch(manifestData, -1)
for _, dataSourceKey := range dataSourceKeys {
Expand All @@ -165,7 +168,7 @@ func generateRFC1123SubdomainCompatibleString() string {
rand.Seed(time.Now().UnixNano())
s := make([]rune, 8)
for i := range s {
s[i] = charset[rand.Intn(len(charset))] // #nosec G404: no need for crypto/rand here
s[i] = charset[rand.Intn(len(charset))] //nolint:gosec // no need for crypto/rand here
}
return fmt.Sprintf("op-%s", string(s))
}
5 changes: 3 additions & 2 deletions internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (
"github.com/upbound/uptest/internal/config"
)

// RunTest runs the specified automated test
func RunTest(o *config.AutomatedTest) error {
// Read examples and inject data source values to manifests
manifests, err := NewPreparer(o.ManifestPaths, WithDataSource(o.DataSourcePath), WithTestDirectory(o.Directory)).PrepareManifests()
manifests, err := newPreparer(o.ManifestPaths, withDataSource(o.DataSourcePath), withTestDirectory(o.Directory)).prepareManifests()
if err != nil {
return errors.Wrap(err, "cannot prepare manifests")
}

// Prepare assert environment and run tests
if err := NewTester(manifests, o).ExecuteTests(); err != nil {
if err := newTester(manifests, o).executeTests(); err != nil {
return errors.Wrap(err, "cannot execute tests")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/templates/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package templates

import _ "embed" // nolint:golint
import _ "embed"

// inputFileTemplate is the template for the input file.
//
Expand Down
4 changes: 4 additions & 0 deletions internal/templates/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package templates contains utilities for rendering kuttl test cases using
// the templates contained in the package.
package templates

import (
Expand All @@ -30,6 +32,8 @@ var fileTemplates = map[string]string{
"01-assert.yaml": assertDeletedFileTemplate,
}

// Render renders the specified list of resources as a test case
// with the specified configuration.
func Render(tc *config.TestCase, resources []config.Resource) (map[string]string, error) {
data := struct {
Resources []config.Resource
Expand Down
12 changes: 6 additions & 6 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ import (
"github.com/upbound/uptest/internal/templates"
)

func NewTester(ms []config.Manifest, opts *config.AutomatedTest) *Tester {
return &Tester{
func newTester(ms []config.Manifest, opts *config.AutomatedTest) *tester {
return &tester{
options: opts,
manifests: ms,
}
}

type Tester struct {
type tester struct {
options *config.AutomatedTest
manifests []config.Manifest
}

func (t *Tester) ExecuteTests() error {
func (t *tester) executeTests() error {
if err := t.writeKuttlFiles(); err != nil {
return errors.Wrap(err, "cannot write kuttl test files")
}
Expand All @@ -60,7 +60,7 @@ func (t *Tester) ExecuteTests() error {
return errors.Wrap(cmd.Wait(), "kuttl failed")
}

func (t *Tester) prepareConfig() (*config.TestCase, []config.Resource, error) { // nolint:gocyclo
func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { //nolint:gocyclo // TODO: can we break this?
tc := &config.TestCase{
Timeout: t.options.DefaultTimeout,
SetupScriptPath: t.options.SetupScriptPath,
Expand Down Expand Up @@ -131,7 +131,7 @@ func (t *Tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
return tc, examples, nil
}

func (t *Tester) writeKuttlFiles() error {
func (t *tester) writeKuttlFiles() error {
tc, examples, err := t.prepareConfig()
if err != nil {
return errors.Wrap(err, "cannot build examples config")
Expand Down

0 comments on commit 4d2c26e

Please sign in to comment.