2016-01-19 20:16:45 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/utils/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInsertFeature(t *testing.T) {
|
2016-05-02 22:33:03 +00:00
|
|
|
datastore, err := openDatabaseForTest("InsertFeature", false)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer datastore.Close()
|
|
|
|
|
|
|
|
// Invalid Feature.
|
|
|
|
id0, err := datastore.insertFeature(database.Feature{})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Zero(t, id0)
|
|
|
|
|
|
|
|
id0, err = datastore.insertFeature(database.Feature{
|
|
|
|
Namespace: database.Namespace{},
|
|
|
|
Name: "TestInsertFeature0",
|
|
|
|
})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Zero(t, id0)
|
|
|
|
|
|
|
|
// Insert Feature and ensure we can find it.
|
|
|
|
feature := database.Feature{
|
|
|
|
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace1"},
|
|
|
|
Name: "TestInsertFeature1",
|
|
|
|
}
|
|
|
|
id1, err := datastore.insertFeature(feature)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
id2, err := datastore.insertFeature(feature)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, id1, id2)
|
|
|
|
|
|
|
|
// Insert invalid FeatureVersion.
|
|
|
|
for _, invalidFeatureVersion := range []database.FeatureVersion{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2015-12-28 20:03:29 +00:00
|
|
|
Feature: database.Feature{},
|
|
|
|
Version: types.NewVersionUnsafe("1.0"),
|
|
|
|
},
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2015-12-28 20:03:29 +00:00
|
|
|
Feature: database.Feature{
|
|
|
|
Namespace: database.Namespace{},
|
|
|
|
Name: "TestInsertFeature2",
|
|
|
|
},
|
|
|
|
Version: types.NewVersionUnsafe("1.0"),
|
|
|
|
},
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2015-12-28 20:03:29 +00:00
|
|
|
Feature: database.Feature{
|
|
|
|
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace2"},
|
|
|
|
Name: "TestInsertFeature2",
|
|
|
|
},
|
|
|
|
Version: types.NewVersionUnsafe(""),
|
|
|
|
},
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2015-12-28 20:03:29 +00:00
|
|
|
Feature: database.Feature{
|
|
|
|
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace2"},
|
|
|
|
Name: "TestInsertFeature2",
|
|
|
|
},
|
|
|
|
Version: types.NewVersionUnsafe("bad version"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
id3, err := datastore.insertFeatureVersion(invalidFeatureVersion)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Zero(t, id3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert FeatureVersion and ensure we can find it.
|
|
|
|
featureVersion := database.FeatureVersion{
|
|
|
|
Feature: database.Feature{
|
|
|
|
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace1"},
|
|
|
|
Name: "TestInsertFeature1",
|
|
|
|
},
|
|
|
|
Version: types.NewVersionUnsafe("2:3.0-imba"),
|
|
|
|
}
|
|
|
|
id4, err := datastore.insertFeatureVersion(featureVersion)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
id5, err := datastore.insertFeatureVersion(featureVersion)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, id4, id5)
|
|
|
|
}
|