github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/get/get_activity_test.go (about) 1 // +build unit 2 3 package get_test 4 5 import ( 6 "os" 7 "testing" 8 "time" 9 10 "github.com/olli-ai/jx/v2/pkg/cmd/get" 11 12 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 13 "github.com/olli-ai/jx/v2/pkg/cmd/testhelpers" 14 "github.com/olli-ai/jx/v2/pkg/gits" 15 helm_test "github.com/olli-ai/jx/v2/pkg/helm/mocks" 16 resources_test "github.com/olli-ai/jx/v2/pkg/kube/resources/mocks" 17 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 18 "k8s.io/apimachinery/pkg/runtime" 19 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 func TestGetActivity(t *testing.T) { 25 RegisterFailHandler(Fail) 26 RunSpecs(t, "Get Activity Suite") 27 } 28 29 var _ = Describe("get activity", func() { 30 Describe("Run()", func() { 31 var ( 32 originalRepoOwner string 33 originalRepoName string 34 originalJobName string 35 originalBranchName string 36 37 sort bool 38 err error 39 stdout *testhelpers.FakeOut 40 ) 41 42 BeforeEach(func() { 43 originalRepoOwner = os.Getenv("REPO_OWNER") 44 originalRepoName = os.Getenv("REPO_NAME") 45 originalJobName = os.Getenv("JOB_NAME") 46 originalBranchName = os.Getenv("BRANCH_NAME") 47 48 os.Setenv("REPO_OWNER", "jx-testing") 49 os.Setenv("REPO_NAME", "jx-testing") 50 os.Setenv("JOB_NAME", "job") 51 os.Setenv("BRANCH_NAME", "job") 52 }) 53 54 AfterEach(func() { 55 os.Setenv("REPO_OWNER", originalRepoOwner) 56 os.Setenv("REPO_NAME", originalRepoName) 57 os.Setenv("JOB_NAME", originalJobName) 58 os.Setenv("BRANCH_NAME", originalBranchName) 59 }) 60 61 JustBeforeEach(func() { 62 stdout = &testhelpers.FakeOut{} 63 commonOpts := &opts.CommonOptions{ 64 Out: stdout, 65 } 66 commonOpts.SetDevNamespace("jx") 67 68 testhelpers.ConfigureTestOptionsWithResources(commonOpts, 69 []runtime.Object{}, 70 []runtime.Object{}, 71 &gits.GitFake{CurrentBranch: "job"}, 72 &gits.FakeProvider{}, 73 helm_test.NewMockHelmer(), 74 resources_test.NewMockInstaller(), 75 ) 76 77 c, ns, _ := commonOpts.JXClient() 78 79 testhelpers.CreateTestPipelineActivityWithTime(c, ns, "jx-testing", "jx-testing", "job", "1", "workflow", v1.Date(2019, time.October, 10, 23, 0, 0, 0, time.UTC)) 80 testhelpers.CreateTestPipelineActivityWithTime(c, ns, "jx-testing", "jx-testing", "job", "2", "workflow", v1.Date(2019, time.January, 10, 23, 0, 0, 0, time.UTC)) 81 82 options := &get.GetActivityOptions{ 83 CommonOptions: commonOpts, 84 Sort: sort, 85 } 86 87 err = options.Run() 88 }) 89 90 Context("Without flags", func() { 91 BeforeEach(func() { 92 sort = false 93 }) 94 95 It("Prints a list of activities", func() { 96 Expect(err).NotTo(HaveOccurred()) 97 Expect(stdout.GetOutput()).To(ContainSubstring(`STARTED AGO DURATION STATUS 98 jx-testing/jx-testing/job #1`)) 99 }) 100 }) 101 102 Context("With the sort flag", func() { 103 BeforeEach(func() { 104 sort = true 105 }) 106 107 It("Prints a sorted list of activities", func() { 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(stdout.GetOutput()).To(ContainSubstring(`STARTED AGO DURATION STATUS 110 jx-testing/jx-testing/job #2`)) 111 }) 112 }) 113 }) 114 })