database: add ability to list namespaces

This commit is contained in:
Quentin Machu 2016-01-28 11:26:34 -05:00 committed by Jimmy Zelinskie
parent 0c5cdab0b1
commit 1e4ded6f2b
4 changed files with 53 additions and 1 deletions

View File

@ -37,6 +37,9 @@ var (
)
type Datastore interface {
// Namespace
ListNamespaces() ([]Namespace, error)
// Layer
InsertLayer(Layer) error
FindLayer(name string, withFeatures, withVulnerabilities bool) (Layer, error)

View File

@ -49,3 +49,27 @@ func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) {
return id, nil
}
func (pgSQL *pgSQL) ListNamespaces() (namespaces []database.Namespace, err error) {
rows, err := pgSQL.Query(getQuery("l_namespace"))
if err != nil {
return namespaces, handleError("l_namespace", err)
}
defer rows.Close()
for rows.Next() {
var namespace database.Namespace
err = rows.Scan(&namespace.ID, &namespace.Name)
if err != nil {
return namespaces, handleError("l_namespace.Scan()", err)
}
namespaces = append(namespaces, namespace)
}
if err = rows.Err(); err != nil {
return namespaces, handleError("l_namespace.Rows()", err)
}
return namespaces, err
}

View File

@ -16,7 +16,8 @@ package pgsql
import (
"testing"
"fmt"
"github.com/coreos/clair/database"
"github.com/stretchr/testify/assert"
)
@ -41,3 +42,25 @@ func TestInsertNamespace(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, id1, id2)
}
func TestListNamespace(t *testing.T) {
datastore, err := OpenForTest("ListNamespaces", true)
if err != nil {
t.Error(err)
return
}
defer datastore.Close()
namespaces, err := datastore.ListNamespaces()
assert.Nil(t, err)
if assert.Len(t, namespaces, 2) {
for _, namespace := range namespaces {
switch namespace.Name {
case "debian:7", "debian:8":
continue
default:
assert.Error(t, fmt.Errorf("ListNamespaces should not have returned '%s'", namespace.Name))
}
}
}
}

View File

@ -43,6 +43,8 @@ func init() {
UNION
SELECT id FROM new_namespace`
queries["l_namespace"] = `SELECT id, name FROM Namespace`
// feature.go
queries["soi_feature"] = `
WITH new_feature AS (