github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/post/step_post_install_integration_test.go (about) 1 // +build integration 2 3 package post_test 4 5 import ( 6 "os" 7 "testing" 8 9 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 10 "github.com/olli-ai/jx/v2/pkg/cmd/step/post" 11 "github.com/olli-ai/jx/v2/pkg/cmd/testhelpers" 12 13 gojenkins "github.com/jenkins-x/golang-jenkins" 14 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 15 "github.com/olli-ai/jx/v2/pkg/gits" 16 "github.com/olli-ai/jx/v2/pkg/helm" 17 "github.com/olli-ai/jx/v2/pkg/jenkins/fake" 18 "github.com/olli-ai/jx/v2/pkg/kube" 19 resources_test "github.com/olli-ai/jx/v2/pkg/kube/resources/mocks" 20 "github.com/olli-ai/jx/v2/pkg/testkube" 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 ) 27 28 func TestStepPostInstall(t *testing.T) { 29 originalJxHome, tempJxHome, err := testhelpers.CreateTestJxHomeDir() 30 assert.NoError(t, err) 31 defer func() { 32 err := testhelpers.CleanupTestJxHomeDir(originalJxHome, tempJxHome) 33 assert.NoError(t, err) 34 }() 35 originalKubeCfg, tempKubeCfg, err := testhelpers.CreateTestKubeConfigDir() 36 assert.NoError(t, err) 37 defer func() { 38 err := testhelpers.CleanupTestKubeConfigDir(originalKubeCfg, tempKubeCfg) 39 assert.NoError(t, err) 40 }() 41 42 dev := kube.CreateDefaultDevEnvironment("jx") 43 testOrg := "mytestorg" 44 testRepo := "mytestrepo" 45 stagingGitURL := "https://fake.git/" + testOrg + "/" + testRepo + ".git" 46 staging := kube.NewPermanentEnvironmentWithGit("staging", stagingGitURL) 47 48 o := post.StepPostInstallOptions{ 49 StepOptions: step.StepOptions{ 50 CommonOptions: &opts.CommonOptions{ 51 In: os.Stdin, 52 Out: os.Stdout, 53 Err: os.Stderr, 54 }, 55 }, 56 } 57 testhelpers.ConfigureTestOptionsWithResources(o.CommonOptions, 58 []runtime.Object{ 59 &corev1.ConfigMap{ 60 ObjectMeta: metav1.ObjectMeta{ 61 Name: kube.ConfigMapJenkinsX, 62 Namespace: "jx", 63 }, 64 Data: map[string]string{}, 65 }, 66 testkube.CreateFakeGitSecret(), 67 }, 68 []runtime.Object{ 69 dev, 70 staging, 71 }, 72 gits.NewGitCLI(), 73 nil, 74 helm.NewHelmCLI("helm", helm.V2, "", true), 75 resources_test.NewMockInstaller(), 76 ) 77 78 o.BatchMode = true 79 jenkinsClient := fake.NewFakeJenkins() 80 o.SetJenkinsClient(jenkinsClient) 81 o.SetGit(&gits.GitFake{}) 82 83 err = o.Run() 84 require.NoError(t, err, "failed to run jx step post install") 85 86 // assert we have a jenkins job for the staging env repo 87 AssertJenkinsJobExists(t, jenkinsClient, testOrg, testRepo) 88 89 // assert we have a webhook for the staging env repo 90 gitProvider := o.Results.GitProviders["staging"] 91 require.NotNil(t, gitProvider, "no GitProvider is registered for staging") 92 93 webhooks, err := gitProvider.ListWebHooks(testOrg, testRepo) 94 require.NoError(t, err, "failed to list webhooks for staging git repository %s", stagingGitURL) 95 96 t.Logf("found %d webhooks\n", len(webhooks)) 97 98 assert.True(t, len(webhooks) > 0, "should have at least 1 WebHook for staging git repository %s", stagingGitURL) 99 100 found := false 101 for _, webhook := range webhooks { 102 repo := webhook.Repo 103 if repo != nil && repo.Organisation == testOrg && repo.Name == testRepo { 104 t.Logf("found WebHook for staging git repository %s: %#v", stagingGitURL, webhook) 105 found = true 106 } 107 } 108 assert.True(t, found, "did not find WebHook for staging git repository %s", stagingGitURL) 109 } 110 111 // AssertJenkinsJobExists asserts that the job exists for the given organisation and repo 112 func AssertJenkinsJobExists(t *testing.T, jenkinsClient *fake.FakeJenkins, testOrg string, testRepo string) { 113 job, err := jenkinsClient.GetJobByPath(testOrg, testRepo) 114 if !assert.NoError(t, err, "failed to query Jenkins Job for %s/%s", testOrg, testRepo) { 115 DumpJenkinsJobs(t, jenkinsClient) 116 return 117 } 118 if !assert.Equal(t, job.Name, testRepo, "job.Name") { 119 DumpJenkinsJobs(t, jenkinsClient) 120 return 121 } 122 123 t.Logf("Found Jenkins Job at URL: %s\n", job.Url) 124 } 125 126 // DumpJenkinsJobs dumps the current jenkins jobs in the given client to aid debugging a failing test 127 func DumpJenkinsJobs(t *testing.T, jenkinsClient gojenkins.JenkinsClient) { 128 jobs, err := jenkinsClient.GetJobs() 129 require.NoError(t, err, "failed to get jobs") 130 131 for _, job := range jobs { 132 t.Logf("Jenkins Job: %s at %s\n", job.Name, job.Url) 133 for _, cj := range job.Jobs { 134 t.Logf("\t child Job: %s at %s\n", cj.Name, cj.Url) 135 } 136 } 137 }