2017-11-22 13:35:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/internal/file"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/controller"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateApiserverClient(t *testing.T) {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
kubeConfigFile := fmt.Sprintf("%v/.kube/config", home)
|
|
|
|
|
|
|
|
cli, err := createApiserverClient("", kubeConfigFile)
|
|
|
|
if err != nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Fatalf("Unexpected error creating Kubernetes REST client: %v", err)
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
if cli == nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Fatal("Expected a REST client but none returned.")
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = createApiserverClient("", "")
|
|
|
|
if err == nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Fatal("Expected an error creating REST client without an API server URL or kubeconfig file.")
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleSigterm(t *testing.T) {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
kubeConfigFile := fmt.Sprintf("%v/.kube/config", home)
|
|
|
|
|
|
|
|
cli, err := createApiserverClient("", kubeConfigFile)
|
|
|
|
if err != nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Fatalf("Unexpected error creating Kubernetes REST client: %v", err)
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resetForTesting(func() { t.Fatal("bad parse") })
|
|
|
|
|
|
|
|
os.Setenv("POD_NAME", "test")
|
|
|
|
os.Setenv("POD_NAMESPACE", "test")
|
|
|
|
defer os.Setenv("POD_NAME", "")
|
|
|
|
defer os.Setenv("POD_NAMESPACE", "")
|
|
|
|
|
|
|
|
oldArgs := os.Args
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
os.Args = []string{"cmd", "--default-backend-service", "ingress-nginx/default-backend-http", "--http-port", "0", "--https-port", "0"}
|
|
|
|
|
|
|
|
_, conf, err := parseFlags()
|
|
|
|
if err != nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Errorf("Unexpected error creating NGINX controller: %v", err)
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
conf.Client = cli
|
|
|
|
|
|
|
|
fs, err := file.NewFakeFS()
|
|
|
|
if err != nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 17:46:18 +00:00
|
|
|
ngx := controller.NewNGINXController(conf, nil, fs)
|
2017-11-22 13:35:47 +00:00
|
|
|
|
|
|
|
go handleSigterm(ngx, func(code int) {
|
|
|
|
if code != 1 {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Errorf("Expected exit code 1 but %d received", code)
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Logf("Sending SIGTERM to PID %d", syscall.Getpid())
|
2017-11-22 13:35:47 +00:00
|
|
|
err = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
|
|
|
|
if err != nil {
|
2018-06-11 09:17:50 +00:00
|
|
|
t.Error("Unexpected error sending SIGTERM signal.")
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterHandlers(t *testing.T) {
|
2018-06-11 09:17:50 +00:00
|
|
|
// TODO
|
2017-11-22 13:35:47 +00:00
|
|
|
}
|