rules

package
v0.1.64 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

README

Requirements Rules Tests

This directory contains tests for the requirements validation rules in the DMT module linter.

Test Structure

The tests have been refactored to improve maintainability and readability by splitting the original 855-line test file into smaller, focused test files:

Test Files
  • requirements_test_helpers.go - Common test utilities and helper functions
  • requirements_edge_cases_test.go - Comprehensive tests for requirements validation including edge cases
  • module_yaml_test.go - Tests for module.yaml parsing and validation
  • license_library_test.go - Tests for license library validation
  • oss_library_test.go - Tests for OSS library validation
Test Helper Structure

The TestHelper struct provides common utilities:

type TestHelper struct {
    t *testing.T
}
Key Methods
  • CreateTempModule(name string) string - Creates a temporary module directory
  • SetupModule(modulePath, content string) - Creates module.yaml with given content
  • SetupGoHooks(modulePath, goModContent, mainGoContent string) - Sets up Go hooks files
  • RunRequirementsCheck(modulePath string) *errors.LintRuleErrorsList - Runs the requirements check
  • AssertErrors(errorList *errors.LintRuleErrorsList, expectedErrors []string) - Asserts expected errors
  • RunTestCase(tc TestCase) - Runs a complete test case
Test Case Structure
type TestCase struct {
    Name           string
    Setup          TestSetup
    ExpectedErrors []string
    Description    string
}

type TestSetup struct {
    ModuleContent string
    SetupFiles    func(string) error
}
Common Test Data

Constants are defined for common test content:

  • ValidModuleContent - Basic valid module.yaml content
  • StageModuleContent - Module with stage field
  • StageWithRequirementsContent - Module with stage and valid requirements
  • GoModWithModuleSDK - go.mod with module-sdk v0.1.0
  • GoModWithModuleSDK03 - go.mod with module-sdk v0.3.0
  • MainGoWithAppRun - main.go with app.Run() call
  • MainGoWithReadiness - main.go with app.WithReadiness() call
  • MainGoEmpty - Empty main.go

Test Categories

1. Basic Functionality Tests
  • Rule creation and initialization
  • Version constraint parsing and validation
  • Module file loading and parsing
  • Constants validation
2. Stage Requirements Tests
  • Stage field detection
  • Minimum Deckhouse version validation (1.68.0)
  • Various constraint formats (>=, >, =, ranges)
  • Error handling for invalid constraints
3. Go Hooks Tests
  • Detection of Go hooks (go.mod + module-sdk + app.Run)
  • Requirements validation for Go hooks (1.68.0 minimum)
  • Edge cases (no module-sdk, no Run calls)
4. Readiness Probes Tests
  • Detection of readiness probes (module-sdk >= 0.3 + app.WithReadiness)
  • Requirements validation for readiness probes (1.71.0 minimum)
  • Version-specific behavior
5. Integration Tests
  • Combined scenarios with multiple requirements
  • End-to-end validation workflows
  • User requirement scenarios

Running Tests

# Run all requirements tests
go test ./pkg/linters/module/rules/... -v

# Run specific test file
go test ./pkg/linters/module/rules/requirements_edge_cases_test.go -v

# Run with coverage
go test ./pkg/linters/module/rules/... -cover

Benefits of Refactoring

  1. Improved Readability - Each test file focuses on a specific aspect
  2. Better Maintainability - Easier to find and modify specific tests
  3. Reduced Duplication - Common setup logic extracted to helpers
  4. Clearer Test Intent - Test names and structure better reflect what's being tested
  5. Faster Development - Easier to add new tests without navigating large files
  6. Better Organization - Logical grouping of related test cases

Adding New Tests

When adding new tests:

  1. Use the existing TestHelper methods for common operations
  2. Follow the TestCase structure for consistent test organization
  3. Add new constants to the common test data section if needed
  4. Place tests in the appropriate file based on functionality
  5. Use descriptive test names that explain the scenario being tested

Documentation

Index

Constants

