text:
<?php

namespace Drupal\Tests\color\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Modify the Bartik theme colors and make sure the changes are reflected on the
 * frontend.
 *
 * @group color
 */
class ColorTest extends BrowserTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public static $modules = array('color', 'color_test', 'block', 'file');

  /**
   * A user with administrative permissions.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $bigUser;

  /**
   * An associative array of settings for themes.
   *
   * @var array
   */
  protected $themes;

  /**
   * Associative array of hex color strings to test.
   *
   * Keys are the color string and values are a Boolean set to TRUE for valid
   * colors.
   *
   * @var array
   */
  protected $colorTests;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create user.
    $this->bigUser = $this->drupalCreateUser(array('administer themes'));

    // This tests the color module in Bartik.
    $this->themes = array(
      'bartik' => array(
        'palette_input' => 'palette[bg]',
        'scheme' => 'slate',
        'scheme_color' => '#3b3b3b',
      ),
      'color_test_theme' => array(
        'palette_input' => 'palette[bg]',
        'scheme' => 'custom',
        'scheme_color' => '#3b3b3b',
      ),
    );
    \Drupal::service('theme_handler')->install(array_keys($this->themes));

    // Array filled with valid and not valid color values.
    $this->colorTests = array(
      '#000' => TRUE,
      '#123456' => TRUE,
      '#abcdef' => TRUE,
      '#0' => FALSE,
      '#00' => FALSE,
      '#0000' => FALSE,
      '#00000' => FALSE,
      '123456' => FALSE,
      '#00000g' => FALSE,
    );
  }

  /**
   * Tests the Color module functionality.
   */
  function testColor() {
    foreach ($this->themes as $theme => $test_values) {
      $this->_testColor($theme, $test_values);
    }
  }

  /**
   * Tests the Color module functionality using the given theme.
   *
   * @param string $theme
   *   The machine name of the theme being tested.
   * @param array $test_values
   *   An associative array of test settings (i.e. 'Main background', 'Text
   *   color', 'Color set', etc) for the theme which being tested.
   */
  function _testColor($theme, $test_values) {
    $this->config('system.theme')
      ->set('default', $theme)
      ->save();
    $settings_path = 'admin/appearance/settings/' . $theme;

    $this->drupalLogin($this->bigUser);
    $this->drupalGet($settings_path);
    $this->assertResponse(200);
    $this->assertUniqueText('Color set');
    $edit['scheme'] = '';
    $edit[$test_values['palette_input']] = '#123456';
    $this->drupalPostForm($settings_path, $edit, t('Save configuration'));

    $this->drupalGet('<front>');
    $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
    foreach ($stylesheets as $stylesheet) {
      $this->assertPattern('|' . file_url_transform_relative(file_create_url($stylesheet)) . '|', 'Make sure the color stylesheet is included in the content. (' . $theme . ')');
      $stylesheet_content = join("\n", file($stylesheet));
      $this->assertTrue(strpos($stylesheet_content, 'color: #123456') !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
    }

    $this->drupalGet($settings_path);
    $this->assertResponse(200);
    $edit['scheme'] = $test_values['scheme'];
    $this->drupalPostForm($settings_path, $edit, t('Save configuration'));

    $this->drupalGet('<front>');
    $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
    foreach ($stylesheets as $stylesheet) {
      $stylesheet_content = join("\n", file($stylesheet));
      $this->assertTrue(strpos($stylesheet_content, 'color: ' . $test_values['scheme_color']) !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
    }

    // Test with aggregated CSS turned on.
    $config = $this->config('system.performance');
    $config->set('css.preprocess', 1);
    $config->save();
    $this->drupalGet('<front>');
    $stylesheets = \Drupal::state()->get('drupal_css_cache_files') ?: array();
    $stylesheet_content = '';
    foreach ($stylesheets as $uri) {
      $stylesheet_content .= join("\n", file(drupal_realpath($uri)));
    }
    $this->assertTrue(strpos($stylesheet_content, 'public://') === FALSE, 'Make sure the color paths have been translated to local paths. (' . $theme . ')');
    $config->set('css.preprocess', 0);
    $config->save();
  }

  /**
   * Tests whether the provided color is valid.
   */
  function testValidColor() {
    $this->config('system.theme')
      ->set('default', 'bartik')
      ->save();
    $settings_path = 'admin/appearance/settings/bartik';

    $this->drupalLogin($this->bigUser);
    $edit['scheme'] = '';

    foreach ($this->colorTests as $color => $is_valid) {
      $edit['palette[bg]'] = $color;
      $this->drupalPostForm($settings_path, $edit, t('Save configuration'));

      if ($is_valid) {
        $this->assertText('The configuration options have been saved.');
      }
      else {
        $this->assertText('You must enter a valid hexadecimal color value for Main background.');
      }
    }
  }

  /**
   * Test whether the custom logo is used in the color preview.
   */
  function testLogoSettingOverride() {
    $this->drupalLogin($this->bigUser);
    $edit = array(
      'default_logo' => FALSE,
      'logo_path' => 'core/misc/druplicon.png',
    );
    $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));

    // Ensure that the overridden logo is present in Bartik, which is colorable.
    $this->drupalGet('admin/appearance/settings/bartik');
    $this->assertIdentical($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']);
  }

  /**
   * Test whether the scheme can be set, viewed anonymously and reset.
   */
  function testOverrideAndResetScheme() {
    $settings_path = 'admin/appearance/settings/bartik';
    $this->config('system.theme')
      ->set('default', 'bartik')
      ->save();

    // Place branding block with site name and slogan into header region.
    $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);

    $this->drupalGet('');
    $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
    $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');

    // Log in and set the color scheme to 'slate'.
    $this->drupalLogin($this->bigUser);
    $edit['scheme'] = 'slate';
    $this->drupalPostForm($settings_path, $edit, t('Save configuration'));

    // Visit the homepage and ensure color changes.
    $this->drupalLogout();
    $this->drupalGet('');
    $this->assertRaw('files/color/bartik-', 'Make sure the color logo is being used.');
    $this->assertNoRaw('bartik/logo.svg', 'Make sure the original bartik logo does not exist.');

    // Log in and set the color scheme back to default (delete config).
    $this->drupalLogin($this->bigUser);
    $edit['scheme'] = 'default';
    $this->drupalPostForm($settings_path, $edit, t('Save configuration'));

    // Log out and ensure there is no color and we have the original logo.
    $this->drupalLogout();
    $this->drupalGet('');
    $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
    $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');
  }

}


text:
namespace chrome_browser_net {

std::unique_ptr<base::DictionaryValue> GetPrerenderInfo(Profile* profile) {
  std::unique_ptr<base::DictionaryValue> value;
  prerender::NoStatePrefetchManager* no_state_prefetch_manager =
      prerender::NoStatePrefetchManagerFactory::GetForBrowserContext(profile);
  if (no_state_prefetch_manager) {
    value = no_state_prefetch_manager->CopyAsValue();
  } else {
    value = std::make_unique<base::DictionaryValue>();
    value->SetBoolean("enabled", false);
    value->SetBoolean("omnibox_enabled", false);
  }
  return value;
}

std::unique_ptr<base::ListValue> GetExtensionInfo(Profile* profile) {
  auto extension_list = std::make_unique<base::ListValue>();
#if BUILDFLAG(ENABLE_EXTENSIONS)
  extensions::ExtensionSystem* extension_system =
      extensions::ExtensionSystem::Get(profile);
  if (extension_system) {
    extensions::ExtensionService* extension_service =
        extension_system->extension_service();
    if (extension_service) {
      std::unique_ptr<const extensions::ExtensionSet> extensions(
          extensions::ExtensionRegistry::Get(profile)
              ->GenerateInstalledExtensionsSet());
      for (const auto& extension : *extensions) {
        std::unique_ptr<base::DictionaryValue> extension_info(
            new base::DictionaryValue());
        bool enabled = extension_service->IsExtensionEnabled(extension->id());
        extensions::GetExtensionBasicInfo(extension.get(), enabled,
                                          extension_info.get());
        extension_list->Append(std::move(extension_info));
      }
    }
  }
#endif
  return extension_list;
}

#if defined(OS_WIN)
std::unique_ptr<base::DictionaryValue> GetWindowsServiceProviders() {
  auto service_providers = std::make_unique<base::DictionaryValue>();

  WinsockLayeredServiceProviderList layered_providers;
  GetWinsockLayeredServiceProviders(&layered_providers);
  auto layered_provider_list = std::make_unique<base::ListValue>();
  for (size_t i = 0; i < layered_providers.size(); ++i) {
    auto service_dict = std::make_unique<base::DictionaryValue>();
    service_dict->SetString("name",
                            base::AsString16(layered_providers[i].name));
    service_dict->SetInteger("version", layered_providers[i].version);
    service_dict->SetInteger("chain_length", layered_providers[i].chain_length);
    service_dict->SetInteger("socket_type", layered_providers[i].socket_type);
    service_dict->SetInteger("socket_protocol",
                             layered_providers[i].socket_protocol);
    service_dict->SetString("path",
                            base::WideToUTF8(layered_providers[i].path));

    layered_provider_list->Append(std::move(service_dict));
  }
  service_providers->Set("service_providers", std::move(layered_provider_list));

  WinsockNamespaceProviderList namespace_providers;
  GetWinsockNamespaceProviders(&namespace_providers);
  auto namespace_list = std::make_unique<base::ListValue>();
  for (size_t i = 0; i < namespace_providers.size(); ++i) {
    auto namespace_dict = std::make_unique<base::DictionaryValue>();
    namespace_dict->SetString("name",
                              base::AsString16(namespace_providers[i].name));
    namespace_dict->SetBoolean("active", namespace_providers[i].active);
    namespace_dict->SetInteger("version", namespace_providers[i].version);
    namespace_dict->SetInteger("type", namespace_providers[i].type);

    namespace_list->Append(std::move(namespace_dict));
  }
  service_providers->Set("namespace_providers", std::move(namespace_list));

  return service_providers;
}
#endif

}  // namespace chrome_browser_net

redpajama_set_name: RedPajamaGithub


text:
{% extends "strongly_typed_no_sidebar.html" %}

{% block base_content %}

You must be logged in as an administrator to access that page.

                        {% if messages %}

                            {% for message in messages %}

{{ message }}

                            {% endfor %}

                        {% endif %}

                        <form name="validateuser" method="post" action="/census/anonymous/">{% csrf_token %}
                            Username
                            <input type="text" name="username" id="username"/>
                            Password
                            <input type="password" name="password" id="password"/>
                            <input class="customsubmit" type="submit" value="Login"/>
                            <input type="hidden" name="referringPageURL" value="{{ referringPageURL }}"/>
                        </form>

{% endblock %}


text:
*** PLEASE NOTE: This document applies to the HEAD of the source
tree only. If you are using a released version of Kubernetes, you almost
certainly want the docs that go with that version.

