Skip to content

Commit

Permalink
Merge pull request #76 from drone-runners/compiler-unit-tests
Browse files Browse the repository at this point in the history
compiler unit tests
  • Loading branch information
TP Honey authored Feb 22, 2022
2 parents 19af767 + 69b2b0d commit 10f6dc3
Show file tree
Hide file tree
Showing 24 changed files with 831 additions and 480 deletions.
395 changes: 197 additions & 198 deletions engine/compiler/compiler.go

Large diffs are not rendered by default.

110 changes: 67 additions & 43 deletions engine/compiler/_compiler_test.go → engine/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ package compiler

import (
"context"
"encoding/base64"
"encoding/json"
"os"
"sort"
"testing"

"github.com/drone-runners/drone-runner-aws/internal/vmpool/cloudaws"

"github.com/drone-runners/drone-runner-aws/internal/vmpool"

"github.com/drone-runners/drone-runner-aws/engine"
"github.com/drone-runners/drone-runner-aws/engine/resource"
"github.com/drone-runners/drone-runner-aws/internal/vmpool"
"github.com/drone-runners/drone-runner-aws/internal/vmpool/cloudaws"

"github.com/drone/drone-go/drone"
"github.com/drone/runner-go/environ/provider"
"github.com/drone/runner-go/manifest"
"github.com/drone/runner-go/pipeline/runtime"
"github.com/drone/runner-go/secret"
lespec "github.com/harness/lite-engine/engine/spec"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -97,48 +98,49 @@ func TestCompile_RunFailure(t *testing.T) {
}
}

