sigs.k8s.io/seccomp-operator@v0.1.0/test/suite_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  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/go-logr/logr"
    30  	"github.com/stretchr/testify/suite"
    31  	"k8s.io/klog/v2/klogr"
    32  	"k8s.io/release/pkg/command"
    33  	"k8s.io/release/pkg/util"
    34  
    35  	"sigs.k8s.io/seccomp-operator/internal/pkg/config"
    36  )
    37  
    38  const (
    39  	kindVersion = "v0.8.1"
    40  	kindSHA512  = "ff71adddbe043df84c4dee82f034c6856bfc51c931bb7839d9c09d02fca93bfe961a9c05d7c657371963cc81febee175133598bba50fb1481a9faa06b42abdc3" // nolint: lll
    41  	kindImage   = "kindest/node:v1.18.2"
    42  	testImage   = config.OperatorName + ":latest"
    43  )
    44  
    45  type e2e struct {
    46  	suite.Suite
    47  	kindPath    string
    48  	kubectlPath string
    49  	clusterName string
    50  	logger      logr.Logger
    51  }
    52  
    53  func TestSuite(t *testing.T) {
    54  	suite.Run(t, &e2e{logger: klogr.New()})
    55  }
    56  
    57  // SetupSuite downloads kind and searches for kubectl in $PATH.
    58  func (e *e2e) SetupSuite() {
    59  	e.logf("Setting up suite")
    60  	command.SetGlobalVerbose(true)
    61  	cwd, err := os.Getwd()
    62  	e.Nil(err)
    63  
    64  	parentCwd := filepath.Dir(cwd)
    65  	e.Nil(os.Chdir(parentCwd))
    66  	buildDir := filepath.Join(parentCwd, "build")
    67  	e.Nil(os.MkdirAll(buildDir, 0o755))
    68  
    69  	e.kindPath = filepath.Join(buildDir, "kind")
    70  	e.downloadAndVerify(
    71  		"https://github.com/kubernetes-sigs/kind/releases/download/"+
    72  			kindVersion+"/kind-linux-amd64",
    73  		e.kindPath, kindSHA512,
    74  	)
    75  
    76  	e.kubectlPath, err = exec.LookPath("kubectl")
    77  	e.Nil(err)
    78  }
    79  
    80  // SetupTest starts a fresh kind cluster for each test.
    81  func (e *e2e) SetupTest() {
    82  	// Deploy the cluster
    83  	e.logf("Deploying the cluster")
    84  	e.clusterName = fmt.Sprintf("so-e2e-%d", time.Now().Unix())
    85  
    86  	cmd := exec.Command( // nolint: gosec
    87  		e.kindPath, "create", "cluster",
    88  		"--name="+e.clusterName,
    89  		"--image="+kindImage,
    90  		"-v=3",
    91  		"--config=test/kind-config.yaml",
    92  	)
    93  	cmd.Stderr = os.Stderr
    94  	cmd.Stdout = os.Stdout
    95  	e.Nil(cmd.Run())
    96  
    97  	// Wait for the nodes to  be ready
    98  	e.logf("Waiting for cluster to be ready")
    99  	e.kubectl("wait", "--for", "condition=ready", "nodes", "--all")
   100  
   101  	// Build and load the test image
   102  	e.logf("Building operator container image")
   103  	e.run("make", "image", "IMAGE="+testImage)
   104  	e.logf("Loading container image into nodes")
   105  	e.run(
   106  		e.kindPath, "load", "docker-image", "--name="+e.clusterName, testImage,
   107  	)
   108  }
   109  
   110  // TearDownTest stops the kind cluster.
   111  func (e *e2e) TearDownTest() {
   112  	e.logf("Destroying cluster")
   113  	e.run(
   114  		e.kindPath, "delete", "cluster",
   115  		"--name="+e.clusterName,
   116  		"-v=3",
   117  	)
   118  }
   119  
   120  func (e *e2e) run(cmd string, args ...string) string {
   121  	output, err := command.New(cmd, args...).RunSuccessOutput()
   122  	e.Nil(err)
   123  	return output.OutputTrimNL()
   124  }
   125  
   126  func (e *e2e) runFailure(cmd string, args ...string) string {
   127  	output, err := command.New(cmd, args...).Run()
   128  	e.Nil(err)
   129  	e.False(output.Success())
   130  	return output.Error()
   131  }
   132  
   133  func (e *e2e) downloadAndVerify(url, binaryPath, sha512 string) {
   134  	if !util.Exists(binaryPath) {
   135  		e.logf("Downloading %s", binaryPath)
   136  		e.run("curl", "-o", binaryPath, "-fL", url)
   137  		e.Nil(os.Chmod(binaryPath, 0o700))
   138  		e.verifySHA512(binaryPath, sha512)
   139  	}
   140  }
   141  
   142  func (e *e2e) verifySHA512(binaryPath, sha512 string) {
   143  	e.Nil(command.New("sha512sum", binaryPath).
   144  		Pipe("grep", sha512).
   145  		RunSilentSuccess(),
   146  	)
   147  }
   148  
   149  func (e *e2e) kubectl(args ...string) string {
   150  	return e.run(e.kubectlPath, args...)
   151  }
   152  
   153  func (e *e2e) kubectlFailure(args ...string) string {
   154  	return e.runFailure(e.kubectlPath, args...)
   155  }
   156  
   157  func (e *e2e) kubectlOperatorNS(args ...string) string {
   158  	return e.kubectl(
   159  		append([]string{"-n", config.OperatorName}, args...)...,
   160  	)
   161  }
   162  
   163  func (e *e2e) logf(format string, a ...interface{}) {
   164  	e.logger.Info(fmt.Sprintf(format, a...))
   165  }
   166  
   167  func (e *e2e) execNode(node string, args ...string) string {
   168  	return e.run("docker", append([]string{"exec", node}, args...)...)
   169  }