Skip to content

Commit fc697e3

Browse files
author
Joshua Clark
committed
Added WaitForReinstallCondition function to siteconfig
1 parent 81e546f commit fc697e3

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

pkg/siteconfig/clusterinstance.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,70 @@ func (builder *CIBuilder) WaitForCondition(expected metav1.Condition, timeout ti
491491
return builder, err
492492
}
493493

494+
// WaitForReinstallCondition waits until the ClusterInstance
495+
// has a reinstall condition that matches the expected, checking only the Type, Status, Reason, and Message fields.
496+
// For the message field, it matches if the message contains the expected.
497+
// Zero fields in the expected condition are ignored.
498+
func (builder *CIBuilder) WaitForReinstallCondition(expected metav1.Condition,
499+
timeout time.Duration) (*CIBuilder, error) {
500+
if valid, err := builder.validate(); !valid {
501+
return builder, err
502+
}
503+
504+
if !builder.Exists() {
505+
glog.V(100).Infof("The clusterinstance does not exist on the cluster")
506+
507+
return builder, fmt.Errorf(
508+
"clusterinstance object %s does not exist in namespace %s", builder.Definition.Name, builder.Definition.Namespace)
509+
}
510+
511+
err := wait.PollUntilContextTimeout(
512+
context.TODO(), 3*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
513+
var err error
514+
builder.Object, err = builder.Get()
515+
516+
if err != nil {
517+
glog.V(100).Info("failed to get clusterinstance %s/%s: %w",
518+
builder.Definition.Namespace, builder.Definition.Name, err)
519+
520+
return false, nil
521+
}
522+
523+
if builder.Object.Status.Reinstall == nil {
524+
glog.V(100).Info("failed to get reinstall status %s/%s: %w",
525+
builder.Definition.Namespace, builder.Definition.Name, err)
526+
527+
return false, nil
528+
}
529+
530+
builder.Definition = builder.Object
531+
532+
for _, condition := range builder.Object.Status.Reinstall.Conditions {
533+
if expected.Type != "" && condition.Type != expected.Type {
534+
continue
535+
}
536+
537+
if expected.Status != "" && condition.Status != expected.Status {
538+
continue
539+
}
540+
541+
if expected.Reason != "" && condition.Reason != expected.Reason {
542+
continue
543+
}
544+
545+
if expected.Message != "" && !strings.Contains(condition.Message, expected.Message) {
546+
continue
547+
}
548+
549+
return true, nil
550+
}
551+
552+
return false, nil
553+
})
554+
555+
return builder, err
556+
}
557+
494558
// WaitForExtraLabel waits up to timeout until the ExtraLabel label exists for manifest of kind.
495559
func (builder *CIBuilder) WaitForExtraLabel(kind, label string, timeout time.Duration) (*CIBuilder, error) {
496560
if valid, err := builder.validate(); !valid {

pkg/siteconfig/clusterinstance_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
var (
2020
// defaultClusterInstanceCondition is a variable which depicts default value of ClusterInstance condition types.
2121
defaultClusterInstanceCondition = metav1.Condition{Type: "", Status: metav1.ConditionTrue}
22+
23+
// defaultClusterInstanceReinstallCondition depicts default value of ClusterInstanceReinstall condition types.
24+
defaultClusterInstanceReinstallCondition = metav1.Condition{Type: "", Status: metav1.ConditionTrue}
2225
)
2326

2427
const (
@@ -592,6 +595,78 @@ func TestClusterInstanceWaitForCondition(t *testing.T) {
592595
}
593596
}
594597

598+
func TestClusterInstanceWaitForReinstallCondition(t *testing.T) {
599+
testCases := []struct {
600+
condition metav1.Condition
601+
exists bool
602+
conditionMet bool
603+
valid bool
604+
expectedError error
605+
}{
606+
{
607+
condition: defaultClusterInstanceReinstallCondition,
608+
exists: true,
609+
conditionMet: true,
610+
valid: true,
611+
expectedError: nil,
612+
},
613+
{
614+
condition: defaultClusterInstanceReinstallCondition,
615+
exists: false,
616+
conditionMet: true,
617+
valid: true,
618+
expectedError: fmt.Errorf("clusterinstance object %s does not exist in namespace %s",
619+
testClusterInstance, testClusterInstance),
620+
},
621+
{
622+
condition: defaultClusterInstanceReinstallCondition,
623+
exists: true,
624+
conditionMet: false,
625+
valid: true,
626+
expectedError: context.DeadlineExceeded,
627+
},
628+
{
629+
condition: defaultClusterInstanceReinstallCondition,
630+
exists: true,
631+
conditionMet: true,
632+
valid: false,
633+
expectedError: fmt.Errorf("clusterinstance 'nsname' cannot be empty"),
634+
},
635+
}
636+
637+
for _, testCase := range testCases {
638+
var (
639+
runtimeObjects []runtime.Object
640+
clusterInstanceBuilder *CIBuilder
641+
)
642+
643+
if testCase.exists {
644+
clusterinstance := generateClusterInstance()
645+
646+
if testCase.conditionMet {
647+
clusterinstance.Status.Reinstall.Conditions = append(clusterinstance.Status.Reinstall.Conditions,
648+
testCase.condition)
649+
}
650+
651+
runtimeObjects = append(runtimeObjects, clusterinstance)
652+
}
653+
654+
testSettings := clients.GetTestClients(clients.TestClientParams{
655+
K8sMockObjects: runtimeObjects,
656+
SchemeAttachers: testSchemes,
657+
})
658+
659+
if testCase.valid {
660+
clusterInstanceBuilder = buildValidClusterInstanceTestBuilder(testSettings)
661+
} else {
662+
clusterInstanceBuilder = buildInvalidClusterInstanceTestBuilder(testSettings)
663+
}
664+
665+
_, err := clusterInstanceBuilder.WaitForReinstallCondition(testCase.condition, time.Second)
666+
assert.Equal(t, testCase.expectedError, err)
667+
}
668+
}
669+
595670
func TestClusterInstanceWaitForExtraLabel(t *testing.T) {
596671
testCases := []struct {
597672
exists bool

0 commit comments

Comments
 (0)