|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 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 internal |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
| 26 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 27 | + "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("LifeCycleHandler", func() { |
| 31 | + Describe("syncRunningSince", func() { |
| 32 | + It("sets runningSince from the Running condition last transition time", func() { |
| 33 | + transitionTime := metav1.NewTime(time.Date(2026, 4, 24, 12, 0, 0, 0, time.UTC)) |
| 34 | + vm := &v1alpha2.VirtualMachine{ |
| 35 | + Status: v1alpha2.VirtualMachineStatus{ |
| 36 | + Conditions: []metav1.Condition{ |
| 37 | + { |
| 38 | + Type: vmcondition.TypeRunning.String(), |
| 39 | + Status: metav1.ConditionTrue, |
| 40 | + LastTransitionTime: transitionTime, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + syncRunningSince(vm) |
| 47 | + |
| 48 | + Expect(vm.Status.RunningSince).NotTo(BeNil()) |
| 49 | + Expect(vm.Status.RunningSince.Time).To(Equal(transitionTime.Time)) |
| 50 | + }) |
| 51 | + |
| 52 | + DescribeTable("clears runningSince when the VM is not running", |
| 53 | + func(conditions []metav1.Condition) { |
| 54 | + transitionTime := metav1.NewTime(time.Date(2026, 4, 24, 12, 0, 0, 0, time.UTC)) |
| 55 | + vm := &v1alpha2.VirtualMachine{ |
| 56 | + Status: v1alpha2.VirtualMachineStatus{ |
| 57 | + RunningSince: &transitionTime, |
| 58 | + Conditions: conditions, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + syncRunningSince(vm) |
| 63 | + |
| 64 | + Expect(vm.Status.RunningSince).To(BeNil()) |
| 65 | + }, |
| 66 | + Entry("without the Running condition", nil), |
| 67 | + Entry("with the Running condition set to False", []metav1.Condition{ |
| 68 | + { |
| 69 | + Type: vmcondition.TypeRunning.String(), |
| 70 | + Status: metav1.ConditionFalse, |
| 71 | + LastTransitionTime: metav1.NewTime(time.Date(2026, 4, 24, 12, 0, 0, 0, time.UTC)), |
| 72 | + }, |
| 73 | + }), |
| 74 | + ) |
| 75 | + }) |
| 76 | +}) |
0 commit comments