2019-07-02 19:30:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 DigitalOcean
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-06-25 20:00:21 +00:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/digitalocean/clusterlint/checks"
|
|
|
|
"github.com/digitalocean/clusterlint/kube"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostpathCheckMeta(t *testing.T) {
|
|
|
|
hostPathCheck := hostPathCheck{}
|
|
|
|
assert.Equal(t, "hostpath-volume", hostPathCheck.Name())
|
|
|
|
assert.Equal(t, []string{"basic"}, hostPathCheck.Groups())
|
2019-06-26 13:25:07 +00:00
|
|
|
assert.NotEmpty(t, hostPathCheck.Description())
|
2019-06-25 20:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostpathCheckRegistration(t *testing.T) {
|
|
|
|
hostPathCheck := &hostPathCheck{}
|
|
|
|
check, err := checks.Get("hostpath-volume")
|
2019-06-26 13:25:07 +00:00
|
|
|
assert.NoError(t, err)
|
2019-06-25 20:00:21 +00:00
|
|
|
assert.Equal(t, check, hostPathCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostpathVolumeError(t *testing.T) {
|
2019-06-26 13:25:07 +00:00
|
|
|
tests := []struct {
|
2019-06-25 20:00:21 +00:00
|
|
|
name string
|
2019-06-26 13:25:07 +00:00
|
|
|
objs *kube.Objects
|
2019-06-25 20:00:21 +00:00
|
|
|
expected []checks.Diagnostic
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no pods",
|
2019-06-26 13:25:07 +00:00
|
|
|
objs: initPod(),
|
2019-06-25 20:00:21 +00:00
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pod with no volumes",
|
2019-06-26 13:25:07 +00:00
|
|
|
objs: container("docker.io/nginx:foo"),
|
2019-06-25 20:00:21 +00:00
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pod with other volume",
|
2019-06-26 13:25:07 +00:00
|
|
|
objs: volume(corev1.VolumeSource{
|
2019-06-25 20:00:21 +00:00
|
|
|
GitRepo: &corev1.GitRepoVolumeSource{Repository: "boo"},
|
|
|
|
}),
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pod with hostpath volume",
|
2019-06-26 13:25:07 +00:00
|
|
|
objs: volume(corev1.VolumeSource{
|
2019-06-25 20:00:21 +00:00
|
|
|
HostPath: &corev1.HostPathVolumeSource{Path: "/tmp"},
|
|
|
|
}),
|
|
|
|
expected: []checks.Diagnostic{
|
|
|
|
{
|
2019-07-02 20:06:24 +00:00
|
|
|
Check: "hostpath-volume",
|
2019-06-25 20:00:21 +00:00
|
|
|
Severity: checks.Error,
|
2019-06-26 12:43:06 +00:00
|
|
|
Message: "Avoid using hostpath for volume 'bar'.",
|
2019-06-25 20:00:21 +00:00
|
|
|
Kind: checks.Pod,
|
|
|
|
Object: GetObjectMeta(),
|
|
|
|
Owners: GetOwners(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPathCheck := hostPathCheck{}
|
|
|
|
|
2019-06-26 13:25:07 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
d, err := hostPathCheck.Run(test.objs)
|
2019-06-25 20:00:21 +00:00
|
|
|
assert.NoError(t, err)
|
2019-06-26 13:25:07 +00:00
|
|
|
assert.ElementsMatch(t, test.expected, d)
|
2019-06-25 20:00:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func volume(volumeSrc corev1.VolumeSource) *kube.Objects {
|
|
|
|
objs := initPod()
|
|
|
|
objs.Pods.Items[0].Spec = corev1.PodSpec{
|
|
|
|
Volumes: []corev1.Volume{
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
VolumeSource: volumeSrc,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
return objs
|
|
|
|
}
|