Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Sep 26, 2024
1 parent c5aef3b commit be08c3a
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

LD_FLAGS := "-s -w"

# TODO: add needed platforms
PLATFORMS = linux darwin
ARCHITECTURES = amd64

Expand All @@ -20,6 +21,10 @@ build-all: clean
test:
go test ./...

# Run integration tests
test-integration:
go test -tags=integrity ./...

bench:
go test -bench=. -benchmem -run=^# ./...

Expand Down
36 changes: 26 additions & 10 deletions integrity_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integrity
// +build integrity

package main_test

import (
Expand All @@ -16,24 +19,37 @@ func TestRelayIntegration(t *testing.T) {
})
}

// setup prepares the test environment by building the relay binary and copying required license files
func setup(env *testscript.Env) error {
binPath := filepath.Join(env.WorkDir, "relay")
cmd := exec.Command("go", "build", "-o", binPath, "./cmd/relay")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
buildCmd := exec.Command("go", "build", "-o", binPath, "./cmd/relay")
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
return fmt.Errorf("failed to build relay binary: %w", err)
}

env.Setenv("PATH", env.Getenv("PATH")+string(os.PathListSeparator)+env.WorkDir)

licenseSrc := filepath.Join(env.Getenv("TESTSCRIPT_ROOT"), "testdata", "license.lic")
licenseDst := filepath.Join(env.WorkDir, "license.lic")
input, err := os.ReadFile(licenseSrc)
// copy license files into the working directory tests
testscriptRoot := env.Getenv("TESTSCRIPT_ROOT")
if err := copyFile(filepath.Join(testscriptRoot, "testdata", "license.lic"), filepath.Join(env.WorkDir, "license.lic")); err != nil {
return fmt.Errorf("failed to copy license.lic: %w", err)
}
if err := copyFile(filepath.Join(testscriptRoot, "testdata", "license_2.lic"), filepath.Join(env.WorkDir, "license_2.lic")); err != nil {
return fmt.Errorf("failed to copy license_2.lic: %w", err)
}

return nil
}

func copyFile(src, dst string) error {
input, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("failed to read license file: %v", err)
return fmt.Errorf("unable to read file %s: %w", src, err)
}
if err := os.WriteFile(licenseDst, input, 0644); err != nil {
return fmt.Errorf("failed to write license file: %v", err)

if err := os.WriteFile(dst, input, 0644); err != nil {
return fmt.Errorf("unable to write file to %s: %w", dst, err)
}

return nil
Expand Down
25 changes: 25 additions & 0 deletions testdata/claim_conflict.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port as environment variable
env PORT=8082

# Start the server with heartbeat disabled
exec relay serve --port $PORT --no-heartbeats &server_process_test&

# Wait for the server to start
exec sleep 1

# Claim the license for the first time and check status code directly
exec curl -s -o /dev/null -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
stdout '201'

# Claim the license again with the same fingerprint and check status code directly
exec curl -s -o /dev/null -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
stdout '409'

# Stop the server
kill server_process_test
29 changes: 29 additions & 0 deletions testdata/claim_license_conflict.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port as environment variable
env PORT=8083

# Start the server with heartbeat disabled
exec relay serve --port $PORT --no-heartbeats &server_process_test&

# Wait for the server to start
exec sleep 1

# Claim the license for the first time
exec curl -s -o /dev/null -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
stdout '201'

# Claim the license again with the same fingerprint
exec curl -s -o response.txt -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint

# Expect a conflict response with status code 409 and error message
stdout '409'

exec grep '{"error":"License claim conflict, heartbeat disabled"}' response.txt

# Stop the server
kill server_process_test
29 changes: 29 additions & 0 deletions testdata/claim_license_extended.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port as environment variable
env PORT=8087

# Start the server with heartbeat enabled (for extension to work)
exec relay serve --port $PORT &server_process_test&

# Wait for the server to start
exec sleep 1

# Claim a license for the first time
exec curl -s -o /dev/null -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
stdout '201'

# Claim the license again with the same fingerprint to trigger an extension
exec curl -s -o response.txt -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
stdout '202'

# Expect the response to contain "license_file" and "license_key" fields for extended license
exec grep '"license_file":' response.txt
exec grep '"license_key":' response.txt

# Stop the server
kill server_process_test
32 changes: 32 additions & 0 deletions testdata/claim_license_lifo_strategy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Remove existing database
rm -f relay.sqlite

# Add two licenses
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Give a pause between adding licenses
exec sleep 1

exec relay add --file license_2.lic --key 9A96B8-FD08CD-8C433B-7657C8-8A8655-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port and strategy as environment variables
env PORT=8089
env STRATEGY=lifo

# Start the server with FIFO strategy
exec relay serve --port $PORT --strategy $STRATEGY &server_process_test&

# Wait for the server to start
exec sleep 1

# Claim a license (LIFO: should return the last license)
exec curl -s -o response.txt -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint

exec relay ls --plain

# Expect the first license to be returned
exec grep '"license_file":' response.txt
exec grep '"license_key":"9A96B8-FD08CD-8C433B-7657C8-8A8655-V3"' response.txt

# Stop the server
kill server_process_test
27 changes: 27 additions & 0 deletions testdata/claim_license_success.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port as environment variable
env PORT=8090

# Start the server with heartbeat disabled
exec relay serve --port $PORT &server_process_test&

# Wait for the server to start
exec sleep 1

# Claim a license
exec curl -s -o response.txt -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint

# Expect a success response with status code 201
stdout '201'

# Check that the response contains license_file and license_key fields (using grep)
exec grep '"license_file":' response.txt
exec grep '"license_key":' response.txt

# Stop the server
kill server_process_test
18 changes: 18 additions & 0 deletions testdata/claim_no_licenses_available.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Remove existing database
rm -f relay.sqlite

# Start the server without adding any licenses
env PORT=8084
exec relay serve --port $PORT &server_process_test&

# Wait for the server to start
exec sleep 1

# Attempt to claim a license when none are available
exec curl -s -o response.txt -w "%{http_code}" -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint

# Expect a gone response with status code 410 and error message
stdout '410'
exec grep 'No licenses available' response.txt

kill server_process_test
19 changes: 19 additions & 0 deletions testdata/cmd_del.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Confirm the license is added
exec relay ls --plain
stdout 'dcea31a4-1664-4633-9f52-4a1b0b5ea2ef'

# Delete the license
exec relay del --id dcea31a4-1664-4633-9f52-4a1b0b5ea2ef

# Expect output indicating success
stdout 'License deleted successfully.'

# Verify the license is deleted
exec relay ls --plain
stdout 'No licenses found.'
32 changes: 32 additions & 0 deletions testdata/cmd_serve.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Set a port as environment variable
env PORT=8081

# Start the server in the background with the environment variable
exec relay serve --port $PORT &server_process&

# Wait for the server to start
exec sleep 1

# Claim a license using the port from the environment variable
exec curl -s -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint

# Expect license data in the response
stdout '"license_file":'

# Release the license
exec curl -s -X DELETE http://localhost:$PORT/v1/nodes/test_fingerprint

# Expect no content in the response
stdout ''

# Stop the server
kill server_process

# Wait for the server to terminate
# wait server_process
11 changes: 11 additions & 0 deletions testdata/cmd_stat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Remove existing database
rm -f relay.sqlite

# Add the license
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788

# Get statistics of the license
exec relay stat --id dcea31a4-1664-4633-9f52-4a1b0b5ea2ef --plain

# Expect output containing the license details
stdout 'dcea31a4-1664-4633-9f52-4a1b0b5ea2ef'
Loading

0 comments on commit be08c3a

Please sign in to comment.