sigs.k8s.io/seccomp-operator@v0.1.0/test/tc_invalid_profile_test.go (about)

     1  // +build e2e
     2  
     3  /*
     4  Copyright 2020 The Kubernetes Authors.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10      http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  package e2e_test
    20  
    21  import (
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  
    26  	"sigs.k8s.io/seccomp-operator/internal/pkg/controllers/profile"
    27  )
    28  
    29  func (e *e2e) testCaseDeployInvalidProfile(nodes []string) {
    30  	const (
    31  		configMapName = "invalid-profile"
    32  		profileName   = "profile-invalid.json"
    33  	)
    34  
    35  	newProfile := func(content string) string {
    36  		configMapString := fmt.Sprintf(`---
    37  apiVersion: v1
    38  kind: ConfigMap
    39  metadata:
    40    name: %s
    41    annotations:
    42      seccomp.security.kubernetes.io/profile: "true"
    43  data:
    44    %s: |-
    45      %s
    46  `, configMapName, profileName, content)
    47  		configMapFile, err := ioutil.TempFile("", configMapName)
    48  		e.Nil(err)
    49  		_, err = configMapFile.WriteString(configMapString)
    50  		e.Nil(err)
    51  		return configMapFile.Name()
    52  	}
    53  
    54  	e.logf("Deploying invalid configMap")
    55  	invalidProfile := newProfile(`{ "defaultAction": true }`)
    56  	e.kubectl("create", "-f", invalidProfile)
    57  	defer func() {
    58  		e.kubectl("delete", "-f", invalidProfile)
    59  		e.Nil(os.RemoveAll(invalidProfile))
    60  	}()
    61  
    62  	// Verify the event
    63  	e.logf("Verifying events")
    64  	eventsOutput := e.kubectl("get", "events")
    65  	for _, s := range []string{
    66  		"Warning",
    67  		"InvalidSeccompProfile",
    68  		"configmap/" + configMapName,
    69  		"decoding seccomp profile: json: cannot unmarshal bool into " +
    70  			"Go struct field Seccomp.defaultAction of type seccomp.Action",
    71  	} {
    72  		e.Contains(eventsOutput, s)
    73  	}
    74  
    75  	// Check that the profile is not reconciled to the node
    76  	e.logf("Verifying node content")
    77  	configMap := e.getConfigMap(configMapName, "default")
    78  	profilePath, err := profile.GetProfilePath(profileName, configMap)
    79  	e.Nil(err)
    80  	for _, node := range nodes {
    81  		e.execNode(node, "bash", "-c", fmt.Sprintf("[ ! -f %s ]", profilePath))
    82  	}
    83  
    84  	// Make the profile valid
    85  	e.logf("Patching invalid configMap to be valid again")
    86  	validProfile := newProfile(`{ "defaultAction": "SCMP_ACT_ALLOW" }`)
    87  	e.kubectl("apply", "-f", validProfile)
    88  	defer e.Nil(os.RemoveAll(validProfile))
    89  	for _, node := range nodes {
    90  		e.execNode(node, "bash", "-c", fmt.Sprintf("[ -f %s ]", profilePath))
    91  	}
    92  }