Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavin1996 <[email protected]>
  • Loading branch information
SudhanshuBawane committed Jan 9, 2025
1 parent 33d8d94 commit 0a6be80
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions v2/fallback_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package v2

import (
"errors"
"fmt"
"net/url"
"path"
"sync"
"time"

stringsutil "github.com/sensu/core/v2/internal/stringutil"
)
Expand All @@ -13,6 +16,46 @@ const (
FallbackPipelineResource = "fallbackPipeline"
)

//// FallbackPipeline defines a list of pipelines with independent execution.
//type FallbackPipeline struct {
// // Metadata contains the name, namespace, labels, and annotations.
// ObjectMeta `protobuf:"bytes,1,opt,name=Metadata,proto3,embedded=Metadata" json:"metadata,omitempty"`
// // FallbackpipelineList contains one or more pipeline list.
// PipelineList []*ResourceReference `protobuf:"bytes,2,rep,name=pipelineList,proto3" json:"pipelinelist" yaml:"pipelinelist"`
// XXX_NoUnkeyedLiteral struct{} `json:"-"`
// XXX_unrecognized []byte `json:"-"`
// XXX_sizecache int32 `json:"-"`
//}

// ExecuteFallbackPipeline executes each pipeline independently, logging errors if any fail.
func (f *FallbackPipeline) ExecuteFallbackPipeline() {
var wg sync.WaitGroup

for _, pipelineRef := range f.PipelineList {
wg.Add(1)
go func(pipeline *ResourceReference) {
defer wg.Done()
if err := executePipeline(pipeline); err != nil {
fmt.Printf("Pipeline %s failed: %v\n", pipeline.Name, err)
} else {
fmt.Printf("Pipeline %s succeeded.\n", pipeline.Name)
}
}(pipelineRef)
}

wg.Wait() // Wait for all pipelines to finish
}

// Simulates the execution of a single pipeline
func executePipeline(pipeline *ResourceReference) error {
fmt.Printf("Executing pipeline: %s\n", pipeline.Name)
time.Sleep(1 * time.Second)
if pipeline.Name == "fail" {
return errors.New("simulated pipeline failure")
}
return nil
}

// GetObjectMeta returns the object metadata for the resource.
func (f *FallbackPipeline) GetObjectMeta() ObjectMeta {
return f.ObjectMeta
Expand Down Expand Up @@ -46,12 +89,11 @@ func (f *FallbackPipeline) URIPath() string {
return path.Join(URLPrefix, "namespaces", url.PathEscape(f.Namespace), FallbackPipelineResource, url.PathEscape(f.Name))
}

// // Validate checks if a pipeline resource passes validation rules.
// Validate checks if a pipeline resource passes validation rules.
func (f *FallbackPipeline) Validate() error {
if err := ValidateName(f.ObjectMeta.Name); err != nil {
return errors.New("name " + err.Error())
}

if f.ObjectMeta.Namespace == "" {
return errors.New("namespace must be set")
}
Expand All @@ -74,16 +116,15 @@ func (f *FallbackPipeline) Fields() map[string]string {
return FallbackPipelineFields(f)
}

// // FixturePipeline returns a testing fixture for a Pipeline object.
// FixtureFallbackPipeline returns a testing fixture for a FallbackPipeline object.
func FixtureFallbackPipeline(name, namespace string) *FallbackPipeline {
return &FallbackPipeline{
ObjectMeta: NewObjectMeta(name, namespace),
PipelineList: []*ResourceReference{},
}
}

// // FixturePipelineReference returns a testing fixture for a ResourceReference
// // object referencing a corev2.Pipeline.
// FixtureFallbackPipelineReference returns a testing fixture for a ResourceReference.
func FixtureFallbackPipelineReference(name string) *ResourceReference {
return &ResourceReference{
APIVersion: "core/v2",
Expand Down

0 comments on commit 0a6be80

Please sign in to comment.