|
| 1 | +package sqlite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/blang/semver/v4" |
| 13 | + "github.com/operator-framework/api/pkg/lib/version" |
| 14 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 15 | + "github.com/operator-framework/operator-registry/pkg/api" |
| 16 | + "github.com/operator-framework/operator-registry/pkg/image" |
| 17 | + "github.com/operator-framework/operator-registry/pkg/registry" |
| 18 | + "github.com/sirupsen/logrus" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + "sigs.k8s.io/yaml" |
| 22 | +) |
| 23 | + |
| 24 | +type testBundle struct { |
| 25 | + version string |
| 26 | + pkg string |
| 27 | + channels []string |
| 28 | + defaultChannel string |
| 29 | + skips []string |
| 30 | + replaces string |
| 31 | +} |
| 32 | + |
| 33 | +func newRegistryBundle(t *testing.T, b testBundle) *registry.Bundle { |
| 34 | + name := b.version |
| 35 | + csvYAML, err := yaml.Marshal(&v1alpha1.ClusterServiceVersion{ |
| 36 | + TypeMeta: metav1.TypeMeta{ |
| 37 | + Kind: v1alpha1.ClusterServiceVersionKind, |
| 38 | + APIVersion: v1alpha1.ClusterServiceVersionAPIVersion, |
| 39 | + }, |
| 40 | + ObjectMeta: metav1.ObjectMeta{Name: name, Annotations: map[string]string{}}, |
| 41 | + Spec: v1alpha1.ClusterServiceVersionSpec{ |
| 42 | + Version: version.OperatorVersion{ |
| 43 | + Version: semver.MustParse(b.version), |
| 44 | + }, |
| 45 | + Replaces: b.replaces, |
| 46 | + Skips: b.skips, |
| 47 | + }, |
| 48 | + }) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + tmpdir, err := ioutil.TempDir(".", "depr-") |
| 52 | + require.NoError(t, err) |
| 53 | + defer os.RemoveAll(tmpdir) |
| 54 | + |
| 55 | + annotationsYAML, err := yaml.Marshal(®istry.AnnotationsFile{ |
| 56 | + Annotations: registry.Annotations{ |
| 57 | + PackageName: b.pkg, |
| 58 | + Channels: strings.Join(b.channels, ","), |
| 59 | + DefaultChannelName: b.defaultChannel, |
| 60 | + }, |
| 61 | + }) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + require.NoError(t, os.Mkdir(filepath.Join(tmpdir, "manifests"), 0755)) |
| 65 | + require.NoError(t, os.Mkdir(filepath.Join(tmpdir, "metadata"), 0755)) |
| 66 | + require.NoError(t, ioutil.WriteFile(filepath.Join(tmpdir, "manifests", "csv.yaml"), csvYAML, 0644)) |
| 67 | + require.NoError(t, ioutil.WriteFile(filepath.Join(tmpdir, "metadata", "annotations.yaml"), annotationsYAML, 0644)) |
| 68 | + |
| 69 | + img, err := registry.NewImageInput(image.SimpleReference(fmt.Sprintf("bundle-%s", b.version)), tmpdir) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + return img.Bundle |
| 73 | +} |
| 74 | + |
| 75 | +func TestDeprecate(t *testing.T) { |
| 76 | + tests := []struct { |
| 77 | + description string |
| 78 | + initial []testBundle |
| 79 | + deprecate []string |
| 80 | + expected []*api.Bundle |
| 81 | + expectedError error |
| 82 | + }{ |
| 83 | + { |
| 84 | + description: "Missing Image", |
| 85 | + initial: []testBundle{ |
| 86 | + { |
| 87 | + version: "0.0.1", |
| 88 | + pkg: "testpkg", |
| 89 | + channels: []string{"stable"}, |
| 90 | + defaultChannel: "stable", |
| 91 | + skips: nil, |
| 92 | + replaces: "", |
| 93 | + }, |
| 94 | + }, |
| 95 | + deprecate: []string{ |
| 96 | + "bundle-nonexistent", |
| 97 | + }, |
| 98 | + expectedError: fmt.Errorf("error deprecating bundle bundle-nonexistent: %s", registry.ErrBundleImageNotInDatabase), |
| 99 | + }, |
| 100 | + { |
| 101 | + description: "Unordered multi-bundle deprecation", |
| 102 | + initial: []testBundle{ |
| 103 | + { |
| 104 | + version: "0.0.1", |
| 105 | + pkg: "testpkg", |
| 106 | + channels: []string{"stable", "0.x"}, |
| 107 | + defaultChannel: "stable", |
| 108 | + skips: []string{"0.0.1-rc0"}, |
| 109 | + }, |
| 110 | + { |
| 111 | + version: "0.0.2", |
| 112 | + pkg: "testpkg", |
| 113 | + channels: []string{"stable", "0.x"}, |
| 114 | + defaultChannel: "stable", |
| 115 | + replaces: "0.0.1", |
| 116 | + }, |
| 117 | + { |
| 118 | + version: "0.0.3-rc0", |
| 119 | + pkg: "testpkg", |
| 120 | + channels: []string{"0.x"}, |
| 121 | + defaultChannel: "stable", |
| 122 | + skips: nil, |
| 123 | + replaces: "0.0.2", |
| 124 | + }, |
| 125 | + { |
| 126 | + version: "1.0.1", |
| 127 | + pkg: "testpkg", |
| 128 | + channels: []string{"stable", "1.x"}, |
| 129 | + defaultChannel: "stable", |
| 130 | + skips: []string{"0.0.3-rc0", "0.0.1"}, |
| 131 | + replaces: "0.0.2", |
| 132 | + }, |
| 133 | + { |
| 134 | + version: "1.0.2-rc0", |
| 135 | + pkg: "testpkg", |
| 136 | + channels: []string{"1.x"}, |
| 137 | + defaultChannel: "stable", |
| 138 | + skips: nil, |
| 139 | + replaces: "1.0.1", |
| 140 | + }, |
| 141 | + { |
| 142 | + version: "1.0.2", |
| 143 | + pkg: "testpkg", |
| 144 | + channels: []string{"stable", "1.x"}, |
| 145 | + defaultChannel: "stable", |
| 146 | + skips: []string{"1.0.2-rc0"}, |
| 147 | + replaces: "1.0.1", |
| 148 | + }, |
| 149 | + { |
| 150 | + version: "2.0.1", |
| 151 | + pkg: "testpkg", |
| 152 | + channels: []string{"stable", "2.x"}, |
| 153 | + defaultChannel: "stable", |
| 154 | + skips: nil, |
| 155 | + replaces: "1.0.2", |
| 156 | + }, |
| 157 | + { |
| 158 | + version: "2.0.2", |
| 159 | + pkg: "testpkg", |
| 160 | + channels: []string{"stable", "2.x"}, |
| 161 | + defaultChannel: "stable", |
| 162 | + skips: nil, |
| 163 | + replaces: "2.0.1", |
| 164 | + }, |
| 165 | + }, |
| 166 | + deprecate: []string{ |
| 167 | + "bundle-1.0.2", |
| 168 | + "bundle-0.0.2", |
| 169 | + "bundle-2.0.2", |
| 170 | + }, |
| 171 | + expected: []*api.Bundle{ |
| 172 | + { |
| 173 | + CsvName: "2.0.2", |
| 174 | + PackageName: "testpkg", |
| 175 | + ChannelName: "2.x", |
| 176 | + CsvJson: "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"2.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"2.0.1\",\"version\":\"2.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 177 | + Object: []string{ |
| 178 | + "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"2.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"2.0.1\",\"version\":\"2.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 179 | + }, |
| 180 | + BundlePath: "bundle-2.0.2", |
| 181 | + Version: "2.0.2", |
| 182 | + Replaces: "", |
| 183 | + Skips: nil, |
| 184 | + Properties: []*api.Property{{ |
| 185 | + Type: registry.DeprecatedType, |
| 186 | + Value: "{}", |
| 187 | + }}, |
| 188 | + }, |
| 189 | + { |
| 190 | + CsvName: "2.0.2", |
| 191 | + PackageName: "testpkg", |
| 192 | + ChannelName: "stable", |
| 193 | + CsvJson: "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"2.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"2.0.1\",\"version\":\"2.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 194 | + Object: []string{ |
| 195 | + "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"2.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"2.0.1\",\"version\":\"2.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 196 | + }, |
| 197 | + BundlePath: "bundle-2.0.2", |
| 198 | + Version: "2.0.2", |
| 199 | + Replaces: "", |
| 200 | + Skips: nil, |
| 201 | + Properties: []*api.Property{{ |
| 202 | + Type: registry.DeprecatedType, |
| 203 | + Value: "{}", |
| 204 | + }}, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + { |
| 209 | + description: "deprecate semver branch", |
| 210 | + initial: []testBundle{ |
| 211 | + { |
| 212 | + version: "1.0.1", |
| 213 | + pkg: "testpkg", |
| 214 | + channels: []string{"stable", "1.x"}, |
| 215 | + defaultChannel: "stable", |
| 216 | + skips: nil, |
| 217 | + replaces: "", |
| 218 | + }, |
| 219 | + { |
| 220 | + version: "1.0.2-rc0", |
| 221 | + pkg: "testpkg", |
| 222 | + channels: []string{"1.x"}, |
| 223 | + defaultChannel: "stable", |
| 224 | + skips: nil, |
| 225 | + replaces: "1.0.1", |
| 226 | + }, |
| 227 | + { |
| 228 | + version: "1.0.2-rc1", |
| 229 | + pkg: "testpkg", |
| 230 | + channels: []string{"1.x"}, |
| 231 | + defaultChannel: "stable", |
| 232 | + skips: []string{"1.0.2-rc0"}, |
| 233 | + replaces: "1.0.1", |
| 234 | + }, |
| 235 | + { |
| 236 | + version: "1.0.2", |
| 237 | + pkg: "testpkg", |
| 238 | + channels: []string{"stable", "1.x"}, |
| 239 | + defaultChannel: "stable", |
| 240 | + skips: []string{"1.0.2-rc0"}, |
| 241 | + replaces: "1.0.1", |
| 242 | + }, |
| 243 | + }, |
| 244 | + deprecate: []string{ |
| 245 | + "bundle-1.0.2-rc1", |
| 246 | + "bundle-1.0.2-rc0", |
| 247 | + }, |
| 248 | + expected: []*api.Bundle{ |
| 249 | + |
| 250 | + { |
| 251 | + CsvName: "1.0.2", |
| 252 | + PackageName: "testpkg", |
| 253 | + ChannelName: "1.x", |
| 254 | + CsvJson: "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 255 | + Object: []string{"{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2\"},\"status\":{\"cleanup\":{}}}"}, |
| 256 | + BundlePath: "bundle-1.0.2", |
| 257 | + Version: "1.0.2", |
| 258 | + Replaces: "1.0.2-rc1", |
| 259 | + }, |
| 260 | + { |
| 261 | + CsvName: "1.0.2", |
| 262 | + PackageName: "testpkg", |
| 263 | + ChannelName: "stable", |
| 264 | + CsvJson: "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2\"},\"status\":{\"cleanup\":{}}}", |
| 265 | + Object: []string{"{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2\"},\"status\":{\"cleanup\":{}}}"}, |
| 266 | + BundlePath: "bundle-1.0.2", |
| 267 | + Version: "1.0.2", |
| 268 | + }, |
| 269 | + { |
| 270 | + CsvName: "1.0.2-rc1", |
| 271 | + PackageName: "testpkg", |
| 272 | + ChannelName: "1.x", |
| 273 | + CsvJson: "{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2-rc1\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2-rc1\"},\"status\":{\"cleanup\":{}}}", |
| 274 | + Object: []string{"{\"apiVersion\":\"operators.coreos.com/v1alpha1\",\"kind\":\"ClusterServiceVersion\",\"metadata\":{\"creationTimestamp\":null,\"name\":\"1.0.2-rc1\"},\"spec\":{\"apiservicedefinitions\":{},\"cleanup\":{\"enabled\":false},\"customresourcedefinitions\":{},\"displayName\":\"\",\"install\":{\"spec\":{\"deployments\":null},\"strategy\":\"\"},\"provider\":{},\"replaces\":\"1.0.1\",\"skips\":[\"1.0.2-rc0\"],\"version\":\"1.0.2-rc1\"},\"status\":{\"cleanup\":{}}}"}, |
| 275 | + BundlePath: "bundle-1.0.2-rc1", |
| 276 | + Version: "1.0.2-rc1", |
| 277 | + Properties: []*api.Property{{ |
| 278 | + Type: registry.DeprecatedType, |
| 279 | + Value: "{}", |
| 280 | + }}, |
| 281 | + }, |
| 282 | + }, |
| 283 | + }, |
| 284 | + } |
| 285 | + for _, tt := range tests { |
| 286 | + t.Run(tt.description, func(t *testing.T) { |
| 287 | + logrus.SetLevel(logrus.DebugLevel) |
| 288 | + db, cleanup := CreateTestDb(t) |
| 289 | + defer cleanup() |
| 290 | + store, err := NewSQLLiteLoader(db) |
| 291 | + require.NoError(t, err) |
| 292 | + err = store.Migrate(context.TODO()) |
| 293 | + require.NoError(t, err) |
| 294 | + |
| 295 | + graphLoader, err := NewSQLGraphLoaderFromDB(db) |
| 296 | + require.NoError(t, err) |
| 297 | + |
| 298 | + querier := NewSQLLiteQuerierFromDb(db) |
| 299 | + |
| 300 | + bundleLoader := registry.BundleGraphLoader{} |
| 301 | + for _, b := range tt.initial { |
| 302 | + graph, err := graphLoader.Generate(b.pkg) |
| 303 | + if err != nil { |
| 304 | + require.EqualError(t, err, registry.ErrPackageNotInDatabase.Error(), "Expected empty error or package not in database") |
| 305 | + } |
| 306 | + bndl := newRegistryBundle(t, b) |
| 307 | + graph, err = bundleLoader.AddBundleToGraph(bndl, graph, ®istry.AnnotationsFile{Annotations: *bndl.Annotations}, false) |
| 308 | + require.NoError(t, err) |
| 309 | + |
| 310 | + require.NoError(t, store.AddBundleSemver(graph, bndl)) |
| 311 | + } |
| 312 | + |
| 313 | + err = NewSQLDeprecatorForBundles(store, querier, tt.deprecate).Deprecate() |
| 314 | + if tt.expectedError != nil && err != nil { |
| 315 | + require.EqualError(t, err, tt.expectedError.Error()) |
| 316 | + } |
| 317 | + var result []*api.Bundle |
| 318 | + if err == nil { |
| 319 | + result, err = querier.ListBundles(context.TODO()) |
| 320 | + } |
| 321 | + if tt.expectedError != nil { |
| 322 | + require.EqualError(t, err, tt.expectedError.Error()) |
| 323 | + return |
| 324 | + } |
| 325 | + require.NoError(t, err) |
| 326 | + require.ElementsMatch(t, tt.expected, result) |
| 327 | + }) |
| 328 | + } |
| 329 | +} |
0 commit comments