Skip to content

Commit

Permalink
feat: add scripts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinLoeper committed Jun 5, 2021
1 parent 6c1ac8e commit 2972c6d
Show file tree
Hide file tree
Showing 15 changed files with 3,273 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.swp

tests/node_modules
raspios/
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ RUN apt-get -y install build-essential cmake
RUN cmake fatcat-* -DCMAKE_CXX_FLAGS='-static'
RUN make -j$(nproc)


# Build the dockerpi VM image
FROM busybox:1.31 AS dockerpi-vm
# note: switched to alpine in order to install mtools easily
FROM alpine AS dockerpi-vm
LABEL maintainer="Luke Childs <[email protected]>"
ARG RPI_KERNEL_URL="https://github.com/dhruvvyas90/qemu-rpi-kernel/archive/afe411f2c9b04730bcc6b2168cdc9adca224227c.zip"
ARG RPI_KERNEL_CHECKSUM="295a22f1cd49ab51b9e7192103ee7c917624b063cc5ca2e11434164638aad5f4"
Expand All @@ -80,6 +80,8 @@ RUN cd /tmp && \
cp qemu-rpi-kernel-*/versatile-pb.dtb /root/qemu-rpi-kernel/ && \
rm -rf /tmp/*

RUN apk add mtools

VOLUME /sdcard

ADD ./entrypoint.sh /entrypoint.sh
Expand All @@ -90,8 +92,8 @@ ENTRYPOINT ["./entrypoint.sh"]
# It's just the VM image with a compressed Raspbian filesystem added
FROM dockerpi-vm as dockerpi
LABEL maintainer="Luke Childs <[email protected]>"
ARG FILESYSTEM_IMAGE_URL="http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/2019-09-26-raspbian-buster-lite.zip"
ARG FILESYSTEM_IMAGE_CHECKSUM="a50237c2f718bd8d806b96df5b9d2174ce8b789eda1f03434ed2213bbca6c6ff"
ARG FILESYSTEM_IMAGE_URL
ARG FILESYSTEM_IMAGE_CHECKSUM

ADD $FILESYSTEM_IMAGE_URL /filesystem.zip

Expand Down
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ if [ "${kernel_pattern}" ] && [ "${dtb_pattern}" ]; then
mkdir -p "${fat_folder}"
fatcat -x "${fat_folder}" "${fat_path}"

# add ssh file to boot folder to enable sshd on startup
# note: already included in the image via enable-ssh-for-image.sh
#echo "Add /boot/ssh to enable sshd on startup"
#touch ssh
#mcopy -i "${fat_path}" ssh ::ssh

root=/dev/mmcblk0p2

echo "Searching for kernel='${kernel_pattern}'"
Expand Down
96 changes: 96 additions & 0 deletions scripts/enable-ssh-for-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash
# Andrew Oakley aoakley.com Public Domain 2016
# Check out cotswoldjam.org for RPi events in Gloucestershire
# I recommend you place this script in /usr/local/sbin

# Help
if [[ "$1" == "-h" || "$1" == "/?" || "$1" == "--help" ]]; then
echo "Enables SSH on a Raspbian image from Nov 2016 or later"
echo "Usage:"
echo " sudo `basename $0` [imagename]"
echo "If imagename is not supplied, downloads the latest version of Raspbian Jessie Lite."
exit
fi

# Need to be root - we'll be mounting loopback device
if [ "$(id -u)" != 0 ]; then
echo "You must be root to run this. Try:"
echo " sudo `basename $0` $*"
echo "or"
echo " `basename $0` -h"
echo "for help"
exit
fi

# Clear up from previous aborted run?
if [[ -e rpi-boot ]]; then
echo "Please remove 'rpi-boot'"
exit
fi

if [[ -z "$1" ]]; then
# No filename? Download latest Jessie Lite.

# Clear up from previous aborted run, part 2
if [[ -e rpi-work ]]; then
echo "Please remove 'rpi-work'"
exit
fi

# Make a working directory and download
mkdir rpi-work
cd rpi-work
curl -L "https://downloads.raspberrypi.org/raspbian_lite_latest" -o raspbian_lite_latest.zip
unzip raspbian_lite_latest.zip

# Did we get what we were expecting?
if [[ `ls -1 *-raspbian-jessie-lite.img | wc -l` -ne 1 ]]; then
echo "Can't find \"*-raspbian-jessie-lite.img\" in raspbian_lite_latest"
exit
fi
rm raspbian_lite_latest.zip

# If run with sudo , change ownership to real user
FILEPATH=`ls -1 *-raspbian-jessie-lite.img`
CALLER=`who am i | awk '{print $1}'`
if [ "$CALLER" != "root" ]; then
chown $CALLER.`groups $CALLER | awk '{print $1}'` "$FILEPATH" 2>/dev/null
fi
else
# Filename supplied, check it exists
FILEPATH="$1"
if [[ ! -f "$FILEPATH" ]]; then
echo "Can't find \"$FILEPATH\" (or it isn't a normal file)"
exit
fi
fi

# Find the first, DOS partition and mount it
BOOTSTART=`fdisk -l "$FILEPATH" | sed -nr "s/^\S+1\s+([0-9]+).* c W95 FAT32 \(LBA\)$/\1/p"`
if [[ -z "$BOOTSTART" ]]; then
echo "Can't find FAT32 first partition in image \"$FILEPATH\""
exit
fi
losetup /dev/loop10 "$FILEPATH" -o $((BOOTSTART*512))
mkdir rpi-boot
mount /dev/loop10 rpi-boot

# Make the change
if [[ -e "rpi-boot/ssh" ]]; then
echo "\"`basename "$FILEPATH"`\" ALREADY had boot/ssh set."
else
touch rpi-boot/ssh
echo "\"`basename "$FILEPATH"`\" now has boot/ssh set."
fi

# Unmount and clear up
umount rpi-boot
rmdir rpi-boot
losetup -d /dev/loop0

# Back out of second working dir if we did a download
if [[ -z "$1" ]]; then
mv -i "$FILEPATH" ..
cd ..
rmdir rpi-work
fi
4 changes: 4 additions & 0 deletions scripts/kill.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
CONTAINER_NAME=pi

docker kill $CONTAINER_NAME
4 changes: 4 additions & 0 deletions scripts/logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
CONTAINER_NAME=pi

docker logs -f $CONTAINER_NAME
4 changes: 4 additions & 0 deletions scripts/manual-ssh-connection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# you need to install sshpass first, for arch e.g.: yay sshpass
sshpass -praspberry ssh pi@localhost -o PreferredAuthentications=password -o PubkeyAuthentication=no -o StrictHostKeyChecking=no -p 5022
21 changes: 21 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
CONTAINER_NAME=pi
RPI_OS=2021-05-07-raspios-buster-armhf-lite

if [ ! -f "$SCRIPT_DIR/../raspios/$RPI_OS.zip" ]; then
curl --output-dir "$SCRIPT_DIR/../raspios" --output "${RPI_OS}.zip" "https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2021-05-28/$RPI_OS.zip"

echo "Could not find .img file. Trying to unzip archive..."
unzip -d "$SCRIPT_DIR/../raspios" "$SCRIPT_DIR/../raspios/$RPI_OS.zip"

sudo $SCRIPT_DIR/enable-ssh-for-image.sh $SCRIPT_DIR/../raspios/$RPI_OS.img
fi

docker build --target dockerpi-vm -t nesto/dockerpi "$SCRIPT_DIR/.."
docker run --name=$CONTAINER_NAME -v $SCRIPT_DIR/../raspios/$RPI_OS.img:/sdcard/filesystem.img -it --rm -p 5022:5022 nesto/dockerpi pi2

# attach to logs
$SCRIPT_DIR/logs.sh
4 changes: 4 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SSH_PORT=5022
SSH_HOSTNAME=localhost
SSH_USER=pi
SSH_PASSWD=raspberry
194 changes: 194 additions & 0 deletions tests/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",

// Automatically clear mock calls and instances between every test
// clearMocks: false,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
// coverageDirectory: undefined,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: undefined,

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state between every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state between every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
// testEnvironment: "jest-environment-node",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",

// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: undefined,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};
Loading

0 comments on commit 2972c6d

Please sign in to comment.