github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/loader/loader_mock.go (about)

     1  package loader
     2  
     3  import (
     4  	"context"
     5  
     6  	toolkit "github.com/konflux-ci/operator-toolkit/loader"
     7  
     8  	ecapiv1alpha1 "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1"
     9  	applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
    10  	"github.com/redhat-appstudio/release-service/api/v1alpha1"
    11  	tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
    12  	corev1 "k8s.io/api/core/v1"
    13  	rbac "k8s.io/api/rbac/v1"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  )
    16  
    17  const (
    18  	ApplicationComponentsContextKey toolkit.ContextKey = iota
    19  	ApplicationContextKey
    20  	EnterpriseContractConfigMapContextKey
    21  	EnterpriseContractPolicyContextKey
    22  	MatchedReleasePlansContextKey
    23  	MatchedReleasePlanAdmissionContextKey
    24  	ProcessingResourcesContextKey
    25  	ReleaseContextKey
    26  	ReleasePipelineRunContextKey
    27  	ReleasePlanAdmissionContextKey
    28  	ReleasePlanContextKey
    29  	ReleaseServiceConfigContextKey
    30  	RoleBindingContextKey
    31  	SnapshotContextKey
    32  )
    33  
    34  type mockLoader struct {
    35  	loader ObjectLoader
    36  }
    37  
    38  func NewMockLoader() ObjectLoader {
    39  	return &mockLoader{
    40  		loader: NewLoader(),
    41  	}
    42  }
    43  
    44  // GetActiveReleasePlanAdmission returns the resource and error passed as values of the context.
    45  func (l *mockLoader) GetActiveReleasePlanAdmission(ctx context.Context, cli client.Client, releasePlan *v1alpha1.ReleasePlan) (*v1alpha1.ReleasePlanAdmission, error) {
    46  	if ctx.Value(ReleasePlanAdmissionContextKey) == nil {
    47  		return l.loader.GetActiveReleasePlanAdmission(ctx, cli, releasePlan)
    48  	}
    49  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleasePlanAdmissionContextKey, &v1alpha1.ReleasePlanAdmission{})
    50  }
    51  
    52  // GetActiveReleasePlanAdmissionFromRelease returns the resource and error passed as values of the context.
    53  func (l *mockLoader) GetActiveReleasePlanAdmissionFromRelease(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*v1alpha1.ReleasePlanAdmission, error) {
    54  	if ctx.Value(ReleasePlanAdmissionContextKey) == nil {
    55  		return l.loader.GetActiveReleasePlanAdmissionFromRelease(ctx, cli, release)
    56  	}
    57  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleasePlanAdmissionContextKey, &v1alpha1.ReleasePlanAdmission{})
    58  }
    59  
    60  // GetApplication returns the resource and error passed as values of the context.
    61  func (l *mockLoader) GetApplication(ctx context.Context, cli client.Client, releasePlan *v1alpha1.ReleasePlan) (*applicationapiv1alpha1.Application, error) {
    62  	if ctx.Value(ApplicationContextKey) == nil {
    63  		return l.loader.GetApplication(ctx, cli, releasePlan)
    64  	}
    65  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ApplicationContextKey, &applicationapiv1alpha1.Application{})
    66  }
    67  
    68  // GetEnterpriseContractPolicy returns the resource and error passed as values of the context.
    69  func (l *mockLoader) GetEnterpriseContractPolicy(ctx context.Context, cli client.Client, releasePlanAdmission *v1alpha1.ReleasePlanAdmission) (*ecapiv1alpha1.EnterpriseContractPolicy, error) {
    70  	if ctx.Value(EnterpriseContractPolicyContextKey) == nil {
    71  		return l.loader.GetEnterpriseContractPolicy(ctx, cli, releasePlanAdmission)
    72  	}
    73  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, EnterpriseContractPolicyContextKey, &ecapiv1alpha1.EnterpriseContractPolicy{})
    74  }
    75  
    76  // GetEnterpriseContractConfigMap returns the resource and error passed as values of the context.
    77  func (l *mockLoader) GetEnterpriseContractConfigMap(ctx context.Context, cli client.Client) (*corev1.ConfigMap, error) {
    78  	if ctx.Value(EnterpriseContractConfigMapContextKey) == nil {
    79  		return l.loader.GetEnterpriseContractConfigMap(ctx, cli)
    80  	}
    81  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, EnterpriseContractConfigMapContextKey, &corev1.ConfigMap{})
    82  }
    83  
    84  // GetMatchingReleasePlanAdmission returns the resource and error passed as values of the context.
    85  func (l *mockLoader) GetMatchingReleasePlanAdmission(ctx context.Context, cli client.Client, releasePlan *v1alpha1.ReleasePlan) (*v1alpha1.ReleasePlanAdmission, error) {
    86  	if ctx.Value(MatchedReleasePlanAdmissionContextKey) == nil {
    87  		return l.loader.GetMatchingReleasePlanAdmission(ctx, cli, releasePlan)
    88  	}
    89  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, MatchedReleasePlanAdmissionContextKey, &v1alpha1.ReleasePlanAdmission{})
    90  }
    91  
    92  // GetMatchingReleasePlans returns the resource and error passed as values of the context.
    93  func (l *mockLoader) GetMatchingReleasePlans(ctx context.Context, cli client.Client, releasePlanAdmission *v1alpha1.ReleasePlanAdmission) (*v1alpha1.ReleasePlanList, error) {
    94  	if ctx.Value(MatchedReleasePlansContextKey) == nil {
    95  		return l.loader.GetMatchingReleasePlans(ctx, cli, releasePlanAdmission)
    96  	}
    97  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, MatchedReleasePlansContextKey, &v1alpha1.ReleasePlanList{})
    98  }
    99  
   100  // GetRelease returns the resource and error passed as values of the context.
   101  func (l *mockLoader) GetRelease(ctx context.Context, cli client.Client, name, namespace string) (*v1alpha1.Release, error) {
   102  	if ctx.Value(ReleaseContextKey) == nil {
   103  		return l.loader.GetRelease(ctx, cli, name, namespace)
   104  	}
   105  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleaseContextKey, &v1alpha1.Release{})
   106  }
   107  
   108  // GetRoleBindingFromReleaseStatus returns the resource and error passed as values of the context.
   109  func (l *mockLoader) GetRoleBindingFromReleaseStatus(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*rbac.RoleBinding, error) {
   110  	if ctx.Value(RoleBindingContextKey) == nil {
   111  		return l.loader.GetRoleBindingFromReleaseStatus(ctx, cli, release)
   112  	}
   113  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, RoleBindingContextKey, &rbac.RoleBinding{})
   114  }
   115  
   116  // GetManagedReleasePipelineRun returns the resource and error passed as values of the context.
   117  func (l *mockLoader) GetManagedReleasePipelineRun(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*tektonv1.PipelineRun, error) {
   118  	if ctx.Value(ReleasePipelineRunContextKey) == nil {
   119  		return l.loader.GetManagedReleasePipelineRun(ctx, cli, release)
   120  	}
   121  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleasePipelineRunContextKey, &tektonv1.PipelineRun{})
   122  }
   123  
   124  // GetReleasePlan returns the resource and error passed as values of the context.
   125  func (l *mockLoader) GetReleasePlan(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*v1alpha1.ReleasePlan, error) {
   126  	if ctx.Value(ReleasePlanContextKey) == nil {
   127  		return l.loader.GetReleasePlan(ctx, cli, release)
   128  	}
   129  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleasePlanContextKey, &v1alpha1.ReleasePlan{})
   130  }
   131  
   132  // GetReleaseServiceConfig returns the resource and error passed as values of the context.
   133  func (l *mockLoader) GetReleaseServiceConfig(ctx context.Context, cli client.Client, name, namespace string) (*v1alpha1.ReleaseServiceConfig, error) {
   134  	if ctx.Value(ReleaseServiceConfigContextKey) == nil {
   135  		return l.loader.GetReleaseServiceConfig(ctx, cli, name, namespace)
   136  	}
   137  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ReleaseServiceConfigContextKey, &v1alpha1.ReleaseServiceConfig{})
   138  }
   139  
   140  // GetSnapshot returns the resource and error passed as values of the context.
   141  func (l *mockLoader) GetSnapshot(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*applicationapiv1alpha1.Snapshot, error) {
   142  	if ctx.Value(SnapshotContextKey) == nil {
   143  		return l.loader.GetSnapshot(ctx, cli, release)
   144  	}
   145  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, SnapshotContextKey, &applicationapiv1alpha1.Snapshot{})
   146  }
   147  
   148  // Composite functions
   149  
   150  // GetProcessingResources returns the resource and error passed as values of the context.
   151  func (l *mockLoader) GetProcessingResources(ctx context.Context, cli client.Client, release *v1alpha1.Release) (*ProcessingResources, error) {
   152  	if ctx.Value(ProcessingResourcesContextKey) == nil {
   153  		return l.loader.GetProcessingResources(ctx, cli, release)
   154  	}
   155  	return toolkit.GetMockedResourceAndErrorFromContext(ctx, ProcessingResourcesContextKey, &ProcessingResources{})
   156  }