github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/git_test.go (about) 1 /* 2 Copyright 2018 BlackRock, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package artifacts 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 corev1 "k8s.io/api/core/v1" 24 25 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" 26 ) 27 28 var gar = &GitArtifactReader{ 29 artifact: &v1alpha1.GitArtifact{ 30 URL: "fake", 31 Creds: &v1alpha1.GitCreds{ 32 Username: &corev1.SecretKeySelector{ 33 Key: "username", 34 LocalObjectReference: corev1.LocalObjectReference{ 35 Name: "git-secret", 36 }, 37 }, 38 Password: &corev1.SecretKeySelector{ 39 Key: "password", 40 LocalObjectReference: corev1.LocalObjectReference{ 41 Name: "git-secret", 42 }, 43 }, 44 }, 45 }, 46 } 47 48 func TestNewGitReader(t *testing.T) { 49 t.Run("Given configuration, get new git reader", func(t *testing.T) { 50 reader, err := NewGitReader(&v1alpha1.GitArtifact{}) 51 assert.NoError(t, err) 52 assert.NotNil(t, reader) 53 }) 54 55 t.Run("bad clone dir", func(t *testing.T) { 56 _, err := NewGitReader(&v1alpha1.GitArtifact{CloneDirectory: "/abc/../opt"}) 57 assert.Error(t, err) 58 assert.Contains(t, err.Error(), "not allowed") 59 }) 60 61 t.Run("bad file path", func(t *testing.T) { 62 _, err := NewGitReader(&v1alpha1.GitArtifact{FilePath: "abc/efg/../../../root"}) 63 assert.Error(t, err) 64 assert.Contains(t, err.Error(), "not allowed") 65 }) 66 } 67 68 func TestGetRemote(t *testing.T) { 69 t.Run("Test git remote", func(t *testing.T) { 70 remote := gar.getRemote() 71 assert.Equal(t, DefaultRemote, remote) 72 }) 73 } 74 75 func TestGetBranchOrTag(t *testing.T) { 76 t.Run("Given a git minio, get the branch or tag", func(t *testing.T) { 77 br := gar.getBranchOrTag() 78 assert.Equal(t, "refs/heads/master", br.Branch.String()) 79 gar.artifact.Branch = "br" 80 br = gar.getBranchOrTag() 81 assert.NotEqual(t, "refs/heads/master", br.Branch.String()) 82 gar.artifact.Tag = "t" 83 tag := gar.getBranchOrTag() 84 assert.NotEqual(t, "refs/heads/master", tag.Branch.String()) 85 }) 86 87 t.Run("Given a git minio with a specific ref, get the ref", func(t *testing.T) { 88 gar.artifact.Ref = "refs/something/weird/or/specific" 89 br := gar.getBranchOrTag() 90 assert.Equal(t, "refs/something/weird/or/specific", br.Branch.String()) 91 }) 92 }