Skip to content

Commit

Permalink
Detect replacement circuits while adding package channels
Browse files Browse the repository at this point in the history
  • Loading branch information
tmckayus committed Jul 26, 2019
1 parent 9e218f9 commit 4e7529c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
30 changes: 30 additions & 0 deletions pkg/sqlite/configmap_test.go
@@ -1,8 +1,11 @@
package sqlite

import (
"bytes"
"context"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -33,6 +36,33 @@ func TestConfigMapLoader(t *testing.T) {
require.NoError(t, loader.Populate())
}

func TestReplaceCircuit(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)

store, err := NewSQLLiteLoader("test.db")
require.NoError(t, err)
defer os.Remove("test.db")

path := "../../configmap.example.yaml"
cmap, err := ioutil.ReadFile(path)

require.NoError(t, err, "unable to load configmap from file %s", path)

// Make etcdoperator.v0.9.0 in the example replace 0.9.2 to create a loop
sReader := strings.NewReader(string(bytes.Replace(cmap,
[]byte("replaces: etcdoperator.v0.6.1"),
[]byte("replaces: etcdoperator.v0.9.2"), 1)))

decoder := yaml.NewYAMLOrJSONDecoder(sReader, 30)
manifest := v1.ConfigMap{}
err = decoder.Decode(&manifest)
require.NoError(t, err, "could not decode contents of file %s into configmap", path)

loader := NewSQLLoaderForConfigMap(store, manifest)
err = loader.Populate()
require.Error(t, err, "Circuit detected, etcdoperator.v0.9.0 replaces etcdoperator.v0.9.2")
}

func TestQuerierForConfigmap(t *testing.T) {
load, err := NewSQLLiteLoader("test.db")
require.NoError(t, err)
Expand Down
10 changes: 9 additions & 1 deletion pkg/sqlite/load.go
Expand Up @@ -187,8 +187,10 @@ func (s *SQLLoader) AddPackageChannels(manifest registry.PackageManifest) error

channelEntryCSVName := c.CurrentCSVName
depth := 1
for {

// Since this loop depends on following 'replaces', keep track of where it's been
replaceCircuit := map[string]bool{channelEntryCSVName: true}
for {
// Get CSV for current entry
channelEntryCSV, err := s.getCSV(tx, channelEntryCSVName)
if err != nil {
Expand Down Expand Up @@ -254,6 +256,12 @@ func (s *SQLLoader) AddPackageChannels(manifest registry.PackageManifest) error
return fmt.Errorf("%s specifies replacement that couldn't be found", c.CurrentCSVName)
}

// If we find 'replaces' in the circuit list then we've seen it already, break out
if _, ok := replaceCircuit[replaces]; ok {
return fmt.Errorf("Circuit detected, %s replaces %s", channelEntryCSVName, replaces)
}
replaceCircuit[channelEntryCSVName] = true

replacedChannelEntry, err := addChannelEntry.Exec(c.Name, manifest.PackageName, replaced.GetName(), depth)
if err != nil {
return err
Expand Down

0 comments on commit 4e7529c

Please sign in to comment.