github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/builds/build_info_test.go (about) 1 // +build unit 2 3 package builds_test 4 5 import ( 6 "io/ioutil" 7 "path/filepath" 8 "testing" 9 10 "github.com/ghodss/yaml" 11 "github.com/olli-ai/jx/v2/pkg/builds" 12 "github.com/olli-ai/jx/v2/pkg/tests" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 corev1 "k8s.io/api/core/v1" 16 ) 17 18 func TestCreateBuildPodInfo(t *testing.T) { 19 t.Parallel() 20 21 pod1File := AssertLoadPod(t, filepath.Join("test_data", "pod1.yml")) 22 if pod1File != nil { 23 b := builds.CreateBuildPodInfo(pod1File) 24 25 //log.Logger().Infof("Found build info %#v\n", b) 26 27 assert.Equal(t, "jenkins-x-jenkins-x-serverless-PR-52-6", b.Name, "Name") 28 assert.Equal(t, "jenkins-x", b.Organisation, "Organisation") 29 assert.Equal(t, "57be1dc2-ddb4-11e8-8ea8-0a580a300275-h59fj", b.PodName, "PodName") 30 assert.Equal(t, "jenkins-x-serverless", b.Repository, "Repository") 31 assert.Equal(t, "PR-52", b.Branch, "Branch") 32 assert.Equal(t, "6", b.Build, "Build") 33 assert.Equal(t, "jenkins-x/jenkins-x-serverless/PR-52", b.Pipeline, "Pipeline") 34 assert.Equal(t, "b662eb177fdd4252220399aa8da809411d87b8ed", b.LastCommitSHA, "LastCommitSHA") 35 assert.Equal(t, "https://github.com/jenkins-x/jenkins-x-serverless.git", b.GitURL, "GitURL") 36 assert.Equal(t, "jenkinsxio/jenkins-cwp:0.1.33", b.FirstStepImage, "FirstStepImage") 37 } 38 } 39 40 func TestBuildInfoFilter(t *testing.T) { 41 t.Parallel() 42 43 tests := []struct { 44 GitURL string 45 ExpectedOwner string 46 ExpectedRepo string 47 ExpectedBranch string 48 }{ 49 { 50 GitURL: "https://github.com/jenkins-x/jenkins-x-platform/pull/6504", 51 ExpectedOwner: "jenkins-x", 52 ExpectedRepo: "jenkins-x-platform", 53 ExpectedBranch: "PR-6504", 54 }, 55 { 56 GitURL: "https://github.com/jenkins-x/jx.git", 57 ExpectedOwner: "jenkins-x", 58 ExpectedRepo: "jx", 59 }, 60 } 61 62 for _, tt := range tests { 63 filter := builds.BuildPodInfoFilter{ 64 GitURL: tt.GitURL, 65 } 66 err := filter.Validate() 67 require.NoError(t, err, "failed to validate filter with URL %s", tt.GitURL) 68 assert.Equal(t, tt.ExpectedOwner, filter.Owner, "filter.Owner") 69 assert.Equal(t, tt.ExpectedRepo, filter.Repository, "filter.Repository") 70 assert.Equal(t, tt.ExpectedBranch, filter.Branch, "filter.Branch") 71 t.Logf("filter on GitURL %s populated %s/%s branch %s", filter.GitURL, filter.Owner, filter.Repository, filter.Branch) 72 } 73 } 74 75 func AssertLoadPod(t *testing.T, fileName string) *corev1.Pod { 76 if tests.AssertFileExists(t, fileName) { 77 pod := &corev1.Pod{} 78 data, err := ioutil.ReadFile(fileName) 79 if assert.NoError(t, err, "Failed to load file %s", fileName) { 80 err = yaml.Unmarshal(data, pod) 81 if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) { 82 return pod 83 } 84 85 } 86 } 87 return nil 88 }