sigs.k8s.io/seccomp-operator@v0.1.0/test/e2e_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  	"encoding/json"
    23  	"fmt"
    24  	"strings"
    25  
    26  	v1 "k8s.io/api/core/v1"
    27  )
    28  
    29  const manifest = "deploy/operator.yaml"
    30  
    31  func (e *e2e) TestSeccompOperator() {
    32  	// Deploy the operator
    33  	e.deployOperator(manifest)
    34  	defer e.run("git", "checkout", manifest)
    35  
    36  	// Retrieve the inputs for the test cases
    37  	nodes := e.getWorkerNodes()
    38  
    39  	// Execute the test cases. Each test case should cleanup on its own and
    40  	// leave a working operator behind.
    41  	for _, testCase := range []struct {
    42  		description string
    43  		fn          func(nodes []string)
    44  	}{
    45  		{
    46  			"Verify default and example profiles",
    47  			e.testCaseDefaultAndExampleProfiles,
    48  		},
    49  		{
    50  			"Run a test pod",
    51  			e.testCaseRunPod,
    52  		},
    53  		{
    54  			"Re-deploy the operator",
    55  			e.testCaseReDeployOperator,
    56  		},
    57  		{
    58  			"Deploy invalid profile",
    59  			e.testCaseDeployInvalidProfile,
    60  		},
    61  	} {
    62  		e.logf("> Running testcase: %s", testCase.description)
    63  		testCase.fn(nodes)
    64  	}
    65  }
    66  
    67  func (e *e2e) deployOperator(manifest string) {
    68  	// Ensure that we do not accidentally pull the image and use the pre-loaded
    69  	// ones from the nodes
    70  	e.logf("Setting imagePullPolicy to 'Never' in manifest: %s", manifest)
    71  	e.run(
    72  		"sed", "-i", "s;imagePullPolicy: Always;imagePullPolicy: Never;g",
    73  		manifest,
    74  	)
    75  
    76  	// Update the image name to match the test image
    77  	e.run(
    78  		"sed", "-i", fmt.Sprintf("s;image: .*gcr.io/.*;image: %s;g", testImage),
    79  		manifest,
    80  	)
    81  
    82  	// Deploy the operator
    83  	e.logf("Deploying operator")
    84  	e.kubectl("create", "-f", manifest)
    85  
    86  	// Wait for the operator to be ready
    87  	e.logf("Waiting for operator to be ready")
    88  	e.kubectlOperatorNS("wait", "--for", "condition=ready", "pod", "--all")
    89  }
    90  
    91  func (e *e2e) getWorkerNodes() []string {
    92  	e.logf("Getting worker nodes")
    93  	nodesOutput := e.kubectl(
    94  		"get", "nodes",
    95  		"-l", "node-role.kubernetes.io/master!=",
    96  		"-o", `jsonpath={range .items[*]}{@.metadata.name}{" "}{end}`,
    97  	)
    98  	nodes := strings.Fields(nodesOutput)
    99  	e.logf("Got worker nodes: %v", nodes)
   100  
   101  	return nodes
   102  }
   103  
   104  func (e *e2e) getConfigMap(name, namespace string) *v1.ConfigMap {
   105  	configMapJSON := e.kubectl(
   106  		"-n", namespace, "get", "configmap", name, "-o", "json",
   107  	)
   108  	configMap := &v1.ConfigMap{}
   109  	e.Nil(json.Unmarshal([]byte(configMapJSON), configMap))
   110  	return configMap
   111  }