Skip to content

Commit

Permalink
[WIP] Inject Parser.Clock for testing (#1299)
Browse files Browse the repository at this point in the history
- Update TestRun & TestRoot_Parse to validate the RootSync status
- Change SourceStatus, RenderingStatus, and SyncStatus to pointers
  and add DeepCopy methods, for easier generation of test
  permutations.
  • Loading branch information
karlkfi committed Jul 1, 2024
1 parent 9bea5a9 commit 71ac1c9
Show file tree
Hide file tree
Showing 11 changed files with 853 additions and 243 deletions.
3 changes: 0 additions & 3 deletions pkg/parse/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ type cacheForCommit struct {

// needToRetry indicates whether a retry is needed.
needToRetry bool

// errs tracks all the errors encountered during the reconciliation.
errs status.MultiError
}

func (c *cacheForCommit) setParserResult(objs []ast.FileObject, parserErrs status.MultiError) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/parse/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (p *namespace) parseSource(ctx context.Context, state sourceState) ([]ast.F
//
// setSourceStatus sets the source status with a given source state and set of errors. If errs is empty, all errors
// will be removed from the status.
func (p *namespace) setSourceStatus(ctx context.Context, newStatus SourceStatus) error {
func (p *namespace) setSourceStatus(ctx context.Context, newStatus *SourceStatus) error {
p.mux.Lock()
defer p.mux.Unlock()
return p.setSourceStatusWithRetries(ctx, newStatus, defaultDenominator)
}

func (p *namespace) setSourceStatusWithRetries(ctx context.Context, newStatus SourceStatus, denominator int) error {
func (p *namespace) setSourceStatusWithRetries(ctx context.Context, newStatus *SourceStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (p *namespace) setRequiresRendering(ctx context.Context, renderingRequired
}

// setRenderingStatus implements the Parser interface
func (p *namespace) setRenderingStatus(ctx context.Context, oldStatus, newStatus RenderingStatus) error {
func (p *namespace) setRenderingStatus(ctx context.Context, oldStatus, newStatus *RenderingStatus) error {
if oldStatus.Equals(newStatus) {
return nil
}
Expand All @@ -197,7 +197,7 @@ func (p *namespace) setRenderingStatus(ctx context.Context, oldStatus, newStatus
return p.setRenderingStatusWithRetires(ctx, newStatus, defaultDenominator)
}

func (p *namespace) setRenderingStatusWithRetires(ctx context.Context, newStatus RenderingStatus, denominator int) error {
func (p *namespace) setRenderingStatusWithRetires(ctx context.Context, newStatus *RenderingStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down Expand Up @@ -251,13 +251,13 @@ func (p *namespace) setRenderingStatusWithRetires(ctx context.Context, newStatus
// SetSyncStatus implements the Parser interface
// SetSyncStatus sets the RepoSync sync status.
// `errs` includes the errors encountered during the apply step;
func (p *namespace) SetSyncStatus(ctx context.Context, newStatus SyncStatus) error {
func (p *namespace) SetSyncStatus(ctx context.Context, newStatus *SyncStatus) error {
p.mux.Lock()
defer p.mux.Unlock()
return p.setSyncStatusWithRetries(ctx, newStatus, defaultDenominator)
}

func (p *namespace) setSyncStatusWithRetries(ctx context.Context, newStatus SyncStatus, denominator int) error {
func (p *namespace) setSyncStatusWithRetries(ctx context.Context, newStatus *SyncStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/parse/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"time"

"k8s.io/utils/clock"
"kpt.dev/configsync/pkg/declared"
"kpt.dev/configsync/pkg/importer/analyzer/ast"
"kpt.dev/configsync/pkg/importer/filesystem"
Expand All @@ -29,6 +30,10 @@ import (

// Options holds configuration and core functionality required by all parsers.
type Options struct {
// Clock is used for time tracking, namely to simplify testing by allowing
// a fake clock, instead of a RealClock.
Clock clock.Clock

// Parser defines the minimum interface required for Reconciler to use a
// Parser to read configs from a filesystem.
Parser filesystem.ConfigParser
Expand Down Expand Up @@ -89,9 +94,9 @@ type Options struct {
// Parser represents a parser that can be pointed at and continuously parse a source.
type Parser interface {
parseSource(ctx context.Context, state sourceState) ([]ast.FileObject, status.MultiError)
setSourceStatus(ctx context.Context, newStatus SourceStatus) error
setRenderingStatus(ctx context.Context, oldStatus, newStatus RenderingStatus) error
SetSyncStatus(ctx context.Context, newStatus SyncStatus) error
setSourceStatus(ctx context.Context, newStatus *SourceStatus) error
setRenderingStatus(ctx context.Context, oldStatus, newStatus *RenderingStatus) error
SetSyncStatus(ctx context.Context, newStatus *SyncStatus) error
options() *Options
// SyncErrors returns all the sync errors, including remediator errors,
// validation errors, applier errors, and watch update errors.
Expand Down
18 changes: 9 additions & 9 deletions pkg/parse/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ func (p *root) parseSource(ctx context.Context, state sourceState) ([]ast.FileOb
}

// setSourceStatus implements the Parser interface
func (p *root) setSourceStatus(ctx context.Context, newStatus SourceStatus) error {
func (p *root) setSourceStatus(ctx context.Context, newStatus *SourceStatus) error {
p.mux.Lock()
defer p.mux.Unlock()
return p.setSourceStatusWithRetries(ctx, newStatus, defaultDenominator)
}

func (p *root) setSourceStatusWithRetries(ctx context.Context, newStatus SourceStatus, denominator int) error {
func (p *root) setSourceStatusWithRetries(ctx context.Context, newStatus *SourceStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (p *root) setSourceStatusWithRetries(ctx context.Context, newStatus SourceS
return nil
}

func setSourceStatusFields(source *v1beta1.SourceStatus, p Parser, newStatus SourceStatus, denominator int) {
func setSourceStatusFields(source *v1beta1.SourceStatus, p Parser, newStatus *SourceStatus, denominator int) {
cse := status.ToCSE(newStatus.Errs)
source.Commit = newStatus.Commit
switch p.options().SourceType {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (p *root) setRequiresRendering(ctx context.Context, renderingRequired bool)
}

// setRenderingStatus implements the Parser interface
func (p *root) setRenderingStatus(ctx context.Context, oldStatus, newStatus RenderingStatus) error {
func (p *root) setRenderingStatus(ctx context.Context, oldStatus, newStatus *RenderingStatus) error {
if oldStatus.Equals(newStatus) {
return nil
}
Expand All @@ -287,7 +287,7 @@ func (p *root) setRenderingStatus(ctx context.Context, oldStatus, newStatus Rend
return p.setRenderingStatusWithRetires(ctx, newStatus, defaultDenominator)
}

func (p *root) setRenderingStatusWithRetires(ctx context.Context, newStatus RenderingStatus, denominator int) error {
func (p *root) setRenderingStatusWithRetires(ctx context.Context, newStatus *RenderingStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down Expand Up @@ -338,7 +338,7 @@ func (p *root) setRenderingStatusWithRetires(ctx context.Context, newStatus Rend
return nil
}

func setRenderingStatusFields(rendering *v1beta1.RenderingStatus, p Parser, newStatus RenderingStatus, denominator int) {
func setRenderingStatusFields(rendering *v1beta1.RenderingStatus, p Parser, newStatus *RenderingStatus, denominator int) {
cse := status.ToCSE(newStatus.Errs)
rendering.Commit = newStatus.Commit
switch p.options().SourceType {
Expand Down Expand Up @@ -384,13 +384,13 @@ func setRenderingStatusFields(rendering *v1beta1.RenderingStatus, p Parser, newS
// SetSyncStatus implements the Parser interface
// SetSyncStatus sets the RootSync sync status.
// `errs` includes the errors encountered during the apply step;
func (p *root) SetSyncStatus(ctx context.Context, newStatus SyncStatus) error {
func (p *root) SetSyncStatus(ctx context.Context, newStatus *SyncStatus) error {
p.mux.Lock()
defer p.mux.Unlock()
return p.setSyncStatusWithRetries(ctx, newStatus, defaultDenominator)
}

func (p *root) setSyncStatusWithRetries(ctx context.Context, newStatus SyncStatus, denominator int) error {
func (p *root) setSyncStatusWithRetries(ctx context.Context, newStatus *SyncStatus, denominator int) error {
if denominator <= 0 {
return fmt.Errorf("The denominator must be a positive number")
}
Expand Down Expand Up @@ -456,7 +456,7 @@ func (p *root) setSyncStatusWithRetries(ctx context.Context, newStatus SyncStatu
return nil
}

func setSyncStatusFields(syncStatus *v1beta1.Status, newStatus SyncStatus, denominator int) {
func setSyncStatusFields(syncStatus *v1beta1.Status, newStatus *SyncStatus, denominator int) {
cse := status.ToCSE(newStatus.Errs)
syncStatus.Sync.Commit = newStatus.Commit
syncStatus.Sync.Git = syncStatus.Source.Git
Expand Down
Loading

0 comments on commit 71ac1c9

Please sign in to comment.