| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package tests |
| 18 |
|
| 19 |
import ( |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"os" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/prometheus/client_golang/prometheus" |
| 27 |
"k8s.io/klog/v2" |
| 28 |
|
| 29 |
metricsclient "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/metrics" |
| 30 |
"sigs.k8s.io/apiserver-network-proxy/tests/framework" |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
agentPath = flag.String("agent-path", "", "Path to the agent binary to test against, or empty to run the in-process agent.") |
| 35 |
) |
| 36 |
|
| 37 |
var Framework = framework.Framework{ |
| 38 |
ProxyServerRunner: &framework.InProcessProxyServerRunner{}, |
| 39 |
} |
| 40 |
|
| 41 |
func TestMain(m *testing.M) { |
| 42 |
fs := flag.NewFlagSet("mock-flags", flag.PanicOnError) |
| 43 |
klog.InitFlags(fs) |
| 44 |
fs.Set("v", "1") // Set klog verbosity. |
| 45 |
metricsclient.Metrics.RegisterMetrics(prometheus.DefaultRegisterer) |
| 46 |
|
| 47 |
flag.Parse() |
| 48 |
if err := initFramework(); err != nil { |
| 49 |
log.Fatalf("Failed to initialize framework: %v", err) |
| 50 |
} |
| 51 |
|
| 52 |
if err := framework.InitCertsDir(); err != nil { |
| 53 |
log.Fatalf("Failed to write test certs: %v", err) |
| 54 |
} |
| 55 |
defer func() { |
| 56 |
os.RemoveAll(framework.CertsDir) |
| 57 |
}() |
| 58 |
|
| 59 |
m.Run() |
| 60 |
} |
| 61 |
|
| 62 |
func initFramework() error { |
| 63 |
if *agentPath == "" { |
| 64 |
log.Print("Running tests with in-process agent") |
| 65 |
Framework.AgentRunner = &framework.InProcessAgentRunner{} |
| 66 |
} else { |
| 67 |
info, err := os.Stat(*agentPath) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
if info.Mode()&0111 == 0 { // Path must be executable |
| 72 |
return fmt.Errorf("%s is not an executable file", *agentPath) |
| 73 |
} |
| 74 |
|
| 75 |
log.Printf("Running tests with external agent %s", *agentPath) |
| 76 |
Framework.AgentRunner = &framework.ExternalAgentRunner{ |
| 77 |
ExecutablePath: *agentPath, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return nil |
| 82 |
} |