Skip to content

Commit

Permalink
make test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikykun committed Aug 4, 2023
1 parent 1ba7f43 commit 52ec064
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out

mkdir -p ${ENVTEST_ASSETS_DIR}
source ./artifacts/scripts/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... -coverprofile cover.out
##@ Build

.PHONY: build
Expand Down
13 changes: 7 additions & 6 deletions pkg/controllers/ruleset/processor/rules/webhook/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ type Server struct {
server *http.Server
}

func (s *Server) Run(stop <-chan struct{}) {
func (s *Server) Run(stop <-chan struct{}) chan struct{} {
go func() {
if err := s.server.ListenAndServe(); err != nil {
fmt.Println(err)
}
}()

<-time.After(5 * time.Second)

finish := make(chan struct{})
fmt.Println("run server")
go func() {
select {
case <-stop:
s.server.Close()
finish <- struct{}{}
}
}()
return
return finish
}

func RunHttpServer(f func(http.ResponseWriter, *http.Request)) chan struct{} {
func RunHttpServer(f func(http.ResponseWriter, *http.Request)) (chan struct{}, chan struct{}) {
fmt.Println("try run http server")
defer fmt.Println("running")
server := &Server{
Expand All @@ -59,8 +60,8 @@ func RunHttpServer(f func(http.ResponseWriter, *http.Request)) chan struct{} {
},
}
stopped := make(chan struct{})
server.Run(stopped)
return stopped
finish := server.Run(stopped)
return stopped, finish
}

func handleHttpAlwaysSuccess(resp http.ResponseWriter, req *http.Request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ var (
)

func TestWebhookCase1(t *testing.T) {
stop := RunHttpServer(handleHttpAlwaysSuccess)
stop, finish := RunHttpServer(handleHttpAlwaysSuccess)
defer func() {
stop <- struct{}{}
<-finish
}()
targets := map[string]*corev1.Pod{
"test-pod-a": (&podTemplate{Name: "test-pod-a", Ip: "11.166.170.58"}).GetPod(),
Expand All @@ -103,9 +104,10 @@ func TestWebhookCase1(t *testing.T) {
}

func TestWebhookCase2(t *testing.T) {
stop := RunHttpServer(handleHttpAlwaysFalse)
stop, finish := RunHttpServer(handleHttpAlwaysFalse)
defer func() {
stop <- struct{}{}
<-finish
}()
targets := map[string]*corev1.Pod{
"test-pod-a": (&podTemplate{Name: "test-pod-a", Ip: "11.166.170.58"}).GetPod(),
Expand All @@ -125,9 +127,10 @@ func TestWebhookCase2(t *testing.T) {
}

func TestWebhookCase3(t *testing.T) {
stop := RunHttpServer(handleHttpWithTrace)
stop, finish := RunHttpServer(handleHttpWithTrace)
defer func() {
stop <- struct{}{}
<-finish
}()
targets := map[string]*corev1.Pod{
"test-pod-a": (&podTemplate{Name: "test-pod-a", Ip: "11.166.170.58"}).GetPod(),
Expand Down
96 changes: 96 additions & 0 deletions scripts/setup-envtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes 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.

set -o errexit
set -o pipefail

# Turn colors in this script off by setting the NO_COLOR variable in your
# environment to any value:
#
# $ NO_COLOR=1 test.sh
NO_COLOR=${NO_COLOR:-""}
if [ -z "$NO_COLOR" ]; then
header=$'\e[1;33m'
reset=$'\e[0m'
else
header=''
reset=''
fi

function header_text {
echo "$header$*$reset"
}

function setup_envtest_env {
header_text "setting up env vars"

# Setup env vars
KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS:-""}
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$1/bin
fi
}

# fetch k8s API gen tools and make it available under envtest_root_dir/bin.
#
# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable
# in your environment to any value:
#
# $ SKIP_FETCH_TOOLS=1 ./check-everything.sh
#
# If you skip fetching tools, this script will use the tools already on your
# machine.
function fetch_envtest_tools {
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
if [ -n "$SKIP_FETCH_TOOLS" ]; then
return 0
fi

tmp_root=/tmp
#envtest_root_dir=$tmp_root/envtest

k8s_version="${ENVTEST_K8S_VERSION:-1.19.2}"
goarch="$(go env GOARCH)"
goos="$(go env GOOS)"

if [[ "$goos" != "linux" && "$goos" != "darwin" ]]; then
echo "OS '$goos' not supported. Aborting." >&2
return 1
fi

local dest_dir="${1}"

# use the pre-existing version in the temporary folder if it matches our k8s version
if [[ -x "${dest_dir}/bin/kube-apiserver" ]]; then
version=$("${dest_dir}"/bin/kube-apiserver --version)
if [[ $version == *"${k8s_version}"* ]]; then
header_text "Using cached envtest tools from ${dest_dir}"
return 0
fi
fi

header_text "fetching envtest tools@${k8s_version} (into '${dest_dir}')"
envtest_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
envtest_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$envtest_tools_archive_name"

envtest_tools_archive_path="$tmp_root/$envtest_tools_archive_name"
if [ ! -f "$envtest_tools_archive_path" ]; then
curl -sL "${envtest_tools_download_url}" -o "$envtest_tools_archive_path"
fi

mkdir -p "${dest_dir}"
tar -C "${dest_dir}" --strip-components=1 -zvxf "$envtest_tools_archive_path"
}

0 comments on commit 52ec064

Please sign in to comment.