1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package server
18
19import (
20	"encoding/json"
21	"io/ioutil"
22	"os"
23	"testing"
24
25	"github.com/containerd/containerd/oci"
26	"github.com/stretchr/testify/assert"
27	"github.com/stretchr/testify/require"
28
29	criconfig "github.com/containerd/containerd/pkg/cri/config"
30	servertesting "github.com/containerd/containerd/pkg/cri/server/testing"
31	containerstore "github.com/containerd/containerd/pkg/cri/store/container"
32	imagestore "github.com/containerd/containerd/pkg/cri/store/image"
33	"github.com/containerd/containerd/pkg/cri/store/label"
34	sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
35	snapshotstore "github.com/containerd/containerd/pkg/cri/store/snapshot"
36	ostesting "github.com/containerd/containerd/pkg/os/testing"
37	"github.com/containerd/containerd/pkg/registrar"
38)
39
40const (
41	testRootDir  = "/test/root"
42	testStateDir = "/test/state"
43	// Use an image id as test sandbox image to avoid image name resolve.
44	// TODO(random-liu): Change this to image name after we have complete image
45	// management unit test framework.
46	testSandboxImage = "sha256:c75bebcdd211f41b3a460c7bf82970ed6c75acaab9cd4c9a4e125b03ca113798"
47	testImageFSPath  = "/test/image/fs/path"
48)
49
50// newTestCRIService creates a fake criService for test.
51func newTestCRIService() *criService {
52	labels := label.NewStore()
53	return &criService{
54		config: criconfig.Config{
55			RootDir:  testRootDir,
56			StateDir: testStateDir,
57			PluginConfig: criconfig.PluginConfig{
58				SandboxImage: testSandboxImage,
59			},
60		},
61		imageFSPath:        testImageFSPath,
62		os:                 ostesting.NewFakeOS(),
63		sandboxStore:       sandboxstore.NewStore(labels),
64		imageStore:         imagestore.NewStore(nil),
65		snapshotStore:      snapshotstore.NewStore(),
66		sandboxNameIndex:   registrar.NewRegistrar(),
67		containerStore:     containerstore.NewStore(labels),
68		containerNameIndex: registrar.NewRegistrar(),
69		netPlugin:          servertesting.NewFakeCNIPlugin(),
70	}
71}
72
73func TestLoadBaseOCISpec(t *testing.T) {
74	spec := oci.Spec{Version: "1.0.2", Hostname: "default"}
75
76	file, err := ioutil.TempFile("", "spec-test-")
77	require.NoError(t, err)
78
79	defer func() {
80		assert.NoError(t, file.Close())
81		assert.NoError(t, os.RemoveAll(file.Name()))
82	}()
83
84	err = json.NewEncoder(file).Encode(&spec)
85	assert.NoError(t, err)
86
87	config := criconfig.Config{}
88	config.Runtimes = map[string]criconfig.Runtime{
89		"runc": {BaseRuntimeSpec: file.Name()},
90	}
91
92	specs, err := loadBaseOCISpecs(&config)
93	assert.NoError(t, err)
94
95	assert.Len(t, specs, 1)
96
97	out, ok := specs[file.Name()]
98	assert.True(t, ok, "expected spec with file name %q", file.Name())
99
100	assert.Equal(t, "1.0.2", out.Version)
101	assert.Equal(t, "default", out.Hostname)
102}
103