|
| 1 | +// Copyright 2024, Nitric Technologies Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package lifecycle_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + |
| 20 | + . "github.com/onsi/ginkgo" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + |
| 23 | + "github.com/nitrictech/go-sdk/nitric/lifecycle" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("Lifecycle", func() { |
| 27 | + BeforeEach(func() { |
| 28 | + // Clear environment before each test |
| 29 | + os.Unsetenv(lifecycle.NITRIC_ENVIRONMENT) |
| 30 | + }) |
| 31 | + |
| 32 | + Describe("GetCurrentLifecycle", func() { |
| 33 | + Context("when environment variable is set to valid stages", func() { |
| 34 | + It("should return LocalRun stage", func() { |
| 35 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "run") |
| 36 | + stage, err := lifecycle.GetCurrentLifecycle() |
| 37 | + Expect(err).NotTo(HaveOccurred()) |
| 38 | + Expect(stage).To(Equal(lifecycle.LocalRun)) |
| 39 | + }) |
| 40 | + |
| 41 | + It("should return Build stage", func() { |
| 42 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "build") |
| 43 | + stage, err := lifecycle.GetCurrentLifecycle() |
| 44 | + Expect(err).NotTo(HaveOccurred()) |
| 45 | + Expect(stage).To(Equal(lifecycle.Build)) |
| 46 | + }) |
| 47 | + |
| 48 | + It("should return Cloud stage", func() { |
| 49 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "cloud") |
| 50 | + stage, err := lifecycle.GetCurrentLifecycle() |
| 51 | + Expect(err).NotTo(HaveOccurred()) |
| 52 | + Expect(stage).To(Equal(lifecycle.Cloud)) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + Context("when environment variable is not set", func() { |
| 57 | + It("should return an error", func() { |
| 58 | + stage, err := lifecycle.GetCurrentLifecycle() |
| 59 | + Expect(err).To(HaveOccurred()) |
| 60 | + Expect(stage).To(BeEmpty()) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + Context("when environment variable is set to invalid stage", func() { |
| 65 | + It("should return an error", func() { |
| 66 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "invalid") |
| 67 | + stage, err := lifecycle.GetCurrentLifecycle() |
| 68 | + Expect(err).To(HaveOccurred()) |
| 69 | + Expect(stage).To(BeEmpty()) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + Describe("IsInLifecycle", func() { |
| 75 | + Context("when current stage matches one of the provided stages", func() { |
| 76 | + It("should return true for LocalRun", func() { |
| 77 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "run") |
| 78 | + result := lifecycle.IsInLifecycle(lifecycle.LocalRun, lifecycle.Cloud) |
| 79 | + Expect(result).To(BeTrue()) |
| 80 | + }) |
| 81 | + |
| 82 | + It("should return true for Cloud", func() { |
| 83 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "cloud") |
| 84 | + result := lifecycle.IsInLifecycle(lifecycle.LocalRun, lifecycle.Cloud) |
| 85 | + Expect(result).To(BeTrue()) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + Context("when current stage does not match any of the provided stages", func() { |
| 90 | + It("should return false", func() { |
| 91 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "build") |
| 92 | + result := lifecycle.IsInLifecycle(lifecycle.LocalRun, lifecycle.Cloud) |
| 93 | + Expect(result).To(BeFalse()) |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + Describe("WhenInLifecycles", func() { |
| 99 | + Context("when current stage matches one of the provided stages", func() { |
| 100 | + It("should execute the callback", func() { |
| 101 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "run") |
| 102 | + result := lifecycle.WhenInLifecycles([]lifecycle.LifecycleStage{lifecycle.LocalRun, lifecycle.Cloud}, func() string { |
| 103 | + return "executed" |
| 104 | + }) |
| 105 | + Expect(result).To(Equal("executed")) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + Context("when current stage does not match any of the provided stages", func() { |
| 110 | + It("should not execute the callback and return zero value", func() { |
| 111 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "build") |
| 112 | + result := lifecycle.WhenInLifecycles([]lifecycle.LifecycleStage{lifecycle.LocalRun, lifecycle.Cloud}, func() string { |
| 113 | + return "executed" |
| 114 | + }) |
| 115 | + Expect(result).To(BeEmpty()) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + Describe("IsRunning", func() { |
| 121 | + Context("when in LocalRun stage", func() { |
| 122 | + It("should return true", func() { |
| 123 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "run") |
| 124 | + Expect(lifecycle.IsRunning()).To(BeTrue()) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + Context("when in Cloud stage", func() { |
| 129 | + It("should return true", func() { |
| 130 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "cloud") |
| 131 | + Expect(lifecycle.IsRunning()).To(BeTrue()) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + Context("when in Build stage", func() { |
| 136 | + It("should return false", func() { |
| 137 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "build") |
| 138 | + Expect(lifecycle.IsRunning()).To(BeFalse()) |
| 139 | + }) |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + Describe("IsCollecting", func() { |
| 144 | + Context("when in Build stage", func() { |
| 145 | + It("should return true", func() { |
| 146 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "build") |
| 147 | + Expect(lifecycle.IsCollecting()).To(BeTrue()) |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + Context("when in LocalRun stage", func() { |
| 152 | + It("should return false", func() { |
| 153 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "run") |
| 154 | + Expect(lifecycle.IsCollecting()).To(BeFalse()) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + Context("when in Cloud stage", func() { |
| 159 | + It("should return false", func() { |
| 160 | + os.Setenv(lifecycle.NITRIC_ENVIRONMENT, "cloud") |
| 161 | + Expect(lifecycle.IsCollecting()).To(BeFalse()) |
| 162 | + }) |
| 163 | + }) |
| 164 | + }) |
| 165 | +}) |
0 commit comments