Skip to content

Commit

Permalink
add group tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Sep 9, 2023
1 parent 38bbd17 commit 9a014b6
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 18 deletions.
102 changes: 102 additions & 0 deletions examples/l337/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ tooling:
service:
default: "web"
describe: "Runs in this service"
groups:
cmd: cat /tmp/groups
service: ":service"
options:
service:
default: "groups-1"
describe: "Runs in this service"
pwd:
service: ":service"
options:
Expand Down Expand Up @@ -137,6 +144,101 @@ services:
dest: "/etc/config/available-shanties.json"
owner: "eddie-teach"

# group example
groups-1:
api: 4
image:
imagefile: "nginx:1.21.5"
context:
# COPY ./folder to /folder
- "./folder"
# COPY ./folder to /thing
- "./folder:thing"
# COPY file1 to /file2
- "file1:/file2"
# COPY file1 to /file3
- src: "file1"
dest: "file3"
# COPY file1 to /file4
- source: "file1"
destination: "/tmp/file4"
# COPY file1 to /file4 with instruction overrides
- source: "file1"
destination: "file4"
instructions: "COPY /file4 /tmp/stuff/file5"
- source: "file1"
destination: "file4"
instructions: |
COPY /file4 /file6
ENV HALL OATES
groups:
# adds a group to potentially conflict with an override
- name: "system-4"
description: "Test group override syntax"
weight: -100000
user: "root"
# adds a root user group called "system" that runs first
- name: "system"
description: "Install system packages and stuff"
weight: -10000
user: "root"
# adds a nginx user group called "user" that runs last
- name: "user"
description: "Allows for user run commands after other groups"
weight: 10000
user: "nginx"
# adds a group called "val-jean" with weight "24601"
# uses root user by default
- val-jean: 24601
steps:
# direct group usage
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "user"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "default"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "context"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system"
- instructions: |
RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
RUN chmod 777 /tmp/groups
group: "system-4"
# override group usage
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system-2"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system-3-after"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system-nginx-after-4"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system-5-after-nginx"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "system-10-before-nginx"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean-2"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean-3-after"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean-nginx-after-4"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean-5-after-nginx"
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: "val-jean-10-before-nginx"
- instructions: |
RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
USER root
group: "val-jean-100-after"
# other cases
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
weight: -99999999
- instructions: RUN echo "${LANDO_IMAGE_USER}-${LANDO_IMAGE_GROUP}-$(whoami)" >> /tmp/groups
group: user
weight: 10

networks:
my-network:
volumes:
Expand Down
44 changes: 44 additions & 0 deletions examples/l337/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,50 @@ lando stat /SeaShanties/lyrics/main/shanties/HeresAHealthToTheCompany.json
lando stat /etc/config/available-shanties.json
lando stat -c "%U:%G" /etc/config/available-shanties.json | grep eddie-teach:eddie-teach
lando env --service context-1 | grep HALL | grep OATES

# should order build steps correctly
lando groups | sed -n '1p' | grep root-default--99999999-root-root
lando groups | sed -n '2p' | grep root-system-4--100000-root-root
lando groups | sed -n '3p' | grep nginx-system--10010-nginx-nginx
lando groups | sed -n '4p' | grep root-system--10000-root-root
lando groups | sed -n '5p' | grep root-system--9998-root-root
lando groups | sed -n '6p' | grep root-system--9997-root-root
lando groups | sed -n '7p' | grep nginx-system--9996-nginx-nginx
lando groups | sed -n '8p' | grep nginx-system--9995-nginx-nginx
lando groups | sed -n '9p' | grep root-context-0-root-root
lando groups | sed -n '10p' | grep nginx-user-10-nginx-nginx
lando groups | sed -n '11p' | grep root-default-1000-root-root
lando groups | sed -n '12p' | grep root-default-1000-root-root
lando groups | sed -n '13p' | grep nginx-user-10000-nginx-nginx
lando groups | sed -n '14p' | grep nginx-val-jean-24591-nginx-nginx
lando groups | sed -n '15p' | grep root-val-jean-24601-root-root
lando groups | sed -n '16p' | grep root-val-jean-24603-root-root
lando groups | sed -n '17p' | grep root-val-jean-24604-root-root
lando groups | sed -n '18p' | grep nginx-val-jean-24605-nginx-nginx
lando groups | sed -n '19p' | grep nginx-val-jean-24606-nginx-nginx
lando groups | sed -n '20p' | grep root-val-jean-24701-root-root