Documentation for specific releases can be found at
[releases.k8s.io](http://releases.k8s.io).

#Kubernetes Cluster Federation
##(a.k.a. "Ubernetes")

## Requirements Analysis and Product Proposal

## _by Quinton Hoole ([quinton@google.com](mailto:quinton@google.com))_
_Initial revision: 2015-03-05_
_Last updated: 2015-03-09_
This doc: [tinyurl.com/ubernetesv2](http://tinyurl.com/ubernetesv2)
Slides: [tinyurl.com/ubernetes-slides](http://tinyurl.com/ubernetes-slides)

## Introduction

Today, each Kubernetes cluster is a relatively self-contained unit,
which typically runs in a single "on-premise" data centre or single
availability zone of a cloud provider (Google's GCE, Amazon's AWS,
etc).

Several current and potential Kubernetes users and customers have
expressed a keen interest in tying together ("federating") multiple
clusters in some sensible way in order to enable the following kinds
of use cases (intentionally vague):

1. _"Preferentially run my workloads in my on-premise cluster(s), but
   automatically overflow to my cloud-hosted cluster(s) if I run out
   of on-premise capacity"_.
1. _"Most of my workloads should run in my preferred cloud-hosted
   cluster(s), but some are privacy-sensitive, and should be
   automatically diverted to run in my secure, on-premise
   cluster(s)"_.
1. _"I want to avoid vendor lock-in, so I want my workloads to run
   across multiple cloud providers all the time.  I change my set of
   such cloud providers, and my pricing contracts with them,
   periodically"_.
1. _"I want to be immune to any single data centre or cloud
   availability zone outage, so I want to spread my service across
   multiple such zones (and ideally even across multiple cloud
   providers)."_

The above use cases are by necessity left imprecisely defined.  The
rest of this document explores these use cases and their implications
in further detail, and compares a few alternative high level
approaches to addressing them.  The idea of cluster federation has
informally become known as_ "Ubernetes"_.

## Summary/TL;DR

TBD

## What exactly is a Kubernetes Cluster?

A central design concept in Kubernetes is that of a _cluster_. While
loosely speaking, a cluster can be thought of as running in a single
data center, or cloud provider availability zone, a more precise
definition is that each cluster provides:

1. a single Kubernetes API entry point,
1. a consistent, cluster-wide resource naming scheme
1. a scheduling/container placement domain
1. a service network routing domain
1. (in future) an authentication and authorization model.
1. ....

The above in turn imply the need for a relatively performant, reliable
and cheap network within each cluster.

There is also assumed to be some degree of failure correlation across
a cluster, i.e.  whole clusters are expected to fail, at least
occasionally (due to cluster-wide power and network failures, natural
disasters etc). Clusters are often relatively homogenous in that all
compute nodes are typically provided by a single cloud provider or
hardware vendor, and connected by a common, unified network fabric.
But these are not hard requirements of Kubernetes.

Other classes of Kubernetes deployments than the one sketched above
are technically feasible, but come with some challenges of their own,
and are not yet common or explicitly supported.

More specifically, having a Kubernetes cluster span multiple
well-connected availability zones within a single geographical region
(e.g. US North East, UK, Japan etc) is worthy of further
consideration, in particular because it potentially addresses
some of these requirements.

## What use cases require Cluster Federation?

Let's name a few concrete use cases to aid the discussion:

## 1.Capacity Overflow

_"I want to preferentially run my workloads in my on-premise cluster(s), but automatically "overflow" to my cloud-hosted cluster(s) when I run out of on-premise capacity."_

This idea is known in some circles as "[cloudbursting](http://searchcloudcomputing.techtarget.com/definition/cloud-bursting)".

**Clarifying questions:** What is the unit of overflow?  Individual
  pods? Probably not always.  Replication controllers and their
  associated sets of pods?  Groups of replication controllers
  (a.k.a. distributed applications)?  How are persistent disks
  overflowed?  Can the "overflowed" pods communicate with their
  brethren and sistren pods and services in the other cluster(s)?
  Presumably yes, at higher cost and latency, provided that they use
  external service discovery. Is "overflow" enabled only when creating
  new workloads/replication controllers, or are existing workloads
  dynamically migrated between clusters based on fluctuating available
  capacity?  If so, what is the desired behaviour, and how is it
  achieved?  How, if at all, does this relate to quota enforcement
  (e.g. if we run out of on-premise capacity, can all or only some
  quotas transfer to other, potentially more expensive off-premise
  capacity?)

It seems that most of this boils down to:

1. **location affinity** (pods relative to each other, and to other
   stateful services like persistent storage - how is this expressed
   and enforced?)
1. **cross-cluster scheduling** (given location affinity constraints
   and other scheduling policy, which resources are assigned to which
   clusters, and by what?)
1. **cross-cluster service discovery** (how do pods in one cluster
   discover and communicate with pods in another cluster?)
1. **cross-cluster migration** (how do compute and storage resources,
   and the distributed applications to which they belong, move from
   one cluster to another)

## 2. Sensitive Workloads

_"I want most of my workloads to run in my preferred cloud-hosted
cluster(s), but some are privacy-sensitive, and should be
automatically diverted to run in my secure, on-premise cluster(s). The
list of privacy-sensitive workloads changes over time, and they're
subject to external auditing."_

**Clarifying questions:** What kinds of rules determine which
  workloads go where?  Is a static mapping from container (or more
  typically, replication controller) to cluster maintained and
  enforced?  If so, is it only enforced on startup, or are things
  migrated between clusters when the mappings change? This starts to
  look quite similar to "1. Capacity Overflow", and again seems to
  boil down to:

1. location affinity
1. cross-cluster scheduling
1. cross-cluster service discovery
1. cross-cluster migration
with the possible addition of:

+ cross-cluster monitoring and auditing (which is conveniently deemed
   to be outside the scope of this document, for the time being at
   least)

## 3. Vendor lock-in avoidance

_"My CTO wants us to avoid vendor lock-in, so she wants our workloads
to run across multiple cloud providers at all times.  She changes our
set of preferred cloud providers and pricing contracts with them
periodically, and doesn't want to have to communicate and manually
enforce these policy changes across the organization every time this
happens.  She wants it centrally and automatically enforced, monitored
and audited."_

**Clarifying questions:** Again, I think that this can potentially be
  reformulated as a Capacity Overflow problem - the fundamental
  principles seem to be the same or substantially similar to those
  above.

## 4. "Unavailability Zones"

_"I want to be immune to any single data centre or cloud availability
zone outage, so I want to spread my service across multiple such zones
(and ideally even across multiple cloud providers), and have my
service remain available even if one of the availability zones or
cloud providers "goes down"_.

It seems useful to split this into two sub use cases:

1. Multiple availability zones within a single cloud provider (across
   which feature sets like private networks, load balancing,
   persistent disks, data snapshots etc are typically consistent and
   explicitly designed to inter-operate).
1. Multiple cloud providers (typically with inconsistent feature sets
   and more limited interoperability).

The single cloud provider case might be easier to implement (although
the multi-cloud provider implementation should just work for a single
cloud provider).  Propose high-level design catering for both, with
initial implementation targeting single cloud provider only.

**Clarifying questions:**
**How does global external service discovery work?** In the steady
  state, which external clients connect to which clusters?  GeoDNS or
  similar?  What is the tolerable failover latency if a cluster goes
  down?  Maybe something like (make up some numbers, notwithstanding
  some buggy DNS resolvers, TTL's, caches etc) ~3 minutes for ~90% of
  clients to re-issue DNS lookups and reconnect to a new cluster when
  their home cluster fails is good enough for most Kubernetes users
  (or at least way better than the status quo), given that these sorts
  of failure only happen a small number of times a year?

**How does dynamic load balancing across clusters work, if at all?**
  One simple starting point might be "it doesn't".  i.e. if a service
  in a cluster is deemed to be "up", it receives as much traffic as is
  generated "nearby" (even if it overloads).  If the service is deemed
  to "be down" in a given cluster, "all" nearby traffic is redirected
  to some other cluster within some number of seconds (failover could
  be automatic or manual).  Failover is essentially binary.  An
  improvement would be to detect when a service in a cluster reaches
  maximum serving capacity, and dynamically divert additional traffic
  to other clusters.  But how exactly does all of this work, and how
  much of it is provided by Kubernetes, as opposed to something else
  bolted on top (e.g. external monitoring and manipulation of GeoDNS)?

**How does this tie in with auto-scaling of services?** More
  specifically, if I run my service across _n_ clusters globally, and
  one (or more) of them fail, how do I ensure that the remaining _n-1_
  clusters have enough capacity to serve the additional, failed-over
  traffic?  Either:

1. I constantly over-provision all clusters by 1/n (potentially expensive), or
1. I "manually" update my replica count configurations in the
   remaining clusters by 1/n when the failure occurs, and Kubernetes
   takes care of the rest for me, or
1. Auto-scaling (not yet available) in the remaining clusters takes
   care of it for me automagically as the additional failed-over
   traffic arrives (with some latency).
1. I manually specify "additional resources to be provisioned" per
   remaining cluster, possibly proportional to both the remaining functioning resources
   and the unavailable resources in the failed cluster(s).
   (All the benefits of over-provisioning, without expensive idle resources.)

Doing nothing (i.e. forcing users to choose between 1 and 2 on their
own) is probably an OK starting point.  Kubernetes autoscaling can get
us to 3 at some later date.

Up to this point, this use case ("Unavailability Zones") seems materially different from all the others above.  It does not require dynamic cross-cluster service migration (we assume that the service is already running in more than one cluster when the failure occurs).  Nor does it necessarily involve cross-cluster service discovery or location affinity.  As a result, I propose that we address this use case somewhat independently of the others (although I strongly suspect that it will become substantially easier once we've solved the others).

All of the above (regarding "Unavailibility Zones") refers primarily
to already-running user-facing services, and minimizing the impact on
end users of those services becoming unavailable in a given cluster.
What about the people and systems that deploy Kubernetes services
(devops etc)?  Should they be automatically shielded from the impact
of the cluster outage? i.e. have their new resource creation requests
automatically diverted to another cluster during the outage?  While
this specific requirement seems non-critical (manual fail-over seems
relatively non-arduous, ignoring the user-facing issues above), it
smells a lot like the first three use cases listed above ("Capacity
Overflow, Sensitive Services, Vendor lock-in..."), so if we address
those, we probably get this one free of charge.

## Core Challenges of Cluster Federation

As we saw above, a few common challenges fall out of most of the use
cases considered above, namely:

## Location Affinity

Can the pods comprising a single distributed application be
partitioned across more than one cluster? More generally, how far
apart, in network terms, can a given client and server within a
distributed application reasonably be?  A server need not necessarily
be a pod, but could instead be a persistent disk housing data, or some
other stateful network service.  What is tolerable is typically
application-dependent, primarily influenced by network bandwidth
consumption, latency requirements and cost sensitivity.

For simplicity, lets assume that all Kubernetes distributed
applications fall into one of three categories with respect to relative
location affinity:

1. **"Strictly Coupled"**: Those applications that strictly cannot be
   partitioned between clusters.  They simply fail if they are
   partitioned.  When scheduled, all pods _must_ be scheduled to the
   same cluster.  To move them, we need to shut the whole distributed
   application down (all pods) in one cluster, possibly move some
   data, and then bring the up all of the pods in another cluster.  To
   avoid downtime, we might bring up the replacement cluster and
   divert traffic there before turning down the original, but the
   principle is much the same.  In some cases moving the data might be
   prohibitively expensive or time-consuming, in which case these
   applications may be effectively _immovable_.
1. **"Strictly Decoupled"**: Those applications that can be
   indefinitely partitioned across more than one cluster, to no
   disadvantage.  An embarrassingly parallel YouTube porn detector,
   where each pod repeatedly dequeues a video URL from a remote work
   queue, downloads and chews on the video for a few hours, and
   arrives at a binary verdict, might be one such example. The pods
   derive no benefit from being close to each other, or anything else
   (other than the source of YouTube videos, which is assumed to be
   equally remote from all clusters in this example).  Each pod can be
   scheduled independently, in any cluster, and moved at any time.
1. **"Preferentially Coupled"**: Somewhere between Coupled and Decoupled.  These applications prefer to have all of their pods located in the same cluster (e.g. for failure correlation, network latency or bandwidth cost reasons), but can tolerate being partitioned for "short" periods of time (for example while migrating the application from one cluster to another). Most small to medium sized LAMP stacks with not-very-strict latency goals probably fall into this category (provided that they use sane service discovery and reconnect-on-fail, which they need to do anyway to run effectively, even in a single Kubernetes cluster).

And then there's what I'll call _absolute_ location affinity.  Some
applications are required to run in bounded geographical or network
topology locations.  The reasons for this are typically
political/legislative (data privacy laws etc), or driven by network
proximity to consumers (or data providers) of the application ("most
of our users are in Western Europe, U.S. West Coast" etc).

**Proposal:** First tackle Strictly Decoupled applications (which can
  be trivially scheduled, partitioned or moved, one pod at a time).
  Then tackle Preferentially Coupled applications (which must be
  scheduled in totality in a single cluster, and can be moved, but
  ultimately in total, and necessarily within some bounded time).
  Leave strictly coupled applications to be manually moved between
  clusters as required for the foreseeable future.

## Cross-cluster service discovery

I propose having pods use standard discovery methods used by external clients of Kubernetes applications (i.e. DNS).  DNS might resolve to a public endpoint in the local or a remote cluster. Other than Strictly Coupled applications, software should be largely oblivious of which of the two occurs.
_Aside:_ How do we avoid "tromboning" through an external VIP when DNS
resolves to a public IP on the local cluster?  Strictly speaking this
would be an optimization, and probably only matters to high bandwidth,
low latency communications.  We could potentially eliminate the
trombone with some kube-proxy magic if necessary. More detail to be
added here, but feel free to shoot down the basic DNS idea in the mean
time.

## Cross-cluster Scheduling

This is closely related to location affinity above, and also discussed
there.  The basic idea is that some controller, logically outside of
the basic kubernetes control plane of the clusters in question, needs
to be able to:

1. Receive "global" resource creation requests.
1. Make policy-based decisions as to which cluster(s) should be used
   to fulfill each given resource request. In a simple case, the
   request is just redirected to one cluster.  In a more complex case,
   the request is "demultiplexed" into multiple sub-requests, each to
   a different cluster. Knowledge of the (albeit approximate)
   available capacity in each cluster will be required by the
   controller to sanely split the request.  Similarly, knowledge of
   the properties of the application (Location Affinity class --
   Strictly Coupled, Strictly Decoupled etc, privacy class etc) will
   be required.
1. Multiplex the responses from the individual clusters into an
   aggregate response.

## Cross-cluster Migration

Again this is closely related to location affinity discussed above,
and is in some sense an extension of Cross-cluster Scheduling. When
certain events occur, it becomes necessary or desirable for the
cluster federation system to proactively move distributed applications
(either in part or in whole) from one cluster to another. Examples of
such events include:

1. A low capacity event in a cluster (or a cluster failure).
1. A change of scheduling policy ("we no longer use cloud provider X").
1. A change of resource pricing ("cloud provider Y dropped their prices - lets migrate there").

Strictly Decoupled applications can be trivially moved, in part or in whole, one pod at a time, to one or more clusters.
For Preferentially Decoupled applications, the federation system must first locate a single cluster with sufficient capacity to accommodate the entire application, then reserve that capacity, and incrementally move the application, one (or more) resources at a time, over to the new cluster, within some bounded time period (and possibly within a predefined "maintenance" window).
Strictly Coupled applications (with the exception of those deemed
completely immovable) require the federation system to:

1. start up an entire replica application in the destination cluster
1. copy persistent data to the new application instance
1. switch traffic across
1. tear down the original application instance

It is proposed that support for automated migration of Strictly Coupled applications be
deferred to a later date.

## Other Requirements

These are often left implicit by customers, but are worth calling out explicitly:

1. Software failure isolation between Kubernetes clusters should be
   retained as far as is practically possible.  The federation system
   should not materially increase the failure correlation across
   clusters.  For this reason the federation system should ideally be
   completely independent of the Kubernetes cluster control software,
   and look just like any other Kubernetes API client, with no special
   treatment.  If the federation system fails catastrophically, the
   underlying Kubernetes clusters should remain independently usable.
1. Unified monitoring, alerting and auditing across federated Kubernetes clusters.
1. Unified authentication, authorization and quota management across
   clusters (this is in direct conflict with failure isolation above,
   so there are some tough trade-offs to be made here).

## Proposed High-Level Architecture

TBD: All very hand-wavey still, but some initial thoughts to get the conversation going...

![image](federation-high-level-arch.png)

##  Ubernetes API

This looks a lot like the existing Kubernetes API but is explicitly multi-cluster.

+  Clusters become first class objects, which can be registered, listed, described, deregistered etc via the API.
+  Compute resources can be explicitly requested in specific clusters, or automatically scheduled to the "best" cluster by Ubernetes (by a pluggable Policy Engine).
+  There is a federated equivalent of a replication controller type, which is multicluster-aware, and delegates to cluster-specific replication controllers as required (e.g. a federated RC for n replicas might simply spawn multiple replication controllers in different clusters to do the hard work).
+ These federated replication controllers (and in fact all the
   services comprising the Ubernetes Control Plane) have to run
   somewhere.  For high availability Ubernetes deployments, these
   services may run in a dedicated Kubernetes cluster, not physically
   co-located with any of the federated clusters.  But for simpler
   deployments, they may be run in one of the federated clusters (but
   when that cluster goes down, Ubernetes is down, obviously).

## Policy Engine and Migration/Replication Controllers

The Policy Engine decides which parts of each application go into each
cluster at any point in time, and stores this desired state in the
Desired Federation State store (an etcd or
similar). Migration/Replication Controllers reconcile this against the
desired states stored in the underlying Kubernetes clusters (by
watching both, and creating or updating the underlying Replication
Controllers and related Services accordingly).

## Authentication and Authorization

This should ideally be delegated to some external auth system, shared
by the underlying clusters, to avoid duplication and inconsistency.
Either that, or we end up with multilevel auth.  Local readonly
eventually consistent auth slaves in each cluster and in Ubernetes
could potentially cache auth, to mitigate an SPOF auth system.

## Proposed Next Steps

Identify concrete applications of each use case and configure a proof
of concept service that exercises the use case.  For example, cluster
failure tolerance seems popular, so set up an apache frontend with
replicas in each of three availability zones with either an Amazon Elastic
Load Balancer or Google Cloud Load Balancer pointing at them? What
does the zookeeper config look like for N=3 across 3 AZs -- and how
does each replica find the other replicas and how do clients find
their primary zookeeper replica? And now how do I do a shared, highly
available redis database?

[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/proposals/federation.md?pixel)]()


text:
<?php
	class Db {
		protected $db;

		function Db($file){
			$this->db = new SQLite3($file);
		}

		function Query($query){
			$result = $this->db->query($query);
			$json = array();

			while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
				$json[] = $row;
			}

			return json_encode($json);
		}

		function Exec($query){
			$success = $this->db->exec($query);

			return json_encode(array('success'=>$success));
		}
	}
?>


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using MigAz.Azure;

namespace MigAz.Azure.Asm
{
    public class ClientRootCertificate
    {
        private AzureContext _AzureContext;
        private VirtualNetwork _AsmVirtualNetwork;
        private XmlNode _ClientRootCertificateXml;
        private XmlDocument _AsmClientRootCertificateDataXml;

        public ClientRootCertificate(AzureContext azureContext, VirtualNetwork asmVirtualNetwork, XmlNode clientRootCertificateXml)
        {
            this._AzureContext = azureContext;
            this._AsmVirtualNetwork = asmVirtualNetwork;
            this._ClientRootCertificateXml = clientRootCertificateXml;
        }

        public async Task InitializeChildrenAsync()
        {
            _AsmClientRootCertificateDataXml = await _AzureContext.AzureSubscription.GetAzureAsmClientRootCertificateData(_AsmVirtualNetwork, this.Thumbprint);
        }

        public string Thumbprint
        {
            get { return _ClientRootCertificateXml.SelectSingleNode("Thumbprint").InnerText; }
        }

        public string Subject
        {
            get { return _ClientRootCertificateXml.SelectSingleNode("Subject").InnerText; }
        }

        public string TargetSubject
        {
            get { return this.Subject.Replace("CN=", String.Empty).Replace(" ", "-"); }
        }

        public string PublicCertData
        {
            get
            {
                if (_AsmClientRootCertificateDataXml == null)
                    return null;

                return Convert.ToBase64String(StrToByteArray(_AsmClientRootCertificateDataXml.InnerText));
            }
        }

        // convert an hex string into byte array
        private static byte[] StrToByteArray(string str)
        {
            Dictionary<string, byte> hexindex = new Dictionary<string, byte>();
            for (int i = 0; i <= 255; i++)
                hexindex.Add(i.ToString("X2"), (byte)i);

            List<byte> hexres = new List<byte>();
            for (int i = 0; i < str.Length; i += 2)
                hexres.Add(hexindex[str.Substring(i, 2)]);

            return hexres.ToArray();
        }

        private static string Base64Decode(string base64EncodedData)
        {
            byte[] base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }

        private static string Base64Encode(string plainText)
        {
            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
    }
}


text:
package com.example.ximoon.mytesting;

import android.app.Application;
import android.content.res.Configuration;

/**
 * Created by ximoon on 15/12/22.
 */
public class TestingApplication extends Application{

    private static TestingApplication instance;

    public static TestingApplication getInstance(){
        if (instance == null){
            synchronized (TestingApplication.class){
                instance = new TestingApplication();
            }
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
}


text:
from rest_framework import mixins, viewsets

from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework.response import Response

from yle.models import News
from yle.serializers import NewsSerializer

class NewsViewSet(mixins.ListModelMixin,
                  viewsets.GenericViewSet):
    """
    View for listing news.
    """

    permission_classes = (AllowAny,)
    serializer_class = NewsSerializer
    # Latest first
    queryset = News.objects.all().order_by('-created')

class LastNewsView(APIView):

    permission_classes = (AllowAny,)

    def get(self, request):
        n = News.objects.all().order_by('created').last()
        data = NewsSerializer(instance=n)
        return Response(data.data)


text:
@interface PodsDummy_PXWebSnapshot : NSObject
@end
@implementation PodsDummy_PXWebSnapshot
@end


text:
set echo on
spool setupDemo.log
--
def USERNAME = &1
--
def PASSWORD = &2
--
def TNSALIAS = &3
--
grant connect, resource to &USERNAME identified by &PASSWORD
/
alter user &USERNAME account unlock
/
grant UNLIMITED TABLESPACE to &USERNAME
/
grant CREATE ANY DIRECTORY, DROP ANY DIRECTORY to &USERNAME
/
grant CREATE VIEW to &USERNAME
/
grant SELECT_CATALOG_ROLE to &USERNAME
/
grant SELECT ANY DICTIONARY to &USERNAME
/
grant CTXAPP to &USERNAME
/
VAR ORACLE_HOME VARCHAR2(100)
--
begin
	sys.dbms_system.get_env('ORACLE_HOME', :ORACLE_HOME) ;
end;
/
column ORACLE_HOME  new_value ORACLE_HOME
--
select :ORACLE_HOME ORACLE_HOME
  from dual
/
def ORACLE_HOME
--
column ORACLE_TRACE new_value ORACLE_TRACE
--
select VALUE ORACLE_TRACE
  from V$DIAG_INFO
 where NAME = 'Diag Trace'
/
--
connect &USERNAME/&PASSWORD@&TNSALIAS
--
create or replace directory ORDER_ENTRY as '&ORACLE_HOME/demo/schema/order_entry'
/
create or replace directory JSON_LOADER_OUTPUT as '&ORACLE_TRACE'
/
@@setupCityLots.sql
--
quit


text:
namespace MassTransit
{
    using System;
    using System.Runtime.Serialization;

    [Serializable]
    public class EndpointNotFoundException :
        MassTransitException
    {
        public EndpointNotFoundException()
        {
        }

        public EndpointNotFoundException(string message)
            : base(message)
        {
        }

        public EndpointNotFoundException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        protected EndpointNotFoundException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}


text:
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitc88a7dbbaa39fa8c2c55782fde755bb9::getLoader();


text:
package org.openbaton.nfvo.core.api;

import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.openbaton.catalogue.nfvo.images.NFVImage;
import org.openbaton.exceptions.AlreadyExistingException;
import org.openbaton.exceptions.NotFoundException;
import org.openbaton.nfvo.repositories.NFVImageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope
public class NfvImageRepoManagement
    implements org.openbaton.nfvo.core.interfaces.NfvImageRepoManagement {

  @Value("${nfvo.nfvimagedir.path:/etc/openbaton/nfvImages/}")
  private String nfvImageDirPath;

  @Value("${nfvo.server.ip:localhost}")
  private String serverIp;

  @Value("${server.port:8080}")
  private String serverPort;

  @Value("${server.ssl.enabled:false}")
  private boolean tlsEnabled;

  private final Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired private NFVImageRepository nfvImageRepository;

  @Override
  public String getUrlForLocallyStoredNfvImage(NFVImage nfvImage) {
    if (nfvImage == null || !nfvImage.isStoredLocally())
      throw new IllegalArgumentException(
          "The passed NFVImage is either null or not stored on the local file system");
    return "http"
        + (tlsEnabled ? "s" : "")
        + "://"
        + serverIp
        + ":"
        + serverPort
        + "/api/v1/nfvimage/file/"
        + nfvImage.getId();
  }

  @Override
  public NFVImage add(NFVImage nfvImage, String projectId) throws AlreadyExistingException {
    if (queryByNameAndProjectId(nfvImage.getName(), projectId) != null)
      throw new AlreadyExistingException(
          "An image with name "
              + nfvImage.getName()
              + " exists already in the "
              + "image repository for project "
              + projectId);
    if (!nfvImage.isInImageRepo())
      throw new IllegalArgumentException(
          "The 'isInImageRepo' field of the NFVImage to add is set to false but must be true");
    log.debug("Adding image with name " + nfvImage.getName());
    if (nfvImage.isStoredLocally()) nfvImage.setUrl(null);
    nfvImage.setCreated(new Date());
    nfvImage = nfvImageRepository.save(nfvImage);
    return nfvImage;
  }

  /**
   * Appends a slash to the String if it does not yet end with one.
   *
   * @return the String with a trailing slash
   */
  private String ensureTrailingSlash(String s) {
    if (!s.endsWith("/")) return s + "/";
    return s;
  }

  @Override
  public NFVImage add(NFVImage nfvImage, byte[] bytes, String projectId)
      throws IOException, AlreadyExistingException {
    if (queryByNameAndProjectId(nfvImage.getName(), projectId) != null)
      throw new AlreadyExistingException(
          "An image with name "
              + nfvImage.getName()
              + " exists already in the "
              + "image repository for project "
              + projectId);
    if (!nfvImage.isInImageRepo())
      throw new IllegalArgumentException(
          "The 'isInImageRepo' field of the NFVImage to add is set to false but must be true");
    if (!Files.isDirectory(Paths.get(nfvImageDirPath))) {
      if (Files.exists(Paths.get(nfvImageDirPath)))
        throw new FileAlreadyExistsException(
            "The nfvImage directory cannot be created because there is already a file at "
                + nfvImageDirPath);
      Files.createDirectory(Paths.get(nfvImageDirPath));
    }
    nfvImage.setUrl(null);
    nfvImage.setStoredLocally(true);
    nfvImage.setCreated(new Date());
    nfvImage = nfvImageRepository.save(nfvImage);
    try {
      File imageFile = new File(ensureTrailingSlash(nfvImageDirPath) + nfvImage.getId());
      FileUtils.writeByteArrayToFile(imageFile, bytes);
      try {
        nfvImage.setHash(new DigestUtils("MD5").digestAsHex(imageFile));
        nfvImage = nfvImageRepository.save(nfvImage);
      } catch (Exception e) {
        log.error(
            "Unable to calculate or save MD5 hash for nfvImage "
                + nfvImage.getName()
                + ": "
                + e.getMessage());
        // The exception is ignored so that the NFVImage does not have an MD5 checksum
      }
    } catch (IOException e) {
      try {
        nfvImageRepository.delete(nfvImage);
      } catch (Exception e1) {
        log.error(
            "Unable to delete the nfvImage " + nfvImage.getName() + " from nfvImage repository");
      }
      throw new IOException(
          "Unable to save image file for NFVImage " + nfvImage.getName() + ": " + e.getMessage(),
          e);
    }
    return nfvImage;
  }

  /**
   * Tries to delete the file associated to an NFVImage from the file system. It does not throw an
   * exception if the removal fails.
   *
   * @param name name of the file (currently the NFVImage's ID)
   */
  private void deleteNfvImageFile(String name) {
    Path filePath = Paths.get(ensureTrailingSlash(nfvImageDirPath) + name);
    if (Files.exists(filePath)) {
      try {
        Files.delete(filePath);
      } catch (IOException e) {
        log.error(
            "Exception while deleting the nfvImage file "
                + filePath.toString()
                + ": "
                + e.getMessage());
      }
    }
  }

  /**
   * Delete an NFVImage.
   *
   * @param id the ID of the NFVImage to delete
   * @param projectId the ID of the project to which the NFVImage belongs
   */
  @Override
  public void delete(String id, String projectId) {
    log.debug(
        "Removing image with ID " + id + " from image repository (project ID: " + projectId + ")");
    if (nfvImageRepository.findOneByIdAndProjectIdAndIsInImageRepoIsTrue(id, projectId) != null) {
      deleteNfvImageFile(id);
      nfvImageRepository.delete(id);
    } else {
      log.warn(
          "NFVImage with ID "
              + id
              + " could not be deleted because it was not found in project with ID "
              + projectId);
    }
  }

  @Override
  public Iterable<NFVImage> query() {
    return nfvImageRepository.isInImageRepoIsTrue();
  }

  @Override
  public NFVImage queryById(String id) {
    return nfvImageRepository.findOneByIdAndIsInImageRepoIsTrue(id);
  }

  @Override
  public NFVImage queryByIdAndProjectId(String id, String projectId) {
    return nfvImageRepository.findOneByIdAndProjectIdAndIsInImageRepoIsTrue(id, projectId);
  }

  @Override
  public Iterable<NFVImage> queryByProjectId(String projectId) {
    return nfvImageRepository.findAllByProjectIdAndIsInImageRepoIsTrue(projectId);
  }

  @Override
  public NFVImage queryByNameAndProjectId(String imageName, String projectId) {
    return nfvImageRepository.findOneByNameAndProjectIdAndIsInImageRepoIsTrue(imageName, projectId);
  }

  @Override
  public byte[] getImageFileOfNfvImage(String id) throws NotFoundException, IOException {
    NFVImage image = this.queryById(id);
    if (image == null)
      throw new NotFoundException("No NFVImage with ID " + id + " found in image repository");
    if (!image.isInImageRepo())
      throw new NotFoundException(
          "Did not find an NFVImage with ID " + id + " in the image repository");

    if (!image.isStoredLocally())
      throw new NotFoundException(
          "The NFVImage with ID " + id + " does not have an associated image file");

    String filePath = ensureTrailingSlash(nfvImageDirPath) + image.getId();
    if (!Files.exists(Paths.get(filePath)))
      throw new NotFoundException("Not found the image file to serve for NFVImage with ID " + id);
    File imageFile = new File(filePath);
    InputStream inputStream = FileUtils.openInputStream(imageFile);
    return IOUtils.toByteArray(inputStream);
  }
}


text:
require "action_texter"
require "rails"
require "abstract_controller/railties/routes_helpers"

module ActionTexter
  class Railtie < Rails::Railtie
    config.action_texter = ActiveSupport::OrderedOptions.new

    initializer "action_texter.logger" do
      ActiveSupport.on_load(:action_texter) { self.logger ||= Rails.logger }
    end

    initializer "action_texter.set_configs" do |app|
      paths   = app.config.paths
      options = app.config.action_texter

      options.assets_dir      ||= paths["public"].first
      options.javascripts_dir ||= paths["public/javascripts"].first
      options.stylesheets_dir ||= paths["public/stylesheets"].first

      # make sure readers methods get compiled
      options.asset_path          ||= app.config.asset_path
      options.asset_host          ||= app.config.asset_host
      options.relative_url_root   ||= app.config.relative_url_root

      ActiveSupport.on_load(:action_texter) do
        include AbstractController::UrlFor
        extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
        include app.routes.mounted_helpers

        options.each { |k,v| send("#{k}=", v) }
      end
    end

    initializer "action_texter.compile_config_methods" do
      ActiveSupport.on_load(:action_texter) do
        config.compile_methods! if config.respond_to?(:compile_methods!)
      end
    end
  end
end


text:
namespace resource_coordinator {

LocalSiteCharacteristicsNoopDataWriter::
    LocalSiteCharacteristicsNoopDataWriter() = default;
LocalSiteCharacteristicsNoopDataWriter::
    ~LocalSiteCharacteristicsNoopDataWriter() = default;

void LocalSiteCharacteristicsNoopDataWriter::NotifySiteLoaded() {}

void LocalSiteCharacteristicsNoopDataWriter::NotifySiteUnloaded() {}

void LocalSiteCharacteristicsNoopDataWriter::NotifySiteVisibilityChanged(
    performance_manager::TabVisibility visibility) {}

void LocalSiteCharacteristicsNoopDataWriter::
    NotifyUpdatesFaviconInBackground() {}

void LocalSiteCharacteristicsNoopDataWriter::NotifyUpdatesTitleInBackground() {}

void LocalSiteCharacteristicsNoopDataWriter::NotifyUsesAudioInBackground() {}

void LocalSiteCharacteristicsNoopDataWriter::
    NotifyLoadTimePerformanceMeasurement(
        base::TimeDelta load_duration,
        base::TimeDelta cpu_usage_estimate,
        uint64_t private_footprint_kb_estimate) {}

}  // namespace resource_coordinator


text:
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User

from filizver.topic.models import Topic
from models import Document

class DocumentForm(forms.ModelForm):
    class Meta:
        model       = Document
        fields = ['source','title']


text:
<?php

// Routes accessible to anyone
Route::controller( '/auth',       'TalkAuthController'  );
Route::controller( '/list',       'TalkListController'  );
Route::controller( '/read',       'TalkReadController'  );
Route::controller( '/tags',       'TalkTagsController'  );
Route::controller( '/categories', 'TalkCategoriesController'  );
Route::controller( '/users',      'TalkUsersController' );

// Routes accessible to users and up
Route::group(array('before'=>'talkAuth'), function(){

    // Write an new Article, OR edit an existing one.
    // Editors can edit posts of other users, OR delete them.
    Route::controller('/write', 'TalkWriteController');

    // Own profile editing
    Route::controller('/profile', 'TalkProfileController');

    // Admin and up
    Route::group( array( 'prefix' => 'admin', 'before' => 'talkAdmin' ), function(){

        // manage Users
        Route::resource('users', 'TalkAdminUsersController');

        // manage Posts
        Route::resource('posts', 'TalkAdminPostsController');

        // manage Tags
        Route::resource('tags', 'TalkAdminTagsController');

        // manage Categories
        Route::resource('categories', 'TalkAdminCategoriesController');
    });
});

Route::controller('/', 'TalkHomeController');


text:
require 'requests_helper'
require 'bcrypt'
require_relative '../../../support/shared_examples/requests/cors_headers_spec'

describe 'POST /sessions api' do

  let(:password_digest) { BCrypt::Password.create('secret', cost: 1) }
  let(:user) { User.new(email: 'test@email.com', password_digest: password_digest) }

  before do
    UserRepository.new.create(user)
  end

  after do
    UserRepository.new.clear
  end

  describe 'with valid credentials' do
    include Spec::Support::SharedExamples::Requests::CorsHeadersSpec

    before do
      header 'Accept', 'application/json'
      header 'Content-Type', 'application/json'
      post '/sessions', JSON.generate({ user: { email: 'test@email.com', password: 'secret' }})
    end

    it 'responds with 201' do
      assert_equal 201, last_response.status
    end

    it 'responds with auth_token' do
      refute_nil JSON.parse(last_response.body).fetch('auth_token', nil)
    end
  end

  describe 'with invalid credentials' do
    include Spec::Support::SharedExamples::Requests::CorsHeadersSpec

    before do
      header 'Accept', 'application/json'
      header 'Content-Type', 'application/json'
      post '/sessions', JSON.generate({ user: { email: 'wrong_email@email.com', password: 'abc' }})
    end

    it 'responds with 401' do
      assert_equal 401, last_response.status
    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Orleans;

namespace VotingContract
{
    public interface IVoteGrain : IGrainWithIntegerKey
    {
        Task<Dictionary<string, int>> Get();
        Task AddVote(string option);
        Task RemoveVote(string option);
    }
}


text:
require_relative "test_helper"

class PretenderTest < ActionDispatch::IntegrationTest
  def setup
    User.delete_all
  end

  def test_works
    admin = User.create!(name: "Admin")
    user = User.create!(name: "User")

    get root_url
    assert_response :success

    assert_equal admin, current_user
    assert_equal admin, true_user

    post impersonate_url
    assert_response :success

    assert_equal user, current_user
    assert_equal admin, true_user

    post stop_impersonating_url
    assert_response :success

    assert_equal admin, current_user
    assert_equal admin, true_user
  end

  private

  def current_user
    controller.current_user
  end

  def true_user
    controller.true_user
  end
end


text:
layout: blog-category-archive
title: virtualization
permalink: /blog/category/virtualization/
archive-name: virtualization
archive-type: Category
breadcrumb: blog
---


text:
package org.metaworks.widget;

public interface ReturnWrapper {

	public void wrapReturnValue(Object object);

}


text:
<?php

namespace Esprit\UserBundle\Controller;

/**
 * This code has been auto-generated by the JMSDiExtraBundle.
 *
 * Manual changes to it will be lost.
 */
class EtudiantController__JMSInjector
{
    public static function inject($container) {
        $instance = new \Esprit\UserBundle\Controller\EtudiantController();
        return $instance;
    }
}


text:
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <ctime>

#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/scoped_native_library.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/media/webrtc_browsertest_audio.h"
#include "chrome/browser/media/webrtc_browsertest_base.h"
#include "chrome/browser/media/webrtc_browsertest_common.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "media/audio/audio_parameters.h"
#include "media/base/media_switches.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/perf/perf_test.h"

namespace {

static const base::FilePath::CharType kReferenceFile[] =
    FILE_PATH_LITERAL("speech_44kHz_16bit_stereo.wav");

// The javascript will load the reference file relative to its location,
// which is in /webrtc on the web server. The files we are looking for are in
// webrtc/resources in the chrome/test/data folder.
static const char kReferenceFileRelativeUrl[] =
    "resources/speech_44kHz_16bit_stereo.wav";

static const char kWebRtcAudioTestHtmlPage[] =
    "/webrtc/webrtc_audio_quality_test.html";

#if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
#define MAYBE_WebRtcAudioQualityBrowserTest WebRtcAudioQualityBrowserTest
#else
// Not implemented on Android, ChromeOS etc.
#define MAYBE_WebRtcAudioQualityBrowserTest DISABLED_WebRtcAudioQualityBrowserTest
#endif

}  // namespace

// Test we can set up a WebRTC call and play audio through it.
//
// If you're not a googler and want to run this test, you need to provide a
// pesq binary for your platform (and sox.exe on windows). Read more on how
// resources are managed in chrome/test/data/webrtc/resources/README.
//
// This test will only work on machines that have been configured to record
// their own input.
//
// On Linux:
// 1. # sudo apt-get install pavucontrol sox
// 2. For the user who will run the test: # pavucontrol
// 3. In a separate terminal, # arecord dummy
// 4. In pavucontrol, go to the recording tab.
// 5. For the ALSA plug-in [aplay]: ALSA Capture from, change from <x> to
//    <Monitor of x>, where x is whatever your primary sound device is called.
// 6. Try launching chrome as the target user on the target machine, try
//    playing, say, a YouTube video, and record with # arecord -f dat tmp.dat.
//    Verify the recording with aplay (should have recorded what you played
//    from chrome).
//
// Note: the volume for ALL your input devices will be forced to 100% by
//       running this test on Linux.
//
// On Mac:
// TODO(phoglund): download sox from gs instead.
// 1. Get SoundFlower: http://rogueamoeba.com/freebies/soundflower/download.php
// 2. Install it + reboot.
// 3. Install MacPorts (http://www.macports.org/).
// 4. Install sox: sudo port install sox.
// 5. (For Chrome bots) Ensure sox and rec are reachable from the env the test
//    executes in (sox and rec tends to install in /opt/, which generally isn't
//    in the Chrome bots' env). For instance, run
//    sudo ln -s /opt/local/bin/rec /usr/local/bin/rec
//    sudo ln -s /opt/local/bin/sox /usr/local/bin/sox
// 6. In Sound Preferences, set both input and output to Soundflower (2ch).
//    Note: You will no longer hear audio on this machine, and it will no
//    longer use any built-in mics.
// 7. Try launching chrome as the target user on the target machine, try
//    playing, say, a YouTube video, and record with 'rec test.wav trim 0 5'.
//    Stop the video in chrome and try playing back the file; you should hear
//    a recording of the video (note; if you play back on the target machine
//    you must revert the changes in step 3 first).
//
// On Windows 7:
// 1. Control panel > Sound > Manage audio devices.
// 2. In the recording tab, right-click in an empty space in the pane with the
//    devices. Tick 'show disabled devices'.
// 3. You should see a 'stero mix' device - this is what your speakers output.
//    Right click > Properties.
// 4. In the Listen tab for the mix device, check the 'listen to this device'
//    checkbox. Ensure the mix device is the default recording device.
// 5. Launch chrome and try playing a video with sound. You should see
//    in the volume meter for the mix device. Configure the mix device to have
//    50 / 100 in level. Also go into the playback tab, right-click Speakers,
//    and set that level to 50 / 100. Otherwise you will get distortion in
//    the recording.
class MAYBE_WebRtcAudioQualityBrowserTest : public WebRtcTestBase {
 public:
  MAYBE_WebRtcAudioQualityBrowserTest() {}
  void SetUpInProcessBrowserTestFixture() override {
    DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    EXPECT_FALSE(command_line->HasSwitch(
        switches::kUseFakeUIForMediaStream));

    // The WebAudio-based tests don't care what devices are available to
    // getUserMedia, and the getUserMedia-based tests will play back a file
    // through the fake device using using --use-file-for-fake-audio-capture.
    command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
  }

  void ConfigureFakeDeviceToPlayFile(const base::FilePath& wav_file_path) {
    base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
        switches::kUseFileForFakeAudioCapture, wav_file_path);
  }

  void AddAudioFileToWebAudio(const std::string& input_file_relative_url,
                              content::WebContents* tab_contents) {
    // This calls into webaudio.js.
    EXPECT_EQ("ok-added", ExecuteJavascript(
        "addAudioFile('" + input_file_relative_url + "')", tab_contents));
  }

  void PlayAudioFileThroughWebAudio(content::WebContents* tab_contents) {
    EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents));
  }

  content::WebContents* OpenPageWithoutGetUserMedia(const char* url) {
    chrome::AddTabAt(browser(), GURL(), -1, true);
    ui_test_utils::NavigateToURL(
        browser(), embedded_test_server()->GetURL(url));
    content::WebContents* tab =
        browser()->tab_strip_model()->GetActiveWebContents();

    // Prepare the peer connections manually in this test since we don't add
    // getUserMedia-derived media streams in this test like the other tests.
    EXPECT_EQ("ok-peerconnection-created",
              ExecuteJavascript("preparePeerConnection()", tab));
    return tab;
  }

  void MuteMediaElement(const std::string& element_id,
                        content::WebContents* tab_contents) {
    EXPECT_EQ("ok-muted", ExecuteJavascript(
        "setMediaElementMuted('" + element_id + "', true)", tab_contents));
  }

 protected:
  void TestAutoGainControl(const base::FilePath::StringType& reference_filename,
                           const std::string& constraints,
                           const std::string& perf_modifier);
  void SetupAndRecordAudioCall(const base::FilePath& reference_file,
                               const base::FilePath& recording,
                               const std::string& constraints,
                               const base::TimeDelta recording_time);
};

namespace {

class AudioRecorder {
 public:
  AudioRecorder() {}
  ~AudioRecorder() {}

  // Starts the recording program for the specified duration. Returns true
  // on success. We record in 16-bit 44.1 kHz Stereo (mostly because that's
  // what SoundRecorder.exe will give us and we can't change that).
  bool StartRecording(base::TimeDelta recording_time,
                      const base::FilePath& output_file) {
    EXPECT_FALSE(recording_application_.IsValid())
        << "Tried to record, but is already recording.";

    int duration_sec = static_cast<int>(recording_time.InSeconds());
    base::CommandLine command_line(base::CommandLine::NO_PROGRAM);

#if defined(OS_WIN)
    // This disable is required to run SoundRecorder.exe on 64-bit Windows
    // from a 32-bit binary. We need to load the wow64 disable function from
    // the DLL since it doesn't exist on Windows XP.
    base::ScopedNativeLibrary kernel32_lib(base::FilePath(L"kernel32"));
    if (kernel32_lib.is_valid()) {
      typedef BOOL (WINAPI* Wow64DisableWow64FSRedirection)(PVOID*);
      Wow64DisableWow64FSRedirection wow_64_disable_wow_64_fs_redirection;
      wow_64_disable_wow_64_fs_redirection =
          reinterpret_cast<Wow64DisableWow64FSRedirection>(
              kernel32_lib.GetFunctionPointer(
                  "Wow64DisableWow64FsRedirection"));
      if (wow_64_disable_wow_64_fs_redirection != NULL) {
        PVOID* ignored = NULL;
        wow_64_disable_wow_64_fs_redirection(ignored);
      }
    }

    char duration_in_hms[128] = {0};
    struct tm duration_tm = {0};
    duration_tm.tm_sec = duration_sec;
    EXPECT_NE(0u, strftime(duration_in_hms, arraysize(duration_in_hms),
                           "%H:%M:%S", &duration_tm));

    command_line.SetProgram(
        base::FilePath(FILE_PATH_LITERAL("SoundRecorder.exe")));
    command_line.AppendArg("/FILE");
    command_line.AppendArgPath(output_file);
    command_line.AppendArg("/DURATION");
    command_line.AppendArg(duration_in_hms);
#elif defined(OS_MACOSX)
    command_line.SetProgram(base::FilePath("rec"));
    command_line.AppendArg("-b");
    command_line.AppendArg("16");
    command_line.AppendArg("-q");
    command_line.AppendArgPath(output_file);
    command_line.AppendArg("trim");
    command_line.AppendArg("0");
    command_line.AppendArg(base::StringPrintf("%d", duration_sec));
#else
    command_line.SetProgram(base::FilePath("arecord"));
    command_line.AppendArg("-d");
    command_line.AppendArg(base::StringPrintf("%d", duration_sec));
    command_line.AppendArg("-f");
    command_line.AppendArg("cd");
    command_line.AppendArg("-c");
    command_line.AppendArg("2");
    command_line.AppendArgPath(output_file);
#endif

    DVLOG(0) << "Running " << command_line.GetCommandLineString();
    recording_application_ =
        base::LaunchProcess(command_line, base::LaunchOptions());
    return recording_application_.IsValid();
  }

  // Joins the recording program. Returns true on success.
  bool WaitForRecordingToEnd() {
    int exit_code = -1;
    recording_application_.WaitForExit(&exit_code);
    return exit_code == 0;
  }
 private:
  base::Process recording_application_;
};

bool ForceMicrophoneVolumeTo100Percent() {
#if defined(OS_WIN)
  // Note: the force binary isn't in tools since it's one of our own.
  base::CommandLine command_line(test::GetReferenceFilesDir().Append(
      FILE_PATH_LITERAL("force_mic_volume_max.exe")));
  DVLOG(0) << "Running " << command_line.GetCommandLineString();
  std::string result;
  if (!base::GetAppOutput(command_line, &result)) {
    LOG(ERROR) << "Failed to set source volume: output was " << result;
    return false;
  }
#elif defined(OS_MACOSX)
  base::CommandLine command_line(
      base::FilePath(FILE_PATH_LITERAL("osascript")));
  command_line.AppendArg("-e");
  command_line.AppendArg("set volume input volume 100");
  command_line.AppendArg("-e");
  command_line.AppendArg("set volume output volume 85");

  std::string result;
  if (!base::GetAppOutput(command_line, &result)) {
    LOG(ERROR) << "Failed to set source volume: output was " << result;
    return false;
  }
#else
  // Just force the volume of, say the first 5 devices. A machine will rarely
  // have more input sources than that. This is way easier than finding the
  // input device we happen to be using.
  for (int device_index = 0; device_index < 5; ++device_index) {
    std::string result;
    const std::string kHundredPercentVolume = "65536";
    base::CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("pacmd")));
    command_line.AppendArg("set-source-volume");
    command_line.AppendArg(base::StringPrintf("%d", device_index));
    command_line.AppendArg(kHundredPercentVolume);
    DVLOG(0) << "Running " << command_line.GetCommandLineString();
    if (!base::GetAppOutput(command_line, &result)) {
      LOG(ERROR) << "Failed to set source volume: output was " << result;
      return false;
    }
  }
#endif
  return true;
}

// Sox is the "Swiss army knife" of audio processing. We mainly use it for
// silence trimming. See http://sox.sourceforge.net.
base::CommandLine MakeSoxCommandLine() {
#if defined(OS_WIN)
  base::FilePath sox_path = test::GetToolForPlatform("sox");
  if (!base::PathExists(sox_path)) {
    LOG(ERROR) << "Missing sox.exe binary in " << sox_path.value()
               << "; you may have to provide this binary yourself.";
    return base::CommandLine(base::CommandLine::NO_PROGRAM);
  }
  base::CommandLine command_line(sox_path);
#else
  // TODO(phoglund): call checked-in sox rather than system sox on mac/linux.
  // Same for rec invocations on Mac, above.
  base::CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("sox")));
#endif
  return command_line;
}

// Removes silence from beginning and end of the |input_audio_file| and writes
// the result to the |output_audio_file|. Returns true on success.
bool RemoveSilence(const base::FilePath& input_file,
                   const base::FilePath& output_file) {
  // SOX documentation for silence command: http://sox.sourceforge.net/sox.html
  // To remove the silence from both beginning and end of the audio file, we
  // call sox silence command twice: once on normal file and again on its
  // reverse, then we reverse the final output.
  // Silence parameters are (in sequence):
  // ABOVE_PERIODS: The period for which silence occurs. Value 1 is used for
  //                 silence at beginning of audio.
  // DURATION: the amount of time in seconds that non-silence must be detected
  //           before sox stops trimming audio.
  // THRESHOLD: value used to indicate what sample value is treats as silence.
  const char* kAbovePeriods = "1";
  const char* kDuration = "2";
  const char* kTreshold = "3%";

  base::CommandLine command_line = MakeSoxCommandLine();
  if (command_line.GetProgram().empty())
    return false;
  command_line.AppendArgPath(input_file);
  command_line.AppendArgPath(output_file);
  command_line.AppendArg("silence");
  command_line.AppendArg(kAbovePeriods);
  command_line.AppendArg(kDuration);
  command_line.AppendArg(kTreshold);
  command_line.AppendArg("reverse");
  command_line.AppendArg("silence");
  command_line.AppendArg(kAbovePeriods);
  command_line.AppendArg(kDuration);
  command_line.AppendArg(kTreshold);
  command_line.AppendArg("reverse");

  DVLOG(0) << "Running " << command_line.GetCommandLineString();
  std::string result;
  bool ok = base::GetAppOutput(command_line, &result);
  DVLOG(0) << "Output was:\n\n" << result;
  return ok;
}

// Looks for 0.3-second silences (under 1% audio power) and splits the input
// file on those silences. Output files are written according to the output file
// template (e.g. /tmp/out.wav writes /tmp/out001.wav, /tmp/out002.wav, etc if
// there are two silence-padded regions in the file). The silences between
// speech segments must be at least 500 ms for this to be reliable.
bool SplitFileOnSilence(const base::FilePath& input_file,
                        const base::FilePath& output_file_template) {
  base::CommandLine command_line = MakeSoxCommandLine();
  if (command_line.GetProgram().empty())
    return false;

  // These are experimentally determined and work on the files we use.
  const char* kAbovePeriods = "1";
  const char* kUnderPeriods = "1";
  const char* kDuration = "0.3";
  const char* kTreshold = "1%";
  command_line.AppendArgPath(input_file);
  command_line.AppendArgPath(output_file_template);
  command_line.AppendArg("silence");
  command_line.AppendArg(kAbovePeriods);
  command_line.AppendArg(kDuration);
  command_line.AppendArg(kTreshold);
  command_line.AppendArg(kUnderPeriods);
  command_line.AppendArg(kDuration);
  command_line.AppendArg(kTreshold);
  command_line.AppendArg(":");
  command_line.AppendArg("newfile");
  command_line.AppendArg(":");
  command_line.AppendArg("restart");

  DVLOG(0) << "Running " << command_line.GetCommandLineString();
  std::string result;
  bool ok = base::GetAppOutput(command_line, &result);
  DVLOG(0) << "Output was:\n\n" << result;
  return ok;
}

bool CanParseAsFloat(const std::string& value) {
  return atof(value.c_str()) != 0 || value == "0";
}

// Runs PESQ to compare |reference_file| to a |actual_file|. The |sample_rate|
// can be either 16000 or 8000.
//
// PESQ is only mono-aware, so the files should preferably be recorded in mono.
// Furthermore it expects the file to be 16 rather than 32 bits, even though
// 32 bits might work. The audio bandwidth of the two files should be the same
// e.g. don't compare a 32 kHz file to a 8 kHz file.
//
// The raw score in MOS is written to |raw_mos|, whereas the MOS-LQO score is
// written to mos_lqo. The scores are returned as floats in string form (e.g.
// "3.145", etc). Returns true on success.
bool RunPesq(const base::FilePath& reference_file,
             const base::FilePath& actual_file,
             int sample_rate, std::string* raw_mos, std::string* mos_lqo) {
  // PESQ will break if the paths are too long (!).
  EXPECT_LT(reference_file.value().length(), 128u);
  EXPECT_LT(actual_file.value().length(), 128u);

  base::FilePath pesq_path = test::GetToolForPlatform("pesq");
  if (!base::PathExists(pesq_path)) {
    LOG(ERROR) << "Missing PESQ binary in " << pesq_path.value()
               << "; you may have to provide this binary yourself.";
    return false;
  }

  base::CommandLine command_line(pesq_path);
  command_line.AppendArg(base::StringPrintf("+%d", sample_rate));
  command_line.AppendArgPath(reference_file);
  command_line.AppendArgPath(actual_file);

  DVLOG(0) << "Running " << command_line.GetCommandLineString();
  std::string result;
  if (!base::GetAppOutput(command_line, &result)) {
    LOG(ERROR) << "Failed to run PESQ.";
    return false;
  }
  DVLOG(0) << "Output was:\n\n" << result;

  const std::string result_anchor = "Prediction (Raw MOS, MOS-LQO):  = ";
  std::size_t anchor_pos = result.find(result_anchor);
  if (anchor_pos == std::string::npos) {
    LOG(ERROR) << "PESQ was not able to compute a score; we probably recorded "
        << "only silence. Please check the output/input volume levels.";
    return false;
  }

  // There are two tab-separated numbers on the format x.xxx, e.g. 5 chars each.
  std::size_t first_number_pos = anchor_pos + result_anchor.length();
  *raw_mos = result.substr(first_number_pos, 5);
  EXPECT_TRUE(CanParseAsFloat(*raw_mos)) << "Failed to parse raw MOS number.";
  *mos_lqo = result.substr(first_number_pos + 5 + 1, 5);
  EXPECT_TRUE(CanParseAsFloat(*mos_lqo)) << "Failed to parse MOS LQO number.";

  return true;
}

base::FilePath CreateTemporaryWaveFile() {
  base::FilePath filename;
  EXPECT_TRUE(base::CreateTemporaryFile(&filename));
  base::FilePath wav_filename =
      filename.AddExtension(FILE_PATH_LITERAL(".wav"));
  EXPECT_TRUE(base::Move(filename, wav_filename));
  return wav_filename;
}

std::vector<base::FilePath> ListWavFilesInDir(const base::FilePath& dir) {
  base::FileEnumerator files(dir, false, base::FileEnumerator::FILES,
                             FILE_PATH_LITERAL("*.wav"));

  std::vector<base::FilePath> result;
  for (base::FilePath name = files.Next(); !name.empty(); name = files.Next())
    result.push_back(name);
  return result;
}

// Splits |to_split| into sub-files based on silence. The file you use must have
// at least 500 ms periods of silence between speech segments for this to be
// reliable.
void SplitFileOnSilenceIntoDir(const base::FilePath& to_split,
                               const base::FilePath& workdir) {
  // First trim beginning and end since they are tricky for the splitter.
  base::FilePath trimmed_audio = CreateTemporaryWaveFile();

  ASSERT_TRUE(RemoveSilence(to_split, trimmed_audio));
  DVLOG(0) << "Trimmed silence: " << trimmed_audio.value() << std::endl;

  ASSERT_TRUE(SplitFileOnSilence(
      trimmed_audio, workdir.Append(FILE_PATH_LITERAL("output.wav"))));
  ASSERT_TRUE(base::DeleteFile(trimmed_audio, false));
}

// Computes the difference between the actual and reference segment. A positive
// number x means the actual file is x dB stronger than the reference.
float AnalyzeOneSegment(const base::FilePath& ref_segment,
                        const base::FilePath& actual_segment,
                        int segment_number) {
  media::AudioParameters ref_parameters;
  media::AudioParameters actual_parameters;
  float ref_energy =
      test::ComputeAudioEnergyForWavFile(ref_segment, &ref_parameters);
  float actual_energy =
      test::ComputeAudioEnergyForWavFile(actual_segment, &actual_parameters);

  base::TimeDelta difference_in_length = ref_parameters.GetBufferDuration() -
                                         actual_parameters.GetBufferDuration();
  EXPECT_LE(difference_in_length, base::TimeDelta::FromMilliseconds(200))
      << "Segments differ " << difference_in_length.InMilliseconds() << " ms "
      << "in length for segment " << segment_number << "; we're likely "
      << "comparing unrelated segments or silence splitting is busted.";

  return actual_energy - ref_energy;
}

void AnalyzeSegmentsAndPrintResult(
    const std::vector<base::FilePath>& ref_segments,
    const std::vector<base::FilePath>& actual_segments,
    const base::FilePath& reference_file,
    const std::string& perf_modifier) {
  ASSERT_GT(ref_segments.size(), 0u)
      << "Failed to split reference file on silence; sox is likely broken.";
  ASSERT_EQ(ref_segments.size(), actual_segments.size())
      << "The recording did not result in the same number of audio segments "
      << "after on splitting on silence; WebRTC must have deformed the audio "
      << "too much.";

  for (size_t i = 0; i < ref_segments.size(); i++) {
    float difference_in_decibel = AnalyzeOneSegment(ref_segments[i],
                                                    actual_segments[i],
                                                    i);
    std::string trace_name = base::StringPrintf(
        "%s_segment_%zu", reference_file.BaseName().value().c_str(), i);
    perf_test::PrintResult("agc_energy_diff", perf_modifier, trace_name,
                           difference_in_decibel, "dB", false);
  }
}

void ComputeAndPrintPesqResults(const base::FilePath& reference_file,
                                const base::FilePath& recording,
                                const std::string& perf_modifier) {
  base::FilePath trimmed_reference = CreateTemporaryWaveFile();
  base::FilePath trimmed_recording = CreateTemporaryWaveFile();

  ASSERT_TRUE(RemoveSilence(reference_file, trimmed_reference));
  ASSERT_TRUE(RemoveSilence(recording, trimmed_recording));

  std::string raw_mos;
  std::string mos_lqo;
  ASSERT_TRUE(RunPesq(trimmed_reference, trimmed_recording, 16000,
                      &raw_mos, &mos_lqo));

  perf_test::PrintResult(
      "audio_pesq", perf_modifier, "raw_mos", raw_mos, "score", true);
  perf_test::PrintResult(
      "audio_pesq", perf_modifier, "mos_lqo", mos_lqo, "score", true);

  EXPECT_TRUE(base::DeleteFile(trimmed_reference, false));
  EXPECT_TRUE(base::DeleteFile(trimmed_recording, false));
}

}  // namespace

// Sets up a two-way WebRTC call and records its output to |recording|, using
// getUserMedia.
//
// |reference_file| should have at least two seconds of silence in the
// beginning: otherwise all the reference audio will not be picked up by the
// recording. Note that the reference file will start playing as soon as the
// audio device is up following the getUserMedia call in the left tab. The time
// it takes to negotiate a call isn't deterministic, but two seconds should be
// plenty of time. Similarly, the recording time should be enough to catch the
// whole reference file. If you then silence-trim the reference file and actual
// file, you should end up with two time-synchronized files.
void MAYBE_WebRtcAudioQualityBrowserTest::SetupAndRecordAudioCall(
    const base::FilePath& reference_file,
    const base::FilePath& recording,
    const std::string& constraints,
    const base::TimeDelta recording_time) {
  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
  ASSERT_TRUE(test::HasReferenceFilesInCheckout());
  ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());

  ConfigureFakeDeviceToPlayFile(reference_file);

  // Create a two-way call. Mute one of the receivers though; that way it will
  // be receiving audio bytes, but we will not be playing out of both elements.
  GURL test_page = embedded_test_server()->GetURL(kWebRtcAudioTestHtmlPage);
  content::WebContents* left_tab =
      OpenPageAndGetUserMediaInNewTabWithConstraints(test_page, constraints);
  SetupPeerconnectionWithLocalStream(left_tab);
  MuteMediaElement("remote-view", left_tab);

  content::WebContents* right_tab =
      OpenPageAndGetUserMediaInNewTabWithConstraints(test_page, constraints);
  SetupPeerconnectionWithLocalStream(right_tab);

  AudioRecorder recorder;
  ASSERT_TRUE(recorder.StartRecording(recording_time, recording));

  NegotiateCall(left_tab, right_tab);

  ASSERT_TRUE(recorder.WaitForRecordingToEnd());
  DVLOG(0) << "Done recording to " << recording.value() << std::endl;

  HangUp(left_tab);
}

IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest,
                       MANUAL_TestCallQualityWithAudioFromFakeDevice) {
  if (OnWinXp() || OnWin8()) {
    // http://crbug.com/379798.
    LOG(ERROR) << "This test is not implemented for Windows XP/Win8.";
    return;
  }

  base::FilePath reference_file =
      test::GetReferenceFilesDir().Append(kReferenceFile);
  base::FilePath recording = CreateTemporaryWaveFile();

  ASSERT_NO_FATAL_FAILURE(SetupAndRecordAudioCall(
      reference_file, recording, kAudioOnlyCallConstraints,
      base::TimeDelta::FromSeconds(25)));
  ComputeAndPrintPesqResults(reference_file, recording, "_getusermedia");

  EXPECT_TRUE(base::DeleteFile(recording, false));
}

IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest,
                       MANUAL_TestCallQualityWithAudioFromWebAudio) {
  if (OnWinXp() || OnWin8()) {
    // http://crbug.com/379798.
    LOG(ERROR) << "This test is not implemented for Windows XP/Win8.";
    return;
  }
  ASSERT_TRUE(test::HasReferenceFilesInCheckout());
  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());

  ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());

  content::WebContents* left_tab =
      OpenPageWithoutGetUserMedia(kWebRtcAudioTestHtmlPage);
  content::WebContents* right_tab =
      OpenPageWithoutGetUserMedia(kWebRtcAudioTestHtmlPage);

  AddAudioFileToWebAudio(kReferenceFileRelativeUrl, left_tab);

  NegotiateCall(left_tab, right_tab);

  base::FilePath recording = CreateTemporaryWaveFile();

  // Note: the sound clip is about 13 seconds: record for 20 seconds to get some
  // safety margins on each side.
  AudioRecorder recorder;
  ASSERT_TRUE(recorder.StartRecording(base::TimeDelta::FromSeconds(20),
                                      recording));

  PlayAudioFileThroughWebAudio(left_tab);

  ASSERT_TRUE(recorder.WaitForRecordingToEnd());
  DVLOG(0) << "Done recording to " << recording.value() << std::endl;

  HangUp(left_tab);

  // Compare with the reference file on disk (this is the same file we played
  // through WebAudio earlier).
  base::FilePath reference_file =
      test::GetReferenceFilesDir().Append(kReferenceFile);
  ComputeAndPrintPesqResults(reference_file, recording, "_webaudio");
}

/**
 * The auto gain control test plays a file into the fake microphone. Then it
 * sets up a one-way WebRTC call with audio only and records Chrome's output on
 * the receiving side using the audio loopback provided by the quality test
 * (see the class comments for more details).
 *
 * Then both the recording and reference file are split on silence. This creates
 * a number of segments with speech in them. The reason for this is to provide
 * a kind of synchronization mechanism so the start of each speech segment is
 * compared to the start of the corresponding speech segment. This is because we
 * will experience inevitable clock drift between the system clock (which runs
 * the fake microphone) and the sound card (which runs play-out). Effectively
 * re-synchronizing on each segment mitigates this.
 *
 * The silence splitting is inherently sensitive to the sound file we run on.
 * Therefore the reference file must have at least 500 ms of pure silence
 * between speech segments; the test will fail if the output produces more
 * segments than the reference.
 *
 * The test reports the difference in decibel between the reference and output
 * file per 10 ms interval in each speech segment. A value of 6 means the
 * output was 6 dB louder than the reference, presumably because the AGC applied
 * gain to the signal.
 *
 * The test only exercises digital AGC for now.
 *
 * We record in CD format here (44.1 kHz) because that's what the fake input
 * device currently supports, and we want to be able to compare directly. See
 * http://crbug.com/421054.
 */
void MAYBE_WebRtcAudioQualityBrowserTest::TestAutoGainControl(
    const base::FilePath::StringType& reference_filename,
    const std::string& constraints,
    const std::string& perf_modifier) {
  if (OnWinXp() || OnWin8()) {
    // http://crbug.com/379798.
    LOG(ERROR) << "This test is not implemented for Windows XP/Win8.";
    return;
  }
  base::FilePath reference_file =
      test::GetReferenceFilesDir().Append(reference_filename);
  base::FilePath recording = CreateTemporaryWaveFile();

  ASSERT_NO_FATAL_FAILURE(SetupAndRecordAudioCall(
      reference_file, recording, constraints,
      base::TimeDelta::FromSeconds(25)));

  // Call Take() on the scoped temp dirs if you want to look at the files after
  // the test exits (the default is to delete the files).
  base::ScopedTempDir split_ref_files;
  ASSERT_TRUE(split_ref_files.CreateUniqueTempDir());
  ASSERT_NO_FATAL_FAILURE(
      SplitFileOnSilenceIntoDir(reference_file, split_ref_files.path()));
  std::vector<base::FilePath> ref_segments =
      ListWavFilesInDir(split_ref_files.path());

  base::ScopedTempDir split_actual_files;
  ASSERT_TRUE(split_actual_files.CreateUniqueTempDir());
  ASSERT_NO_FATAL_FAILURE(
      SplitFileOnSilenceIntoDir(recording, split_actual_files.path()));
  std::vector<base::FilePath> actual_segments =
      ListWavFilesInDir(split_actual_files.path());

  AnalyzeSegmentsAndPrintResult(ref_segments, actual_segments, reference_file,
                                perf_modifier);

  EXPECT_TRUE(base::DeleteFile(recording, false));
}

// Only implemented for Linux for now.
#if defined(OS_LINUX)
#define MAYBE_MANUAL_TestAutoGainControlOnLowAudio \
        MANUAL_TestAutoGainControlOnLowAudio
#else
#define MAYBE_MANUAL_TestAutoGainControlOnLowAudio \
        DISABLED_MANUAL_TestAutoGainControlOnLowAudio
#endif

// The AGC should apply non-zero gain here.
IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest,
                       MAYBE_MANUAL_TestAutoGainControlOnLowAudio) {
  ASSERT_NO_FATAL_FAILURE(TestAutoGainControl(
      kReferenceFile, kAudioOnlyCallConstraints, "_with_agc"));
}

// Only implemented for Linux for now.
#if defined(OS_LINUX)
#define MAYBE_MANUAL_TestAutoGainIsOffWithAudioProcessingOff \
        MANUAL_TestAutoGainIsOffWithAudioProcessingOff
#else
#define MAYBE_MANUAL_TestAutoGainIsOffWithAudioProcessingOff \
        DISABLED_MANUAL_TestAutoGainIsOffWithAudioProcessingOff
#endif

// Since the AGC is off here there should be no gain at all.
IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest,
                       MAYBE_MANUAL_TestAutoGainIsOffWithAudioProcessingOff) {
  const char* kAudioCallWithoutAudioProcessing =
      "{audio: { mandatory: { echoCancellation: false } } }";
  ASSERT_NO_FATAL_FAILURE(TestAutoGainControl(
      kReferenceFile, kAudioCallWithoutAudioProcessing, "_no_agc"));
}


text:
namespace {

// String constants used for parsing D-Bus Introspection XML data.
const char kInterfaceNode[] = "interface";
const char kInterfaceNameAttribute[] = "name";

}

namespace chromeos {

std::vector<std::string> GetInterfacesFromIntrospectResult(
    const std::string& xml_data) {
  std::vector<std::string> interfaces;

  XmlReader reader;
  if (!reader.Load(xml_data))
    return interfaces;

  do {
    // Skip to the next open tag, exit when done.
    while (!reader.SkipToElement()) {
      if (!reader.Read()) {
        return interfaces;
      }
    }

    // Only look at interface nodes.
    if (reader.NodeName() != kInterfaceNode)
      continue;

    // Skip if missing the interface name.
    std::string interface_name;
    if (!reader.NodeAttribute(kInterfaceNameAttribute, &interface_name))
      continue;

    interfaces.push_back(interface_name);
  } while (reader.Read());

  return interfaces;
}

}


text:
namespace {

enum CrcEngine {
  X86 = 0,
  NONTEMPORAL = 1,
  FALLBACK = 2,
};

// Correctness tests:
// - Every source/destination byte alignment 0-15, every size 0-511 bytes
// - Arbitrarily aligned source, large size
template <size_t max_size>
class CrcMemcpyTest : public testing::Test {
 protected:
  CrcMemcpyTest() {
    source_ = std::make_unique<char[]>(kSize);
    destination_ = std::make_unique<char[]>(kSize);
  }
  static constexpr size_t kAlignment = 16;
  static constexpr size_t kMaxCopySize = max_size;
  static constexpr size_t kSize = kAlignment + kMaxCopySize;
  std::unique_ptr<char[]> source_;
  std::unique_ptr<char[]> destination_;

