Skip to content

Commit b1b0a41

Browse files
Merge pull request #63 from tmckayus/replacecircuit
Bug 1783030: Detect replacement cycles while adding package channels
2 parents aec9bf2 + 19a37f5 commit b1b0a41

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

pkg/sqlite/configmap_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package sqlite
22

33
import (
4+
"bytes"
45
"context"
56
"database/sql"
67
"fmt"
8+
"io/ioutil"
79
"math/rand"
810
"os"
11+
"strings"
912
"testing"
1013

1114
"github.com/sirupsen/logrus"
@@ -58,6 +61,34 @@ func TestConfigMapLoader(t *testing.T) {
5861
require.NoError(t, loader.Populate())
5962
}
6063

64+
func TestReplaceCycle(t *testing.T) {
65+
logrus.SetLevel(logrus.DebugLevel)
66+
67+
db, cleanup := CreateTestDb(t)
68+
defer cleanup()
69+
store, err := NewSQLLiteLoader(db)
70+
require.NoError(t, err)
71+
72+
path := "../../configmap.example.yaml"
73+
cmap, err := ioutil.ReadFile(path)
74+
75+
require.NoError(t, err, "unable to load configmap from file %s", path)
76+
77+
// Make etcdoperator.v0.9.0 in the example replace 0.9.2 to create a loop
78+
sReader := strings.NewReader(string(bytes.Replace(cmap,
79+
[]byte("replaces: etcdoperator.v0.6.1"),
80+
[]byte("replaces: etcdoperator.v0.9.2"), 1)))
81+
82+
decoder := yaml.NewYAMLOrJSONDecoder(sReader, 30)
83+
manifest := v1.ConfigMap{}
84+
err = decoder.Decode(&manifest)
85+
require.NoError(t, err, "could not decode contents of file %s into configmap", path)
86+
87+
loader := NewSQLLoaderForConfigMap(store, manifest)
88+
err = loader.Populate()
89+
require.Error(t, err, "Cycle detected, etcdoperator.v0.9.0 replaces etcdoperator.v0.9.2")
90+
}
91+
6192
func TestQuerierForConfigmap(t *testing.T) {
6293
db, cleanup := CreateTestDb(t)
6394
defer cleanup()

pkg/sqlite/load.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ func (s *SQLLoader) AddPackageChannels(manifest registry.PackageManifest) error
190190

191191
channelEntryCSVName := c.CurrentCSVName
192192
depth := 1
193-
for {
194193

194+
// Since this loop depends on following 'replaces', keep track of where it's been
195+
replaceCycle := map[string]bool{channelEntryCSVName: true}
196+
for {
195197
// Get CSV for current entry
196198
channelEntryCSV, err := s.getCSV(tx, channelEntryCSVName)
197199
if err != nil {
@@ -265,6 +267,14 @@ func (s *SQLLoader) AddPackageChannels(manifest registry.PackageManifest) error
265267
errs = append(errs, err)
266268
break
267269
}
270+
271+
// If we find 'replaces' in the circuit list then we've seen it already, break out
272+
if _, ok := replaceCycle[replaces]; ok {
273+
errs = append(errs, fmt.Errorf("Cycle detected, %s replaces %s", channelEntryCSVName, replaces))
274+
break
275+
}
276+
replaceCycle[replaces] = true
277+
268278
replacedID, err := replacedChannelEntry.LastInsertId()
269279
if err != nil {
270280
errs = append(errs, err)

0 commit comments

Comments
 (0)