framework.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package framework contains provider-independent helper code for
  14. // building and running E2E tests with Ginkgo. The actual Ginkgo test
  15. // suites gets assembled by combining this framework, the optional
  16. // provider support code and specific tests via a separate .go file
  17. // like Kubernetes' test/e2e.go.
  18. package framework
  19. import (
  20. "bufio"
  21. "bytes"
  22. "fmt"
  23. "math/rand"
  24. "os"
  25. "strings"
  26. "sync"
  27. "time"
  28. "k8s.io/api/core/v1"
  29. apierrors "k8s.io/apimachinery/pkg/api/errors"
  30. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  31. "k8s.io/apimachinery/pkg/labels"
  32. "k8s.io/apimachinery/pkg/runtime/schema"
  33. "k8s.io/apimachinery/pkg/util/intstr"
  34. "k8s.io/apimachinery/pkg/util/wait"
  35. "k8s.io/client-go/discovery"
  36. cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
  37. "k8s.io/client-go/dynamic"
  38. clientset "k8s.io/client-go/kubernetes"
  39. "k8s.io/client-go/kubernetes/scheme"
  40. "k8s.io/client-go/rest"
  41. "k8s.io/client-go/restmapper"
  42. scaleclient "k8s.io/client-go/scale"
  43. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  44. "k8s.io/kubernetes/test/e2e/framework/metrics"
  45. testutils "k8s.io/kubernetes/test/utils"
  46. "github.com/onsi/ginkgo"
  47. "github.com/onsi/gomega"
  48. )
  49. const (
  50. maxKubectlExecRetries = 5
  51. // DefaultNamespaceDeletionTimeout is timeout duration for waiting for a namespace deletion.
  52. // TODO(mikedanese): reset this to 5 minutes once #47135 is resolved.
  53. // ref https://github.com/kubernetes/kubernetes/issues/47135
  54. DefaultNamespaceDeletionTimeout = 10 * time.Minute
  55. )
  56. // Framework supports common operations used by e2e tests; it will keep a client & a namespace for you.
  57. // Eventual goal is to merge this with integration test framework.
  58. type Framework struct {
  59. BaseName string
  60. // Set together with creating the ClientSet and the namespace.
  61. // Guaranteed to be unique in the cluster even when running the same
  62. // test multiple times in parallel.
  63. UniqueName string
  64. ClientSet clientset.Interface
  65. KubemarkExternalClusterClientSet clientset.Interface
  66. DynamicClient dynamic.Interface
  67. ScalesGetter scaleclient.ScalesGetter
  68. SkipNamespaceCreation bool // Whether to skip creating a namespace
  69. Namespace *v1.Namespace // Every test has at least one namespace unless creation is skipped
  70. namespacesToDelete []*v1.Namespace // Some tests have more than one.
  71. NamespaceDeletionTimeout time.Duration
  72. SkipPrivilegedPSPBinding bool // Whether to skip creating a binding to the privileged PSP in the test namespace
  73. gatherer *ContainerResourceGatherer
  74. // Constraints that passed to a check which is executed after data is gathered to
  75. // see if 99% of results are within acceptable bounds. It has to be injected in the test,
  76. // as expectations vary greatly. Constraints are grouped by the container names.
  77. AddonResourceConstraints map[string]ResourceConstraint
  78. logsSizeWaitGroup sync.WaitGroup
  79. logsSizeCloseChannel chan bool
  80. logsSizeVerifier *LogsSizeVerifier
  81. // Flaky operation failures in an e2e test can be captured through this.
  82. flakeReport *FlakeReport
  83. // To make sure that this framework cleans up after itself, no matter what,
  84. // we install a Cleanup action before each test and clear it after. If we
  85. // should abort, the AfterSuite hook should run all Cleanup actions.
  86. cleanupHandle CleanupActionHandle
  87. // configuration for framework's client
  88. Options Options
  89. // Place where various additional data is stored during test run to be printed to ReportDir,
  90. // or stdout if ReportDir is not set once test ends.
  91. TestSummaries []TestDataSummary
  92. // Place to keep ClusterAutoscaler metrics from before test in order to compute delta.
  93. clusterAutoscalerMetricsBeforeTest metrics.Collection
  94. }
  95. // TestDataSummary is an interface for managing test data.
  96. type TestDataSummary interface {
  97. SummaryKind() string
  98. PrintHumanReadable() string
  99. PrintJSON() string
  100. }
  101. // Options is a struct for managing test framework options.
  102. type Options struct {
  103. ClientQPS float32
  104. ClientBurst int
  105. GroupVersion *schema.GroupVersion
  106. }
  107. // NewDefaultFramework makes a new framework and sets up a BeforeEach/AfterEach for
  108. // you (you can write additional before/after each functions).
  109. func NewDefaultFramework(baseName string) *Framework {
  110. options := Options{
  111. ClientQPS: 20,
  112. ClientBurst: 50,
  113. }
  114. return NewFramework(baseName, options, nil)
  115. }
  116. // NewFramework creates a test framework.
  117. func NewFramework(baseName string, options Options, client clientset.Interface) *Framework {
  118. f := &Framework{
  119. BaseName: baseName,
  120. AddonResourceConstraints: make(map[string]ResourceConstraint),
  121. Options: options,
  122. ClientSet: client,
  123. }
  124. ginkgo.BeforeEach(f.BeforeEach)
  125. ginkgo.AfterEach(f.AfterEach)
  126. return f
  127. }
  128. // BeforeEach gets a client and makes a namespace.
  129. func (f *Framework) BeforeEach() {
  130. // The fact that we need this feels like a bug in ginkgo.
  131. // https://github.com/onsi/ginkgo/issues/222
  132. f.cleanupHandle = AddCleanupAction(f.AfterEach)
  133. if f.ClientSet == nil {
  134. ginkgo.By("Creating a kubernetes client")
  135. config, err := LoadConfig()
  136. testDesc := ginkgo.CurrentGinkgoTestDescription()
  137. if len(testDesc.ComponentTexts) > 0 {
  138. componentTexts := strings.Join(testDesc.ComponentTexts, " ")
  139. config.UserAgent = fmt.Sprintf(
  140. "%v -- %v",
  141. rest.DefaultKubernetesUserAgent(),
  142. componentTexts)
  143. }
  144. ExpectNoError(err)
  145. config.QPS = f.Options.ClientQPS
  146. config.Burst = f.Options.ClientBurst
  147. if f.Options.GroupVersion != nil {
  148. config.GroupVersion = f.Options.GroupVersion
  149. }
  150. if TestContext.KubeAPIContentType != "" {
  151. config.ContentType = TestContext.KubeAPIContentType
  152. }
  153. f.ClientSet, err = clientset.NewForConfig(config)
  154. ExpectNoError(err)
  155. f.DynamicClient, err = dynamic.NewForConfig(config)
  156. ExpectNoError(err)
  157. // node.k8s.io is based on CRD, which is served only as JSON
  158. jsonConfig := config
  159. jsonConfig.ContentType = "application/json"
  160. ExpectNoError(err)
  161. // create scales getter, set GroupVersion and NegotiatedSerializer to default values
  162. // as they are required when creating a REST client.
  163. if config.GroupVersion == nil {
  164. config.GroupVersion = &schema.GroupVersion{}
  165. }
  166. if config.NegotiatedSerializer == nil {
  167. config.NegotiatedSerializer = scheme.Codecs
  168. }
  169. restClient, err := rest.RESTClientFor(config)
  170. ExpectNoError(err)
  171. discoClient, err := discovery.NewDiscoveryClientForConfig(config)
  172. ExpectNoError(err)
  173. cachedDiscoClient := cacheddiscovery.NewMemCacheClient(discoClient)
  174. restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoClient)
  175. restMapper.Reset()
  176. resolver := scaleclient.NewDiscoveryScaleKindResolver(cachedDiscoClient)
  177. f.ScalesGetter = scaleclient.New(restClient, restMapper, dynamic.LegacyAPIPathResolverFunc, resolver)
  178. TestContext.CloudConfig.Provider.FrameworkBeforeEach(f)
  179. }
  180. if !f.SkipNamespaceCreation {
  181. ginkgo.By(fmt.Sprintf("Building a namespace api object, basename %s", f.BaseName))
  182. namespace, err := f.CreateNamespace(f.BaseName, map[string]string{
  183. "e2e-framework": f.BaseName,
  184. })
  185. ExpectNoError(err)
  186. f.Namespace = namespace
  187. if TestContext.VerifyServiceAccount {
  188. ginkgo.By("Waiting for a default service account to be provisioned in namespace")
  189. err = WaitForDefaultServiceAccountInNamespace(f.ClientSet, namespace.Name)
  190. ExpectNoError(err)
  191. } else {
  192. e2elog.Logf("Skipping waiting for service account")
  193. }
  194. f.UniqueName = f.Namespace.GetName()
  195. } else {
  196. // not guaranteed to be unique, but very likely
  197. f.UniqueName = fmt.Sprintf("%s-%08x", f.BaseName, rand.Int31())
  198. }
  199. if TestContext.GatherKubeSystemResourceUsageData != "false" && TestContext.GatherKubeSystemResourceUsageData != "none" {
  200. var err error
  201. var nodeMode NodesSet
  202. switch TestContext.GatherKubeSystemResourceUsageData {
  203. case "master":
  204. nodeMode = MasterNodes
  205. case "masteranddns":
  206. nodeMode = MasterAndDNSNodes
  207. default:
  208. nodeMode = AllNodes
  209. }
  210. f.gatherer, err = NewResourceUsageGatherer(f.ClientSet, ResourceGathererOptions{
  211. InKubemark: ProviderIs("kubemark"),
  212. Nodes: nodeMode,
  213. ResourceDataGatheringPeriod: 60 * time.Second,
  214. ProbeDuration: 15 * time.Second,
  215. PrintVerboseLogs: false,
  216. }, nil)
  217. if err != nil {
  218. e2elog.Logf("Error while creating NewResourceUsageGatherer: %v", err)
  219. } else {
  220. go f.gatherer.StartGatheringData()
  221. }
  222. }
  223. if TestContext.GatherLogsSizes {
  224. f.logsSizeWaitGroup = sync.WaitGroup{}
  225. f.logsSizeWaitGroup.Add(1)
  226. f.logsSizeCloseChannel = make(chan bool)
  227. f.logsSizeVerifier = NewLogsVerifier(f.ClientSet, f.logsSizeCloseChannel)
  228. go func() {
  229. f.logsSizeVerifier.Run()
  230. f.logsSizeWaitGroup.Done()
  231. }()
  232. }
  233. gatherMetricsAfterTest := TestContext.GatherMetricsAfterTest == "true" || TestContext.GatherMetricsAfterTest == "master"
  234. if gatherMetricsAfterTest && TestContext.IncludeClusterAutoscalerMetrics {
  235. grabber, err := metrics.NewMetricsGrabber(f.ClientSet, f.KubemarkExternalClusterClientSet, !ProviderIs("kubemark"), false, false, false, TestContext.IncludeClusterAutoscalerMetrics)
  236. if err != nil {
  237. e2elog.Logf("Failed to create MetricsGrabber (skipping ClusterAutoscaler metrics gathering before test): %v", err)
  238. } else {
  239. f.clusterAutoscalerMetricsBeforeTest, err = grabber.Grab()
  240. if err != nil {
  241. e2elog.Logf("MetricsGrabber failed to grab CA metrics before test (skipping metrics gathering): %v", err)
  242. } else {
  243. e2elog.Logf("Gathered ClusterAutoscaler metrics before test")
  244. }
  245. }
  246. }
  247. f.flakeReport = NewFlakeReport()
  248. }
  249. // AfterEach deletes the namespace, after reading its events.
  250. func (f *Framework) AfterEach() {
  251. RemoveCleanupAction(f.cleanupHandle)
  252. // DeleteNamespace at the very end in defer, to avoid any
  253. // expectation failures preventing deleting the namespace.
  254. defer func() {
  255. nsDeletionErrors := map[string]error{}
  256. // Whether to delete namespace is determined by 3 factors: delete-namespace flag, delete-namespace-on-failure flag and the test result
  257. // if delete-namespace set to false, namespace will always be preserved.
  258. // if delete-namespace is true and delete-namespace-on-failure is false, namespace will be preserved if test failed.
  259. if TestContext.DeleteNamespace && (TestContext.DeleteNamespaceOnFailure || !ginkgo.CurrentGinkgoTestDescription().Failed) {
  260. for _, ns := range f.namespacesToDelete {
  261. ginkgo.By(fmt.Sprintf("Destroying namespace %q for this suite.", ns.Name))
  262. timeout := DefaultNamespaceDeletionTimeout
  263. if f.NamespaceDeletionTimeout != 0 {
  264. timeout = f.NamespaceDeletionTimeout
  265. }
  266. if err := deleteNS(f.ClientSet, f.DynamicClient, ns.Name, timeout); err != nil {
  267. if !apierrors.IsNotFound(err) {
  268. nsDeletionErrors[ns.Name] = err
  269. } else {
  270. e2elog.Logf("Namespace %v was already deleted", ns.Name)
  271. }
  272. }
  273. }
  274. } else {
  275. if !TestContext.DeleteNamespace {
  276. e2elog.Logf("Found DeleteNamespace=false, skipping namespace deletion!")
  277. } else {
  278. e2elog.Logf("Found DeleteNamespaceOnFailure=false and current test failed, skipping namespace deletion!")
  279. }
  280. }
  281. // Paranoia-- prevent reuse!
  282. f.Namespace = nil
  283. f.ClientSet = nil
  284. f.namespacesToDelete = nil
  285. // if we had errors deleting, report them now.
  286. if len(nsDeletionErrors) != 0 {
  287. messages := []string{}
  288. for namespaceKey, namespaceErr := range nsDeletionErrors {
  289. messages = append(messages, fmt.Sprintf("Couldn't delete ns: %q: %s (%#v)", namespaceKey, namespaceErr, namespaceErr))
  290. }
  291. Failf(strings.Join(messages, ","))
  292. }
  293. }()
  294. // Print events if the test failed.
  295. if ginkgo.CurrentGinkgoTestDescription().Failed && TestContext.DumpLogsOnFailure {
  296. // Pass both unversioned client and versioned clientset, till we have removed all uses of the unversioned client.
  297. if !f.SkipNamespaceCreation {
  298. DumpAllNamespaceInfo(f.ClientSet, f.Namespace.Name)
  299. }
  300. }
  301. if TestContext.GatherKubeSystemResourceUsageData != "false" && TestContext.GatherKubeSystemResourceUsageData != "none" && f.gatherer != nil {
  302. ginkgo.By("Collecting resource usage data")
  303. summary, resourceViolationError := f.gatherer.StopAndSummarize([]int{90, 99, 100}, f.AddonResourceConstraints)
  304. defer ExpectNoError(resourceViolationError)
  305. f.TestSummaries = append(f.TestSummaries, summary)
  306. }
  307. if TestContext.GatherLogsSizes {
  308. ginkgo.By("Gathering log sizes data")
  309. close(f.logsSizeCloseChannel)
  310. f.logsSizeWaitGroup.Wait()
  311. f.TestSummaries = append(f.TestSummaries, f.logsSizeVerifier.GetSummary())
  312. }
  313. if TestContext.GatherMetricsAfterTest != "false" {
  314. ginkgo.By("Gathering metrics")
  315. // Grab apiserver, scheduler, controller-manager metrics and (optionally) nodes' kubelet metrics.
  316. grabMetricsFromKubelets := TestContext.GatherMetricsAfterTest != "master" && !ProviderIs("kubemark")
  317. grabber, err := metrics.NewMetricsGrabber(f.ClientSet, f.KubemarkExternalClusterClientSet, grabMetricsFromKubelets, true, true, true, TestContext.IncludeClusterAutoscalerMetrics)
  318. if err != nil {
  319. e2elog.Logf("Failed to create MetricsGrabber (skipping metrics gathering): %v", err)
  320. } else {
  321. received, err := grabber.Grab()
  322. if err != nil {
  323. e2elog.Logf("MetricsGrabber failed to grab some of the metrics: %v", err)
  324. }
  325. (*MetricsForE2E)(&received).computeClusterAutoscalerMetricsDelta(f.clusterAutoscalerMetricsBeforeTest)
  326. f.TestSummaries = append(f.TestSummaries, (*MetricsForE2E)(&received))
  327. }
  328. }
  329. TestContext.CloudConfig.Provider.FrameworkAfterEach(f)
  330. // Report any flakes that were observed in the e2e test and reset.
  331. if f.flakeReport != nil && f.flakeReport.GetFlakeCount() > 0 {
  332. f.TestSummaries = append(f.TestSummaries, f.flakeReport)
  333. f.flakeReport = nil
  334. }
  335. PrintSummaries(f.TestSummaries, f.BaseName)
  336. // Check whether all nodes are ready after the test.
  337. // This is explicitly done at the very end of the test, to avoid
  338. // e.g. not removing namespace in case of this failure.
  339. if err := AllNodesReady(f.ClientSet, 3*time.Minute); err != nil {
  340. Failf("All nodes should be ready after test, %v", err)
  341. }
  342. }
  343. // CreateNamespace creates a namespace for e2e testing.
  344. func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*v1.Namespace, error) {
  345. createTestingNS := TestContext.CreateTestingNS
  346. if createTestingNS == nil {
  347. createTestingNS = CreateTestingNS
  348. }
  349. ns, err := createTestingNS(baseName, f.ClientSet, labels)
  350. // check ns instead of err to see if it's nil as we may
  351. // fail to create serviceAccount in it.
  352. f.AddNamespacesToDelete(ns)
  353. if err == nil && !f.SkipPrivilegedPSPBinding {
  354. createPrivilegedPSPBinding(f, ns.Name)
  355. }
  356. return ns, err
  357. }
  358. // RecordFlakeIfError records flakeness info if error happens.
  359. // NOTE: This function is not used at any places yet, but we are in progress for https://github.com/kubernetes/kubernetes/issues/66239 which requires this. Please don't remove this.
  360. func (f *Framework) RecordFlakeIfError(err error, optionalDescription ...interface{}) {
  361. f.flakeReport.RecordFlakeIfError(err, optionalDescription)
  362. }
  363. // AddNamespacesToDelete adds one or more namespaces to be deleted when the test
  364. // completes.
  365. func (f *Framework) AddNamespacesToDelete(namespaces ...*v1.Namespace) {
  366. for _, ns := range namespaces {
  367. if ns == nil {
  368. continue
  369. }
  370. f.namespacesToDelete = append(f.namespacesToDelete, ns)
  371. }
  372. }
  373. // WaitForPodTerminated waits for the pod to be terminated with the given reason.
  374. func (f *Framework) WaitForPodTerminated(podName, reason string) error {
  375. return waitForPodTerminatedInNamespace(f.ClientSet, podName, reason, f.Namespace.Name)
  376. }
  377. // WaitForPodNotFound waits for the pod to be completely terminated (not "Get-able").
  378. func (f *Framework) WaitForPodNotFound(podName string, timeout time.Duration) error {
  379. return waitForPodNotFoundInNamespace(f.ClientSet, podName, f.Namespace.Name, timeout)
  380. }
  381. // WaitForPodRunning waits for the pod to run in the namespace.
  382. func (f *Framework) WaitForPodRunning(podName string) error {
  383. return WaitForPodNameRunningInNamespace(f.ClientSet, podName, f.Namespace.Name)
  384. }
  385. // WaitForPodReady waits for the pod to flip to ready in the namespace.
  386. func (f *Framework) WaitForPodReady(podName string) error {
  387. return waitTimeoutForPodReadyInNamespace(f.ClientSet, podName, f.Namespace.Name, PodStartTimeout)
  388. }
  389. // WaitForPodRunningSlow waits for the pod to run in the namespace.
  390. // It has a longer timeout then WaitForPodRunning (util.slowPodStartTimeout).
  391. func (f *Framework) WaitForPodRunningSlow(podName string) error {
  392. return waitForPodRunningInNamespaceSlow(f.ClientSet, podName, f.Namespace.Name)
  393. }
  394. // WaitForPodNoLongerRunning waits for the pod to no longer be running in the namespace, for either
  395. // success or failure.
  396. func (f *Framework) WaitForPodNoLongerRunning(podName string) error {
  397. return WaitForPodNoLongerRunningInNamespace(f.ClientSet, podName, f.Namespace.Name)
  398. }
  399. // TestContainerOutput runs the given pod in the given namespace and waits
  400. // for all of the containers in the podSpec to move into the 'Success' status, and tests
  401. // the specified container log against the given expected output using a substring matcher.
  402. func (f *Framework) TestContainerOutput(scenarioName string, pod *v1.Pod, containerIndex int, expectedOutput []string) {
  403. f.testContainerOutputMatcher(scenarioName, pod, containerIndex, expectedOutput, gomega.ContainSubstring)
  404. }
  405. // TestContainerOutputRegexp runs the given pod in the given namespace and waits
  406. // for all of the containers in the podSpec to move into the 'Success' status, and tests
  407. // the specified container log against the given expected output using a regexp matcher.
  408. func (f *Framework) TestContainerOutputRegexp(scenarioName string, pod *v1.Pod, containerIndex int, expectedOutput []string) {
  409. f.testContainerOutputMatcher(scenarioName, pod, containerIndex, expectedOutput, gomega.MatchRegexp)
  410. }
  411. // WriteFileViaContainer writes a file using kubectl exec echo <contents> > <path> via specified container
  412. // because of the primitive technique we're using here, we only allow ASCII alphanumeric characters
  413. func (f *Framework) WriteFileViaContainer(podName, containerName string, path string, contents string) error {
  414. ginkgo.By("writing a file in the container")
  415. allowedCharacters := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  416. for _, c := range contents {
  417. if !strings.ContainsRune(allowedCharacters, c) {
  418. return fmt.Errorf("Unsupported character in string to write: %v", c)
  419. }
  420. }
  421. command := fmt.Sprintf("echo '%s' > '%s'", contents, path)
  422. stdout, stderr, err := kubectlExecWithRetry(f.Namespace.Name, podName, containerName, "--", "/bin/sh", "-c", command)
  423. if err != nil {
  424. e2elog.Logf("error running kubectl exec to write file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
  425. }
  426. return err
  427. }
  428. // ReadFileViaContainer reads a file using kubectl exec cat <path>.
  429. func (f *Framework) ReadFileViaContainer(podName, containerName string, path string) (string, error) {
  430. ginkgo.By("reading a file in the container")
  431. stdout, stderr, err := kubectlExecWithRetry(f.Namespace.Name, podName, containerName, "--", "cat", path)
  432. if err != nil {
  433. e2elog.Logf("error running kubectl exec to read file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
  434. }
  435. return string(stdout), err
  436. }
  437. // CheckFileSizeViaContainer returns the list of file size under the specified path.
  438. func (f *Framework) CheckFileSizeViaContainer(podName, containerName, path string) (string, error) {
  439. ginkgo.By("checking a file size in the container")
  440. stdout, stderr, err := kubectlExecWithRetry(f.Namespace.Name, podName, containerName, "--", "ls", "-l", path)
  441. if err != nil {
  442. e2elog.Logf("error running kubectl exec to read file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
  443. }
  444. return string(stdout), err
  445. }
  446. // CreateServiceForSimpleAppWithPods is a convenience wrapper to create a service and its matching pods all at once.
  447. func (f *Framework) CreateServiceForSimpleAppWithPods(contPort int, svcPort int, appName string, podSpec func(n v1.Node) v1.PodSpec, count int, block bool) (*v1.Service, error) {
  448. var err error
  449. theService := f.CreateServiceForSimpleApp(contPort, svcPort, appName)
  450. f.CreatePodsPerNodeForSimpleApp(appName, podSpec, count)
  451. if block {
  452. err = testutils.WaitForPodsWithLabelRunning(f.ClientSet, f.Namespace.Name, labels.SelectorFromSet(labels.Set(theService.Spec.Selector)))
  453. }
  454. return theService, err
  455. }
  456. // CreateServiceForSimpleApp returns a service that selects/exposes pods (send -1 ports if no exposure needed) with an app label.
  457. func (f *Framework) CreateServiceForSimpleApp(contPort, svcPort int, appName string) *v1.Service {
  458. if appName == "" {
  459. panic(fmt.Sprintf("no app name provided"))
  460. }
  461. serviceSelector := map[string]string{
  462. "app": appName + "-pod",
  463. }
  464. // For convenience, user sending ports are optional.
  465. portsFunc := func() []v1.ServicePort {
  466. if contPort < 1 || svcPort < 1 {
  467. return nil
  468. }
  469. return []v1.ServicePort{{
  470. Protocol: v1.ProtocolTCP,
  471. Port: int32(svcPort),
  472. TargetPort: intstr.FromInt(contPort),
  473. }}
  474. }
  475. e2elog.Logf("Creating a service-for-%v for selecting app=%v-pod", appName, appName)
  476. service, err := f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(&v1.Service{
  477. ObjectMeta: metav1.ObjectMeta{
  478. Name: "service-for-" + appName,
  479. Labels: map[string]string{
  480. "app": appName + "-service",
  481. },
  482. },
  483. Spec: v1.ServiceSpec{
  484. Ports: portsFunc(),
  485. Selector: serviceSelector,
  486. },
  487. })
  488. ExpectNoError(err)
  489. return service
  490. }
  491. // CreatePodsPerNodeForSimpleApp creates pods w/ labels. Useful for tests which make a bunch of pods w/o any networking.
  492. func (f *Framework) CreatePodsPerNodeForSimpleApp(appName string, podSpec func(n v1.Node) v1.PodSpec, maxCount int) map[string]string {
  493. nodes := GetReadySchedulableNodesOrDie(f.ClientSet)
  494. labels := map[string]string{
  495. "app": appName + "-pod",
  496. }
  497. for i, node := range nodes.Items {
  498. // one per node, but no more than maxCount.
  499. if i <= maxCount {
  500. e2elog.Logf("%v/%v : Creating container with label app=%v-pod", i, maxCount, appName)
  501. _, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(&v1.Pod{
  502. ObjectMeta: metav1.ObjectMeta{
  503. Name: fmt.Sprintf(appName+"-pod-%v", i),
  504. Labels: labels,
  505. },
  506. Spec: podSpec(node),
  507. })
  508. ExpectNoError(err)
  509. }
  510. }
  511. return labels
  512. }
  513. // KubeUser is a struct for managing kubernetes user info.
  514. type KubeUser struct {
  515. Name string `yaml:"name"`
  516. User struct {
  517. Username string `yaml:"username"`
  518. Password string `yaml:"password"`
  519. Token string `yaml:"token"`
  520. } `yaml:"user"`
  521. }
  522. // KubeCluster is a struct for managing kubernetes cluster info.
  523. type KubeCluster struct {
  524. Name string `yaml:"name"`
  525. Cluster struct {
  526. CertificateAuthorityData string `yaml:"certificate-authority-data"`
  527. Server string `yaml:"server"`
  528. } `yaml:"cluster"`
  529. }
  530. // KubeConfig is a struct for managing kubernetes config.
  531. type KubeConfig struct {
  532. Contexts []struct {
  533. Name string `yaml:"name"`
  534. Context struct {
  535. Cluster string `yaml:"cluster"`
  536. User string
  537. } `yaml:"context"`
  538. } `yaml:"contexts"`
  539. Clusters []KubeCluster `yaml:"clusters"`
  540. Users []KubeUser `yaml:"users"`
  541. }
  542. // FindUser returns user info which is the specified user name.
  543. func (kc *KubeConfig) FindUser(name string) *KubeUser {
  544. for _, user := range kc.Users {
  545. if user.Name == name {
  546. return &user
  547. }
  548. }
  549. return nil
  550. }
  551. // FindCluster returns cluster info which is the specified cluster name.
  552. func (kc *KubeConfig) FindCluster(name string) *KubeCluster {
  553. for _, cluster := range kc.Clusters {
  554. if cluster.Name == name {
  555. return &cluster
  556. }
  557. }
  558. return nil
  559. }
  560. func kubectlExecWithRetry(namespace string, podName, containerName string, args ...string) ([]byte, []byte, error) {
  561. for numRetries := 0; numRetries < maxKubectlExecRetries; numRetries++ {
  562. if numRetries > 0 {
  563. e2elog.Logf("Retrying kubectl exec (retry count=%v/%v)", numRetries+1, maxKubectlExecRetries)
  564. }
  565. stdOutBytes, stdErrBytes, err := kubectlExec(namespace, podName, containerName, args...)
  566. if err != nil {
  567. if strings.Contains(strings.ToLower(string(stdErrBytes)), "i/o timeout") {
  568. // Retry on "i/o timeout" errors
  569. e2elog.Logf("Warning: kubectl exec encountered i/o timeout.\nerr=%v\nstdout=%v\nstderr=%v)", err, string(stdOutBytes), string(stdErrBytes))
  570. continue
  571. }
  572. if strings.Contains(strings.ToLower(string(stdErrBytes)), "container not found") {
  573. // Retry on "container not found" errors
  574. e2elog.Logf("Warning: kubectl exec encountered container not found.\nerr=%v\nstdout=%v\nstderr=%v)", err, string(stdOutBytes), string(stdErrBytes))
  575. time.Sleep(2 * time.Second)
  576. continue
  577. }
  578. }
  579. return stdOutBytes, stdErrBytes, err
  580. }
  581. err := fmt.Errorf("Failed: kubectl exec failed %d times with \"i/o timeout\". Giving up", maxKubectlExecRetries)
  582. return nil, nil, err
  583. }
  584. func kubectlExec(namespace string, podName, containerName string, args ...string) ([]byte, []byte, error) {
  585. var stdout, stderr bytes.Buffer
  586. cmdArgs := []string{
  587. "exec",
  588. fmt.Sprintf("--namespace=%v", namespace),
  589. podName,
  590. fmt.Sprintf("-c=%v", containerName),
  591. }
  592. cmdArgs = append(cmdArgs, args...)
  593. cmd := KubectlCmd(cmdArgs...)
  594. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  595. e2elog.Logf("Running '%s %s'", cmd.Path, strings.Join(cmdArgs, " "))
  596. err := cmd.Run()
  597. return stdout.Bytes(), stderr.Bytes(), err
  598. }
  599. // KubeDescribe is wrapper function for ginkgo describe. Adds namespacing.
  600. // TODO: Support type safe tagging as well https://github.com/kubernetes/kubernetes/pull/22401.
  601. func KubeDescribe(text string, body func()) bool {
  602. return ginkgo.Describe("[k8s.io] "+text, body)
  603. }
  604. // ConformanceIt is wrapper function for ginkgo It. Adds "[Conformance]" tag and makes static analysis easier.
  605. func ConformanceIt(text string, body interface{}, timeout ...float64) bool {
  606. return ginkgo.It(text+" [Conformance]", body, timeout...)
  607. }
  608. // PodStateVerification represents a verification of pod state.
  609. // Any time you have a set of pods that you want to operate against or query,
  610. // this struct can be used to declaratively identify those pods.
  611. type PodStateVerification struct {
  612. // Optional: only pods that have k=v labels will pass this filter.
  613. Selectors map[string]string
  614. // Required: The phases which are valid for your pod.
  615. ValidPhases []v1.PodPhase
  616. // Optional: only pods passing this function will pass the filter
  617. // Verify a pod.
  618. // As an optimization, in addition to specifying filter (boolean),
  619. // this function allows specifying an error as well.
  620. // The error indicates that the polling of the pod spectrum should stop.
  621. Verify func(v1.Pod) (bool, error)
  622. // Optional: only pods with this name will pass the filter.
  623. PodName string
  624. }
  625. // ClusterVerification is a struct for a verification of cluster state.
  626. type ClusterVerification struct {
  627. client clientset.Interface
  628. namespace *v1.Namespace // pointer rather than string, since ns isn't created until before each.
  629. podState PodStateVerification
  630. }
  631. // NewClusterVerification creates a new cluster verification.
  632. func (f *Framework) NewClusterVerification(namespace *v1.Namespace, filter PodStateVerification) *ClusterVerification {
  633. return &ClusterVerification{
  634. f.ClientSet,
  635. namespace,
  636. filter,
  637. }
  638. }
  639. func passesPodNameFilter(pod v1.Pod, name string) bool {
  640. return name == "" || strings.Contains(pod.Name, name)
  641. }
  642. func passesVerifyFilter(pod v1.Pod, verify func(p v1.Pod) (bool, error)) (bool, error) {
  643. if verify == nil {
  644. return true, nil
  645. }
  646. verified, err := verify(pod)
  647. // If an error is returned, by definition, pod verification fails
  648. if err != nil {
  649. return false, err
  650. }
  651. return verified, nil
  652. }
  653. func passesPhasesFilter(pod v1.Pod, validPhases []v1.PodPhase) bool {
  654. passesPhaseFilter := false
  655. for _, phase := range validPhases {
  656. if pod.Status.Phase == phase {
  657. passesPhaseFilter = true
  658. }
  659. }
  660. return passesPhaseFilter
  661. }
  662. // filterLabels returns a list of pods which have labels.
  663. func filterLabels(selectors map[string]string, cli clientset.Interface, ns string) (*v1.PodList, error) {
  664. var err error
  665. var selector labels.Selector
  666. var pl *v1.PodList
  667. // List pods based on selectors. This might be a tiny optimization rather then filtering
  668. // everything manually.
  669. if len(selectors) > 0 {
  670. selector = labels.SelectorFromSet(labels.Set(selectors))
  671. options := metav1.ListOptions{LabelSelector: selector.String()}
  672. pl, err = cli.CoreV1().Pods(ns).List(options)
  673. } else {
  674. pl, err = cli.CoreV1().Pods(ns).List(metav1.ListOptions{})
  675. }
  676. return pl, err
  677. }
  678. // filter filters pods which pass a filter. It can be used to compose
  679. // the more useful abstractions like ForEach, WaitFor, and so on, which
  680. // can be used directly by tests.
  681. func (p *PodStateVerification) filter(c clientset.Interface, namespace *v1.Namespace) ([]v1.Pod, error) {
  682. if len(p.ValidPhases) == 0 || namespace == nil {
  683. panic(fmt.Errorf("Need to specify a valid pod phases (%v) and namespace (%v). ", p.ValidPhases, namespace))
  684. }
  685. ns := namespace.Name
  686. pl, err := filterLabels(p.Selectors, c, ns) // Build an v1.PodList to operate against.
  687. e2elog.Logf("Selector matched %v pods for %v", len(pl.Items), p.Selectors)
  688. if len(pl.Items) == 0 || err != nil {
  689. return pl.Items, err
  690. }
  691. unfilteredPods := pl.Items
  692. filteredPods := []v1.Pod{}
  693. ReturnPodsSoFar:
  694. // Next: Pod must match at least one of the states that the user specified
  695. for _, pod := range unfilteredPods {
  696. if !(passesPhasesFilter(pod, p.ValidPhases) && passesPodNameFilter(pod, p.PodName)) {
  697. continue
  698. }
  699. passesVerify, err := passesVerifyFilter(pod, p.Verify)
  700. if err != nil {
  701. e2elog.Logf("Error detected on %v : %v !", pod.Name, err)
  702. break ReturnPodsSoFar
  703. }
  704. if passesVerify {
  705. filteredPods = append(filteredPods, pod)
  706. }
  707. }
  708. return filteredPods, err
  709. }
  710. // WaitFor waits for some minimum number of pods to be verified, according to the PodStateVerification
  711. // definition.
  712. func (cl *ClusterVerification) WaitFor(atLeast int, timeout time.Duration) ([]v1.Pod, error) {
  713. pods := []v1.Pod{}
  714. var returnedErr error
  715. err := wait.Poll(1*time.Second, timeout, func() (bool, error) {
  716. pods, returnedErr = cl.podState.filter(cl.client, cl.namespace)
  717. // Failure
  718. if returnedErr != nil {
  719. e2elog.Logf("Cutting polling short: We got an error from the pod filtering layer.")
  720. // stop polling if the pod filtering returns an error. that should never happen.
  721. // it indicates, for example, that the client is broken or something non-pod related.
  722. return false, returnedErr
  723. }
  724. e2elog.Logf("Found %v / %v", len(pods), atLeast)
  725. // Success
  726. if len(pods) >= atLeast {
  727. return true, nil
  728. }
  729. // Keep trying...
  730. return false, nil
  731. })
  732. e2elog.Logf("WaitFor completed with timeout %v. Pods found = %v out of %v", timeout, len(pods), atLeast)
  733. return pods, err
  734. }
  735. // WaitForOrFail provides a shorthand WaitFor with failure as an option if anything goes wrong.
  736. func (cl *ClusterVerification) WaitForOrFail(atLeast int, timeout time.Duration) {
  737. pods, err := cl.WaitFor(atLeast, timeout)
  738. if err != nil || len(pods) < atLeast {
  739. Failf("Verified %v of %v pods , error : %v", len(pods), atLeast, err)
  740. }
  741. }
  742. // ForEach runs a function against every verifiable pod. Be warned that this doesn't wait for "n" pods to verify,
  743. // so it may return very quickly if you have strict pod state requirements.
  744. //
  745. // For example, if you require at least 5 pods to be running before your test will pass,
  746. // its smart to first call "clusterVerification.WaitFor(5)" before you call clusterVerification.ForEach.
  747. func (cl *ClusterVerification) ForEach(podFunc func(v1.Pod)) error {
  748. pods, err := cl.podState.filter(cl.client, cl.namespace)
  749. if err == nil {
  750. if len(pods) == 0 {
  751. Failf("No pods matched the filter.")
  752. }
  753. e2elog.Logf("ForEach: Found %v pods from the filter. Now looping through them.", len(pods))
  754. for _, p := range pods {
  755. podFunc(p)
  756. }
  757. } else {
  758. e2elog.Logf("ForEach: Something went wrong when filtering pods to execute against: %v", err)
  759. }
  760. return err
  761. }
  762. // GetLogToFileFunc is a convenience function that returns a function that have the same interface as
  763. // e2elog.Logf, but writes to a specified file.
  764. func GetLogToFileFunc(file *os.File) func(format string, args ...interface{}) {
  765. return func(format string, args ...interface{}) {
  766. writer := bufio.NewWriter(file)
  767. if _, err := fmt.Fprintf(writer, format, args...); err != nil {
  768. e2elog.Logf("Failed to write file %v with test performance data: %v", file.Name(), err)
  769. }
  770. writer.Flush()
  771. }
  772. }