Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cmd/gpuop-cfg/validate/clusterpolicy/clusterpolicy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 clusterpolicy

import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCommand(t *testing.T) {
cmd := NewCommand(logrus.New())

require.NotNil(t, cmd)
assert.Equal(t, "clusterpolicy", cmd.Name)
assert.NotEmpty(t, cmd.Usage)

flag := cmd.Flags[0]
assert.Contains(t, flag.Names(), "input")
}
71 changes: 71 additions & 0 deletions cmd/gpuop-cfg/validate/clusterpolicy/images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 clusterpolicy

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

v1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
)

func TestValidateImage_InvalidReference(t *testing.T) {
testCases := []struct {
description string
path string
}{
{
description: "empty reference",
path: "",
},
{
description: "malformed reference",
path: "@@bad::ref",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
err := validateImage(context.Background(), tc.path)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to construct an image reference")
})
}
}

func TestValidateImages_EmptyDriverSpecImagePathError(t *testing.T) {
t.Setenv("DRIVER_IMAGE", "")

spec := &v1.ClusterPolicySpec{}

err := validateImages(context.Background(), spec)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to construct the image path")
}

func TestValidateImages_InvalidDriverImageRefError(t *testing.T) {
spec := &v1.ClusterPolicySpec{}
spec.Driver.Image = "@@bad::ref"

err := validateImages(context.Background(), spec)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to validate image")
assert.Contains(t, err.Error(), "failed to construct an image reference")
}
96 changes: 96 additions & 0 deletions cmd/gpuop-cfg/validate/csv/alm-examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 csv

import (
"testing"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestValidateALMExample(t *testing.T) {
testCases := []struct {
description string
annotations map[string]string
expectError bool
errContains string
}{
{
description: "valid example with Kind ClusterPolicy returns nil",
annotations: map[string]string{
"alm-examples": `[{"kind":"ClusterPolicy","apiVersion":"nvidia.com/v1","spec":{}}]`,
},
expectError: false,
},
{
description: "malformed JSON returns an unmarshal error",
annotations: map[string]string{
"alm-examples": `{not valid json`,
},
expectError: true,
},
{
description: "missing alm-examples annotation returns an unmarshal error on empty string",
annotations: map[string]string{},
expectError: true,
},
{
description: "empty alm-examples annotation returns an unmarshal error on empty string",
annotations: map[string]string{
"alm-examples": "",
},
expectError: true,
},
{
description: "empty list returns 'no example clusterpolicy found'",
annotations: map[string]string{
"alm-examples": `[]`,
},
expectError: true,
errContains: "no example clusterpolicy found",
},
{
description: "first item with Kind != ClusterPolicy returns 'invalid example clusterpolicy'",
annotations: map[string]string{
"alm-examples": `[{"kind":"NotAClusterPolicy","apiVersion":"nvidia.com/v1"}]`,
},
expectError: true,
errContains: "invalid example clusterpolicy",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
csv := &v1alpha1.ClusterServiceVersion{}
csv.Annotations = tc.annotations

err := validateALMExample(csv)

if !tc.expectError {
require.NoError(t, err)
return
}

require.Error(t, err)
if tc.errContains != "" {
assert.Contains(t, err.Error(), tc.errContains)
}
})
}
}
36 changes: 36 additions & 0 deletions cmd/gpuop-cfg/validate/csv/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 csv

import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCommand(t *testing.T) {
cmd := NewCommand(logrus.New())

require.NotNil(t, cmd)
assert.Equal(t, "csv", cmd.Name)
assert.NotEmpty(t, cmd.Usage)

flag := cmd.Flags[0]
assert.Contains(t, flag.Names(), "input")
}
62 changes: 62 additions & 0 deletions cmd/gpuop-cfg/validate/csv/images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 csv

import (
"context"
"testing"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestValidateImage_InvalidReference(t *testing.T) {
testCases := []struct {
description string
path string
}{
{
description: "empty reference",
path: "",
},
{
description: "malformed reference",
path: "@@bad::ref",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
err := validateImage(context.Background(), tc.path)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to construct an image reference")
})
}
}

func TestValidateImages_InvalidRelatedImage(t *testing.T) {
csv := &v1alpha1.ClusterServiceVersion{}
csv.Spec.RelatedImages = []v1alpha1.RelatedImage{
{Name: "bad-image", Image: "@@bad::ref"},
}

err := validateImages(context.Background(), csv)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to validate image bad-image")
assert.Contains(t, err.Error(), "failed to construct an image reference")
}
42 changes: 42 additions & 0 deletions cmd/gpuop-cfg/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 validate

import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCommand(t *testing.T) {
cmd := NewCommand(logrus.New())

require.NotNil(t, cmd)
assert.Equal(t, "validate", cmd.Name)
assert.NotEmpty(t, cmd.Usage)

require.Len(t, cmd.Commands, 2)

names := []string{}
for _, sub := range cmd.Commands {
names = append(names, sub.Name)
}
assert.Contains(t, names, "csv")
assert.Contains(t, names, "clusterpolicy")
}
Loading