Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCP4: Add remediation equality unit tests #5743

Merged
merged 3 commits into from
May 7, 2020
Merged
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
3 changes: 2 additions & 1 deletion tests/unit/kubernetes/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PROFILE?=
CONTENT_IMAGE?=
export ROOT_DIR=$(shell git rev-parse --show-toplevel)
export GOFLAGS=-mod=vendor
TEST_DIR=$(ROOT_DIR)/tests/unit/kubernetes
TEST_FLAGS?=-v
TEST_FLAGS?=-v -count=1

.PHONY: all
all: unit
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/kubernetes/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/ajeddeloh/go-json v0.0.0-20200220154158-5ae607161559 // indirect
github.com/coreos/ignition v0.35.0 // indirect
github.com/coreos/ignition v0.35.0
github.com/google/gofuzz v1.1.0 // indirect
github.com/openshift/machine-config-operator v4.2.0-alpha.0.0.20190917115525-033375cbe820+incompatible
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50 // indirect
Expand Down
61 changes: 60 additions & 1 deletion tests/unit/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

ign "github.com/coreos/ignition/config/v2_2/types"
mcfgapi "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"gopkg.in/yaml.v2"
Expand All @@ -34,6 +36,11 @@ type undesiredSelection struct {
Reason string
}

type fileTracker struct {
FileInContent string
File *ign.File
}

func newUnitTestContext(t *testing.T) *unitTestContext {
rootdir := os.Getenv("ROOT_DIR")
if rootdir == "" {
Expand Down Expand Up @@ -65,16 +72,37 @@ func (ctx *unitTestContext) assertProfileIsValid(path string) *profile {
return obj
}

func (ctx *unitTestContext) assertMachineConfigIsValid(obj *unstructured.Unstructured, path string) {
func (ctx *unitTestContext) assertMachineConfigIsValid(obj *unstructured.Unstructured, path string) *mcfgv1.MachineConfig {
mcfg := &mcfgv1.MachineConfig{}
unstructured := obj.UnstructuredContent()
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, mcfg)

if err != nil {
ctx.t.Errorf("The MachineConfig in the following path is not valid '%s': %s", path, err)
return nil
}
return mcfg
}

func (ctx *unitTestContext) assertFileDoesntDiffer(mcfg *mcfgv1.MachineConfig, contentPath string, affectedFiles map[string]fileTracker) {
files := mcfg.Spec.Config.Storage.Files
for i, file := range files {
trackedFile, found := affectedFiles[file.Path]
if found {
if !fileDiff(trackedFile.File, &file) {
ctx.t.Errorf("Files differ: %s - %s", trackedFile.FileInContent, contentPath)
}
//ctx.t.Logf("Files EQUAL: %s - %s", trackedFile.FileInContent, contentPath)
} else {
affectedFiles[file.Path] = fileTracker{FileInContent: contentPath, File: &files[i]}
}
}
}

func fileDiff(f1, f2 *ign.File) bool {
return reflect.DeepEqual(f1, f2)
}

func (ctx *unitTestContext) assertWithRelevantFiles(startDir string,
isRelevantDir func(string, os.FileInfo) bool, isRelevantFile func(string) bool,
assertion func(path string)) {
Expand Down Expand Up @@ -129,6 +157,10 @@ func isRelevantContentDir(path string, info os.FileInfo) bool {
if strings.Contains(path, "ocp4e2e/") {
return false
}
// We don't care about the build directory
if strings.Contains(path, "build/") {
return false
}
// We don't care about this folder
if strings.Contains(path, "unit/kubernetes") {
return false
Expand Down Expand Up @@ -159,6 +191,33 @@ func isMachineConfig(obj *unstructured.Unstructured) bool {
return "MachineConfig" == objgvk.Kind && mcfgapi.GroupName == objgvk.Group
}

func mcfgChangesFile(mcfg *mcfgv1.MachineConfig, t *testing.T) bool {
if mcfg == nil {
return false
}

files := mcfg.Spec.Config.Storage.Files
if files == nil || len(files) == 0 {
return false
}

for _, file := range files {
if file.Mode != nil {
return true
}
if file.Group != nil {
return true
}
if file.User != nil {
return true
}
if file.Contents.Source != "" {
return true
}
}
return false
}

// Reads a YAML file and returns an unstructured object from it. This object
// can be taken into use by the dynamic client
func readObjFromYAMLFilePath(path string) (*unstructured.Unstructured, error) {
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/kubernetes/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
func TestUnit(t *testing.T) {
ctx := newUnitTestContext(t)
t.Run("Verify kubernetes files are correct", func(t *testing.T) {
filesTouchedByMCs := make(map[string]fileTracker)
ctx.assertWithRelevantContentFiles(func(path string) {
obj := ctx.assertObjectIsValid(path)

if isMachineConfig(obj) {
ctx.assertMachineConfigIsValid(obj, path)
mcfg := ctx.assertMachineConfigIsValid(obj, path)
if mcfgChangesFile(mcfg, ctx.t) {
ctx.assertFileDoesntDiffer(mcfg, path, filesTouchedByMCs)
}
}
})
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.