Skip to content

Commit

Permalink
database: add ability to list namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 0c5cdab commit 1e4ded6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
)

type Datastore interface {
// Namespace
ListNamespaces() ([]Namespace, error)

// Layer
InsertLayer(Layer) error
FindLayer(name string, withFeatures, withVulnerabilities bool) (Layer, error)
Expand Down
24 changes: 24 additions & 0 deletions database/pgsql/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion database/pgsql/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package pgsql

import (
"testing"

"fmt"

"github.com/coreos/clair/database"
"github.com/stretchr/testify/assert"
)
Expand All @@ -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))
}
}
}
}
2 changes: 2 additions & 0 deletions database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 1e4ded6

Please sign in to comment.