framework.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "reflect"
  20. "strings"
  21. "sync"
  22. "time"
  23. release_1_4 "k8s.io/client-go/1.4/kubernetes"
  24. "k8s.io/client-go/1.4/pkg/util/sets"
  25. clientreporestclient "k8s.io/client-go/1.4/rest"
  26. "k8s.io/kubernetes/federation/client/clientset_generated/federation_release_1_4"
  27. "k8s.io/kubernetes/pkg/api"
  28. apierrs "k8s.io/kubernetes/pkg/api/errors"
  29. "k8s.io/kubernetes/pkg/api/unversioned"
  30. "k8s.io/kubernetes/pkg/api/v1"
  31. "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_2"
  32. "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_3"
  33. "k8s.io/kubernetes/pkg/client/restclient"
  34. "k8s.io/kubernetes/pkg/client/typed/dynamic"
  35. client "k8s.io/kubernetes/pkg/client/unversioned"
  36. "k8s.io/kubernetes/pkg/fields"
  37. "k8s.io/kubernetes/pkg/labels"
  38. "k8s.io/kubernetes/pkg/metrics"
  39. "k8s.io/kubernetes/pkg/util/intstr"
  40. "k8s.io/kubernetes/pkg/util/wait"
  41. . "github.com/onsi/ginkgo"
  42. . "github.com/onsi/gomega"
  43. yaml "gopkg.in/yaml.v2"
  44. )
  45. const (
  46. maxKubectlExecRetries = 5
  47. )
  48. // Framework supports common operations used by e2e tests; it will keep a client & a namespace for you.
  49. // Eventual goal is to merge this with integration test framework.
  50. type Framework struct {
  51. BaseName string
  52. Client *client.Client
  53. Clientset_1_2 *release_1_2.Clientset
  54. Clientset_1_3 *release_1_3.Clientset
  55. StagingClient *release_1_4.Clientset
  56. ClientPool dynamic.ClientPool
  57. Namespace *api.Namespace // Every test has at least one namespace
  58. namespacesToDelete []*api.Namespace // Some tests have more than one.
  59. NamespaceDeletionTimeout time.Duration
  60. gatherer *containerResourceGatherer
  61. // Constraints that passed to a check which is executed after data is gathered to
  62. // see if 99% of results are within acceptable bounds. It as to be injected in the test,
  63. // as expectations vary greatly. Constraints are groupped by the container names.
  64. AddonResourceConstraints map[string]ResourceConstraint
  65. logsSizeWaitGroup sync.WaitGroup
  66. logsSizeCloseChannel chan bool
  67. logsSizeVerifier *LogsSizeVerifier
  68. // To make sure that this framework cleans up after itself, no matter what,
  69. // we install a Cleanup action before each test and clear it after. If we
  70. // should abort, the AfterSuite hook should run all Cleanup actions.
  71. cleanupHandle CleanupActionHandle
  72. // configuration for framework's client
  73. options FrameworkOptions
  74. // will this framework exercise a federated cluster as well
  75. federated bool
  76. // Federation specific params. These are set only if federated = true.
  77. FederationClientset_1_4 *federation_release_1_4.Clientset
  78. FederationNamespace *v1.Namespace
  79. }
  80. type TestDataSummary interface {
  81. PrintHumanReadable() string
  82. PrintJSON() string
  83. }
  84. type FrameworkOptions struct {
  85. ClientQPS float32
  86. ClientBurst int
  87. GroupVersion *unversioned.GroupVersion
  88. }
  89. // NewFramework makes a new framework and sets up a BeforeEach/AfterEach for
  90. // you (you can write additional before/after each functions).
  91. func NewDefaultFramework(baseName string) *Framework {
  92. options := FrameworkOptions{
  93. ClientQPS: 20,
  94. ClientBurst: 50,
  95. }
  96. return NewFramework(baseName, options, nil)
  97. }
  98. func NewDefaultFederatedFramework(baseName string) *Framework {
  99. f := NewDefaultFramework(baseName)
  100. f.federated = true
  101. return f
  102. }
  103. func NewFramework(baseName string, options FrameworkOptions, client *client.Client) *Framework {
  104. f := &Framework{
  105. BaseName: baseName,
  106. AddonResourceConstraints: make(map[string]ResourceConstraint),
  107. options: options,
  108. Client: client,
  109. }
  110. BeforeEach(f.BeforeEach)
  111. AfterEach(f.AfterEach)
  112. return f
  113. }
  114. // getClientRepoConfig copies k8s.io/kubernetes/pkg/client/restclient.Config to
  115. // a k8s.io/client-go/pkg/client/restclient.Config. It's not a deep copy. Two
  116. // configs may share some common struct.
  117. func getClientRepoConfig(src *restclient.Config) (dst *clientreporestclient.Config) {
  118. skippedFields := sets.NewString("Transport", "WrapTransport", "RateLimiter", "AuthConfigPersister")
  119. dst = &clientreporestclient.Config{}
  120. dst.Transport = src.Transport
  121. dst.WrapTransport = src.WrapTransport
  122. dst.RateLimiter = src.RateLimiter
  123. dst.AuthConfigPersister = src.AuthConfigPersister
  124. sv := reflect.ValueOf(src).Elem()
  125. dv := reflect.ValueOf(dst).Elem()
  126. for i := 0; i < sv.NumField(); i++ {
  127. if skippedFields.Has(sv.Type().Field(i).Name) {
  128. continue
  129. }
  130. sf := sv.Field(i).Interface()
  131. data, err := json.Marshal(sf)
  132. if err != nil {
  133. Expect(err).NotTo(HaveOccurred())
  134. }
  135. if !dv.Field(i).CanAddr() {
  136. Failf("unaddressable field: %v", dv.Type().Field(i).Name)
  137. } else {
  138. if err := json.Unmarshal(data, dv.Field(i).Addr().Interface()); err != nil {
  139. Expect(err).NotTo(HaveOccurred())
  140. }
  141. }
  142. }
  143. return dst
  144. }
  145. // BeforeEach gets a client and makes a namespace.
  146. func (f *Framework) BeforeEach() {
  147. // The fact that we need this feels like a bug in ginkgo.
  148. // https://github.com/onsi/ginkgo/issues/222
  149. f.cleanupHandle = AddCleanupAction(f.AfterEach)
  150. if f.Client == nil {
  151. By("Creating a kubernetes client")
  152. config, err := LoadConfig()
  153. Expect(err).NotTo(HaveOccurred())
  154. config.QPS = f.options.ClientQPS
  155. config.Burst = f.options.ClientBurst
  156. if f.options.GroupVersion != nil {
  157. config.GroupVersion = f.options.GroupVersion
  158. }
  159. if TestContext.KubeAPIContentType != "" {
  160. config.ContentType = TestContext.KubeAPIContentType
  161. }
  162. c, err := loadClientFromConfig(config)
  163. Expect(err).NotTo(HaveOccurred())
  164. f.Client = c
  165. f.Clientset_1_2, err = release_1_2.NewForConfig(config)
  166. f.Clientset_1_3, err = release_1_3.NewForConfig(config)
  167. Expect(err).NotTo(HaveOccurred())
  168. clientRepoConfig := getClientRepoConfig(config)
  169. f.StagingClient, err = release_1_4.NewForConfig(clientRepoConfig)
  170. Expect(err).NotTo(HaveOccurred())
  171. f.ClientPool = dynamic.NewClientPool(config, dynamic.LegacyAPIPathResolverFunc)
  172. }
  173. if f.federated {
  174. if f.FederationClientset_1_4 == nil {
  175. By("Creating a release 1.4 federation Clientset")
  176. var err error
  177. f.FederationClientset_1_4, err = LoadFederationClientset_1_4()
  178. Expect(err).NotTo(HaveOccurred())
  179. }
  180. By("Waiting for federation-apiserver to be ready")
  181. err := WaitForFederationApiserverReady(f.FederationClientset_1_4)
  182. Expect(err).NotTo(HaveOccurred())
  183. By("federation-apiserver is ready")
  184. By("Creating a federation namespace")
  185. ns, err := f.createFederationNamespace(f.BaseName)
  186. Expect(err).NotTo(HaveOccurred())
  187. f.FederationNamespace = ns
  188. By(fmt.Sprintf("Created federation namespace %s", ns.Name))
  189. }
  190. By("Building a namespace api object")
  191. namespace, err := f.CreateNamespace(f.BaseName, map[string]string{
  192. "e2e-framework": f.BaseName,
  193. })
  194. Expect(err).NotTo(HaveOccurred())
  195. f.Namespace = namespace
  196. if TestContext.VerifyServiceAccount {
  197. By("Waiting for a default service account to be provisioned in namespace")
  198. err = WaitForDefaultServiceAccountInNamespace(f.Client, namespace.Name)
  199. Expect(err).NotTo(HaveOccurred())
  200. } else {
  201. Logf("Skipping waiting for service account")
  202. }
  203. if TestContext.GatherKubeSystemResourceUsageData != "false" && TestContext.GatherKubeSystemResourceUsageData != "none" {
  204. f.gatherer, err = NewResourceUsageGatherer(f.Client, ResourceGathererOptions{
  205. inKubemark: ProviderIs("kubemark"),
  206. masterOnly: TestContext.GatherKubeSystemResourceUsageData == "master",
  207. })
  208. if err != nil {
  209. Logf("Error while creating NewResourceUsageGatherer: %v", err)
  210. } else {
  211. go f.gatherer.startGatheringData()
  212. }
  213. }
  214. if TestContext.GatherLogsSizes {
  215. f.logsSizeWaitGroup = sync.WaitGroup{}
  216. f.logsSizeWaitGroup.Add(1)
  217. f.logsSizeCloseChannel = make(chan bool)
  218. f.logsSizeVerifier = NewLogsVerifier(f.Client, f.logsSizeCloseChannel)
  219. go func() {
  220. f.logsSizeVerifier.Run()
  221. f.logsSizeWaitGroup.Done()
  222. }()
  223. }
  224. }
  225. func (f *Framework) deleteFederationNs() {
  226. ns := f.FederationNamespace
  227. By(fmt.Sprintf("Destroying federation namespace %q for this suite.", ns.Name))
  228. timeout := 5 * time.Minute
  229. if f.NamespaceDeletionTimeout != 0 {
  230. timeout = f.NamespaceDeletionTimeout
  231. }
  232. clientset := f.FederationClientset_1_4
  233. // First delete the namespace from federation apiserver.
  234. if err := clientset.Core().Namespaces().Delete(ns.Name, &api.DeleteOptions{}); err != nil {
  235. Failf("Error while deleting federation namespace %s: %s", ns.Name, err)
  236. }
  237. // Verify that it got deleted.
  238. err := wait.PollImmediate(5*time.Second, timeout, func() (bool, error) {
  239. if _, err := clientset.Core().Namespaces().Get(ns.Name); err != nil {
  240. if apierrs.IsNotFound(err) {
  241. return true, nil
  242. }
  243. Logf("Error while waiting for namespace to be terminated: %v", err)
  244. return false, nil
  245. }
  246. return false, nil
  247. })
  248. if err != nil {
  249. if !apierrs.IsNotFound(err) {
  250. Failf("Couldn't delete ns %q: %s", ns.Name, err)
  251. } else {
  252. Logf("Namespace %v was already deleted", ns.Name)
  253. }
  254. }
  255. // TODO: Delete the namespace from underlying clusters.
  256. }
  257. // AfterEach deletes the namespace, after reading its events.
  258. func (f *Framework) AfterEach() {
  259. RemoveCleanupAction(f.cleanupHandle)
  260. // DeleteNamespace at the very end in defer, to avoid any
  261. // expectation failures preventing deleting the namespace.
  262. defer func() {
  263. nsDeletionErrors := map[string]error{}
  264. if TestContext.DeleteNamespace {
  265. for _, ns := range f.namespacesToDelete {
  266. By(fmt.Sprintf("Destroying namespace %q for this suite.", ns.Name))
  267. timeout := 5 * time.Minute
  268. if f.NamespaceDeletionTimeout != 0 {
  269. timeout = f.NamespaceDeletionTimeout
  270. }
  271. if err := deleteNS(f.Client, f.ClientPool, ns.Name, timeout); err != nil {
  272. if !apierrs.IsNotFound(err) {
  273. nsDeletionErrors[ns.Name] = err
  274. } else {
  275. Logf("Namespace %v was already deleted", ns.Name)
  276. }
  277. }
  278. }
  279. // Delete the federation namespace.
  280. // TODO(nikhiljindal): Uncomment this, once https://github.com/kubernetes/kubernetes/issues/31077 is fixed.
  281. // In the meantime, we will have these extra namespaces in all clusters.
  282. // Note: this will not cause any failure since we create a new namespace for each test in BeforeEach().
  283. // f.deleteFederationNs()
  284. } else {
  285. Logf("Found DeleteNamespace=false, skipping namespace deletion!")
  286. }
  287. // Paranoia-- prevent reuse!
  288. f.Namespace = nil
  289. f.FederationNamespace = nil
  290. f.Client = nil
  291. f.namespacesToDelete = nil
  292. // if we had errors deleting, report them now.
  293. if len(nsDeletionErrors) != 0 {
  294. messages := []string{}
  295. for namespaceKey, namespaceErr := range nsDeletionErrors {
  296. messages = append(messages, fmt.Sprintf("Couldn't delete ns: %q: %s", namespaceKey, namespaceErr))
  297. }
  298. Failf(strings.Join(messages, ","))
  299. }
  300. }()
  301. if f.federated {
  302. defer func() {
  303. if f.FederationClientset_1_4 == nil {
  304. Logf("Warning: framework is marked federated, but has no federation 1.4 clientset")
  305. return
  306. }
  307. if err := f.FederationClientset_1_4.Federation().Clusters().DeleteCollection(nil, api.ListOptions{}); err != nil {
  308. Logf("Error: failed to delete Clusters: %+v", err)
  309. }
  310. }()
  311. }
  312. // Print events if the test failed.
  313. if CurrentGinkgoTestDescription().Failed && TestContext.DumpLogsOnFailure {
  314. DumpAllNamespaceInfo(f.Client, f.Namespace.Name)
  315. By(fmt.Sprintf("Dumping a list of prepulled images on each node"))
  316. LogContainersInPodsWithLabels(f.Client, api.NamespaceSystem, ImagePullerLabels, "image-puller")
  317. if f.federated {
  318. // Print logs of federation control plane pods (federation-apiserver and federation-controller-manager)
  319. LogPodsWithLabels(f.Client, "federation", map[string]string{"app": "federated-cluster"})
  320. // Print logs of kube-dns pod
  321. LogPodsWithLabels(f.Client, "kube-system", map[string]string{"k8s-app": "kube-dns"})
  322. }
  323. }
  324. summaries := make([]TestDataSummary, 0)
  325. if TestContext.GatherKubeSystemResourceUsageData != "false" && TestContext.GatherKubeSystemResourceUsageData != "none" && f.gatherer != nil {
  326. By("Collecting resource usage data")
  327. summaries = append(summaries, f.gatherer.stopAndSummarize([]int{90, 99, 100}, f.AddonResourceConstraints))
  328. }
  329. if TestContext.GatherLogsSizes {
  330. By("Gathering log sizes data")
  331. close(f.logsSizeCloseChannel)
  332. f.logsSizeWaitGroup.Wait()
  333. summaries = append(summaries, f.logsSizeVerifier.GetSummary())
  334. }
  335. if TestContext.GatherMetricsAfterTest {
  336. By("Gathering metrics")
  337. // TODO: enable Scheduler and ControllerManager metrics grabbing when Master's Kubelet will be registered.
  338. grabber, err := metrics.NewMetricsGrabber(f.Client, true, false, false, true)
  339. if err != nil {
  340. Logf("Failed to create MetricsGrabber. Skipping metrics gathering.")
  341. } else {
  342. received, err := grabber.Grab()
  343. if err != nil {
  344. Logf("MetricsGrabber failed grab metrics. Skipping metrics gathering.")
  345. } else {
  346. summaries = append(summaries, (*MetricsForE2E)(&received))
  347. }
  348. }
  349. }
  350. outputTypes := strings.Split(TestContext.OutputPrintType, ",")
  351. for _, printType := range outputTypes {
  352. switch printType {
  353. case "hr":
  354. for i := range summaries {
  355. Logf(summaries[i].PrintHumanReadable())
  356. }
  357. case "json":
  358. for i := range summaries {
  359. typeName := reflect.TypeOf(summaries[i]).String()
  360. Logf("%v JSON\n%v", typeName[strings.LastIndex(typeName, ".")+1:], summaries[i].PrintJSON())
  361. Logf("Finished")
  362. }
  363. default:
  364. Logf("Unknown output type: %v. Skipping.", printType)
  365. }
  366. }
  367. // Check whether all nodes are ready after the test.
  368. // This is explicitly done at the very end of the test, to avoid
  369. // e.g. not removing namespace in case of this failure.
  370. if err := AllNodesReady(f.Client, time.Minute); err != nil {
  371. Failf("All nodes should be ready after test, %v", err)
  372. }
  373. }
  374. func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*api.Namespace, error) {
  375. createTestingNS := TestContext.CreateTestingNS
  376. if createTestingNS == nil {
  377. createTestingNS = CreateTestingNS
  378. }
  379. ns, err := createTestingNS(baseName, f.Client, labels)
  380. if err == nil {
  381. f.namespacesToDelete = append(f.namespacesToDelete, ns)
  382. }
  383. return ns, err
  384. }
  385. func (f *Framework) createFederationNamespace(baseName string) (*v1.Namespace, error) {
  386. clientset := f.FederationClientset_1_4
  387. namespaceObj := &v1.Namespace{
  388. ObjectMeta: v1.ObjectMeta{
  389. GenerateName: fmt.Sprintf("e2e-tests-%v-", baseName),
  390. },
  391. }
  392. // Be robust about making the namespace creation call.
  393. var got *v1.Namespace
  394. if err := wait.PollImmediate(Poll, SingleCallTimeout, func() (bool, error) {
  395. var err error
  396. got, err = clientset.Core().Namespaces().Create(namespaceObj)
  397. if err != nil {
  398. Logf("Unexpected error while creating namespace: %v", err)
  399. return false, nil
  400. }
  401. return true, nil
  402. }); err != nil {
  403. return nil, err
  404. }
  405. return got, nil
  406. }
  407. // WaitForPodTerminated waits for the pod to be terminated with the given reason.
  408. func (f *Framework) WaitForPodTerminated(podName, reason string) error {
  409. return waitForPodTerminatedInNamespace(f.Client, podName, reason, f.Namespace.Name)
  410. }
  411. // WaitForPodRunning waits for the pod to run in the namespace.
  412. func (f *Framework) WaitForPodRunning(podName string) error {
  413. return WaitForPodNameRunningInNamespace(f.Client, podName, f.Namespace.Name)
  414. }
  415. // WaitForPodReady waits for the pod to flip to ready in the namespace.
  416. func (f *Framework) WaitForPodReady(podName string) error {
  417. return waitTimeoutForPodReadyInNamespace(f.Client, podName, f.Namespace.Name, "", PodStartTimeout)
  418. }
  419. // WaitForPodRunningSlow waits for the pod to run in the namespace.
  420. // It has a longer timeout then WaitForPodRunning (util.slowPodStartTimeout).
  421. func (f *Framework) WaitForPodRunningSlow(podName string) error {
  422. return waitForPodRunningInNamespaceSlow(f.Client, podName, f.Namespace.Name, "")
  423. }
  424. // WaitForPodNoLongerRunning waits for the pod to no longer be running in the namespace, for either
  425. // success or failure.
  426. func (f *Framework) WaitForPodNoLongerRunning(podName string) error {
  427. return WaitForPodNoLongerRunningInNamespace(f.Client, podName, f.Namespace.Name, "")
  428. }
  429. // TestContainerOutput runs the given pod in the given namespace and waits
  430. // for all of the containers in the podSpec to move into the 'Success' status, and tests
  431. // the specified container log against the given expected output using a substring matcher.
  432. func (f *Framework) TestContainerOutput(scenarioName string, pod *api.Pod, containerIndex int, expectedOutput []string) {
  433. f.testContainerOutputMatcher(scenarioName, pod, containerIndex, expectedOutput, ContainSubstring)
  434. }
  435. // TestContainerOutputRegexp runs the given pod in the given namespace and waits
  436. // for all of the containers in the podSpec to move into the 'Success' status, and tests
  437. // the specified container log against the given expected output using a regexp matcher.
  438. func (f *Framework) TestContainerOutputRegexp(scenarioName string, pod *api.Pod, containerIndex int, expectedOutput []string) {
  439. f.testContainerOutputMatcher(scenarioName, pod, containerIndex, expectedOutput, MatchRegexp)
  440. }
  441. // WaitForAnEndpoint waits for at least one endpoint to become available in the
  442. // service's corresponding endpoints object.
  443. func (f *Framework) WaitForAnEndpoint(serviceName string) error {
  444. for {
  445. // TODO: Endpoints client should take a field selector so we
  446. // don't have to list everything.
  447. list, err := f.Client.Endpoints(f.Namespace.Name).List(api.ListOptions{})
  448. if err != nil {
  449. return err
  450. }
  451. rv := list.ResourceVersion
  452. isOK := func(e *api.Endpoints) bool {
  453. return e.Name == serviceName && len(e.Subsets) > 0 && len(e.Subsets[0].Addresses) > 0
  454. }
  455. for i := range list.Items {
  456. if isOK(&list.Items[i]) {
  457. return nil
  458. }
  459. }
  460. options := api.ListOptions{
  461. FieldSelector: fields.Set{"metadata.name": serviceName}.AsSelector(),
  462. ResourceVersion: rv,
  463. }
  464. w, err := f.Client.Endpoints(f.Namespace.Name).Watch(options)
  465. if err != nil {
  466. return err
  467. }
  468. defer w.Stop()
  469. for {
  470. val, ok := <-w.ResultChan()
  471. if !ok {
  472. // reget and re-watch
  473. break
  474. }
  475. if e, ok := val.Object.(*api.Endpoints); ok {
  476. if isOK(e) {
  477. return nil
  478. }
  479. }
  480. }
  481. }
  482. }
  483. // Write a file using kubectl exec echo <contents> > <path> via specified container
  484. // Because of the primitive technique we're using here, we only allow ASCII alphanumeric characters
  485. func (f *Framework) WriteFileViaContainer(podName, containerName string, path string, contents string) error {
  486. By("writing a file in the container")
  487. allowedCharacters := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  488. for _, c := range contents {
  489. if !strings.ContainsRune(allowedCharacters, c) {
  490. return fmt.Errorf("Unsupported character in string to write: %v", c)
  491. }
  492. }
  493. command := fmt.Sprintf("echo '%s' > '%s'", contents, path)
  494. stdout, stderr, err := kubectlExecWithRetry(f.Namespace.Name, podName, containerName, "--", "/bin/sh", "-c", command)
  495. if err != nil {
  496. Logf("error running kubectl exec to write file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
  497. }
  498. return err
  499. }
  500. // Read a file using kubectl exec cat <path>
  501. func (f *Framework) ReadFileViaContainer(podName, containerName string, path string) (string, error) {
  502. By("reading a file in the container")
  503. stdout, stderr, err := kubectlExecWithRetry(f.Namespace.Name, podName, containerName, "--", "cat", path)
  504. if err != nil {
  505. Logf("error running kubectl exec to read file: %v\nstdout=%v\nstderr=%v)", err, string(stdout), string(stderr))
  506. }
  507. return string(stdout), err
  508. }
  509. // CreateServiceForSimpleAppWithPods is a convenience wrapper to create a service and its matching pods all at once.
  510. func (f *Framework) CreateServiceForSimpleAppWithPods(contPort int, svcPort int, appName string, podSpec func(n api.Node) api.PodSpec, count int, block bool) (error, *api.Service) {
  511. var err error = nil
  512. theService := f.CreateServiceForSimpleApp(contPort, svcPort, appName)
  513. f.CreatePodsPerNodeForSimpleApp(appName, podSpec, count)
  514. if block {
  515. err = WaitForPodsWithLabelRunning(f.Client, f.Namespace.Name, labels.SelectorFromSet(labels.Set(theService.Spec.Selector)))
  516. }
  517. return err, theService
  518. }
  519. // CreateServiceForSimpleApp returns a service that selects/exposes pods (send -1 ports if no exposure needed) with an app label.
  520. func (f *Framework) CreateServiceForSimpleApp(contPort, svcPort int, appName string) *api.Service {
  521. if appName == "" {
  522. panic(fmt.Sprintf("no app name provided"))
  523. }
  524. serviceSelector := map[string]string{
  525. "app": appName + "-pod",
  526. }
  527. // For convenience, user sending ports are optional.
  528. portsFunc := func() []api.ServicePort {
  529. if contPort < 1 || svcPort < 1 {
  530. return nil
  531. } else {
  532. return []api.ServicePort{{
  533. Protocol: "TCP",
  534. Port: int32(svcPort),
  535. TargetPort: intstr.FromInt(contPort),
  536. }}
  537. }
  538. }
  539. Logf("Creating a service-for-%v for selecting app=%v-pod", appName, appName)
  540. service, err := f.Client.Services(f.Namespace.Name).Create(&api.Service{
  541. ObjectMeta: api.ObjectMeta{
  542. Name: "service-for-" + appName,
  543. Labels: map[string]string{
  544. "app": appName + "-service",
  545. },
  546. },
  547. Spec: api.ServiceSpec{
  548. Ports: portsFunc(),
  549. Selector: serviceSelector,
  550. },
  551. })
  552. ExpectNoError(err)
  553. return service
  554. }
  555. // CreatePodsPerNodeForSimpleApp Creates pods w/ labels. Useful for tests which make a bunch of pods w/o any networking.
  556. func (f *Framework) CreatePodsPerNodeForSimpleApp(appName string, podSpec func(n api.Node) api.PodSpec, maxCount int) map[string]string {
  557. nodes := GetReadySchedulableNodesOrDie(f.Client)
  558. labels := map[string]string{
  559. "app": appName + "-pod",
  560. }
  561. for i, node := range nodes.Items {
  562. // one per node, but no more than maxCount.
  563. if i <= maxCount {
  564. Logf("%v/%v : Creating container with label app=%v-pod", i, maxCount, appName)
  565. _, err := f.Client.Pods(f.Namespace.Name).Create(&api.Pod{
  566. ObjectMeta: api.ObjectMeta{
  567. Name: fmt.Sprintf(appName+"-pod-%v", i),
  568. Labels: labels,
  569. },
  570. Spec: podSpec(node),
  571. })
  572. ExpectNoError(err)
  573. }
  574. }
  575. return labels
  576. }
  577. type KubeUser struct {
  578. Name string `yaml:"name"`
  579. User struct {
  580. Username string `yaml:"username"`
  581. Password string `yaml:"password"`
  582. Token string `yaml:"token"`
  583. } `yaml:"user"`
  584. }
  585. type KubeCluster struct {
  586. Name string `yaml:"name"`
  587. Cluster struct {
  588. CertificateAuthorityData string `yaml:"certificate-authority-data"`
  589. Server string `yaml:"server"`
  590. } `yaml:"cluster"`
  591. }
  592. type KubeConfig struct {
  593. Contexts []struct {
  594. Name string `yaml:"name"`
  595. Context struct {
  596. Cluster string `yaml:"cluster"`
  597. User string
  598. } `yaml:"context"`
  599. } `yaml:"contexts"`
  600. Clusters []KubeCluster `yaml:"clusters"`
  601. Users []KubeUser `yaml:"users"`
  602. }
  603. func (kc *KubeConfig) findUser(name string) *KubeUser {
  604. for _, user := range kc.Users {
  605. if user.Name == name {
  606. return &user
  607. }
  608. }
  609. return nil
  610. }
  611. func (kc *KubeConfig) findCluster(name string) *KubeCluster {
  612. for _, cluster := range kc.Clusters {
  613. if cluster.Name == name {
  614. return &cluster
  615. }
  616. }
  617. return nil
  618. }
  619. type E2EContext struct {
  620. // Raw context name,
  621. RawName string `yaml:"rawName"`
  622. // A valid dns subdomain which can be used as the name of kubernetes resources.
  623. Name string `yaml:"name"`
  624. Cluster *KubeCluster `yaml:"cluster"`
  625. User *KubeUser `yaml:"user"`
  626. }
  627. func (f *Framework) GetUnderlyingFederatedContexts() []E2EContext {
  628. if !f.federated {
  629. Failf("getUnderlyingFederatedContexts called on non-federated framework")
  630. }
  631. kubeconfig := KubeConfig{}
  632. configBytes, err := ioutil.ReadFile(TestContext.KubeConfig)
  633. ExpectNoError(err)
  634. err = yaml.Unmarshal(configBytes, &kubeconfig)
  635. ExpectNoError(err)
  636. e2eContexts := []E2EContext{}
  637. for _, context := range kubeconfig.Contexts {
  638. if strings.HasPrefix(context.Name, "federation") && context.Name != "federation-cluster" {
  639. user := kubeconfig.findUser(context.Context.User)
  640. if user == nil {
  641. Failf("Could not find user for context %+v", context)
  642. }
  643. cluster := kubeconfig.findCluster(context.Context.Cluster)
  644. if cluster == nil {
  645. Failf("Could not find cluster for context %+v", context)
  646. }
  647. dnsSubdomainName, err := GetValidDNSSubdomainName(context.Name)
  648. if err != nil {
  649. Failf("Could not convert context name %s to a valid dns subdomain name, error: %s", context.Name, err)
  650. }
  651. e2eContexts = append(e2eContexts, E2EContext{
  652. RawName: context.Name,
  653. Name: dnsSubdomainName,
  654. Cluster: cluster,
  655. User: user,
  656. })
  657. }
  658. }
  659. return e2eContexts
  660. }
  661. func kubectlExecWithRetry(namespace string, podName, containerName string, args ...string) ([]byte, []byte, error) {
  662. for numRetries := 0; numRetries < maxKubectlExecRetries; numRetries++ {
  663. if numRetries > 0 {
  664. Logf("Retrying kubectl exec (retry count=%v/%v)", numRetries+1, maxKubectlExecRetries)
  665. }
  666. stdOutBytes, stdErrBytes, err := kubectlExec(namespace, podName, containerName, args...)
  667. if err != nil {
  668. if strings.Contains(strings.ToLower(string(stdErrBytes)), "i/o timeout") {
  669. // Retry on "i/o timeout" errors
  670. Logf("Warning: kubectl exec encountered i/o timeout.\nerr=%v\nstdout=%v\nstderr=%v)", err, string(stdOutBytes), string(stdErrBytes))
  671. continue
  672. }
  673. if strings.Contains(strings.ToLower(string(stdErrBytes)), "container not found") {
  674. // Retry on "container not found" errors
  675. Logf("Warning: kubectl exec encountered container not found.\nerr=%v\nstdout=%v\nstderr=%v)", err, string(stdOutBytes), string(stdErrBytes))
  676. time.Sleep(2 * time.Second)
  677. continue
  678. }
  679. }
  680. return stdOutBytes, stdErrBytes, err
  681. }
  682. err := fmt.Errorf("Failed: kubectl exec failed %d times with \"i/o timeout\". Giving up.", maxKubectlExecRetries)
  683. return nil, nil, err
  684. }
  685. func kubectlExec(namespace string, podName, containerName string, args ...string) ([]byte, []byte, error) {
  686. var stdout, stderr bytes.Buffer
  687. cmdArgs := []string{
  688. "exec",
  689. fmt.Sprintf("--namespace=%v", namespace),
  690. podName,
  691. fmt.Sprintf("-c=%v", containerName),
  692. }
  693. cmdArgs = append(cmdArgs, args...)
  694. cmd := KubectlCmd(cmdArgs...)
  695. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  696. Logf("Running '%s %s'", cmd.Path, strings.Join(cmdArgs, " "))
  697. err := cmd.Run()
  698. return stdout.Bytes(), stderr.Bytes(), err
  699. }
  700. // Wrapper function for ginkgo describe. Adds namespacing.
  701. // TODO: Support type safe tagging as well https://github.com/kubernetes/kubernetes/pull/22401.
  702. func KubeDescribe(text string, body func()) bool {
  703. return Describe("[k8s.io] "+text, body)
  704. }
  705. // PodStateVerification represents a verification of pod state.
  706. // Any time you have a set of pods that you want to operate against or query,
  707. // this struct can be used to declaratively identify those pods.
  708. type PodStateVerification struct {
  709. // Optional: only pods that have k=v labels will pass this filter.
  710. Selectors map[string]string
  711. // Required: The phases which are valid for your pod.
  712. ValidPhases []api.PodPhase
  713. // Optional: only pods passing this function will pass the filter
  714. // Verify a pod.
  715. // As an optimization, in addition to specfying filter (boolean),
  716. // this function allows specifying an error as well.
  717. // The error indicates that the polling of the pod spectrum should stop.
  718. Verify func(api.Pod) (bool, error)
  719. // Optional: only pods with this name will pass the filter.
  720. PodName string
  721. }
  722. type ClusterVerification struct {
  723. client *client.Client
  724. namespace *api.Namespace // pointer rather than string, since ns isn't created until before each.
  725. podState PodStateVerification
  726. }
  727. func (f *Framework) NewClusterVerification(filter PodStateVerification) *ClusterVerification {
  728. return &ClusterVerification{
  729. f.Client,
  730. f.Namespace,
  731. filter,
  732. }
  733. }
  734. func passesPodNameFilter(pod api.Pod, name string) bool {
  735. return name == "" || strings.Contains(pod.Name, name)
  736. }
  737. func passesVerifyFilter(pod api.Pod, verify func(p api.Pod) (bool, error)) (bool, error) {
  738. if verify == nil {
  739. return true, nil
  740. } else {
  741. verified, err := verify(pod)
  742. // If an error is returned, by definition, pod verification fails
  743. if err != nil {
  744. return false, err
  745. } else {
  746. return verified, nil
  747. }
  748. }
  749. }
  750. func passesPhasesFilter(pod api.Pod, validPhases []api.PodPhase) bool {
  751. passesPhaseFilter := false
  752. for _, phase := range validPhases {
  753. if pod.Status.Phase == phase {
  754. passesPhaseFilter = true
  755. }
  756. }
  757. return passesPhaseFilter
  758. }
  759. // filterLabels returns a list of pods which have labels.
  760. func filterLabels(selectors map[string]string, cli *client.Client, ns string) (*api.PodList, error) {
  761. var err error
  762. var selector labels.Selector
  763. var pl *api.PodList
  764. // List pods based on selectors. This might be a tiny optimization rather then filtering
  765. // everything manually.
  766. if len(selectors) > 0 {
  767. selector = labels.SelectorFromSet(labels.Set(selectors))
  768. options := api.ListOptions{LabelSelector: selector}
  769. pl, err = cli.Pods(ns).List(options)
  770. } else {
  771. pl, err = cli.Pods(ns).List(api.ListOptions{})
  772. }
  773. return pl, err
  774. }
  775. // filter filters pods which pass a filter. It can be used to compose
  776. // the more useful abstractions like ForEach, WaitFor, and so on, which
  777. // can be used directly by tests.
  778. func (p *PodStateVerification) filter(c *client.Client, namespace *api.Namespace) ([]api.Pod, error) {
  779. if len(p.ValidPhases) == 0 || namespace == nil {
  780. panic(fmt.Errorf("Need to specify a valid pod phases (%v) and namespace (%v). ", p.ValidPhases, namespace))
  781. }
  782. ns := namespace.Name
  783. pl, err := filterLabels(p.Selectors, c, ns) // Build an api.PodList to operate against.
  784. Logf("Selector matched %v pods for %v", len(pl.Items), p.Selectors)
  785. if len(pl.Items) == 0 || err != nil {
  786. return pl.Items, err
  787. }
  788. unfilteredPods := pl.Items
  789. filteredPods := []api.Pod{}
  790. ReturnPodsSoFar:
  791. // Next: Pod must match at least one of the states that the user specified
  792. for _, pod := range unfilteredPods {
  793. if !(passesPhasesFilter(pod, p.ValidPhases) && passesPodNameFilter(pod, p.PodName)) {
  794. continue
  795. }
  796. passesVerify, err := passesVerifyFilter(pod, p.Verify)
  797. if err != nil {
  798. Logf("Error detected on %v : %v !", pod.Name, err)
  799. break ReturnPodsSoFar
  800. }
  801. if passesVerify {
  802. filteredPods = append(filteredPods, pod)
  803. }
  804. }
  805. return filteredPods, err
  806. }
  807. // WaitFor waits for some minimum number of pods to be verified, according to the PodStateVerification
  808. // definition.
  809. func (cl *ClusterVerification) WaitFor(atLeast int, timeout time.Duration) ([]api.Pod, error) {
  810. pods := []api.Pod{}
  811. var returnedErr error
  812. err := wait.Poll(1*time.Second, timeout, func() (bool, error) {
  813. pods, returnedErr = cl.podState.filter(cl.client, cl.namespace)
  814. // Failure
  815. if returnedErr != nil {
  816. Logf("Cutting polling short: We got an error from the pod filtering layer.")
  817. // stop polling if the pod filtering returns an error. that should never happen.
  818. // it indicates, for example, that the client is broken or something non-pod related.
  819. return false, returnedErr
  820. }
  821. Logf("Found %v / %v", len(pods), atLeast)
  822. // Success
  823. if len(pods) >= atLeast {
  824. return true, nil
  825. }
  826. // Keep trying...
  827. return false, nil
  828. })
  829. Logf("WaitFor completed with timeout %v. Pods found = %v out of %v", timeout, len(pods), atLeast)
  830. return pods, err
  831. }
  832. // WaitForOrFail provides a shorthand WaitFor with failure as an option if anything goes wrong.
  833. func (cl *ClusterVerification) WaitForOrFail(atLeast int, timeout time.Duration) {
  834. pods, err := cl.WaitFor(atLeast, timeout)
  835. if err != nil || len(pods) < atLeast {
  836. Failf("Verified %v of %v pods , error : %v", len(pods), atLeast, err)
  837. }
  838. }
  839. // ForEach runs a function against every verifiable pod. Be warned that this doesn't wait for "n" pods to verifiy,
  840. // so it may return very quickly if you have strict pod state requirements.
  841. //
  842. // For example, if you require at least 5 pods to be running before your test will pass,
  843. // its smart to first call "clusterVerification.WaitFor(5)" before you call clusterVerification.ForEach.
  844. func (cl *ClusterVerification) ForEach(podFunc func(api.Pod)) error {
  845. pods, err := cl.podState.filter(cl.client, cl.namespace)
  846. if err == nil {
  847. if len(pods) == 0 {
  848. Failf("No pods matched the filter.")
  849. }
  850. Logf("ForEach: Found %v pods from the filter. Now looping through them.", len(pods))
  851. for _, p := range pods {
  852. podFunc(p)
  853. }
  854. } else {
  855. Logf("ForEach: Something went wrong when filtering pods to execute against: %v", err)
  856. }
  857. return err
  858. }