github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/get/get_pipeline_test.go (about)

     1  // +build unit
     2  
     3  package get_test
     4  
     5  import (
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/clients"
    10  	"github.com/jenkins-x/jx/v2/pkg/cmd/get"
    11  	"github.com/jenkins-x/jx/v2/pkg/cmd/testhelpers"
    12  	"github.com/jenkins-x/jx/v2/pkg/tekton"
    13  	"github.com/jenkins-x/jx/v2/pkg/tekton/syntax"
    14  	"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
    15  	tektonv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
    16  	tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
    17  	"github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake"
    18  
    19  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    20  	"github.com/stretchr/testify/assert"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  const testDevNameSpace = "jx-test"
    25  
    26  func pipelineRun(ns, repo, branch, owner, context string, now metav1.Time) *tektonv1alpha1.PipelineRun {
    27  	return &v1alpha1.PipelineRun{
    28  		ObjectMeta: metav1.ObjectMeta{
    29  			Name:      "PR1",
    30  			Namespace: ns,
    31  			Labels: map[string]string{
    32  				tekton.LabelRepo:    repo,
    33  				tekton.LabelBranch:  branch,
    34  				tekton.LabelOwner:   owner,
    35  				tekton.LabelContext: context,
    36  			},
    37  		},
    38  		Spec: v1alpha1.PipelineRunSpec{
    39  			Params: []v1alpha1.Param{
    40  				{
    41  					Name:  "version",
    42  					Value: syntax.StringParamValue("v1"),
    43  				},
    44  				{
    45  					Name:  "build_id",
    46  					Value: syntax.StringParamValue("1"),
    47  				},
    48  			},
    49  		},
    50  		Status: v1alpha1.PipelineRunStatus{
    51  			PipelineRunStatusFields: tektonv1beta1.PipelineRunStatusFields{
    52  				CompletionTime: &now,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  var pipelineCases = []struct {
    59  	desc      string
    60  	namespace string
    61  	repo      string
    62  	branch    string
    63  	owner     string
    64  	context   string
    65  }{
    66  	{"", testDevNameSpace, "testRepo", "testBranch", "testOwner", "testContext"},
    67  	{"", testDevNameSpace, "testRepo", "testBranch", "testOwner", ""},
    68  }
    69  
    70  func TestExecuteGetPipelines(t *testing.T) {
    71  	for _, v := range pipelineCases {
    72  		t.Run(v.desc, func(t *testing.T) {
    73  			// fakeout the output for the tests
    74  			out := &testhelpers.FakeOut{}
    75  			commonOpts := opts.NewCommonOptionsWithTerm(clients.NewFactory(), os.Stdin, out, os.Stderr)
    76  
    77  			// Set batchmode to true for tests
    78  			commonOpts.BatchMode = true
    79  
    80  			// Set dev namespace
    81  			commonOpts.SetDevNamespace(v.namespace)
    82  
    83  			// Fake tekton client
    84  			client := fake.NewSimpleClientset(pipelineRun(v.namespace, v.repo, v.branch, v.owner, v.context, metav1.Now()))
    85  
    86  			commonOpts.SetTektonClient(client)
    87  			command := get.NewCmdGetPipeline(commonOpts)
    88  			err := command.Execute()
    89  
    90  			// Execution should not error out
    91  			assert.NoError(t, err, "execute get pipelines")
    92  		})
    93  	}
    94  
    95  }