|
4 | 4 | "context"
|
5 | 5 | "database/sql"
|
6 | 6 | "encoding/json"
|
| 7 | + "errors" |
7 | 8 | "fmt"
|
8 | 9 | "strings"
|
9 | 10 | "testing"
|
@@ -154,6 +155,71 @@ func TestAddPackageChannels(t *testing.T) {
|
154 | 155 | }
|
155 | 156 | }
|
156 | 157 |
|
| 158 | +func TestAddBundleSemver(t *testing.T) { |
| 159 | + // Create a test DB |
| 160 | + db, cleanup := CreateTestDb(t) |
| 161 | + defer cleanup() |
| 162 | + store, err := NewSQLLiteLoader(db) |
| 163 | + require.NoError(t, err) |
| 164 | + err = store.Migrate(context.TODO()) |
| 165 | + require.NoError(t, err) |
| 166 | + graphLoader, err := NewSQLGraphLoaderFromDB(db) |
| 167 | + require.NoError(t, err) |
| 168 | + |
| 169 | + // Seed the db with a replaces-mode bundle/package |
| 170 | + replacesBundle := newBundle(t, "csv-a", "pkg-foo", []string{"stable"}, newUnstructuredCSV(t, "csv-a", "")) |
| 171 | + err = store.AddOperatorBundle(replacesBundle) |
| 172 | + require.NoError(t, err) |
| 173 | + |
| 174 | + err = store.AddPackageChannels(registry.PackageManifest{ |
| 175 | + PackageName: "pkg-foo", |
| 176 | + Channels: []registry.PackageChannel{ |
| 177 | + { |
| 178 | + Name: "stable", |
| 179 | + CurrentCSVName: "csv-a", |
| 180 | + }, |
| 181 | + }, |
| 182 | + DefaultChannelName: "stable", |
| 183 | + }) |
| 184 | + require.NoError(t, err) |
| 185 | + |
| 186 | + // Add semver bundles in non-semver order. |
| 187 | + bundles := []*registry.Bundle{ |
| 188 | + newBundle(t, "csv-3", "pkg-0", []string{"stable"}, newUnstructuredCSVWithVersion(t, "csv-3", "0.3.0")), |
| 189 | + newBundle(t, "csv-1", "pkg-0", []string{"stable"}, newUnstructuredCSVWithVersion(t, "csv-1", "0.1.0")), |
| 190 | + newBundle(t, "csv-2", "pkg-0", []string{"stable"}, newUnstructuredCSVWithVersion(t, "csv-2", "0.2.0")), |
| 191 | + } |
| 192 | + for _, b := range bundles { |
| 193 | + graph, err := graphLoader.Generate(b.Package) |
| 194 | + require.Conditionf(t, func() bool { |
| 195 | + return err == nil || errors.Is(err, registry.ErrPackageNotInDatabase) |
| 196 | + }, "got unexpected error: %v", err) |
| 197 | + bundleLoader := registry.BundleGraphLoader{} |
| 198 | + updatedGraph, err := bundleLoader.AddBundleToGraph(b, graph, ®istry.AnnotationsFile{Annotations: *b.Annotations}, false) |
| 199 | + require.NoError(t, err) |
| 200 | + err = store.AddBundleSemver(updatedGraph, b) |
| 201 | + require.NoError(t, err) |
| 202 | + } |
| 203 | + |
| 204 | + // Ensure bundles can be queried with expected replaces and skips values. |
| 205 | + querier := NewSQLLiteQuerierFromDb(db) |
| 206 | + gotBundles, err := querier.ListBundles(context.Background()) |
| 207 | + require.NoError(t, err) |
| 208 | + replaces := map[string]string{} |
| 209 | + for _, b := range gotBundles { |
| 210 | + if b.PackageName != "pkg-0" { |
| 211 | + continue |
| 212 | + } |
| 213 | + require.Len(t, b.Skips, 0, "unexpected skips value(s) for bundle %q", b.CsvName) |
| 214 | + replaces[b.CsvName] = b.Replaces |
| 215 | + } |
| 216 | + require.Equal(t, map[string]string{ |
| 217 | + "csv-3": "csv-2", |
| 218 | + "csv-2": "csv-1", |
| 219 | + "csv-1": "", |
| 220 | + }, replaces) |
| 221 | +} |
| 222 | + |
157 | 223 | func TestClearNonHeadBundles(t *testing.T) {
|
158 | 224 | db, cleanup := CreateTestDb(t)
|
159 | 225 | defer cleanup()
|
@@ -237,6 +303,18 @@ func newUnstructuredCSVWithSkips(t *testing.T, name, replaces string, skips ...s
|
237 | 303 | return &unstructured.Unstructured{Object: out}
|
238 | 304 | }
|
239 | 305 |
|
| 306 | +func newUnstructuredCSVWithVersion(t *testing.T, name, version string) *unstructured.Unstructured { |
| 307 | + csv := ®istry.ClusterServiceVersion{} |
| 308 | + csv.TypeMeta.Kind = "ClusterServiceVersion" |
| 309 | + csv.SetName(name) |
| 310 | + versionJson := fmt.Sprintf(`{"version": "%s"}`, version) |
| 311 | + csv.Spec = json.RawMessage(versionJson) |
| 312 | + |
| 313 | + out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(csv) |
| 314 | + require.NoError(t, err) |
| 315 | + return &unstructured.Unstructured{Object: out} |
| 316 | +} |
| 317 | + |
240 | 318 | func newBundle(t *testing.T, name, pkgName string, channels []string, objs ...*unstructured.Unstructured) *registry.Bundle {
|
241 | 319 | bundle := registry.NewBundle(name, ®istry.Annotations{
|
242 | 320 | PackageName: pkgName,
|
|
0 commit comments