Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e tests for Ruby #26

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
name: Ruby Agent CI

on:
pull_request:
paths:
- 'src/ruby/**'
- '.github/workflows/ruby.yml'
push:
paths:
- 'ruby/**'
- '.github/workflows/ruby.yml'
branches:
- main
# Do not run when a tag is created.
tags-ignore:
- "**"
release:
types:
- published
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
INITCONTAINER_LANGUAGE: ruby
K8S_OPERATOR_IMAGE_TAG: edge

jobs:
check-modified-files:
name: Check whether any Ruby-related files were modified, skip the test job if not
uses: ./.github/workflows/check-modified-files.yml
secrets: inherit
permissions:
contents: read
with:
agent-language: ruby

test:
name: Run Ruby init container tests
runs-on: ubuntu-latest
needs: check-modified-files
# run only if files were modified or the workflow was manually invoked
if: needs.check-modified-files.outputs.files-changed == 'true' || github.event_name == 'workflow_dispatch'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will probably want to add || (github.event_name == 'release' && endsWith(github.ref_name, '_ruby') to ensure the tests job runs on release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publish job depends on the test job to run, but from what I understand, publish won't run if test job conditions aren't met. So this is a great call out!

Other agents appear to be missing this condition as well. I'll update Ruby's.


steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # 4.1.1
with:
persist-credentials: false

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # 3.3.0

- name: Start minikube
uses: medyagh/setup-minikube@317d92317e473a10540357f1f4b2878b80ee7b95 # 0.0.16

- name: Deploy cert-manager to minikube
run: |
helm repo add jetstack https://charts.jetstack.io --force-update
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.14.5 --set installCRDs=true
sleep 5
kubectl wait --for=condition=Ready -n cert-manager --all pods

- name: Deploy New Relic k8s-agents-operator to minikube
run: |
helm repo add k8s-agents-operator https://newrelic.github.io/k8s-agents-operator
helm upgrade --install k8s-agents-operator k8s-agents-operator/k8s-agents-operator \
--namespace=default \
--set=licenseKey=${{ secrets.NEW_RELIC_LICENSE_KEY }} \
--set=controllerManager.manager.image.tag=${{ env.K8S_OPERATOR_IMAGE_TAG }}
sleep 5
kubectl wait --for=condition=Ready -n default --all pods

- name: Build init container
run: |
minikube image build -t e2e/newrelic-${{ env.INITCONTAINER_LANGUAGE }}-init:e2e src/${{ env.INITCONTAINER_LANGUAGE }}/

- name: Build test app container
run: |
minikube image build -t e2e/test-app-${{ env.INITCONTAINER_LANGUAGE }}:e2e tests/${{ env.INITCONTAINER_LANGUAGE }}/

- name: Run e2e-test
uses: newrelic/newrelic-integration-e2e-action@a97ced80a4841c8c6261d1f9dca6706b1d89acb1 # 1.11.0
with:
retry_seconds: 60
retry_attempts: 5
agent_enabled: false
spec_path: tests/${{ env.INITCONTAINER_LANGUAGE }}/test-specs.yml
account_id: ${{ secrets.NEW_RELIC_ACCOUNT_ID }}
api_key: ${{ secrets.NEW_RELIC_API_KEY }}
license_key: ${{ secrets.NEW_RELIC_LICENSE_KEY }}

publish:
if: github.event_name == 'release' && endsWith(github.ref, '_ruby')
runs-on: ubuntu-latest
needs:
- test

steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # 4.1.1
with:
persist-credentials: false

- name: Extract Agent Version
id: version
run: |
agent_version=${{ github.ref_name }} # Use tag name
agent_version=${agent_version##v} # Remove v prefix
agent_version=${agent_version%%_${{ env.INITCONTAINER_LANGUAGE }}} # Remove language suffix
echo "agent_version=${agent_version}" | tee -a "$GITHUB_OUTPUT"

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # 3.3.0

- name: Generate Docker metadata (tags and labels)
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # 5.5.1
with:
images: newrelic/newrelic-${{ env.INITCONTAINER_LANGUAGE }}-init
tags: |
type=raw,value=${{ steps.version.outputs.agent_version }}
type=raw,value=latest

- name: Login to Docker Hub Container Registry
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # 3.1.0
with:
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and publish init container image
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # 5.3.0
with:
push: true
context: src/${{ env.INITCONTAINER_LANGUAGE }}/
platforms: linux/amd64,linux/arm64,linux/arm
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
AGENT_VERSION=${{ steps.version.outputs.agent_version }}
18 changes: 18 additions & 0 deletions tests/ruby/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ruby:latest

WORKDIR /app

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

COPY Gemfile ./

RUN bundle install

COPY . .

EXPOSE 4567

#Ensure executable permissions
RUN chmod +x start.sh

CMD ["./start.sh"]
8 changes: 8 additions & 0 deletions tests/ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'sinatra'
gem 'newrelic_rpm'

gem "rackup", "~> 2.1"
37 changes: 37 additions & 0 deletions tests/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
newrelic_rpm (9.10.0)
rack (3.0.11)
rack-protection (4.0.0)
base64 (>= 0.1.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
ruby2_keywords (0.0.5)
sinatra (4.0.0)
mustermann (~> 3.0)
rack (>= 3.0.0, < 4)
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
tilt (2.3.0)
webrick (1.8.1)

PLATFORMS
ruby
x86_64-darwin-23

DEPENDENCIES
newrelic_rpm
rackup (~> 2.1)
sinatra

BUNDLED WITH
2.5.6
13 changes: 13 additions & 0 deletions tests/ruby/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'sinatra'
require 'newrelic_rpm'

set :bind, '0.0.0.0'
set :port, 4567

get '/' do
return 'no new relic txn' unless NewRelic::Agent::Tracer.current_transaction

'hello, world!'
end
23 changes: 23 additions & 0 deletions tests/ruby/chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions tests/ruby/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: test-app-ruby
description: A Helm chart for Kubernetes
type: application
version: 1.0.0
appVersion: "1.0.0"
49 changes: 49 additions & 0 deletions tests/ruby/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app-ruby
spec:
selector:
matchLabels:
app: test-app-ruby
replicas: 1
template:
metadata:
labels:
app: test-app-ruby
annotations:
instrumentation.newrelic.com/inject-ruby: "true"
spec:
containers:
- name: test-app-ruby
image: e2e/test-app-ruby:e2e
imagePullPolicy: Never
ports:
- containerPort: 4567
env:
- name: NEW_RELIC_APP_NAME
value: k8s-e2e-test-app-ruby
# used by the e2e Github action
- name: NEW_RELIC_LABELS
value: "testKey:{{ .Values.scenarioTag | default "NOTSET" }}"
- name: NEW_RELIC_LOG_LEVEL
value: info
# set the host to staging if using a staging license key
# - name: NEW_RELIC_HOST
# value: staging-collector.newrelic.com
# - name: NEW_RELIC_LOG_FILE_PATH
# value: STDOUT

---
apiVersion: v1
kind: Service
metadata:
name: test-app-ruby-service
spec:
type: NodePort
ports:
- port: 4567
targetPort: 4567
selector:
app: test-app-ruby
11 changes: 11 additions & 0 deletions tests/ruby/chart/templates/instrumentation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: newrelic.com/v1alpha1
kind: Instrumentation
metadata:
labels:
app.kubernetes.io/name: instrumentation
app.kubernetes.io/created-by: newrelic-agent-operator
name: newrelic-instrumentation
spec:
ruby:
image: e2e/newrelic-ruby-init:e2e
1 change: 1 addition & 0 deletions tests/ruby/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scenarioTag: ""
3 changes: 3 additions & 0 deletions tests/ruby/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require './app.rb'

run Sinatra::Application
10 changes: 10 additions & 0 deletions tests/ruby/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

# Start ruby app and provide time to boot Puma
ruby app.rb & sleep 5

# Hit endponint
while true; do
curl http://0.0.0.0:4567
sleep 3
done
15 changes: 15 additions & 0 deletions tests/ruby/test-specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
description: End-to-end tests for ruby initcontainer
custom_test_key: tags.testKey
scenarios:
- description: This scenario will verify that a transaction is reported by the test app after a curl request
before:
- helm install test-ruby ./chart/ --set=scenarioTag="${SCENARIO_TAG}" -n default
- sleep 5
- kubectl wait --for=condition=Ready -n default --all pods
- curl --fail-with-body $(minikube service test-app-ruby-service --url -n default)/
tests:
nrqls:
- query: SELECT latest(duration) AS duration FROM Transaction WHERE appName = 'k8s-e2e-test-app-ruby'
expected_results:
- key: "duration"
lowerBoundedValue: 0.0
Loading