View Source
const (
	RequirementsRuleName = "requirements"
	// MinimalDeckhouseVersionForStage defines the minimum required Deckhouse version for stage usage
	MinimalDeckhouseVersionForStage = "1.68.0"
	// MinimalDeckhouseVersionForGoHooks defines the minimum required Deckhouse version for Go hooks usage
	MinimalDeckhouseVersionForGoHooks = "1.68.0"
	// MinimalDeckhouseVersionForReadinessProbes defines the minimum required Deckhouse version for readiness probes usage
	MinimalDeckhouseVersionForReadinessProbes = "1.71.0"
	// MinimalDeckhouseVersionForOptionalModules defines the minimum required Deckhouse version for optional modules usage
	MinimalDeckhouseVersionForOptionalModules = "1.73.0"

	// MinimalModuleSDKVersionRequiresDeckhouse171 defines the minimum module-sdk version that requires Deckhouse >= 1.71
	MinimalModuleSDKVersionRequiresDeckhouse171 = "0.3.0"
	// MinimalDeckhouseVersionForModuleSDK03 defines the minimum Deckhouse version required for Module-SDK >= 0.3
	MinimalDeckhouseVersionForModuleSDK03 = "1.71.0"

	// Common patterns used in Go files
	AppRunPattern = `\w+\.Run\(`
)
View Source
const (
	// Default directory permissions for test files
	DefaultDirPerm = 0755
	// Default file permissions for test files
	DefaultFilePerm = 0600
)
View Source
const (
	ValidModuleContent = `name: test-module
namespace: test`

	StageModuleContent = `name: test-module
namespace: test
stage: "General Availability"`

	StageWithRequirementsContent = `name: test-module
namespace: test
stage: "General Availability"
requirements:
  deckhouse: ">= 1.68.0"`

	GoModWithModuleSDK = `module test
require github.com/deckhouse/module-sdk v0.1.0`

	GoModWithModuleSDK03 = `module test
require github.com/deckhouse/module-sdk v0.3.0`

	MainGoWithAppRun = `package main
func main() { app.Run() }`

	MainGoWithReadiness = `package main
func main() { app.WithReadiness() }`

	MainGoEmpty = `package main
func main() { }`
)

Common test data constants

View Source
const (
	ConversionsRuleName = "conversions"
)
View Source
const (
	DefinitionFileRuleName = "definition-file"
)
View Source
const (
	HelmignoreRuleName = "helmignore"
)
View Source
const (
	LegacyReleaseFileRuleName = "legacy-release-file"
)
View Source
const (
	LicenseRuleName = "license"
)
View Source
const (
	ModuleConfigFilename = "module.yaml"
)
View Source
const (
	OSSRuleName = "oss"
)

Variables

View Source
var ValidBundles = []string{
	"Minimal",
	"Managed",
	"Default",
}

Valid bundle values

View Source
var ValidEditions = []string{
	"ce",
	"fe",
	"ee",
	"se",
	"se-plus",
	"be",
	"_default",
}

Valid edition values

Functions

func RunRequirementsCheck added in v0.1.30

func RunRequirementsCheck(modulePath string) *errors.LintRuleErrorsList

RunRequirementsCheck runs the requirements check and returns the error list

Types

type CommentStyle added in v0.1.41

type CommentStyle struct {
	LinePrefix string // Prefix for single-line comments (e.g., "//", "#")
	BlockStart string // Start of block comment (e.g., "/*", "<!--")
	BlockEnd   string // End of block comment (e.g., "*/", "-->")
	BlockLine  string // Optional prefix for lines within block (e.g., " * ")
}

CommentStyle defines how comments are formatted in different file types

type ComponentRequirement added in v0.1.30

type ComponentRequirement struct {
	ComponentType ComponentType
	MinVersion    string
	Description   string
}

ComponentRequirement defines a requirement for a specific component

type ComponentType added in v0.1.30

type ComponentType string

ComponentType represents the type of component for requirements validation

const (
	ComponentDeckhouse ComponentType = "deckhouse"
	ComponentK8s       ComponentType = "kubernetes"
	ComponentModule    ComponentType = "module"
)

type ConversionsRule

type ConversionsRule struct {
	pkg.RuleMeta
	pkg.BoolRule
}

func NewConversionsRule

func NewConversionsRule(disable bool) *ConversionsRule

func (*ConversionsRule) CheckConversions

func (r *ConversionsRule) CheckConversions(modulePath string, errorList *errors.LintRuleErrorsList)

type DeckhouseModule

