|
| 1 | +package olm |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/labels" |
| 14 | + ktesting "k8s.io/client-go/testing" |
| 15 | + |
| 16 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 17 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake" |
| 18 | + listersv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1" |
| 19 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes" |
| 20 | +) |
| 21 | + |
| 22 | +func TestCopyToNamespace(t *testing.T) { |
| 23 | + gvr := v1alpha1.SchemeGroupVersion.WithResource("clusterserviceversions") |
| 24 | + |
| 25 | + for _, tc := range []struct { |
| 26 | + Name string |
| 27 | + Namespace string |
| 28 | + Original *v1alpha1.ClusterServiceVersion |
| 29 | + ExistingCopy *v1alpha1.ClusterServiceVersion |
| 30 | + ExpectedResult *v1alpha1.ClusterServiceVersion |
| 31 | + ExpectedError string |
| 32 | + ExpectedActions []ktesting.Action |
| 33 | + }{ |
| 34 | + { |
| 35 | + Name: "copy to original namespace returns error", |
| 36 | + Namespace: "foo", |
| 37 | + Original: &v1alpha1.ClusterServiceVersion{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Namespace: "foo", |
| 40 | + }, |
| 41 | + }, |
| 42 | + ExpectedError: "bug: can not copy to active namespace foo", |
| 43 | + }, |
| 44 | + { |
| 45 | + Name: "status updated if meaningfully changed", |
| 46 | + Namespace: "bar", |
| 47 | + Original: &v1alpha1.ClusterServiceVersion{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "name", |
| 50 | + Namespace: "foo", |
| 51 | + }, |
| 52 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 53 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + ExistingCopy: &v1alpha1.ClusterServiceVersion{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "name", |
| 59 | + Namespace: "bar", |
| 60 | + }, |
| 61 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 62 | + LastUpdateTime: &metav1.Time{Time: time.Unix(1, 0)}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ExpectedResult: &v1alpha1.ClusterServiceVersion{ |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Name: "name", |
| 68 | + Namespace: "bar", |
| 69 | + }, |
| 70 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 71 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 72 | + Message: "The operator is running in foo but is managing this namespace", |
| 73 | + Reason: v1alpha1.CSVReasonCopied, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ExpectedActions: []ktesting.Action{ |
| 77 | + ktesting.NewUpdateSubresourceAction(gvr, "status", "bar", &v1alpha1.ClusterServiceVersion{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: "name", |
| 80 | + Namespace: "bar", |
| 81 | + }, |
| 82 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 83 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 84 | + Message: "The operator is running in foo but is managing this namespace", |
| 85 | + Reason: v1alpha1.CSVReasonCopied, |
| 86 | + }, |
| 87 | + }), |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + Name: "status not updated if not meaningfully changed", |
| 92 | + Namespace: "bar", |
| 93 | + Original: &v1alpha1.ClusterServiceVersion{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: "name", |
| 96 | + Namespace: "foo", |
| 97 | + }, |
| 98 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 99 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ExistingCopy: &v1alpha1.ClusterServiceVersion{ |
| 103 | + ObjectMeta: metav1.ObjectMeta{ |
| 104 | + Name: "name", |
| 105 | + Namespace: "bar", |
| 106 | + }, |
| 107 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 108 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 109 | + Message: "The operator is running in foo but is managing this namespace", |
| 110 | + Reason: v1alpha1.CSVReasonCopied}, |
| 111 | + }, |
| 112 | + ExpectedResult: &v1alpha1.ClusterServiceVersion{ |
| 113 | + ObjectMeta: metav1.ObjectMeta{ |
| 114 | + Name: "name", |
| 115 | + Namespace: "bar", |
| 116 | + }, |
| 117 | + Status: v1alpha1.ClusterServiceVersionStatus{ |
| 118 | + LastUpdateTime: &metav1.Time{Time: time.Unix(2, 0)}, |
| 119 | + Message: "The operator is running in foo but is managing this namespace", |
| 120 | + Reason: v1alpha1.CSVReasonCopied, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } { |
| 125 | + t.Run(tc.Name, func(t *testing.T) { |
| 126 | + lister := &operatorlisterfakes.FakeOperatorLister{} |
| 127 | + v1alpha1lister := &operatorlisterfakes.FakeOperatorsV1alpha1Lister{} |
| 128 | + lister.OperatorsV1alpha1Returns(v1alpha1lister) |
| 129 | + |
| 130 | + client := fake.NewSimpleClientset() |
| 131 | + if tc.ExistingCopy != nil { |
| 132 | + client = fake.NewSimpleClientset(tc.ExistingCopy) |
| 133 | + v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister{tc.ExistingCopy}) |
| 134 | + } |
| 135 | + |
| 136 | + logger := logrus.New() |
| 137 | + logger.SetOutput(ioutil.Discard) |
| 138 | + |
| 139 | + o := &Operator{ |
| 140 | + lister: lister, |
| 141 | + client: client, |
| 142 | + logger: logger, |
| 143 | + } |
| 144 | + result, err := o.copyToNamespace(tc.Original, tc.Namespace) |
| 145 | + |
| 146 | + require := require.New(t) |
| 147 | + if tc.ExpectedError == "" { |
| 148 | + require.NoError(err) |
| 149 | + } else { |
| 150 | + require.EqualError(err, tc.ExpectedError) |
| 151 | + } |
| 152 | + require.Equal(tc.ExpectedResult, result) |
| 153 | + |
| 154 | + actions := client.Actions() |
| 155 | + if len(actions) == 0 { |
| 156 | + actions = nil |
| 157 | + } |
| 158 | + require.Equal(tc.ExpectedActions, actions) |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +type FakeClusterServiceVersionLister []*v1alpha1.ClusterServiceVersion |
| 164 | + |
| 165 | +func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*v1alpha1.ClusterServiceVersion, error) { |
| 166 | + var result []*v1alpha1.ClusterServiceVersion |
| 167 | + for _, csv := range l { |
| 168 | + if !selector.Matches(labels.Set(csv.GetLabels())) { |
| 169 | + continue |
| 170 | + } |
| 171 | + result = append(result, csv) |
| 172 | + } |
| 173 | + return result, nil |
| 174 | +} |
| 175 | + |
| 176 | +func (l FakeClusterServiceVersionLister) ClusterServiceVersions(namespace string) listersv1alpha1.ClusterServiceVersionNamespaceLister { |
| 177 | + var filtered []*v1alpha1.ClusterServiceVersion |
| 178 | + for _, csv := range l { |
| 179 | + if csv.GetNamespace() != namespace { |
| 180 | + continue |
| 181 | + } |
| 182 | + filtered = append(filtered, csv) |
| 183 | + } |
| 184 | + return FakeClusterServiceVersionLister(filtered) |
| 185 | +} |
| 186 | + |
| 187 | +func (l FakeClusterServiceVersionLister) Get(name string) (*v1alpha1.ClusterServiceVersion, error) { |
| 188 | + for _, csv := range l { |
| 189 | + if csv.GetName() == name { |
| 190 | + return csv, nil |
| 191 | + } |
| 192 | + } |
| 193 | + return nil, errors.NewNotFound(v1alpha1.Resource("clusterserviceversion"), name) |
| 194 | +} |
| 195 | + |
| 196 | +var ( |
| 197 | + _ listersv1alpha1.ClusterServiceVersionLister = FakeClusterServiceVersionLister{} |
| 198 | + _ listersv1alpha1.ClusterServiceVersionNamespaceLister = FakeClusterServiceVersionLister{} |
| 199 | +) |
0 commit comments