utils: remove string.go
This commit is contained in:
parent
c2f4a44068
commit
3e4dc3834f
@ -29,7 +29,6 @@ import (
|
|||||||
"github.com/coreos/clair"
|
"github.com/coreos/clair"
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
||||||
"github.com/coreos/clair/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -157,7 +156,7 @@ func TestRaceAffects(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(t, utils.CompareStringLists(expectedAffectedNames, actualAffectedNames), 0)
|
assert.Len(t, compareStringLists(expectedAffectedNames, actualAffectedNames), 0)
|
||||||
assert.Len(t, utils.CompareStringLists(actualAffectedNames, expectedAffectedNames), 0)
|
assert.Len(t, compareStringLists(actualAffectedNames, expectedAffectedNames), 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/pkg/commonerr"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
"github.com/coreos/clair/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) {
|
func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) {
|
||||||
@ -362,8 +361,8 @@ func (pgSQL *pgSQL) updateDiffFeatureVersions(tx *sql.Tx, layer, existingLayer *
|
|||||||
parentLayerFeaturesMapNV, parentLayerFeaturesNV := createNV(layer.Parent.Features)
|
parentLayerFeaturesMapNV, parentLayerFeaturesNV := createNV(layer.Parent.Features)
|
||||||
|
|
||||||
// Calculate the added and deleted FeatureVersions name:version.
|
// Calculate the added and deleted FeatureVersions name:version.
|
||||||
addNV := utils.CompareStringLists(layerFeaturesNV, parentLayerFeaturesNV)
|
addNV := compareStringLists(layerFeaturesNV, parentLayerFeaturesNV)
|
||||||
delNV := utils.CompareStringLists(parentLayerFeaturesNV, layerFeaturesNV)
|
delNV := compareStringLists(parentLayerFeaturesNV, layerFeaturesNV)
|
||||||
|
|
||||||
// Fill the structures containing the added and deleted FeatureVersions.
|
// Fill the structures containing the added and deleted FeatureVersions.
|
||||||
for _, nv := range addNV {
|
for _, nv := range addNV {
|
||||||
|
@ -23,10 +23,48 @@ import (
|
|||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/ext/versionfmt"
|
"github.com/coreos/clair/ext/versionfmt"
|
||||||
"github.com/coreos/clair/pkg/commonerr"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
"github.com/coreos/clair/utils"
|
|
||||||
"github.com/guregu/null/zero"
|
"github.com/guregu/null/zero"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// compareStringLists returns the strings that are present in X but not in Y.
|
||||||
|
func compareStringLists(X, Y []string) []string {
|
||||||
|
m := make(map[string]bool)
|
||||||
|
|
||||||
|
for _, y := range Y {
|
||||||
|
m[y] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
diff := []string{}
|
||||||
|
for _, x := range X {
|
||||||
|
if m[x] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
diff = append(diff, x)
|
||||||
|
m[x] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareStringListsInBoth(X, Y []string) []string {
|
||||||
|
m := make(map[string]struct{})
|
||||||
|
|
||||||
|
for _, y := range Y {
|
||||||
|
m[y] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
diff := []string{}
|
||||||
|
for _, x := range X {
|
||||||
|
if _, e := m[x]; e {
|
||||||
|
diff = append(diff, x)
|
||||||
|
delete(m, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID int) ([]database.Vulnerability, int, error) {
|
func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID int) ([]database.Vulnerability, int, error) {
|
||||||
defer observeQueryTime("listVulnerabilities", "all", time.Now())
|
defer observeQueryTime("listVulnerabilities", "all", time.Now())
|
||||||
|
|
||||||
@ -341,8 +379,8 @@ func applyFixedInDiff(currentList, diff []database.FeatureVersion) ([]database.F
|
|||||||
currentMap, currentNames := createFeatureVersionNameMap(currentList)
|
currentMap, currentNames := createFeatureVersionNameMap(currentList)
|
||||||
diffMap, diffNames := createFeatureVersionNameMap(diff)
|
diffMap, diffNames := createFeatureVersionNameMap(diff)
|
||||||
|
|
||||||
addedNames := utils.CompareStringLists(diffNames, currentNames)
|
addedNames := compareStringLists(diffNames, currentNames)
|
||||||
inBothNames := utils.CompareStringListsInBoth(diffNames, currentNames)
|
inBothNames := compareStringListsInBoth(diffNames, currentNames)
|
||||||
|
|
||||||
different := false
|
different := false
|
||||||
|
|
||||||
|
@ -282,3 +282,16 @@ func equalsVuln(t *testing.T, expected, actual *database.Vulnerability) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringComparison(t *testing.T) {
|
||||||
|
cmp := compareStringLists([]string{"a", "b", "b", "a"}, []string{"a", "c"})
|
||||||
|
assert.Len(t, cmp, 1)
|
||||||
|
assert.NotContains(t, cmp, "a")
|
||||||
|
assert.Contains(t, cmp, "b")
|
||||||
|
|
||||||
|
cmp = compareStringListsInBoth([]string{"a", "a", "b", "c"}, []string{"a", "c", "c"})
|
||||||
|
assert.Len(t, cmp, 2)
|
||||||
|
assert.NotContains(t, cmp, "b")
|
||||||
|
assert.Contains(t, cmp, "a")
|
||||||
|
assert.Contains(t, cmp, "c")
|
||||||
|
}
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
// Copyright 2015 clair authors
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package utils
|
|
||||||
|
|
||||||
import "regexp"
|
|
||||||
|
|
||||||
var urlParametersRegexp = regexp.MustCompile(`(\?|\&)([^=]+)\=([^ &]+)`)
|
|
||||||
|
|
||||||
// CleanURL removes all parameters from an URL.
|
|
||||||
func CleanURL(str string) string {
|
|
||||||
return urlParametersRegexp.ReplaceAllString(str, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Contains looks for a string into an array of strings and returns whether
|
|
||||||
// the string exists.
|
|
||||||
func Contains(needle string, haystack []string) bool {
|
|
||||||
for _, h := range haystack {
|
|
||||||
if h == needle {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompareStringLists returns the strings that are present in X but not in Y.
|
|
||||||
func CompareStringLists(X, Y []string) []string {
|
|
||||||
m := make(map[string]bool)
|
|
||||||
|
|
||||||
for _, y := range Y {
|
|
||||||
m[y] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
diff := []string{}
|
|
||||||
for _, x := range X {
|
|
||||||
if m[x] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
diff = append(diff, x)
|
|
||||||
m[x] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return diff
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompareStringListsInBoth returns the strings that are present in both X and Y.
|
|
||||||
func CompareStringListsInBoth(X, Y []string) []string {
|
|
||||||
m := make(map[string]struct{})
|
|
||||||
|
|
||||||
for _, y := range Y {
|
|
||||||
m[y] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
diff := []string{}
|
|
||||||
for _, x := range X {
|
|
||||||
if _, e := m[x]; e {
|
|
||||||
diff = append(diff, x)
|
|
||||||
delete(m, x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return diff
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
// Copyright 2015 clair authors
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/pborman/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
const fileToDownload = "http://www.google.com/robots.txt"
|
|
||||||
|
|
||||||
// TestDiff tests the diff.go source file
|
|
||||||
func TestDiff(t *testing.T) {
|
|
||||||
cmp := CompareStringLists([]string{"a", "b", "b", "a"}, []string{"a", "c"})
|
|
||||||
assert.Len(t, cmp, 1)
|
|
||||||
assert.NotContains(t, cmp, "a")
|
|
||||||
assert.Contains(t, cmp, "b")
|
|
||||||
|
|
||||||
cmp = CompareStringListsInBoth([]string{"a", "a", "b", "c"}, []string{"a", "c", "c"})
|
|
||||||
assert.Len(t, cmp, 2)
|
|
||||||
assert.NotContains(t, cmp, "b")
|
|
||||||
assert.Contains(t, cmp, "a")
|
|
||||||
assert.Contains(t, cmp, "c")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestExec tests the exec.go source file
|
|
||||||
func TestExec(t *testing.T) {
|
|
||||||
_, err := Exec(uuid.New(), "touch", uuid.New())
|
|
||||||
assert.Error(t, err, "Exec should not be able to run in a not existing directory")
|
|
||||||
|
|
||||||
o, err := Exec("/tmp", "echo", "test")
|
|
||||||
assert.Nil(t, err, "Could not exec echo")
|
|
||||||
assert.Equal(t, "test\n", string(o), "Could not exec echo")
|
|
||||||
|
|
||||||
_, err = Exec("/tmp", uuid.New())
|
|
||||||
assert.Error(t, err, "An invalid command should return an error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestString tests the string.go file
|
|
||||||
func TestString(t *testing.T) {
|
|
||||||
assert.False(t, Contains("", []string{}))
|
|
||||||
assert.True(t, Contains("a", []string{"a", "b"}))
|
|
||||||
assert.False(t, Contains("c", []string{"a", "b"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCleanURL(t *testing.T) {
|
|
||||||
assert.Equal(t, "Test http://test.cn/test Test", CleanURL("Test http://test.cn/test?foo=bar&bar=foo Test"))
|
|
||||||
}
|
|
@ -17,6 +17,8 @@
|
|||||||
package worker
|
package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/coreos/pkg/capnslog"
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
@ -25,7 +27,6 @@ import (
|
|||||||
"github.com/coreos/clair/ext/imagefmt"
|
"github.com/coreos/clair/ext/imagefmt"
|
||||||
"github.com/coreos/clair/pkg/commonerr"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
"github.com/coreos/clair/pkg/tarutil"
|
"github.com/coreos/clair/pkg/tarutil"
|
||||||
"github.com/coreos/clair/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -44,8 +45,15 @@ var (
|
|||||||
// ErrParentUnknown is the error that should be raised when a parent layer
|
// ErrParentUnknown is the error that should be raised when a parent layer
|
||||||
// has yet to be processed for the current layer.
|
// has yet to be processed for the current layer.
|
||||||
ErrParentUnknown = commonerr.NewBadRequestError("worker: parent layer is unknown, it must be processed first")
|
ErrParentUnknown = commonerr.NewBadRequestError("worker: parent layer is unknown, it must be processed first")
|
||||||
|
|
||||||
|
urlParametersRegexp = regexp.MustCompile(`(\?|\&)([^=]+)\=([^ &]+)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// cleanURL removes all parameters from an URL.
|
||||||
|
func cleanURL(str string) string {
|
||||||
|
return urlParametersRegexp.ReplaceAllString(str, "")
|
||||||
|
}
|
||||||
|
|
||||||
// Process detects the Namespace of a layer, the features it adds/removes, and
|
// Process detects the Namespace of a layer, the features it adds/removes, and
|
||||||
// then stores everything in the database.
|
// then stores everything in the database.
|
||||||
// TODO(Quentin-M): We could have a goroutine that looks for layers that have been analyzed with an
|
// TODO(Quentin-M): We could have a goroutine that looks for layers that have been analyzed with an
|
||||||
@ -65,7 +73,7 @@ func Process(datastore database.Datastore, imageFormat, name, parentName, path s
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("layer %s: processing (Location: %s, Engine version: %d, Parent: %s, Format: %s)",
|
log.Debugf("layer %s: processing (Location: %s, Engine version: %d, Parent: %s, Format: %s)",
|
||||||
name, utils.CleanURL(path), Version, parentName, imageFormat)
|
name, cleanURL(path), Version, parentName, imageFormat)
|
||||||
|
|
||||||
// Check to see if the layer is already in the database.
|
// Check to see if the layer is already in the database.
|
||||||
layer, err := datastore.FindLayer(name, false, false)
|
layer, err := datastore.FindLayer(name, false, false)
|
||||||
@ -118,7 +126,7 @@ func detectContent(imageFormat, name, path string, headers map[string]string, pa
|
|||||||
totalRequiredFiles := append(featurefmt.RequiredFilenames(), featurens.RequiredFilenames()...)
|
totalRequiredFiles := append(featurefmt.RequiredFilenames(), featurens.RequiredFilenames()...)
|
||||||
files, err := imagefmt.Extract(imageFormat, path, headers, totalRequiredFiles)
|
files, err := imagefmt.Extract(imageFormat, path, headers, totalRequiredFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("layer %s: failed to extract data from %s: %s", name, utils.CleanURL(path), err)
|
log.Errorf("layer %s: failed to extract data from %s: %s", name, cleanURL(path), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user