type DeckhouseModule struct {
	Name          string               `json:"name"`
	Critical      bool                 `json:"critical,omitempty"`
	Namespace     string               `json:"namespace"`
	Weight        uint32               `json:"weight,omitempty"`
	Tags          []string             `json:"tags"`
	Subsystems    []string             `json:"subsystems,omitempty"`
	Stage         string               `json:"stage"`
	Description   string               `json:"description,omitempty"`
	Descriptions  ModuleDescriptions   `json:"descriptions,omitempty"`
	Requirements  *ModuleRequirements  `json:"requirements,omitempty"`
	Accessibility *ModuleAccessibility `json:"accessibility,omitempty"`
	Update        *ModuleUpdate        `json:"update,omitempty"`
}

type DefinitionFileRule

type DefinitionFileRule struct {
	pkg.RuleMeta
	pkg.BoolRule
}

func NewDefinitionFileRule

func NewDefinitionFileRule(disable bool) *DefinitionFileRule

func (*DefinitionFileRule) CheckDefinitionFile

func (r *DefinitionFileRule) CheckDefinitionFile(modulePath string, errorList *errors.LintRuleErrorsList)

type FileTypeConfig added in v0.1.41

type FileTypeConfig struct {
	Extensions    []string       // File extensions (e.g., ".go", ".py")
	CommentStyles []CommentStyle // Supported comment styles
}

FileTypeConfig defines comment styles for specific file types

type HelmignoreRule added in v0.1.28

type HelmignoreRule struct {
	pkg.RuleMeta
	pkg.BoolRule
}

func NewHelmignoreRule added in v0.1.28

func NewHelmignoreRule(disable bool) *HelmignoreRule

func (*HelmignoreRule) CheckHelmignore added in v0.1.28

func (r *HelmignoreRule) CheckHelmignore(modulePath string, errorList *errors.LintRuleErrorsList)

type LegacyReleaseFileRule added in v0.1.42

type LegacyReleaseFileRule struct {
	pkg.RuleMeta
}

func NewLegacyReleaseFileRule added in v0.1.42

func NewLegacyReleaseFileRule() *LegacyReleaseFileRule

func (*LegacyReleaseFileRule) CheckLegacyReleaseFile added in v0.1.42

func (r *LegacyReleaseFileRule) CheckLegacyReleaseFile(modulePath string, errorList *errors.LintRuleErrorsList)

type License added in v0.1.41

type License struct {
	Type        string // "CE" or "EE"
	Name        string // Human-readable name
	Template    string // License template with {{YEAR}} placeholder
	YearPattern string // Regex pattern for year validation
}

License represents a license type with its template

type LicenseInfo added in v0.1.41

type LicenseInfo struct {
	Type  string // "CE", "EE", or empty
	Year  string // Extracted year
	Valid bool   // Whether license is valid
	Error string // Error message if invalid
}

LicenseInfo contains information about parsed license

type LicenseParser added in v0.1.41

type LicenseParser struct {
	// contains filtered or unexported fields
}

LicenseParser handles license parsing and validation

func NewLicenseParser added in v0.1.41

func NewLicenseParser() *LicenseParser

NewLicenseParser creates a new license parser with default configuration

func (*LicenseParser) ParseFile added in v0.1.41

func (p *LicenseParser) ParseFile(filename string) (*LicenseInfo, error)

ParseFile parses a file and extracts license information

type LicenseRule added in v0.1.3

type LicenseRule struct {
	pkg.RuleMeta
	pkg.PathRule
}

func NewLicenseRule added in v0.1.3

func NewLicenseRule(excludeFilesRules []pkg.StringRuleExclude,
	excludeDirectoryRules []pkg.PrefixRuleExclude) *LicenseRule

func (*LicenseRule) CheckFiles added in v0.1.3

func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *errors.LintRuleErrorsList)

type ModuleAccessibility added in v0.1.33

type ModuleAccessibility struct {
	Editions map[string]ModuleEdition `json:"editions"`
}

type ModuleDescriptions added in v0.1.4

type ModuleDescriptions struct {
	English string `json:"en,omitempty"`
	Russian string `json:"ru,omitempty"`
}

type ModuleEdition added in v0.1.33

type ModuleEdition struct {
	Available        bool     `json:"available"`
	EnabledInBundles []string `json:"enabledInBundles"`
}