// This test verifies that secrets defined in the yaml are
// requested and stored in the intermediate representation
// at compile time.
func TestCompile_Secrets(t *testing.T) {
mnfst, _ := manifest.ParseFile("testdata/secret.yml")
// This test verifies the pipelines with container images and services.
func TestCompile_Image(t *testing.T) {
testCompile(t, "testdata/image.yml", "testdata/image.json")
}

pools, err := cloudaws.ProcessPoolFile("testdata/drone_pool.yml", &defaultPoolSettings)
if err != nil {
t.Error(err)
return
// This test verifies the pipelines with container images that use volumes.
func TestCompile_Plugin(t *testing.T) {
ir := testCompile(t, "testdata/plugins.yml", "testdata/plugins.json")
if ir.Steps[1].Envs["PLUGIN_LOCATION"] != "production" {
t.Error("incorrect or missing 'location' setting from the step environment")
}

poolManager := &vmpool.Manager{}
err = poolManager.Add(pools...)
if err != nil {
t.Error(err)
return
if ir.Steps[1].Envs["PLUGIN_LOCATION"] != "production" {
t.Error("incorrect or missing 'location' setting from the step environment")
}

compiler := &Compiler{
Environ: provider.Static(nil),
Secret: secret.StaticVars(map[string]string{
"token": "3DA541559918A808C2402BBA5012F6C60B27661C",
"password": "password",
"my_username": "octocat",
}),
PoolManager: poolManager,
var username, password string
for _, s := range ir.Steps[1].Secrets {
if s.Env == "PLUGIN_USERNAME" {
username = string(s.Data)
} else if s.Env == "PLUGIN_PASSWORD" {
password = string(s.Data)
}
}
args := runtime.CompilerArgs{
Repo: &drone.Repo{},
Build: &drone.Build{},
Stage: &drone.Stage{},
System: &drone.System{},
Netrc: &drone.Netrc{},
Manifest: mnfst,
Pipeline: mnfst.Resources[0].(*resource.Pipeline),
Secret: secret.Static(nil),
if username != "octocat" {
t.Error("incorrect or missing 'username' setting from the step secrets")
}
if password != "password" {
t.Error("incorrect or missing 'password' setting from the step secrets")
}
}

// This test verifies the pipelines with container images that use volumes.
func TestCompile_Image_Volumes(t *testing.T) {
testCompile(t, "testdata/volumes.yml", "testdata/volumes.json")
}

// This test verifies that secrets defined in the yaml are
// requested and stored in the intermediate representation
// at compile time.
func TestCompile_Secrets(t *testing.T) {
ir := testCompile(t, "testdata/secret.yml", "testdata/secret.json")

ir := compiler.Compile(nocontext, args).(*engine.Spec)
got := ir.Steps[0].Secrets
want := []*engine.Secret{
want := []*lespec.Secret{
{
Name: "my_password",
Env: "PASSWORD",
Expand All @@ -152,10 +154,16 @@ func TestCompile_Secrets(t *testing.T) {
Mask: true,
},
}

sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
sort.Slice(want, func(i, j int) bool {
return want[i].Name < want[j].Name
})

if diff := cmp.Diff(got, want); diff != "" {
// BUG(bradrydzewski) ordering is not guaranteed. this
// unit tests needs to be adjusted accordingly.
t.Skipf(diff)
t.Errorf(diff)
}
}

Expand Down Expand Up @@ -220,11 +228,27 @@ func testCompile(t *testing.T, source, golden string) *engine.Spec {
err = json.Unmarshal(raw, want)
if err != nil {
t.Error(err)
return want
}

// convert file data to base64 for easier comparison
for _, f := range got.(*engine.Spec).Files {
if !f.IsDir {
f.Data = base64.StdEncoding.EncodeToString([]byte(f.Data))
}
}
for _, step := range got.(*engine.Spec).Steps {
for _, f := range step.Files {
if !f.IsDir {
f.Data = base64.StdEncoding.EncodeToString([]byte(f.Data))
}
}
}

opts := cmp.Options{
cmpopts.IgnoreUnexported(engine.Spec{}),
cmpopts.IgnoreFields(engine.Step{}, "Envs", "Secrets"),
cmpopts.IgnoreFields(lespec.VolumeHostPath{}, "Labels"),
cmpopts.IgnoreFields(lespec.VolumeEmptyDir{}, "Labels"),
}
if diff := cmp.Diff(got, want, opts...); diff != "" {
t.Errorf("%s\n%v", t.Name(), diff)
Expand Down
50 changes: 18 additions & 32 deletions engine/compiler/testdata/graph.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"root": "/tmp/aws",
"name": "default",
"cloud_instance": {
"pool_name": "ubuntu",
"cloud": "",
"id": "",
"ip": ""
"pool_name": "ubuntu"
},
"files": [
{
Expand Down Expand Up @@ -41,11 +37,10 @@
],
"steps": [
{
"args": [
"-e",
"/tmp/aws/opt/clone"
],
"command": "/bin/sh",
"id": "random",
"name": "clone",
"entrypoint": ["sh", "-c"],
"args": ["/tmp/aws/opt/clone"],
"environment": {},
"files": [
{
Expand All @@ -54,49 +49,40 @@
"data": "CnNldCAtZQoKZWNobyArICJnaXQgaW5pdCIKZ2l0IGluaXQKCmVjaG8gKyAiZ2l0IHJlbW90ZSBhZGQgb3JpZ2luICIKZ2l0IHJlbW90ZSBhZGQgb3JpZ2luIAoKZWNobyArICJnaXQgZmV0Y2ggIG9yaWdpbiArcmVmcy9oZWFkcy9tYXN0ZXI6IgpnaXQgZmV0Y2ggIG9yaWdpbiArcmVmcy9oZWFkcy9tYXN0ZXI6CgplY2hvICsgImdpdCBjaGVja291dCAgLWIgbWFzdGVyIgpnaXQgY2hlY2tvdXQgIC1iIG1hc3Rlcgo="
}
],
"name": "clone",
"run_policy": "always",
"working_dir": "/tmp/aws/drone/src"
},
{
"args": [
"-e",
"/tmp/aws/opt/build"
],
"command": "/bin/sh",
"depends_on": [
"clone"
],
"id": "random",
"name": "build",
"entrypoint": ["sh", "-c"],
"args": ["/tmp/aws/opt/random"],
"depends_on": ["clone"],
"environment": {},
"files": [
{
"path": "/tmp/aws/opt/build",
"path": "/tmp/aws/opt/random",
"mode": 448,
"data": "CnNldCAtZQoKZWNobyArICJnbyBidWlsZCIKZ28gYnVpbGQK"
}
],
"name": "build",
"working_dir": "/tmp/aws/drone/src"
},
{
"args": [
"-e",
"/tmp/aws/opt/test"
],
"command": "/bin/sh",
"depends_on": [
"build"
],
"id": "random",
"name": "test",
"entrypoint": ["sh", "-c"],
"args": ["/tmp/aws/opt/random"],
"depends_on": ["build"],
"environment": {},
"files": [
{
"path": "/tmp/aws/opt/test",
"path": "/tmp/aws/opt/random",
"mode": 448,
"data": "CnNldCAtZQoKZWNobyArICJnbyB0ZXN0IgpnbyB0ZXN0Cg=="
}
],
"name": "test",
"working_dir": "/tmp/aws/drone/src"
}
]
}
}
16 changes: 8 additions & 8 deletions engine/compiler/testdata/graph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ name: default

pool:
use: ubuntu

steps:
- name: build
commands:
- go build
- name: build
commands:
- go build

- name: test
commands:
- go test
depends_on: [ build ]
- name: test
commands:
- go test
depends_on: [ build ]
97 changes: 97 additions & 0 deletions engine/compiler/testdata/image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "default",
"cloud_instance": {
"pool_name": "ubuntu"
},
"files": [
{
"path": "/tmp/aws/home",
"mode": 448,
"is_dir": true
},
{
"path": "/tmp/aws/home/drone",
"mode": 448,
"is_dir": true
},
{
"path": "/tmp/aws/drone",
"mode": 448,
"is_dir": true
},
{
"path": "/tmp/aws/drone/src",
"mode": 448,
"is_dir": true
},
{
"path": "/tmp/aws/opt",
"mode": 448,
"is_dir": true
},
{
"path": "/tmp/aws/home/drone/.netrc",
"mode": 384,
"data": "bWFjaGluZSBnaXRodWIuY29tIGxvZ2luIG9jdG9jYXQgcGFzc3dvcmQgY29ycmVjdC1ob3JzZS1iYXR0ZXJ5LXN0YXBsZQ=="
}
],
"steps": [
{
"id": "random",
"name": "clone",
"entrypoint": ["sh", "-c"],
"args": ["/tmp/aws/opt/clone"],
"files": [
{
"path": "/tmp/aws/opt/clone",
"mode": 448,
"data": "CnNldCAtZQoKZWNobyArICJnaXQgaW5pdCIKZ2l0IGluaXQKCmVjaG8gKyAiZ2l0IHJlbW90ZSBhZGQgb3JpZ2luICIKZ2l0IHJlbW90ZSBhZGQgb3JpZ2luIAoKZWNobyArICJnaXQgZmV0Y2ggIG9yaWdpbiArcmVmcy9oZWFkcy9tYXN0ZXI6IgpnaXQgZmV0Y2ggIG9yaWdpbiArcmVmcy9oZWFkcy9tYXN0ZXI6CgplY2hvICsgImdpdCBjaGVja291dCAgLWIgbWFzdGVyIgpnaXQgY2hlY2tvdXQgIC1iIG1hc3Rlcgo="
}
],
"run_policy": "always",
"working_dir": "/tmp/aws/drone/src"
},
{
"id": "random",
"name": "redis",
"run_policy": "on-success",
"depends_on": ["clone"],
"detach": true,
"image": "redis",
"privileged": true,
"working_dir": "/drone/src",
"volumes": [
{"name": "source_dir", "path": "/drone/src"}
]
},
{
"id": "random",
"name": "build",
"entrypoint": ["sh", "-c"],
"args": ["/tmp/aws/opt/random"],
"depends_on": ["redis"],
"files": [
{
"path": "/tmp/aws/opt/random",
"mode": 448,
"data": "CnNldCAtZQoKZWNobyArICJnbyB0ZXN0IgpnbyB0ZXN0CgplY2hvICsgImdvIGJ1aWxkIgpnbyBidWlsZAo="
}
],
"image": "golang:latest",
"privileged": true,
"working_dir": "/drone/src",
"volumes": [
{"name": "source_dir", "path": "/drone/src"},
{"name": "script_dir", "path": "/tmp/aws/opt"}
]
}
],
"volumes": [
{
"host": {"id": "source_dir_random", "name": "source_dir", "path": "/tmp/aws/drone/src"}
},
{
"host": {"id": "script_dir_random", "name": "script_dir", "path": "/tmp/aws/opt"}
}
]
}
17 changes: 17 additions & 0 deletions engine/compiler/testdata/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: pipeline
type: aws
name: default

pool:
use: ubuntu

steps:
- name: build
image: golang:latest
commands:
- go test
- go build

services:
- name: redis
image: redis
Loading

0 comments on commit 10f6dc3

Please sign in to comment.