Skip to content

Commit

Permalink
Add additional filesystem checks for OCI devices (knative#4074)
Browse files Browse the repository at this point in the history
* Add additional filesystem checks for OCI devices

This adds checks for the default OCI devices to our conformance test for
filesystem validation. This test also refactors where the file paths to
check are located to reduce the number of transformations and simplify
adding additional paths.

Fixes knative#2973

* Fix comments

* Code review comments
  • Loading branch information
Dan Gerdesmeier authored and JRBANCEL committed May 29, 2019
1 parent 955d8b5 commit 93054a6
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 184 deletions.
16 changes: 13 additions & 3 deletions test/conformance/envvars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ package conformance

import (
"reflect"
"strconv"
"testing"

"github.com/knative/serving/test"
"github.com/knative/serving/test/types"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -35,7 +37,7 @@ func TestShouldEnvVars(t *testing.T) {
t.Fatal(err)
}
r := reflect.ValueOf(names)
for k, v := range ShouldEnvVars {
for k, v := range types.ShouldEnvVars {
value, exist := ri.Host.EnvVars[k]
if !exist {
t.Fatalf("Runtime contract env variable %q is not set", k)
Expand All @@ -51,15 +53,23 @@ func TestShouldEnvVars(t *testing.T) {
func TestMustEnvVars(t *testing.T) {
t.Parallel()
clients := setup(t)
portStr, ok := types.MustEnvVars["PORT"]
if !ok {
t.Fatal("Missing PORT from set of MustEnvVars")
}
port, err := strconv.Atoi(portStr)
if err != nil {
t.Fatal("Invalid PORT value in MustEnvVars")
}
_, ri, err := fetchRuntimeInfo(t, clients, &test.Options{
ContainerPorts: []corev1.ContainerPort{
{ContainerPort: int32(mustEnvCustomPort)},
{ContainerPort: int32(port)},
},
})
if err != nil {
t.Fatal(err)
}
for k, v := range MustEnvVars {
for k, v := range types.MustEnvVars {
value, exist := ri.Host.EnvVars[k]
if !exist {
t.Fatalf("Runtime contract env variable %q is not set", k)
Expand Down
92 changes: 0 additions & 92 deletions test/conformance/filesystem_perm_test.go

This file was deleted.

93 changes: 93 additions & 0 deletions test/conformance/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// +build e2e

/*
Copyright 2018 The Knative 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.
*/

package conformance

import (
"fmt"
"testing"

"github.com/knative/serving/test"
"github.com/knative/serving/test/types"
)

func verifyModeString(resp string, expected string) error {
if len(resp) != len(expected) {
return fmt.Errorf("mode = %q (len:%d), want: %q (len:%d)", resp, len(resp), expected, len(expected))
}

for index := range expected {
if expected[index] != '*' && expected[index] != resp[index] {
return fmt.Errorf("mode[%d] = %c, want: %c", index, expected[index], resp[index])
}
}
return nil
}

func testFiles(t *testing.T, clients *test.Clients, paths map[string]types.FileInfo) error {
_, ri, err := fetchRuntimeInfo(t, clients, &test.Options{})
if err != nil {
return err
}

for path, file := range paths {
riFile, ok := ri.Host.Files[path]
if !ok {
return fmt.Errorf("runtime contract file info not present: %s", path)
}

if file.Error != "" && file.Error != riFile.Error {
return fmt.Errorf("%s.Error = %s, want: %s", path, riFile.Error, file.Error)
}

if file.IsDir != nil && *file.IsDir != *riFile.IsDir {
return fmt.Errorf("%s.IsDir = %t, want: %t", path, *riFile.IsDir, *file.IsDir)
}

if file.SourceFile != "" && file.SourceFile != riFile.SourceFile {
return fmt.Errorf("%s.SourceFile = %s, want: %s", path, riFile.SourceFile, file.SourceFile)
}

if file.Mode != "" {
if err := verifyModeString(riFile.Mode, file.Mode); err != nil {
return fmt.Errorf("%s has invalid mode string %s: %v", path, riFile.Mode, err)
}
}
}
return nil
}

// TestMustHaveFiles asserts that the file system has all the MUST have paths and they have appropriate permissions
// and type as applicable.
func TestMustHaveFiles(t *testing.T) {
t.Parallel()
clients := setup(t)
if err := testFiles(t, clients, types.MustFiles); err != nil {
t.Error(err)
}
}

// TestShouldHaveFiles asserts that the file system has all the SHOULD have paths and that they have the appropriate
// permissions and type as applicable.
func TestShouldHaveFiles(t *testing.T) {
t.Parallel()
clients := setup(t)
if err := testFiles(t, clients, types.ShouldFiles); err != nil {
t.Error(err)
}
}
67 changes: 0 additions & 67 deletions test/conformance/runtime_contract_types.go

This file was deleted.

32 changes: 13 additions & 19 deletions test/test_images/runtime/handlers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,25 @@ import (
"github.com/knative/serving/test/types"
)

// filePaths is the set of filepaths probed and returned to the
// client as FileInfo.
var filePaths = []string{"/tmp",
"/var/log",
"/dev/log",
"/etc/hosts",
"/etc/hostname",
"/etc/resolv.conf"}

func fileInfo(paths ...string) []*types.FileInfo {
var infoList []*types.FileInfo

func fileInfo(paths ...string) map[string]types.FileInfo {
files := map[string]types.FileInfo{}
for _, path := range paths {
file, err := os.Stat(path)
if err != nil {
infoList = append(infoList, &types.FileInfo{Name: path, Error: err.Error()})
files[path] = types.FileInfo{Error: err.Error()}
continue
}
size := file.Size()
dir := file.IsDir()
infoList = append(infoList, &types.FileInfo{Name: path,
Size: &size,
Mode: file.Mode().String(),
ModTime: file.ModTime(),
IsDir: &dir})
mode := file.Mode()
source, _ := os.Readlink(path)

files[path] = types.FileInfo{
Size: &size,
Mode: mode.String(),
ModTime: file.ModTime(),
SourceFile: source,
IsDir: &dir}
}
return infoList
return files
}
11 changes: 11 additions & 0 deletions test/test_images/runtime/handlers/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ func runtimeHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Retrieving Runtime Information")
w.Header().Set("Content-Type", "application/json")

filePaths := make([]string, len(types.MustFiles)+len(types.ShouldFiles))
i := 0
for key := range types.MustFiles {
filePaths[i] = key
i++
}
for key := range types.ShouldFiles {
filePaths[i] = key
i++
}

k := &types.RuntimeInfo{
Request: requestInfo(r),
Host: &types.HostInfo{EnvVars: env(),
Expand Down
Loading

0 comments on commit 93054a6

Please sign in to comment.