type ModulePlatformRequirements

type ModulePlatformRequirements struct {
	Deckhouse    string `json:"deckhouse,omitempty"`
	Kubernetes   string `json:"kubernetes,omitempty"`
	Bootstrapped bool   `json:"bootstrapped,omitempty"`
}

type ModuleRequirements

type ModuleRequirements struct {
	ModulePlatformRequirements `json:",inline"`
	ParentModules              map[string]string `json:"modules,omitempty"`
}

type ModuleUpdate added in v0.1.36

type ModuleUpdate struct {
	Versions []ModuleUpdateVersion `json:"versions,omitempty"`
}

type ModuleUpdateVersion added in v0.1.36

type ModuleUpdateVersion struct {
	From string `json:"from"`
	To   string `json:"to"`
}

type OSSRule

type OSSRule struct {
	pkg.RuleMeta
	pkg.BoolRule
}

func NewOSSRule

func NewOSSRule(disable bool) *OSSRule

func (*OSSRule) OssModuleRule

func (r *OSSRule) OssModuleRule(moduleRoot string, errorList *errors.LintRuleErrorsList)

type RequirementCheck added in v0.1.30

type RequirementCheck struct {
	Name         string
	Requirements []ComponentRequirement
	Description  string
	Detector     func(modulePath string, module *DeckhouseModule) bool
}

RequirementCheck defines a single requirement check configuration Detector returns true if the rule should be applied to the module Requirements defines the minimum versions required for this rule Description is the rule description

type RequirementsRegistry added in v0.1.30

type RequirementsRegistry struct {
	// contains filtered or unexported fields
}

RequirementsRegistry holds all requirement checks

func NewRequirementsRegistry added in v0.1.30

func NewRequirementsRegistry() *RequirementsRegistry

NewRequirementsRegistry creates a new registry with default checks

func (*RequirementsRegistry) RegisterCheck added in v0.1.30

func (r *RequirementsRegistry) RegisterCheck(check RequirementCheck)

RegisterCheck adds a new requirement check to the registry

func (*RequirementsRegistry) RunAllChecks added in v0.1.30

func (r *RequirementsRegistry) RunAllChecks(modulePath string, module *DeckhouseModule, errorList *errors.LintRuleErrorsList)

RunAllChecks executes all registered requirement checks

type RequirementsRule added in v0.1.30

type RequirementsRule struct {
	pkg.RuleMeta
}

func NewRequirementsRule added in v0.1.30

func NewRequirementsRule() *RequirementsRule

func (*RequirementsRule) CheckRequirements added in v0.1.30

func (r *RequirementsRule) CheckRequirements(modulePath string, errorList *errors.LintRuleErrorsList)

type TestCase added in v0.1.30

type TestCase struct {
	Name           string
	Setup          TestSetup
	ExpectedErrors []string
	Description    string
}

TestCase represents a single test case

type TestHelper added in v0.1.30

type TestHelper struct {
	// contains filtered or unexported fields
}

TestHelper provides common testing utilities

func NewTestHelper added in v0.1.30

func NewTestHelper(t *testing.T) *TestHelper

NewTestHelper creates a new test helper

func (*TestHelper) AssertErrors added in v0.1.30

func (h *TestHelper) AssertErrors(errorList *errors.LintRuleErrorsList, expectedErrors []string)

AssertErrors asserts that the error list contains the expected errors

func (*TestHelper) CreateTempModule added in v0.1.30

func (h *TestHelper) CreateTempModule(name string) string

CreateTempModule creates a temporary module directory for testing

func (*TestHelper) RunTestCase added in v0.1.30

func (h *TestHelper) RunTestCase(tc *TestCase)

RunTestCase runs a single test case with the given setup

func (*TestHelper) SetupGoHooks added in v0.1.30

func (h *TestHelper) SetupGoHooks(modulePath, goModContent, mainGoContent string)

SetupGoHooks creates hooks directory with go.mod and main.go files

func (*TestHelper) SetupModule added in v0.1.30

func (h *TestHelper) SetupModule(modulePath, content string)

SetupModule creates module.yaml with given content

type TestSetup added in v0.1.30

type TestSetup struct {
	ModuleContent string
	SetupFiles    func(string) error
}

TestSetup represents the setup configuration for a test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL