├── .rspec ├── lib ├── beaker-gke │ └── version.rb ├── beaker-gke.rb └── beaker │ └── hypervisor │ └── gke.rb ├── .github ├── dependabot.yml └── workflows │ ├── release.yml │ ├── ci.yml │ └── codeql-analysis.yml ├── bin └── beaker-gke ├── config ├── service.yaml └── pod.yaml ├── .gitignore ├── gke.md ├── .rubocop.yml ├── spec ├── spec_helper.rb └── gke │ └── hypervisor │ └── gke_spec.rb ├── Rakefile ├── Gemfile ├── README.md ├── beaker-gke.gemspec └── LICENSE /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper -------------------------------------------------------------------------------- /lib/beaker-gke/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module BeakerGke 4 | VERSION = '0.1.0' 5 | end 6 | -------------------------------------------------------------------------------- /lib/beaker-gke.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'pathname' 4 | 5 | ROOT_DIR = Pathname.new(File.expand_path('..', __dir__)) unless defined?(ROOT_DIR) 6 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "13:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /bin/beaker-gke: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require 'rubygems' unless defined?(Gem) 5 | require 'beaker-gke' 6 | 7 | puts BeakerGke::VERSION 8 | 9 | exit 0 10 | -------------------------------------------------------------------------------- /config/service.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | :apiVersion: v1 3 | :kind: Service 4 | :metadata: 5 | :name: "%{pod_name}" 6 | :namespace: gke-puppetagent-ci 7 | :labels: 8 | :app: "%{pod_name}" 9 | :spec: 10 | :selector: 11 | :app: "%{pod_name}" 12 | :clusterIP: "None" 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # bundler 2 | .bundle 3 | Gemfile.local 4 | Gemfile.lock 5 | 6 | # JetBrains IDEA 7 | *.iml 8 | .idea/ 9 | 10 | # rbenv file 11 | .ruby-version 12 | .ruby-gemset 13 | 14 | /.yardoc 15 | /_yardoc/ 16 | /coverage/ 17 | /doc/ 18 | /pkg/ 19 | /spec/reports/ 20 | /tmp/ 21 | -------------------------------------------------------------------------------- /gke.md: -------------------------------------------------------------------------------- 1 | # Google Kubernetes Engine -gke 2 | Google Kubernetes Engine (GKE) is a "managed, production-ready environment for deploying containerized applications." 3 | 4 | [GKE site](https://cloud.google.com/kubernetes-engine/). 5 | 6 | # Getting started 7 | 8 | ### Requirements 9 | 10 | - Get GKE access from your IT dept, particularly your 'GOOGLE_APPLICATION_CREDENTIALS' and 'CLIENT_CONFIG' 11 | - export this as OS environment variable -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | --- 2 | require: 3 | - rubocop-rspec 4 | - rubocop-performance 5 | AllCops: 6 | TargetRubyVersion: 2.5 7 | NewCops: enable 8 | 9 | Style/Documentation: 10 | Enabled: false 11 | 12 | Metrics/MethodLength: 13 | Max: 25 14 | 15 | Layout/LineLength: 16 | Max: 120 17 | 18 | Metrics/AbcSize: 19 | Enabled: false 20 | 21 | Naming/FileName: 22 | Exclude: 23 | - 'lib/beaker-gke.rb' 24 | 25 | Security/Eval: 26 | Exclude: 27 | - 'Gemfile' 28 | 29 | Metrics/BlockLength: 30 | Enabled: false 31 | 32 | Metrics/ModuleLength: 33 | Enabled: false 34 | 35 | RSpec/FilePath: 36 | Enabled: false 37 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'beaker' 4 | require 'simplecov' 5 | require 'climate_control' 6 | require 'fakefs/spec_helpers' 7 | 8 | Dir.glob(Dir.pwd + '/lib/beaker/hypervisor/*.rb').sort { |file| require file } 9 | 10 | # setup & require beaker's spec_helper.rb 11 | beaker_gem_spec = Gem::Specification.find_by_name('beaker') 12 | beaker_gem_dir = beaker_gem_spec.gem_dir 13 | beaker_spec_path = File.join(beaker_gem_dir, 'spec') 14 | $LOAD_PATH << beaker_spec_path 15 | require File.join(beaker_spec_path, 'spec_helper.rb') 16 | 17 | RSpec.configure do |config| 18 | config.include TestFileHelpers 19 | config.include HostHelpers 20 | end 21 | 22 | def with_modified_env(options, &block) 23 | ClimateControl.modify(options, &block) 24 | end 25 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rspec/core/rake_task' 4 | 5 | namespace :test do 6 | namespace :spec do 7 | desc 'Run spec tests' 8 | RSpec::Core::RakeTask.new(:run) do |t| 9 | t.rspec_opts = ['--color'] 10 | t.pattern = 'spec/' 11 | end 12 | end 13 | end 14 | 15 | desc 'run static analysis with rubocop' 16 | task(:rubocop) do 17 | require 'rubocop' 18 | cli = RuboCop::CLI.new 19 | exit_code = cli.run(%w[--display-cop-names --format simple]) 20 | raise 'RuboCop detected offenses' if exit_code != 0 21 | end 22 | 23 | # namespace-named default tasks. 24 | # these are the default tasks invoked when only the namespace is referenced. 25 | # they're needed because `task :default` in those blocks doesn't work as expected. 26 | task 'test:spec' => 'test:spec:run' 27 | task 'test:acceptance' => 'test:acceptance:quick' 28 | 29 | # global defaults 30 | task test: 'test:spec' 31 | task default: :test 32 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source ENV['GEM_SOURCE'] || 'https://rubygems.org' 4 | 5 | gemspec 6 | 7 | group :development, :test do 8 | gem 'rubocop', require: false 9 | gem 'rubocop-performance', require: false 10 | gem 'rubocop-rspec', require: false 11 | end 12 | 13 | def location_for(place, fake_version = nil) 14 | if place =~ /^git:([^#]*)#(.*)/ 15 | [fake_version, { git: Regexp.last_match(1), branch: Regexp.last_match(2), require: false }].compact 16 | elsif place =~ %r{^file://(.*)} 17 | ['>= 0', { path: File.expand_path(Regexp.last_match(1)), require: false }] 18 | else 19 | [place, { require: false }] 20 | end 21 | end 22 | 23 | # We don't put beaker in as a test dependency because we 24 | # don't want to create a transitive dependency 25 | group :acceptance_testing do 26 | gem 'beaker', *location_for(ENV['BEAKER_VERSION'] || '~> 4.0') 27 | end 28 | 29 | eval(File.read("#{__FILE__}.local"), binding) if File.exist? "#{__FILE__}.local" 30 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | release: 10 | runs-on: ubuntu-latest 11 | if: github.repository_owner == 'voxpupuli' 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Install Ruby 3.1 15 | uses: ruby/setup-ruby@v1 16 | with: 17 | ruby-version: '3.1' 18 | env: 19 | BUNDLE_WITHOUT: release 20 | - name: Build gem 21 | run: gem build *.gemspec 22 | - name: Publish gem to rubygems.org 23 | run: gem push *.gem 24 | env: 25 | GEM_HOST_API_KEY: '${{ secrets.RUBYGEMS_AUTH_TOKEN }}' 26 | - name: Setup GitHub packages access 27 | run: | 28 | mkdir -p ~/.gem 29 | echo ":github: Bearer ${{ secrets.GITHUB_TOKEN }}" >> ~/.gem/credentials 30 | chmod 0600 ~/.gem/credentials 31 | - name: Publish gem to GitHub packages 32 | run: gem push --key github --host https://rubygems.pkg.github.com/voxpupuli *.gem 33 | -------------------------------------------------------------------------------- /config/pod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | :apiVersion: v1 3 | :kind: Pod 4 | :metadata: 5 | :name: "%{pod_name}" 6 | :namespace: gke-puppetagent-ci 7 | :labels: 8 | :app: "%{pod_name}" 9 | :project: puppet-agent-ci 10 | :spec: 11 | :hostname: "%{pod_name}" 12 | :containers: 13 | - :image: <%= ENV.fetch("CONTAINER_IMAGE") { "gcr.io/puppetagent-ci/centos-7-base:v3" } %> 14 | :name: "centos-7-base" 15 | :securityContext: 16 | :capabilities: 17 | :add: 18 | - NET_ADMIN 19 | :dnsPolicy: "None" 20 | :dnsConfig: 21 | nameservers: 22 | - 10.240.0.10 23 | - 10.240.1.10 24 | :searches: 25 | - gke-puppetagent-ci.puppet.net 26 | :hostAliases: 27 | - :ip: "169.254.169.254" 28 | :hostnames: 29 | - "metadata.google.internal" 30 | :affinity: 31 | :podAntiAffinity: 32 | :preferredDuringSchedulingIgnoredDuringExecution: 33 | - :weight: 100 34 | :podAffinityTerm: 35 | :labelSelector: 36 | :matchLabels: 37 | :project: puppet-agent-ci 38 | :topologyKey: kubernetes.io/hostname 39 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | - pull_request 5 | 6 | env: 7 | BUNDLE_WITHOUT: release 8 | 9 | jobs: 10 | rubocop: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Install Ruby 3.1 15 | uses: ruby/setup-ruby@v1 16 | with: 17 | ruby-version: "3.1" 18 | bundler-cache: true 19 | - name: Run rubocop 20 | run: bundle exec rake rubocop 21 | test: 22 | runs-on: ubuntu-latest 23 | strategy: 24 | fail-fast: false 25 | matrix: 26 | include: 27 | - ruby: "2.7" 28 | - ruby: "3.0" 29 | - ruby: "3.1" 30 | coverage: "yes" 31 | env: 32 | COVERAGE: ${{ matrix.coverage }} 33 | name: Ruby ${{ matrix.ruby }} 34 | steps: 35 | - uses: actions/checkout@v2 36 | - name: Install Ruby ${{ matrix.ruby }} 37 | uses: ruby/setup-ruby@v1 38 | with: 39 | ruby-version: ${{ matrix.ruby }} 40 | bundler-cache: true 41 | - name: Verify gem builds 42 | run: gem build *.gemspec 43 | - name: Run spec tests 44 | run: bundle exec rake 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # beacker GKE 2 | Beaker library to use the Google Kubernetes Engine hypervisor 3 | 4 | # How to use this wizardry 5 | 6 | This gem that allows you to use hosts with [GKE](gke.md) hypervisor with [beaker](https://github.com/puppetlabs/beaker). 7 | 8 | Beaker will automatically load the appropriate hypervisors for any given hosts file, so as long as your project dependencies are satisfied there's nothing else to do. No need to `require` this library in your tests. 9 | 10 | ## With Beaker 4.x 11 | 12 | As of Beaker 4.0, all hypervisor and DSL extension libraries have been removed and are no longer dependencies. In order to use a specific hypervisor or DSL extension library in your project, you will need to include them alongside Beaker in your Gemfile or project.gemspec. E.g. 13 | 14 | ~~~ruby 15 | # Gemfile 16 | gem 'beaker', '~>4.0' 17 | gem 'beaker-gke' 18 | # project.gemspec 19 | s.add_runtime_dependency 'beaker', '~>4.0' 20 | s.add_runtime_dependency 'beaker-gke' 21 | ~~~ 22 | 23 | # Spec tests 24 | 25 | Spec test live under the `spec` folder. There are the default rake task and therefore can run with a simple command: 26 | ```bash 27 | bundle exec rake test:spec 28 | ``` 29 | 30 | # Contributing 31 | 32 | Pull requests are welcome! 33 | -------------------------------------------------------------------------------- /beaker-gke.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH.unshift File.expand_path('lib', __dir__) 4 | 5 | require 'beaker-gke/version' 6 | 7 | Gem::Specification.new do |s| 8 | s.name = 'beaker-gke' 9 | s.version = BeakerGke::VERSION 10 | s.authors = ["Night's Watch"] 11 | s.email = ['team-nw@puppet.com'] 12 | s.homepage = 'https://github.com/puppetlabs/beaker-gke' 13 | s.summary = 'Beaker hypervisor for GKE!' 14 | s.description = 'Add GKE support to Beaker acceptance testing tool' 15 | s.license = 'Apache-2.0' 16 | 17 | s.files = `git ls-files`.split("\n") 18 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 19 | s.executables = `git ls-files -- bin/*`.split("\n").map { |file| File.basename(file) } 20 | s.require_paths = ['lib'] 21 | 22 | # Required ruby version 23 | s.required_ruby_version = '~> 2.5' 24 | 25 | # Testing dependencies 26 | s.add_development_dependency 'climate_control' 27 | s.add_development_dependency 'fakefs', '~> 1.2', '<= 1.2.3' 28 | s.add_development_dependency 'rake', '~> 13.0' 29 | s.add_development_dependency 'rspec', '~> 3.0' 30 | s.add_development_dependency 'rspec-its' 31 | s.add_development_dependency 'simplecov' 32 | 33 | # Run time dependencies 34 | s.add_runtime_dependency 'googleauth', '~> 0.9' 35 | s.add_runtime_dependency 'kubeclient', '>= 4.4', '< 4.10' 36 | end 37 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ main ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ main ] 20 | schedule: 21 | - cron: '30 0 * * 4' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'ruby' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://git.io/codeql-language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v2 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v1 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 52 | 53 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 54 | # If this step fails, then you should remove it and run the build manually (see below) 55 | - name: Autobuild 56 | uses: github/codeql-action/autobuild@v1 57 | 58 | # ℹ️ Command-line programs to run using the OS shell. 59 | # 📚 https://git.io/JvXDl 60 | 61 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 62 | # and modify them (or add more) to build your code if your project 63 | # uses a compiled language 64 | 65 | #- run: | 66 | # make bootstrap 67 | # make release 68 | 69 | - name: Perform CodeQL Analysis 70 | uses: github/codeql-action/analyze@v1 71 | -------------------------------------------------------------------------------- /lib/beaker/hypervisor/gke.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'kubeclient' 4 | require 'beaker-gke' 5 | require 'googleauth' 6 | require 'erb' 7 | 8 | module Beaker 9 | class Gke < Beaker::Hypervisor 10 | SERVICE_NAMESPACE = 'gke-puppetagent-ci' 11 | PROXY_IP = '10.236.0.3' 12 | PROXY_PORT = 8899 13 | MAX_RETRIES = 5 14 | # OS environment variable must be set to continue 15 | # ENV['KUBECONFIG'] = 'path/.kube/config' 16 | # ENV['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/.kube/puppetagent-ci.json' 17 | 18 | def initialize(hosts, options) 19 | begin 20 | ENV.fetch('KUBECONFIG') 21 | ENV.fetch('GOOGLE_APPLICATION_CREDENTIALS') 22 | rescue KeyError 23 | raise( 24 | ArgumentError, 25 | 'OS environment variable KUBECONFIG and GOOGLE_APPLICATION_CREDENTIALS must be set' 26 | ) 27 | end 28 | @hosts = hosts 29 | @options = options 30 | @client = client 31 | @logger = options[:logger] 32 | end 33 | 34 | def provision 35 | @hosts.each do |host| 36 | hostname = generate_host_name 37 | create_pod(hostname) 38 | create_srv(hostname) 39 | retries = 0 40 | 41 | begin 42 | pod = get_pod(hostname) 43 | raise StandardError unless pod.status.podIP 44 | rescue StandardError => e 45 | raise "Timeout: #{e.message}" unless retries <= MAX_RETRIES 46 | 47 | @logger.info("Retrying, could not get podIP for #{hostname}") 48 | 49 | retries += 1 50 | sleep(2**retries) 51 | retry 52 | end 53 | 54 | host[:vmhostname] = "#{hostname}.gke-puppetagent-ci.puppet.net" 55 | host[:hostname] = hostname 56 | host[:ip] = pod.status.podIP 57 | host[:gke_container] = true 58 | end 59 | nil 60 | end 61 | 62 | def cleanup 63 | @hosts.each do |host| 64 | @logger.info("Deleting POD with ID: #{host[:hostname]}") 65 | 66 | delete_pod(host[:hostname]) 67 | delete_service(host[:hostname]) 68 | end 69 | end 70 | 71 | def connection_preference(_host) 72 | %i[ip vmhostname hostname] 73 | end 74 | 75 | def create_pod(name) 76 | pod_config = read_symbols('pod.yaml', pod_name: name) 77 | @client.create_pod(pod_config) 78 | end 79 | 80 | def get_pod(name) 81 | @client.get_pod(name, SERVICE_NAMESPACE) 82 | end 83 | 84 | def create_srv(name) 85 | service_config = read_symbols('service.yaml', pod_name: name) 86 | @client.create_service(service_config) 87 | end 88 | 89 | def delete_pod(pod_name) 90 | @client.delete_pod( 91 | pod_name, 92 | SERVICE_NAMESPACE, 93 | delete_options: { 'force': 1, '--grace-period': 0 } 94 | ) 95 | end 96 | 97 | def delete_service(srv_name) 98 | @client.delete_service(srv_name, SERVICE_NAMESPACE) 99 | rescue Kubeclient::ResourceNotFoundError => e 100 | @logger.info("Service #{srv_name} could not be deleted #{e}") 101 | end 102 | 103 | private 104 | 105 | def client 106 | config = Kubeclient::Config.read(ENV['KUBECONFIG']) 107 | context = config.context 108 | proxy_uri = URI::HTTP.build(host: PROXY_IP, port: PROXY_PORT) 109 | Kubeclient::Client.new( 110 | context.api_endpoint, 'v1', 111 | http_proxy_uri: proxy_uri, 112 | ssl_options: context.ssl_options, 113 | auth_options: context.auth_options 114 | ) 115 | end 116 | 117 | def read_file(file_name) 118 | File.read(File.join(ROOT_DIR, 'config', file_name)) 119 | end 120 | 121 | def read_symbols(file, substitution = {}) 122 | data = ERB.new(read_file(file)).result 123 | Psych.load(data % substitution, symbolize_names: true) 124 | end 125 | end 126 | end 127 | -------------------------------------------------------------------------------- /spec/gke/hypervisor/gke_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'beaker/hypervisor/gke' 4 | 5 | describe Beaker::Gke do 6 | let(:hosts) { make_hosts } 7 | 8 | let(:options) { { logger: logger } } 9 | 10 | let(:logger) do 11 | logger = instance_double('logger') 12 | allow(logger).to receive(:debug) 13 | allow(logger).to receive(:info) 14 | allow(logger).to receive(:warn) 15 | allow(logger).to receive(:error) 16 | allow(logger).to receive(:notify) 17 | logger 18 | end 19 | 20 | let(:config) { instance_double('config') } 21 | 22 | let(:context) do 23 | instance_double('context', 24 | api_endpoint: 'v1', 25 | ssl_options: { 26 | verify_ssl: 1, 27 | cert_store: true 28 | }, 29 | auth_options: { 30 | bearer_token: 'TOKEN_STRING' 31 | }) 32 | end 33 | 34 | let(:gke) { ::Beaker::Gke.new(hosts, options) } 35 | 36 | def pass_through_initialization 37 | allow(ENV).to receive(:fetch).with('KUBECONFIG').and_return('default_value') 38 | allow(ENV).to receive(:fetch).with('GOOGLE_APPLICATION_CREDENTIALS').and_return('default_value') 39 | allow(config).to receive(:context).and_return(context) 40 | allow(Kubeclient::Config).to receive(:read).with(ENV['KUBECONFIG']).and_return(config) 41 | end 42 | 43 | before do 44 | FakeFS.deactivate! 45 | end 46 | 47 | describe ' #initialize' do 48 | let(:env_error_message) { 'OS environment variable KUBECONFIG and GOOGLE_APPLICATION_CREDENTIALS must be set' } 49 | 50 | it 'raises error when KUBECONFIG and GOOGLE_APPLICATION_CREDENTIALS ENV variables are not set' do 51 | expect { gke }.to raise_error(ArgumentError, env_error_message) 52 | end 53 | 54 | it 'raises error when only GOOGLE_APPLICATION_CREDENTIALS ENV variable is set' do 55 | with_modified_env GOOGLE_APPLICATION_CREDENTIALS: 'default_value' do 56 | expect { gke }.to raise_error(ArgumentError, env_error_message) 57 | end 58 | end 59 | 60 | it 'raises error when only KUBECONFIG ENV variable is set' do 61 | with_modified_env KUBECONFIG: 'default_value' do 62 | expect { gke }.to raise_error(ArgumentError, env_error_message) 63 | end 64 | end 65 | 66 | context 'when both KUBECONFIG and GOOGLE_APPLICATION_CREDENTIALS ENV are set' do 67 | before do 68 | pass_through_initialization 69 | end 70 | 71 | it 'sets the hosts data member accordingly' do 72 | expect(gke.instance_variable_get(:@hosts)).to equal(hosts) 73 | end 74 | 75 | it 'sets the options data member accordingly' do 76 | expect(gke.instance_variable_get(:@options)).to equal(options) 77 | end 78 | 79 | it 'sets the logger data member accordingly' do 80 | expect(gke.instance_variable_get(:@logger)).to equal(logger) 81 | end 82 | 83 | it 'succeeds and does not raise any errors' do 84 | expect { gke }.not_to raise_error 85 | end 86 | end 87 | end 88 | 89 | describe ' #provision' do 90 | let(:pod) { instance_double('pod') } 91 | 92 | let(:status) { instance_double('status', podIP: '10.236.246.250') } 93 | let(:empty_ip_status) { instance_double('status', podIP: nil) } 94 | 95 | def pass_through_pod_and_service_creation 96 | allow(gke).to receive(:create_pod).and_return(nil) 97 | allow(gke).to receive(:read_file).with('pod.yaml').and_return('pod.yaml content') 98 | 99 | allow(gke).to receive(:create_srv).and_return(nil) 100 | allow(gke).to receive(:read_file).with('service.yaml').and_return('service.yaml content') 101 | end 102 | 103 | before do 104 | pass_through_initialization 105 | pass_through_pod_and_service_creation 106 | allow(gke).to receive(:sleep).and_return(true) 107 | end 108 | 109 | context 'when no hosts given' do 110 | let(:no_hosts_gke) { ::Beaker::Gke.new([], options) } 111 | 112 | it 'returns nil' do 113 | expect(no_hosts_gke.provision).to eq(nil) 114 | end 115 | 116 | it 'logs no info' do 117 | no_hosts_gke.provision 118 | expect(logger).not_to have_received(:info) 119 | end 120 | 121 | it 'does not raise any error' do 122 | expect { no_hosts_gke.provision }.not_to raise_error 123 | end 124 | end 125 | 126 | it 'raises StandardError and logs podIP retrieval attempts' do # rubocop:disable RSpec/MultipleExpectations 127 | allow(gke).to receive(:get_pod).and_return(pod) 128 | allow(pod).to receive(:status).and_return(empty_ip_status) 129 | 130 | expect { gke.provision }.to raise_error(StandardError) 131 | expect(logger).to have_received(:info).with(/Retrying, could not get podIP for/).at_least(:once) 132 | end 133 | 134 | it 'succeeds' do 135 | allow(gke).to receive(:get_pod).and_return(pod) 136 | allow(pod).to receive(:status).and_return(status) 137 | 138 | expect(gke.provision).to eq(nil) 139 | end 140 | end 141 | 142 | describe ' #cleanup' do 143 | before do 144 | pass_through_initialization 145 | end 146 | 147 | context 'when no hosts given' do 148 | let(:no_hosts_gke) { ::Beaker::Gke.new([], options) } 149 | 150 | it 'does not raise any error' do 151 | expect { no_hosts_gke.cleanup }.not_to raise_error 152 | end 153 | 154 | it 'logs no info' do 155 | no_hosts_gke.cleanup 156 | expect(logger).not_to have_received(:info) 157 | end 158 | end 159 | 160 | context 'when succeeds' do 161 | before do 162 | allow(gke).to receive(:delete_pod).and_return(nil) 163 | allow(gke).to receive(:delete_service).and_return(nil) 164 | end 165 | 166 | it 'does not raise any error' do 167 | expect { gke.cleanup }.not_to raise_error 168 | end 169 | 170 | it 'logs deleted pods' do 171 | gke.cleanup 172 | expect(logger).to have_received(:info).with(/Deleting POD with ID:/).exactly(hosts.size).times 173 | end 174 | end 175 | end 176 | end 177 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------