github.com/jenkins-x/jx/v2@v2.1.155/pkg/tekton/metapipeline/clientfactory_test.go (about) 1 // +build unit 2 3 package metapipeline 4 5 import ( 6 "os" 7 "path/filepath" 8 "testing" 9 10 jxv1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1" 11 "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned/fake" 12 "github.com/jenkins-x/jx/v2/pkg/config" 13 "github.com/jenkins-x/jx/v2/pkg/gits" 14 "github.com/jenkins-x/jx/v2/pkg/kube" 15 "github.com/jenkins-x/jx/v2/pkg/tekton" 16 "github.com/jenkins-x/jx/v2/pkg/util" 17 "github.com/stretchr/testify/assert" 18 "k8s.io/apimachinery/pkg/runtime" 19 ) 20 21 type testTypeAndPullRef struct { 22 pipelineKind PipelineKind 23 pullRef PullRef 24 expected string 25 errorMessage string 26 } 27 28 func Test_determine_branch_identifier_from_pull_refs(t *testing.T) { 29 testCases := []testTypeAndPullRef{ 30 { 31 ReleasePipeline, NewPullRefWithPullRequest("http://foo", "master", "0967f9ecd7dd2d0acf883c7656c9dc2ad2bf9815", PullRequestRef{}), "master", "", 32 }, 33 { 34 PullRequestPipeline, NewPullRefWithPullRequest("http://foo", "master", "0967f9ecd7dd2d0acf883c7656c9dc2ad2bf9815", PullRequestRef{ID: "4554", MergeSHA: "1c313425db5b014271d0d074dd5aac635ffc617e"}), "PR-4554", "", 35 }, 36 { 37 PullRequestPipeline, NewPullRef("http://foo", "master", "0967f9ecd7dd2d0acf883c7656c9dc2ad2bf9815"), "", "pullrequest pipeline requested, but no pull requests specified", 38 }, 39 } 40 41 clientFactory := clientFactory{} 42 43 for _, testCase := range testCases { 44 actualBranchIdentifier, err := clientFactory.determineBranchIdentifier(testCase.pipelineKind, testCase.pullRef) 45 if testCase.errorMessage == "" { 46 assert.NoError(t, err) 47 } else { 48 assert.Error(t, err) 49 assert.Equal(t, testCase.errorMessage, err.Error()) 50 } 51 assert.Equal(t, testCase.expected, actualBranchIdentifier) 52 } 53 } 54 55 func Test_default_version_stream_and_url(t *testing.T) { 56 ns := "jx" 57 jxClient := fake.NewSimpleClientset() 58 59 url, ref, err := versionStreamURLAndRef(jxClient, ns) 60 assert.NoError(t, err) 61 assert.Equal(t, config.DefaultVersionsURL, url) 62 assert.Equal(t, config.DefaultVersionsRef, ref) 63 } 64 65 func Test_version_stream_and_url_from_team_setting(t *testing.T) { 66 var jxObjects []runtime.Object 67 expectedURL := "https://github.com/jenkins-x/my-jenkins-x-versions.git" 68 expectedVersion := "v1.0.0" 69 70 ns := "jx" 71 devEnv := kube.NewPermanentEnvironment("dev") 72 devEnv.Spec.Namespace = ns 73 devEnv.Spec.Kind = jxv1.EnvironmentKindTypeDevelopment 74 devEnv.Spec.TeamSettings.VersionStreamURL = expectedURL 75 devEnv.Spec.TeamSettings.VersionStreamRef = expectedVersion 76 77 jxObjects = append(jxObjects, devEnv) 78 jxClient := fake.NewSimpleClientset(jxObjects...) 79 80 url, ref, err := versionStreamURLAndRef(jxClient, ns) 81 assert.NoError(t, err) 82 assert.Equal(t, expectedURL, url) 83 assert.Equal(t, expectedVersion, ref) 84 } 85 86 func Test_clone_version_stream_from_tag(t *testing.T) { 87 ref := "v1.0.8" 88 dir, err := cloneVersionStream("https://github.com/jenkins-x/jenkins-x-versions.git", ref) 89 defer func() { 90 _ = os.RemoveAll(dir) 91 }() 92 93 assert.NoError(t, err) 94 assert.DirExists(t, dir) 95 96 args := []string{"describe", "--tags", "--abbrev=0"} 97 cmd := util.Command{ 98 Dir: dir, 99 Name: "git", 100 Args: args, 101 } 102 output, err := cmd.RunWithoutRetry() 103 assert.NoError(t, err) 104 assert.Equal(t, ref, output) 105 } 106 107 func Test_clone_version_stream_from_sha(t *testing.T) { 108 // This ref is the HEAD on https://github.com/jenkins-x/jenkins-x-versions/pull/417, which is closed and won't change. 109 ref := "72d36667196e2bfbb52b8220d55ef79747283a5b" 110 dir, err := cloneVersionStream("https://github.com/jenkins-x/jenkins-x-versions.git", ref) 111 defer func() { 112 _ = os.RemoveAll(dir) 113 }() 114 115 assert.NoError(t, err) 116 assert.DirExists(t, dir) 117 118 args := []string{"rev-parse", "HEAD"} 119 cmd := util.Command{ 120 Dir: dir, 121 Name: "git", 122 Args: args, 123 } 124 output, err := cmd.RunWithoutRetry() 125 assert.NoError(t, err) 126 assert.Equal(t, ref, output) 127 } 128 129 func Test_verify_pull_refs_on_activity(t *testing.T) { 130 branchIdentifier := "master" 131 buildNumber := "1" 132 gitInfo, _ := gits.NewGitFake().Info("/acme") 133 pullRef := NewPullRefWithPullRequest("https://github.com/jenkins-x/jx", "master", "0967f9ecd7dd2d0acf883c7656c9dc2ad2bf9815", PullRequestRef{ID: "42", MergeSHA: "db8e2d275df53477b1c6871f7d7f4281dacf3169"}) 134 135 testParams := CRDCreationParameters{ 136 PipelineName: "test-pipeline", 137 PipelineKind: PullRequestPipeline, 138 BranchIdentifier: branchIdentifier, 139 PullRef: pullRef, 140 GitInfo: *gitInfo, 141 Labels: map[string]string{"someLabel": "someValue"}, 142 EnvVars: map[string]string{"SOME_VAR": "SOME_VAL", "OTHER_VAR": "OTHER_VAL"}, 143 BuildNumber: buildNumber, 144 SourceDir: "source", 145 ServiceAccount: tekton.DefaultPipelineSA, 146 VersionsDir: filepath.Join("test_data", "stable_versions"), 147 } 148 149 clientFactory := clientFactory{} 150 151 activityKey, _, err := clientFactory.createActualCRDs(buildNumber, branchIdentifier, "", pullRef, testParams) 152 assert.NoError(t, err) 153 assert.NotNil(t, activityKey) 154 assert.NotNil(t, activityKey.PullRefs) 155 156 for _, prRef := range pullRef.pullRequests { 157 assert.Equal(t, prRef.MergeSHA, activityKey.PullRefs[prRef.ID]) 158 } 159 }