Skip to content

Commit

Permalink
URL validation, improve macOS testing and testing guide (#8)
Browse files Browse the repository at this point in the history
* Several improvements for Mac

- Add URL validation for VSTS_URL variable
- Update soon-to-be depracated node attributes from "set" to "default"
- Use homebrew for installing openssl on macOS
- More detailed and updated testing guide
- Fix style and foodcritic
  • Loading branch information
americanhanko authored and ivadim committed May 26, 2017
1 parent a8a9ef4 commit d6c2655
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 155 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bin
.kitchen.local.yml
.coverage
.kitchen/
.idea/
37 changes: 33 additions & 4 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,58 @@ platforms:
provisioner:
require_chef_omnibus: 12.20.3

# private boxes
# The following platforms are private boxes and are not available from the vagrant box repository
# Follow instructions provided in TESTING.md to create a box

# To a use a private box:
# 1. Create a box. To do so, follow the instructions in TESTING.md
# 2. Once you've added the vagrant box, modify this file to use it
# 3. In the 'suites' section of this file, list your box name(s) under 'includes' for the corresponding platform
# 4. Confirm your box shows up when running `vagrant box list` and `kitchen list` when in this cookbook directory

- name: osx109-desktop
driver_config:
box: osx109-desktop #private
box: osx109-desktop
provisioner:
require_chef_omnibus: 13.0.118

- name: osx1010
driver:
box: osx1010
username: <%= ENV['BOX_USERNAME'] %>
provisioner:
require_chef_omnibus: 13.0.118

- name: windows10
driver_config:
guest: windows
communicator: 'winrm'
box: win10x64-enterprise #private
box: win10x64-enterprise
provisioner:
require_chef_omnibus: 12.20.3

- name: windows-2012-r2
driver_config:
guest: windows
communicator: 'winrm'
box: windows-2012-r2
provisioner:
require_chef_omnibus: 13.0.118

suites:
- name: windows-basic
run_list:
- recipe[windows-basic::default]
includes:
- windows10
- windows-2012-r2
attributes:
vsts_agent_test:
username: <%= ENV['BOX_USERNAME'] %>
vsts_url: <%= ENV['VSTS_URL'] %>
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>

- name: debian-basic
run_list:
- recipe[debian-basic::default]
Expand All @@ -65,6 +90,7 @@ suites:
- ubuntu1604
attributes:
vsts_agent_test:
username: <%= ENV['BOX_USERNAME'] %>
vsts_url: <%= ENV['VSTS_URL'] %>
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>
Expand All @@ -74,9 +100,12 @@ suites:
- recipe[osx-basic::default]
includes:
- osx109-desktop
- osx1010

attributes:
vsts_agent_test:
username: <%= ENV['BOX_USERNAME'] %>
vsts_url: <%= ENV['VSTS_URL'] %>
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>

7 changes: 1 addition & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ Style/SpaceBeforeFirstArg:
Exclude:
- '**/metadata.rb'

HashSyntax:
EnforcedStyle: hash_rockets
Exclude:
- '**/Berksfile'

Metrics/MethodLength:
Max: 30

Metrics/AbcSize:
Max: 30

EndOfLine:
Enabled: false
Enabled: false
6 changes: 3 additions & 3 deletions Berksfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cookbook 'ark', git: 'git://github.com/ivadim/ark.git'
metadata

group :integration do
cookbook 'debian-basic', :path => './test/cookbooks/debian-basic'
cookbook 'windows-basic', :path => './test/cookbooks/windows-basic'
cookbook 'osx-basic', :path => './test/cookbooks/osx-basic'
cookbook 'debian-basic', path: './test/cookbooks/debian-basic'
cookbook 'windows-basic', path: './test/cookbooks/windows-basic'
cookbook 'osx-basic', path: './test/cookbooks/osx-basic'
end
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ One of the easiest ways to contribute is to participate in discussions and discu
Please start a discussion on the [issue tracker](https://github.com/Microsoft/vsts-agent-cookbook/issues).

## Contributing code and content
Get familiar with github pull requests https://help.github.com/articles/using-pull-requests/
Get familiar with GitHub pull requests https://help.github.com/articles/using-pull-requests/

You will need to sign a [Contributor License Agreement](https://cla2.dotnetfoundation.org/) before submitting your pull request.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ The following platforms are tested and supported:
- Debian 8 x64 (Jessie)
- Ubuntu 16.04
- Windows 10
- Mac OS X 10.11.4
- Windows Server 2012 R2
- OS X 10.10.5
- OS X 10.11.4

Attributes
----------
* `node['vsts_agent']['binary']['version']` - set version of package to install
* `node['vsts_agent']['prerequisites']['osx']['install']` - control osx dependencies installation. Default true
* `node['vsts_agent']['prerequisites']['osx']['install']` - control macos dependencies installation. Default true
* `node['vsts_agent']['prerequisites']['debian']['install']` - control debian dependencies installation. Default true

Resource/Provider
Expand All @@ -39,8 +41,8 @@ This resource installs and configures the vsts build and release agent
- `agent_name`: Name attribute. The name of the vsts agent
- `version`: an agent version to install. Default version from an attribute
- `install_dir`: A target directory to install the vsts agent
- `path`: Overwrite system PATH environment variable values. Linux and Mac OS X only
- `env`: Additional environment variables. Linux and Mac OS X only
- `path`: Overwrite system PATH environment variable values. Linux and macOS only
- `env`: Additional environment variables. Linux and macOS only
- `user`: Set a local user to run the vsts agent
- `group`: Set a local group to run the vsts agent
- `runasservice`: run agent as a service. Default 'true'
Expand Down
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace :style do
end

desc 'Run all style checks'
task :style => ['style:chef', 'style:ruby']
task style: ['style:chef', 'style:ruby']

desc 'Run ChefSpec'
RSpec::Core::RakeTask.new(:spec)
Expand All @@ -29,8 +29,8 @@ task :supermarket do
exec 'chef exec knife supermarket share vsts_agent Other -o .. -k supermarket.pem -u vsts_agent_cookbook'
end

task :default => ['style', 'kitchen:linux']
task default: ['style', 'kitchen:linux']

task :travis => ['style']
task travis: ['style']

task :release => ['supermarket']
task release: ['supermarket']
69 changes: 43 additions & 26 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,67 @@
# Testing Documentation

## Prerequisites
VSTS Build Agent cookbook requires ChefDK installation. ChefDK can be downloaded at https://downloads.chef.io/chef-dk/
VSTS Build Agent cookbook requires ChefDK installation - download it [here](https://downloads.chef.io/chef-dk/).

Integration testing uses Hashicorp's [Vagrant](https://www.vagrantup.com/downloads.html) and Oracle's [Virtualbox](https://www.virtualbox.org/wiki/Downloads), which must be installed first.

## Style Testing
Ruby and Chef([Foodcritic](http://www.foodcritic.io/)) style checks can be performed by running:
```
chef exec rake style
```
or
```
rake style
```
You'll also need the following from VSTS:
- Username
- [Account URL](https://www.visualstudio.com/en-us/get-started/setup/sign-up-for-visual-studio-online)
- [Personal access token](http://roadtoalm.com/2015/07/22/using-personal-access-tokens-to-access-visual-studio-online/)
- [Build pool name](http://blog.devmatter.com/understanding-pools-and-queues-in-vso/)

### Integration testing using boxes and Virtualbox
Integration tests are orchestrated by [test-kitchen](https://github.com/test-kitchen/test-kitchen).
Although Linux boxes are freely available from Vagrant's repository, for Mac and Windows, you'll need to build your own using Hashicorp's [Packer](https://www.packer.io/).

## Integration Testing
Integration tests are orchestrated by [test-kitchen](https://github.com/test-kitchen/test-kitchen). Currently kitchen.yml contains mix of public(linux) and private boxes(Windows and MacOSX). Windows and MacOSX boxes can be built locally with help of Hashicorp's [packer](https://www.packer.io/) tool.
:small_red_triangle:NOTE: To use MacOSX boxes you need an Apple-branded computer.
#### Guides for building Mac and Windows boxes
- [Mac VM templates for Packer and VeeWee](https://github.com/timsutton/osx-vm-templates)
- [Windows Packer templates](https://github.com/joefitzgerald/packer-windows)
- [Boxcutter](https://github.com/boxcutter) (All platforms)

Additionally you need [vsts account url](https://www.visualstudio.com/en-us/get-started/setup/sign-up-for-visual-studio-online), username, [personal access token](http://roadtoalm.com/2015/07/22/using-personal-access-tokens-to-access-visual-studio-online/) and [pool name](http://blog.devmatter.com/understanding-pools-and-queues-in-vso/).

Set environment variables
### Running the tests
#### Set environment variables

on windows:
Windows:
```
set VSTS_URL=account.visualstudio.com
set VSTS_URL='https://account.visualstudio.com'
set VSTS_POOL=default
set VSTS_USER=username
set VSTS_TOKEN=my_secret_token_from_vsts
```
on linux or macosx:
Linux / Mac:
```
export VSTS_URL=account.visualstudio.com
export VSTS_URL='https://account.visualstudio.com'
export VSTS_POOL=default
export VSTS_USER=username
export VSTS_TOKEN=my_secret_token_from_vsts
```

To run test against specific platform run:
If the username of the box you're testing differs from standard "vagrant":
```
export BOX_USERNAME=username
```

#### Executing the tests
The `kitchen test` instance states are in order: destroy, create, converge, setup, verify, destroy. `kitchen test` changes the state of one or more instances to destroyed, then executes the actions for each state up to destroy. At any sign of failure, executing the actions stops and the instance is left in the last successful execution state.
```
kitchen verify PLATFORM
kitchen test VAGRANT_BOX_NAME
```

Available platforms:
* debian8 (public)
* ubuntu1604 (public)
* osx109-desktop (private)
* windows10 (private)
#### Examples:
`$ kitchen test osx1010`
`$ kitchen test windows_2012_r2`
`$ kitchen test ubuntu1604`


:small_red_triangle: The Windows and Mac boxes must be [built](#guides-for-building-mac-and-windows-boxes) prior to testing. Once built, you'll need to modify the `.kitchen.yml` file. To find out list of available boxes and their names, simply execute `vagrant box list`. See the [Chef documentation](https://docs.chef.io/config_yml_kitchen.html) or comments in `.kitchen.yml` for how to modify it for your boxes.

#### Style Testing
Several style checks can be performed by running any of the following:

`$ chef exec rake style`
`$ rake style`
`$ foodcritic .`
`$ rubocop .`
3 changes: 0 additions & 3 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@
end

default['vsts_agent']['prerequisites']['osx']['install'] = true
default['vsts_agent']['prerequisites']['osx']['openssl']['url'] = 'https://www.openssl.org/source/openssl-1.0.2h.tar.gz'
default['vsts_agent']['prerequisites']['osx']['openssl']['version'] = '1.0.2h'

default['vsts_agent']['prerequisites']['debian']['install'] = true
default['vsts_agent']['prerequisites']['debian']['libicu52']['url'] = 'http://security.ubuntu.com/ubuntu/pool/main/i/icu/libicu52_52.1-8ubuntu0.2_amd64.deb'
18 changes: 11 additions & 7 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def archive_name(resource)

def download_url(version, node)
url = node['vsts_agent']['binary']['url']
url = url.gsub '%s', version
url
url.gsub '%s', version
end

def windows?
Expand All @@ -44,8 +43,13 @@ def agent_exists?(install_dir)
::File.exist?("#{install_dir}/.agent")
end

def valid_vsts_url?(url)
vsts_url_pattern = %r{https:\/\/.*\.visualstudio\.com}
vsts_url_pattern.match(url)
end

def save_vars(resource, node)
VARS_TO_SAVE.each { |var| node.set['vsts_agent']['agents'][resource.agent_name][var] = resource.send(var) if resource.respond_to?(var.to_sym) }
VARS_TO_SAVE.each { |var| node.default['vsts_agent']['agents'][resource.agent_name][var] = resource.send(var) if resource.respond_to?(var.to_sym) }
node.save
end

Expand All @@ -64,7 +68,7 @@ def load_current_state(resource, node)
end

def load_data_from_json(resource)
f = ::File.read(::File.join(resource.install_dir, '.agent'), :mode => 'r:bom|utf-8').strip
f = ::File.read(::File.join(resource.install_dir, '.agent'), mode: 'r:bom|utf-8').strip
agent = JSON.parse(f)
resource.vsts_url(agent['serverUrl'])
resource.vsts_pool(agent['poolName'])
Expand All @@ -83,7 +87,7 @@ def agent_attribute?(agent_name, node)
end

def remove_current_state(resource, node)
node.set['vsts_agent']['agents'][resource.agent_name] = {}
node.default['vsts_agent']['agents'][resource.agent_name] = {}
node.save
end

Expand All @@ -100,13 +104,13 @@ def set_auth(args, resource)
def vsagentexec(args = {})
command = 'Agent.Listener '
command = './' + command unless windows?
args.each { |key, value| command += append_arguments(key, value) + ' ' }
args.each { |key, value| command += append_arguments(key.to_s, value.to_s) + ' ' }
command
end

def append_arguments(key, value)
result = ''
if key == 'configure' || key == 'remove'
if key.include?('configure') || key.include?('remove')
result += key
else
result += "--#{key}"
Expand Down
12 changes: 6 additions & 6 deletions libraries/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class VstsAgentService < Chef::Resource::LWRPBase

default_action :enable

attribute :name, :kind_of => String, :name_attribute => true
attribute :install_dir, :kind_of => String
attribute :user, :kind_of => String
attribute :group, :kind_of => String
attribute :name, kind_of: String, name_attribute: true
attribute :install_dir, kind_of: String
attribute :user, kind_of: String
attribute :group, kind_of: String
end
end
end
Expand Down Expand Up @@ -94,13 +94,13 @@ def vsts_unix_service(operation)
elsif operation == 'disable'
operation = 'uninstall'
end
envvars = { 'HOME' => "/Users/#{new_resource.user}" }
envvars = { HOME: "/Users/#{new_resource.user}" }
execute "Run action '#{operation}' on service '#{new_resource.name}'" do
cwd new_resource.install_dir
command "./svc.sh #{operation}"
user new_resource.user if osx?
group new_resource.group if osx?
environment envvars if osx?
environment envvars.to_s if osx?
action :run
retries 3
end
Expand Down
8 changes: 4 additions & 4 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
source_url 'https://github.com/Microsoft/vsts-agent-cookbook' if respond_to?(:source_url)
issues_url 'https://github.com/Microsoft/vsts-agent-cookbook/issues' if respond_to?(:issues_url)
chef_version '>= 12.4' if respond_to?(:chef_version)
version '1.2.0'

%w(ubuntu debian mac_os_x mac_os_x_server windows).each do |os|
supports os
%w(ubuntu debian mac_os_x mac_os_x_server windows).each do |operating_system|
supports operating_system
end

chef_version '>= 12.4' if respond_to?(:chef_version)

depends 'homebrew'
depends 'windows'
depends 'ark'
depends 'seven_zip', '~> 2.0.0'
Loading

0 comments on commit d6c2655

Please sign in to comment.