-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from grafana/28-add-support-for-running-archive
add support for running archive
- Loading branch information
Showing
11 changed files
with
259 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package cmd | ||
|
||
import ( | ||
"archive/tar" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/grafana/k6deps" | ||
"github.com/grafana/k6pack" | ||
) | ||
|
||
//nolint:forbidigo | ||
func analyzeArchive(filename string) (k6deps.Dependencies, error) { | ||
dir, err := os.MkdirTemp("", "k6-archive-*") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer os.RemoveAll(dir) //nolint:errcheck | ||
|
||
err = extractArchive(dir, filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts, err := loadMetadata(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return k6deps.Analyze(opts) | ||
} | ||
|
||
//nolint:forbidigo | ||
func loadMetadata(dir string) (*k6deps.Options, error) { | ||
var meta archiveMetadata | ||
|
||
data, err := os.ReadFile(filepath.Join(filepath.Clean(dir), "metadata.json")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = json.Unmarshal(data, &meta); err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := new(k6deps.Options) | ||
|
||
opts.Manifest.Ignore = true // no manifest (yet) in archive | ||
|
||
opts.Script.Name = filepath.Join( | ||
dir, | ||
"file", | ||
filepath.FromSlash(strings.TrimPrefix(meta.Filename, "file:///")), | ||
) | ||
|
||
if value, found := meta.Env[k6deps.EnvDependencies]; found { | ||
opts.Env.Name = k6deps.EnvDependencies | ||
opts.Env.Contents = []byte(value) | ||
} else { | ||
opts.Env.Ignore = true | ||
} | ||
|
||
contents, err := os.ReadFile(filepath.Join(filepath.Clean(dir), "data")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
script, _, err := k6pack.Pack(string(contents), &k6pack.Options{Filename: opts.Script.Name}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts.Script.Contents = script | ||
|
||
return opts, nil | ||
} | ||
|
||
type archiveMetadata struct { | ||
Filename string `json:"filename"` | ||
Env map[string]string `json:"env"` | ||
} | ||
|
||
//nolint:forbidigo | ||
func extractArchive(dir string, filename string) error { | ||
input, err := os.Open(filepath.Clean(filename)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer input.Close() //nolint:errcheck | ||
|
||
reader := tar.NewReader(input) | ||
|
||
const maxFileSize = 1024 * 1024 * 10 // 10M | ||
|
||
for { | ||
header, err := reader.Next() | ||
|
||
switch { | ||
case err == io.EOF: | ||
return nil | ||
case err != nil: | ||
return err | ||
case header == nil: | ||
continue | ||
} | ||
|
||
target := filepath.Join(dir, filepath.Clean(filepath.FromSlash(header.Name))) | ||
|
||
switch header.Typeflag { | ||
case tar.TypeDir: | ||
if err := os.MkdirAll(target, 0o750); err != nil { | ||
return err | ||
} | ||
|
||
case tar.TypeReg: | ||
if ext := filepath.Ext(target); ext == ".csv" || (ext == ".json" && filepath.Base(target) != "metadata.json") { | ||
continue | ||
} | ||
|
||
file, err := os.OpenFile(filepath.Clean(target), os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := io.CopyN(file, reader, maxFileSize); err != nil && !errors.Is(err, io.EOF) { | ||
return err | ||
} | ||
|
||
if err = file.Close(); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/grafana/k6deps" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_analyzeArchive(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual, err := analyzeArchive(filepath.Join("testdata", "archive.tar")) | ||
|
||
require.NoError(t, err) | ||
|
||
opts := &k6deps.Options{ | ||
Script: k6deps.Source{Name: filepath.Join("testdata", "combined.js")}, | ||
Manifest: k6deps.Source{Ignore: true}, | ||
Env: k6deps.Source{Ignore: true}, | ||
} | ||
|
||
expected, err := k6deps.Analyze(opts) | ||
|
||
require.NoError(t, err) | ||
|
||
require.Equal(t, expected, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use k6 = 0.52"; | ||
"use k6 with k6/x/faker >= 0.3.0"; | ||
"use k6 with k6/x/sql >= 0.4.0"; | ||
|
||
import faker from "./faker.js"; | ||
import sqlite from "./sqlite.js"; | ||
|
||
export { setup, teardown } from "./sqlite.js"; | ||
|
||
export default () => { | ||
faker(); | ||
sqlite(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// source: https://github.com/szkiba/xk6-faker/blob/v0.3.0/examples/custom-faker.js | ||
"use k6 = 0.52"; | ||
import { Faker } from "k6/x/faker"; | ||
|
||
const faker = new Faker(11); | ||
|
||
export default function () { | ||
console.log(faker.person.firstName()); | ||
} | ||
|
||
// output: Josiah |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// source: https://github.com/grafana/xk6-sql/blob/v0.4.0/examples/sqlite3_test.js | ||
import sql from "k6/x/sql"; | ||
|
||
const db = sql.open("sqlite3", "./test.db"); | ||
|
||
export function setup() { | ||
db.exec(`CREATE TABLE IF NOT EXISTS keyvalues ( | ||
id integer PRIMARY KEY AUTOINCREMENT, | ||
key varchar NOT NULL, | ||
value varchar);`); | ||
} | ||
|
||
export function teardown() { | ||
db.close(); | ||
} | ||
|
||
export default function () { | ||
db.exec("INSERT INTO keyvalues (key, value) VALUES('plugin-name', 'k6-plugin-sql');"); | ||
|
||
let results = sql.query(db, "SELECT * FROM keyvalues WHERE key = $1;", "plugin-name"); | ||
for (const row of results) { | ||
console.log(`key: ${row.key}, value: ${row.value}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
k6exec `v0.1.6` is here 🎉! | ||
|
||
This is an internal maintenance release. | ||
|
||
**New features**: | ||
|
||
- The archive file created with the `archive` command can now be run using the `run` command. | ||
```bash | ||
k6exec run archive.tar | ||
``` | ||
|
||
**Dependency upgrades**: | ||
|
||
- k6build v0.3.0 | ||
|