2023-12-14 13:51:59 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build !plan9
|
|
|
|
|
|
|
|
package kube
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
|
|
|
|
"tailscale.com/tstest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetConnectorCondition(t *testing.T) {
|
|
|
|
cn := tsapi.Connector{}
|
|
|
|
clock := tstest.NewClock(tstest.ClockOpts{})
|
2024-02-13 05:27:54 +00:00
|
|
|
fakeNow := metav1.NewTime(clock.Now().Truncate(time.Second))
|
|
|
|
fakePast := metav1.NewTime(clock.Now().Truncate(time.Second).Add(-5 * time.Minute))
|
2023-12-14 13:51:59 +00:00
|
|
|
zl, err := zap.NewDevelopment()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// Set up a new condition
|
|
|
|
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "someReason", "someMsg", 1, clock, zl.Sugar())
|
|
|
|
assert.Equal(t, cn, tsapi.Connector{
|
|
|
|
Status: tsapi.ConnectorStatus{
|
2024-06-18 19:01:40 +01:00
|
|
|
Conditions: []metav1.Condition{
|
2023-12-14 13:51:59 +00:00
|
|
|
{
|
2024-06-18 19:01:40 +01:00
|
|
|
Type: string(tsapi.ConnectorReady),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
Reason: "someReason",
|
|
|
|
Message: "someMsg",
|
|
|
|
ObservedGeneration: 1,
|
2024-06-18 19:01:40 +01:00
|
|
|
LastTransitionTime: fakeNow,
|
2023-12-14 13:51:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Modify status of an existing condition
|
|
|
|
cn.Status = tsapi.ConnectorStatus{
|
2024-06-18 19:01:40 +01:00
|
|
|
Conditions: []metav1.Condition{
|
2023-12-14 13:51:59 +00:00
|
|
|
{
|
2024-06-18 19:01:40 +01:00
|
|
|
Type: string(tsapi.ConnectorReady),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: metav1.ConditionFalse,
|
|
|
|
Reason: "someReason",
|
|
|
|
Message: "someMsg",
|
|
|
|
ObservedGeneration: 1,
|
2024-06-18 19:01:40 +01:00
|
|
|
LastTransitionTime: fakePast,
|
2023-12-14 13:51:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "anotherReason", "anotherMsg", 2, clock, zl.Sugar())
|
|
|
|
assert.Equal(t, cn, tsapi.Connector{
|
|
|
|
Status: tsapi.ConnectorStatus{
|
2024-06-18 19:01:40 +01:00
|
|
|
Conditions: []metav1.Condition{
|
2023-12-14 13:51:59 +00:00
|
|
|
{
|
2024-06-18 19:01:40 +01:00
|
|
|
Type: string(tsapi.ConnectorReady),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
Reason: "anotherReason",
|
|
|
|
Message: "anotherMsg",
|
|
|
|
ObservedGeneration: 2,
|
2024-06-18 19:01:40 +01:00
|
|
|
LastTransitionTime: fakeNow,
|
2023-12-14 13:51:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Don't modify last transition time if status hasn't changed
|
|
|
|
cn.Status = tsapi.ConnectorStatus{
|
2024-06-18 19:01:40 +01:00
|
|
|
Conditions: []metav1.Condition{
|
2023-12-14 13:51:59 +00:00
|
|
|
{
|
2024-06-18 19:01:40 +01:00
|
|
|
Type: string(tsapi.ConnectorReady),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
Reason: "someReason",
|
|
|
|
Message: "someMsg",
|
|
|
|
ObservedGeneration: 1,
|
2024-06-18 19:01:40 +01:00
|
|
|
LastTransitionTime: fakePast,
|
2023-12-14 13:51:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "anotherReason", "anotherMsg", 2, clock, zl.Sugar())
|
|
|
|
assert.Equal(t, cn, tsapi.Connector{
|
|
|
|
Status: tsapi.ConnectorStatus{
|
2024-06-18 19:01:40 +01:00
|
|
|
Conditions: []metav1.Condition{
|
2023-12-14 13:51:59 +00:00
|
|
|
{
|
2024-06-18 19:01:40 +01:00
|
|
|
Type: string(tsapi.ConnectorReady),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
Reason: "anotherReason",
|
|
|
|
Message: "anotherMsg",
|
|
|
|
ObservedGeneration: 2,
|
2024-06-18 19:01:40 +01:00
|
|
|
LastTransitionTime: fakePast,
|
2023-12-14 13:51:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|