66 "testing"
77 "time"
88
9+ "github.com/google/go-cmp/cmp"
10+
911 corev1 "k8s.io/api/core/v1"
1012 "k8s.io/apimachinery/pkg/labels"
1113 corev1listers "k8s.io/client-go/listers/core/v1"
@@ -63,7 +65,8 @@ func TestUpdateOperatorStatus(t *testing.T) {
6365 errors []error
6466 previousConditions []operatorv1.OperatorCondition
6567
66- validateOperatorStatus func (* operatorv1.OperatorStatus ) error
68+ validateOperatorStatus func (* operatorv1.OperatorStatus ) error
69+ validateVersionRecorder func (* fakeVersionRecorder ) error
6770 }{
6871 {
6972 name : "no workload" ,
@@ -841,6 +844,49 @@ func TestUpdateOperatorStatus(t *testing.T) {
841844 return areCondidtionsEqual (expectedConditions , actualStatus .Conditions )
842845 },
843846 },
847+ {
848+ name : "version recorded when at highest revision" ,
849+ operatorConfigAtHighestRevision : true ,
850+ workload : & appsv1.Deployment {
851+ ObjectMeta : metav1.ObjectMeta {Name : "apiserver" , Namespace : "openshift-apiserver" , Generation : 1 },
852+ Spec : appsv1.DeploymentSpec {Replicas : ptr.To [int32 ](1 )},
853+ Status : appsv1.DeploymentStatus {
854+ AvailableReplicas : 1 , UpdatedReplicas : 1 , ObservedGeneration : 1 ,
855+ Conditions : []appsv1.DeploymentCondition {
856+ {Type : appsv1 .DeploymentProgressing , Status : corev1 .ConditionTrue , Reason : "NewReplicaSetAvailable" },
857+ },
858+ },
859+ },
860+ validateOperatorStatus : func (* operatorv1.OperatorStatus ) error { return nil },
861+ validateVersionRecorder : func (r * fakeVersionRecorder ) error {
862+ expected := []setVersionCall {{OperandName : "apiserver" , Version : "v1.0.0-test" }}
863+ if d := cmp .Diff (expected , r .setVersionCalls ); d != "" {
864+ return fmt .Errorf ("unexpected SetVersion calls (-want +got):\n %s" , d )
865+ }
866+ return nil
867+ },
868+ },
869+ {
870+ name : "version not recorded when not at highest revision" ,
871+ operatorConfigAtHighestRevision : false ,
872+ workload : & appsv1.Deployment {
873+ ObjectMeta : metav1.ObjectMeta {Name : "apiserver" , Namespace : "openshift-apiserver" , Generation : 1 },
874+ Spec : appsv1.DeploymentSpec {Replicas : ptr.To [int32 ](1 )},
875+ Status : appsv1.DeploymentStatus {
876+ AvailableReplicas : 1 , UpdatedReplicas : 1 , ObservedGeneration : 1 ,
877+ Conditions : []appsv1.DeploymentCondition {
878+ {Type : appsv1 .DeploymentProgressing , Status : corev1 .ConditionTrue , Reason : "NewReplicaSetAvailable" },
879+ },
880+ },
881+ },
882+ validateOperatorStatus : func (* operatorv1.OperatorStatus ) error { return nil },
883+ validateVersionRecorder : func (r * fakeVersionRecorder ) error {
884+ if d := cmp .Diff ([]setVersionCall (nil ), r .setVersionCalls ); d != "" {
885+ return fmt .Errorf ("unexpected SetVersion calls (-want +got):\n %s" , d )
886+ }
887+ return nil
888+ },
889+ },
844890 }
845891
846892 for _ , scenario := range scenarios {
@@ -869,12 +915,16 @@ func TestUpdateOperatorStatus(t *testing.T) {
869915 syncErrrors : scenario .errors ,
870916 }
871917
918+ recorder := & fakeVersionRecorder {}
919+
872920 // act
873921 target := & Controller {
874- operatorClient : fakeOperatorClient ,
875- targetNamespace : targetNs ,
876- podsLister : & fakePodLister {pods : scenario .pods },
877- delegate : delegate ,
922+ operatorClient : fakeOperatorClient ,
923+ targetNamespace : targetNs ,
924+ targetOperandVersion : "v1.0.0-test" ,
925+ podsLister : & fakePodLister {pods : scenario .pods },
926+ delegate : delegate ,
927+ versionRecorder : recorder ,
878928 }
879929
880930 err := target .sync (context .TODO (), factory .NewSyncContext ("workloadcontroller_test" , events .NewInMemoryRecorder ("workloadcontroller_test" , clocktesting .NewFakePassiveClock (time .Now ()))))
@@ -887,10 +937,14 @@ func TestUpdateOperatorStatus(t *testing.T) {
887937 if err != nil {
888938 t .Fatal (err )
889939 }
890- err = scenario .validateOperatorStatus (actualOperatorStatus )
891- if err != nil {
940+ if err := scenario .validateOperatorStatus (actualOperatorStatus ); err != nil {
892941 t .Fatal (err )
893942 }
943+ if scenario .validateVersionRecorder != nil {
944+ if err := scenario .validateVersionRecorder (recorder ); err != nil {
945+ t .Fatal (err )
946+ }
947+ }
894948 })
895949 }
896950}
@@ -921,6 +975,22 @@ func (f *fakePodLister) Pods(namespace string) corev1listers.PodNamespaceLister
921975 }
922976}
923977
978+ type setVersionCall struct {
979+ OperandName , Version string
980+ }
981+
982+ type fakeVersionRecorder struct {
983+ setVersionCalls []setVersionCall
984+ }
985+
986+ func (f * fakeVersionRecorder ) SetVersion (operandName , version string ) {
987+ f .setVersionCalls = append (f .setVersionCalls , setVersionCall {operandName , version })
988+ }
989+
990+ func (f * fakeVersionRecorder ) UnsetVersion (_ string ) {}
991+ func (f * fakeVersionRecorder ) GetVersions () map [string ]string { return nil }
992+ func (f * fakeVersionRecorder ) VersionChangedChannel () <- chan struct {} { return nil }
993+
924994func areCondidtionsEqual (expectedConditions []operatorv1.OperatorCondition , actualConditions []operatorv1.OperatorCondition ) error {
925995 if len (expectedConditions ) != len (actualConditions ) {
926996 return fmt .Errorf ("expected %d conditions but got %d" , len (expectedConditions ), len (actualConditions ))
0 commit comments