2021-10-29 14:44:31 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, 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 oci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadFrom(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
contents []byte
|
|
|
|
isError bool
|
|
|
|
spec *specs.Spec
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
contents: []byte{},
|
|
|
|
isError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
contents: []byte("{}"),
|
|
|
|
isError: false,
|
|
|
|
spec: &specs.Spec{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2022-01-31 14:11:36 +00:00
|
|
|
var spec *specs.Spec
|
2022-03-14 11:53:22 +00:00
|
|
|
spec, err := LoadFrom(bytes.NewReader(tc.contents))
|
2021-10-29 14:44:31 +00:00
|
|
|
|
|
|
|
if tc.isError {
|
|
|
|
require.Error(t, err, "%d: %v", i, tc)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.spec == nil {
|
2022-01-31 14:11:36 +00:00
|
|
|
require.Nil(t, spec, "%d: %v", i, tc)
|
2021-10-29 14:44:31 +00:00
|
|
|
} else {
|
2022-01-31 14:11:36 +00:00
|
|
|
require.EqualValues(t, tc.spec, spec, "%d: %v", i, tc)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlushTo(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
isError bool
|
|
|
|
spec *specs.Spec
|
|
|
|
contents string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
spec: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
spec: &specs.Spec{},
|
|
|
|
contents: "{\"ociVersion\":\"\"}\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
buffer := bytes.Buffer{}
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
err := flushTo(tc.spec, &buffer)
|
2021-10-29 14:44:31 +00:00
|
|
|
|
|
|
|
if tc.isError {
|
|
|
|
require.Error(t, err, "%d: %v", i, tc)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.EqualValues(t, tc.contents, buffer.String(), "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a simple test for a writer that returns an error when writing
|
2022-01-31 14:11:36 +00:00
|
|
|
err := flushTo(&specs.Spec{}, errorWriter{})
|
2021-10-29 14:44:31 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorWriter implements the io.Writer interface, always returning an error when
|
|
|
|
// writing.
|
|
|
|
type errorWriter struct{}
|
|
|
|
|
|
|
|
func (e errorWriter) Write([]byte) (int, error) {
|
|
|
|
return 0, fmt.Errorf("error writing")
|
|
|
|
}
|