  absl::BitGen gen_;
};

// Small test is slightly larger 4096 bytes to allow coverage of the "large"
// copy function.  The minimum size to exercise all code paths in that function
// would be around 256 consecutive tests (getting every possible tail value
// and 0-2 small copy loops after the main block), so testing from 4096-4500
// will cover all of those code paths multiple times.
typedef CrcMemcpyTest<4500> CrcSmallTest;
typedef CrcMemcpyTest<(1 << 24)> CrcLargeTest;
// Parametrize the small test so that it can be done with all configurations.
template <typename ParamsT>
class x86ParamTestTemplate : public CrcSmallTest,
                             public ::testing::WithParamInterface<ParamsT> {
 protected:
  x86ParamTestTemplate() {
    if (GetParam().crc_engine_selector == FALLBACK) {
      engine_ = std::make_unique<absl::crc_internal::FallbackCrcMemcpyEngine>();
    } else if (GetParam().crc_engine_selector == NONTEMPORAL) {
      engine_ =
          std::make_unique<absl::crc_internal::CrcNonTemporalMemcpyEngine>();
    } else {
      engine_ = absl::crc_internal::CrcMemcpy::GetTestEngine(
          GetParam().vector_lanes, GetParam().integer_lanes);
    }
  }

  // Convenience method.
  ParamsT GetParam() const {
    return ::testing::WithParamInterface<ParamsT>::GetParam();
  }

  std::unique_ptr<absl::crc_internal::CrcMemcpyEngine> engine_;
};
struct TestParams {
  CrcEngine crc_engine_selector = X86;
  int vector_lanes = 0;
  int integer_lanes = 0;
};
using x86ParamTest = x86ParamTestTemplate<TestParams>;
// SmallCorrectness is designed to exercise every possible set of code paths
// in the memcpy code, not including the loop.
TEST_P(x86ParamTest, SmallCorrectnessCheckSourceAlignment) {
  constexpr size_t kTestSizes[] = {0, 100, 255, 512, 1024, 4000, kMaxCopySize};

  for (size_t source_alignment = 0; source_alignment < kAlignment;
       source_alignment++) {
    for (auto size : kTestSizes) {
      char* base_data = static_cast<char*>(source_.get()) + source_alignment;
      for (size_t i = 0; i < size; i++) {
        *(base_data + i) =
            static_cast<char>(absl::Uniform<unsigned char>(gen_));
      }
      absl::crc32c_t initial_crc =
          absl::ToCrc32c(absl::Uniform<uint32_t>(gen_));
      absl::crc32c_t experiment_crc =
          engine_->Compute(destination_.get(), source_.get() + source_alignment,
                           size, initial_crc);
      // Check the memory region to make sure it is the same
      int mem_comparison =
          memcmp(destination_.get(), source_.get() + source_alignment, size);
      SCOPED_TRACE(absl::StrCat("Error in memcpy of size: ", size,
                                " with source alignment: ", source_alignment));
      ASSERT_EQ(mem_comparison, 0);
      absl::crc32c_t baseline_crc = absl::ExtendCrc32c(
          initial_crc,
          absl::string_view(
              static_cast<char*>(source_.get()) + source_alignment, size));
      ASSERT_EQ(baseline_crc, experiment_crc);
    }
  }
}

TEST_P(x86ParamTest, SmallCorrectnessCheckDestAlignment) {
  constexpr size_t kTestSizes[] = {0, 100, 255, 512, 1024, 4000, kMaxCopySize};

  for (size_t dest_alignment = 0; dest_alignment < kAlignment;
       dest_alignment++) {
    for (auto size : kTestSizes) {
      char* base_data = static_cast<char*>(source_.get());
      for (size_t i = 0; i < size; i++) {
        *(base_data + i) =
            static_cast<char>(absl::Uniform<unsigned char>(gen_));
      }
      absl::crc32c_t initial_crc =
          absl::ToCrc32c(absl::Uniform<uint32_t>(gen_));
      absl::crc32c_t experiment_crc =
          engine_->Compute(destination_.get() + dest_alignment, source_.get(),
                           size, initial_crc);
      // Check the memory region to make sure it is the same
      int mem_comparison =
          memcmp(destination_.get() + dest_alignment, source_.get(), size);
      SCOPED_TRACE(absl::StrCat("Error in memcpy of size: ", size,
                                " with dest alignment: ", dest_alignment));
      ASSERT_EQ(mem_comparison, 0);
      absl::crc32c_t baseline_crc = absl::ExtendCrc32c(
          initial_crc,
          absl::string_view(static_cast<char*>(source_.get()), size));
      ASSERT_EQ(baseline_crc, experiment_crc);
    }
  }
}

INSTANTIATE_TEST_SUITE_P(x86ParamTest, x86ParamTest,
                         ::testing::Values(
                             // Tests for configurations that may occur in prod.
                             TestParams{X86, 3, 0}, TestParams{X86, 1, 2},
                             // Fallback test.
                             TestParams{FALLBACK, 0, 0},
                             // Non Temporal
                             TestParams{NONTEMPORAL, 0, 0}));

}  // namespace


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.lorislab.barn</groupId>
        <artifactId>barn</artifactId>
        <version>0.0.3-SNAPSHOT</version>
		<relativePath>../</relativePath>
    </parent>

    <artifactId>barn-transformer</artifactId>
    <name>barn-transformer</name>

	<dependencies>
		<dependency>
			<groupId>org.lorislab.barn</groupId>
			<artifactId>barn-api</artifactId>
			<version>${project.version}</version>
		</dependency>

		<dependency>
			<groupId>org.lorislab.transformer</groupId>
			<artifactId>transformer-api</artifactId>
		</dependency>
	</dependencies>
</project>


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/AdvancedImmersiveMode.iml" filepath="$PROJECT_DIR$/AdvancedImmersiveMode.iml" />
      <module fileurl="file://$PROJECT_DIR$/Application/Application.iml" filepath="$PROJECT_DIR$/Application/Application.iml" />
    </modules>
  </component>
</project>


text:
"""Validate token."""
from .exceptions import InvalidUsage

def validate_token(cursor, connection, token, endpoint):
    """
    Validate Token.

    :param cursor: database cursor object
    :param connection: database connection object
    :param token: user token
    :param endpoint: route name
    """
    if endpoint != 'login':
        if token is None:
            raise InvalidUsage("No token", 403)
        cursor.execute(
            """
            select exists(select id from user where token='{}') as token
            """.format(token)
        )
        if cursor.fetchone()['token'] == 0:
            raise InvalidUsage("Invalid Token", status_code=403)


text:
<component name="libraryTable">
  <library name="support-vector-drawable-25.3.1">
    <CLASSES>
      <root url="jar://$USER_HOME$/.android/build-cache/34d87397535fe4fbd9237ab28a3b33bed993be18/output/jars/classes.jar!/" />
      <root url="file://$USER_HOME$/.android/build-cache/34d87397535fe4fbd9237ab28a3b33bed993be18/output/res" />
    </CLASSES>
    <JAVADOC />
    <SOURCES>
      <root url="jar://$USER_HOME$/Android/Sdk/extras/android/m2repository/com/android/support/support-vector-drawable/25.3.1/support-vector-drawable-25.3.1-sources.jar!/" />
    </SOURCES>
  </library>
</component>