github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/helper/GitManager_test.go (about) 1 package helper 2 3 import ( 4 "encoding/json" 5 test_data "github.com/devtron-labs/ci-runner/test-data" 6 "github.com/devtron-labs/ci-runner/util" 7 "os" 8 "strings" 9 "testing" 10 ) 11 12 // before running test cases locally convert WORKINGDIR to "/tmp/devtroncd" from "/devtroncd" 13 func TestGitHelper(t *testing.T) { 14 t.Run("Test1_ValidCiProjectDetailsAnonymous", func(t *testing.T) { 15 16 // Prepare test data, ANONYMOUS and SOURCE_TYPE_BRANCH_FIXED data 17 ciCdRequest := &CiCdTriggerEvent{} 18 json.Unmarshal([]byte(test_data.CiTriggerEventPayloadWithoutPrePostStep), ciCdRequest) 19 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 20 21 os.RemoveAll(util.WORKINGDIR) 22 // Call the function 23 err := CloneAndCheckout(ciProjectDetails) 24 25 // Assert the expected results 26 if err != nil { 27 t.Errorf("Error in Test1_ValidCiProjectDetailsAnonymous") 28 } 29 }) 30 t.Run("Test2_ValidCiProjectDetailsUsernamePassword", func(t *testing.T) { 31 32 // Prepare test data, USERNAME_PASSWORD and SOURCE_TYPE_BRANCH_FIXED data 33 ciCdRequest := &CiCdTriggerEvent{} 34 json.Unmarshal([]byte(test_data.CdTriggerEventPayloadWithTaskYaml), ciCdRequest) 35 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 36 37 os.RemoveAll(util.WORKINGDIR) 38 // Call the function 39 err := CloneAndCheckout(ciProjectDetails) 40 41 // Assert the expected results 42 if err != nil { 43 t.Errorf("Error in Test2_ValidCiProjectDetailsUsernamePassword") 44 } 45 }) 46 t.Run("Test3_ValidCiProjectDetailsWebhookType", func(t *testing.T) { 47 48 // Prepare test data, USERNAME_PASSWORD and WEBHOOK data 49 ciCdRequest := &CiCdTriggerEvent{} 50 json.Unmarshal([]byte(test_data.CiTriggerEventSourceTypeWebhookPRBased), ciCdRequest) 51 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 52 53 os.RemoveAll(util.WORKINGDIR) 54 // Call the function 55 err := CloneAndCheckout(ciProjectDetails) 56 57 // Assert the expected results 58 if err != nil { 59 t.Errorf("Error in Test3_ValidCiProjectDetailsWebhookType") 60 } 61 }) 62 t.Run("Test4_ValidCiProjectDetailsSSHBasedGitTrigger", func(t *testing.T) { 63 64 // Prepare test data, SSH and SOURCE_TYPE_BRANCH_FIXED data 65 ciCdRequest := &CiCdTriggerEvent{} 66 json.Unmarshal([]byte(test_data.CiTriggerEventSSHBased), ciCdRequest) 67 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 68 69 os.RemoveAll(util.WORKINGDIR) 70 // Call the function 71 err := CloneAndCheckout(ciProjectDetails) 72 73 // Assert the expected results 74 if err != nil { 75 t.Errorf("Error in Test4_ValidCiProjectDetailsSSHBasedGitTrigger") 76 } 77 }) 78 t.Run("Test5_ValidCiProjectDetailsEmptyGitCommit", func(t *testing.T) { 79 80 // Prepare test data, ANONYMOUS and SOURCE_TYPE_BRANCH_FIXED data 81 ciCdRequest := &CiCdTriggerEvent{} 82 json.Unmarshal([]byte(test_data.CiTriggerEventWithEmptyGitHash), ciCdRequest) 83 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 84 85 os.RemoveAll(util.WORKINGDIR) 86 // Call the function 87 err := CloneAndCheckout(ciProjectDetails) 88 89 // Assert the expected results 90 if err != nil { 91 t.Errorf("Error in Test5_ValidCiProjectDetailsEmptyGitCommit") 92 } 93 }) 94 t.Run("Test6_ValidCiProjectDetailsEmptyGitCommitAndSourceValue", func(t *testing.T) { 95 96 // Prepare test data, ANONYMOUS and SOURCE_TYPE_BRANCH_FIXED data 97 ciCdRequest := &CiCdTriggerEvent{} 98 json.Unmarshal([]byte(test_data.CiTriggerEventWithEmptyGitHashAndSourceValue), ciCdRequest) 99 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 100 101 os.RemoveAll(util.WORKINGDIR) 102 // Call the function 103 err := CloneAndCheckout(ciProjectDetails) 104 105 // Assert the expected results 106 if err != nil { 107 t.Errorf("Error in Test6_ValidCiProjectDetailsEmptyGitCommitAndSourceValue") 108 } 109 }) 110 t.Run("Test7_ValidCiProjectDetailsPullSubmodules", func(t *testing.T) { 111 112 // Prepare test data, ANONYMOUS and SOURCE_TYPE_BRANCH_FIXED data 113 ciCdRequest := &CiCdTriggerEvent{} 114 json.Unmarshal([]byte(test_data.CiTriggerEventWithValidGitHash), ciCdRequest) 115 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 116 117 os.RemoveAll(util.WORKINGDIR) 118 // Call the function 119 err := CloneAndCheckout(ciProjectDetails) 120 121 // Assert the expected results 122 if err != nil { 123 t.Errorf("Error in Test7_ValidCiProjectDetailsPullSubmodules") 124 } 125 }) 126 t.Run("Test8_ValidCiProjectDetailsPullSubmodulesUsernamePassword", func(t *testing.T) { 127 128 // Prepare test data, USERNAME_PASSWORD and SOURCE_TYPE_BRANCH_FIXED data 129 ciCdRequest := &CiCdTriggerEvent{} 130 json.Unmarshal([]byte(test_data.CiTriggerEventUsernamePasswordAndPullSubmodules), ciCdRequest) 131 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 132 133 os.RemoveAll(util.WORKINGDIR) 134 // Call the function 135 err := CloneAndCheckout(ciProjectDetails) 136 137 // Assert the expected results 138 if err != nil { 139 t.Errorf("Error in Test8_ValidCiProjectDetailsPullSubmodulesUsernamePassword") 140 } 141 }) 142 t.Run("Test9_ValidCiProjectDetailsInvalidCommitHash", func(t *testing.T) { 143 144 // Prepare test data, ANONYMOUS and SOURCE_TYPE_BRANCH_FIXED data 145 ciCdRequest := &CiCdTriggerEvent{} 146 json.Unmarshal([]byte(test_data.CiTriggerEventWithInValidGitHash), ciCdRequest) 147 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 148 149 clonedRepo := ciProjectDetails[0].GitRepository[strings.LastIndex(ciProjectDetails[0].GitRepository, "/"):] 150 os.RemoveAll(util.WORKINGDIR) 151 // Call the function 152 err := CloneAndCheckout(ciProjectDetails) 153 err = os.Chdir(util.WORKINGDIR + clonedRepo) 154 // Assert the expected results 155 if err == nil { 156 t.Errorf("Error in Test9_ValidCiProjectDetailsInvalidCommitHash") 157 } 158 }) 159 t.Run("Test10_ValidCiProjectDetailsInvalidUsernamePassword", func(t *testing.T) { 160 161 // Prepare test data, USERNAME_PASSWORD and SOURCE_TYPE_BRANCH_FIXED data 162 ciCdRequest := &CiCdTriggerEvent{} 163 json.Unmarshal([]byte(test_data.CiTriggerEventUsernamePasswordAndPullSubmodules), ciCdRequest) 164 ciProjectDetails := ciCdRequest.CommonWorkflowRequest.CiProjectDetails 165 ciProjectDetails[0].GitOptions.UserName = "hjgbuhibj" 166 ciProjectDetails[0].GitOptions.Password = "ihvfis" 167 clonedRepo := ciProjectDetails[0].GitRepository[strings.LastIndex(ciProjectDetails[0].GitRepository, "/"):] 168 os.RemoveAll(util.WORKINGDIR) 169 // Call the function 170 err := CloneAndCheckout(ciProjectDetails) 171 err = os.Chdir(util.WORKINGDIR + clonedRepo) 172 // Assert the expected results 173 if err == nil { 174 t.Errorf("Error in Test10_ValidCiProjectDetailsInvalidUsernamePassword") 175 } 176 }) 177 }