# should run build steps as the correct user
lando groups | sed -n '1p' | grep root-default--99999999-root-root
lando groups | sed -n '2p' | grep root-system-4--100000-root-root
lando groups | sed -n '3p' | grep nginx-system--10010-nginx-nginx
lando groups | sed -n '4p' | grep root-system--10000-root-root
lando groups | sed -n '5p' | grep root-system--9998-root-root
lando groups | sed -n '6p' | grep root-system--9997-root-root
lando groups | sed -n '7p' | grep nginx-system--9996-nginx-nginx
lando groups | sed -n '8p' | grep nginx-system--9995-nginx-nginx
lando groups | sed -n '9p' | grep root-context-0-root-root
lando groups | sed -n '10p' | grep nginx-user-10-nginx-nginx
lando groups | sed -n '11p' | grep root-default-1000-root-root
lando groups | sed -n '12p' | grep root-default-1000-root-root
lando groups | sed -n '13p' | grep nginx-user-10000-nginx-nginx
lando groups | sed -n '14p' | grep nginx-val-jean-24591-nginx-nginx
lando groups | sed -n '15p' | grep root-val-jean-24601-root-root
lando groups | sed -n '16p' | grep root-val-jean-24603-root-root
lando groups | sed -n '17p' | grep root-val-jean-24604-root-root
lando groups | sed -n '18p' | grep nginx-val-jean-24605-nginx-nginx
lando groups | sed -n '19p' | grep nginx-val-jean-24606-nginx-nginx
lando groups | sed -n '20p' | grep root-val-jean-24701-root-root
```

Destroy tests
Expand Down
31 changes: 13 additions & 18 deletions plugins/lando-services-v4/lib/l337-v4.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ class ComposeServiceV4 {

// ensure instructions are an array
if (typeof file.instructions === 'string') file.instructions = [`${file.instructions}`];
// just normalize the usage of newlines
file.instructions = file.instructions
.map(instruction => instruction.split('\n').filter(part => part && part !== ''))
.flat(Number.POSITIVE_INFINITY);
// add an empty element so we can newline consistently
file.instructions.push('');

// remove extraneous keys
if (isObject(file) && file.dest) delete file.dest;
Expand Down Expand Up @@ -356,22 +350,16 @@ class ComposeServiceV4 {
if (step.group && this.getOverrideGroup(step.group) && this.getGroupOverrides(step.group)) {
step = merge({}, step, this.getGroupOverrides(step.group));
}

// if no group or a group we cannot match then assume the default group
if (!step.group || this.getOverrideGroup(step.group) !== false) step.group = 'default';

// we should have stnadardized groups at this point so we can rebase on those things in as well
step = merge({}, {stage: this.#data.stages.default, weight: 1000}, this.#data.groups[step.group], step);

// now lets modify the weight by the offset if we have one
if (step.offset && Number(step.offset)) step.weight = step.weight + step.offset;

// and finally lets rewrite the group for better instruction grouping
step.group = `${step.group}-${step.weight}-${step.user}`;

// log
this.debug('%o added build step %o', this.id, step);

// push
this.#data.steps.push(step);
});
Expand Down Expand Up @@ -424,10 +412,19 @@ class ComposeServiceV4 {
steps[group] = data
.map(data => data.instructions)
.map(data => Array.isArray(data) ? generateDockerFileFromArray(data) : data);
// prefix user and comment data
steps[group].unshift(`USER ${user}\n`);
steps[group].unshift(`# ${group}\n`);
steps[group] = steps[group].join('');

// attempt to normalize newling usage mostly for aesthetic considerations
steps[group] = steps[group]
.map(instructions => instructions.split('\n').filter(instruction => instruction && instruction !== ''))
.flat(Number.POSITIVE_INFINITY);
steps[group].push('');

// prefix user and comment data and some helpful envvars
steps[group].unshift(`USER ${user}`);
steps[group].unshift(`ENV LANDO_IMAGE_GROUP ${group}`);
steps[group].unshift(`ENV LANDO_IMAGE_USER ${user}`);
steps[group].unshift(`# group: ${group}`);
steps[group] = steps[group].join('\n');
}

// we should have raw instructions data now
Expand Down Expand Up @@ -461,8 +458,6 @@ class ComposeServiceV4 {
}

getSteps(stage) {
// @TODO: validate stage?

// if we have a stage then filter by that
if (stage) return this.#data.steps.filter(step => step.stage === stage);
// otherwise return the whole thing
Expand Down

0 comments on commit 9a014b6

Please sign in to comment.