Skip to content

Commit

Permalink
Merge pull request #10 from grafana/paul/adding-ci
Browse files Browse the repository at this point in the history
Add CI workflows and update to correct linter issues
  • Loading branch information
javaducky authored Apr 5, 2023
2 parents 5e90a87 + b3abf0e commit 7b32941
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 14 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: CI
on:
# Enable manually triggering this workflow via the API or web UI
workflow_dispatch:
push:
branches:
- main
tags:
- v*
pull_request:

defaults:
run:
shell: bash

jobs:

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
- name: Retrieve golangci-lint version
run: |
echo "Version=$(head -n 1 "${GITHUB_WORKSPACE}/.golangci.yml" | tr -d '# ')" >> $GITHUB_OUTPUT
id: version
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: ${{ steps.version.outputs.Version }}
only-new-issues: true

test-go-versions:
strategy:
fail-fast: false
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x, tip]
platform: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go ${{ matrix.go-version }}
if: matrix.go-version != 'tip'
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Install Go stable
if: matrix.go-version == 'tip'
uses: actions/setup-go@v3
with:
go-version: 1.x
- name: Install Go tip
shell: bash
if: matrix.go-version == 'tip'
run: |
go install golang.org/dl/gotip@latest
gotip download
echo "GOROOT=$HOME/sdk/gotip" >> "$GITHUB_ENV"
echo "GOPATH=$HOME/go" >> "$GITHUB_ENV"
echo "$HOME/go/bin" >> "$GITHUB_PATH"
echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH"
- name: Run tests
run: |
which go
go version
go test -race -timeout 800s ./...
test-build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
- name: Check build
run: |
go version
pwd && ls -l
go install go.k6.io/xk6/cmd/xk6@master
MODULE_NAME=`go list -m`
GOPRIVATE="go.k6.io/k6" xk6 build \
--output ./k6ext \
--with $MODULE_NAME="."
./k6ext version
129 changes: 129 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# v1.52.2
# Please don't remove the first line. It uses in CI to determine the golangci version
run:
deadline: 5m

issues:
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

# We want to try and improve the comments in the k6 codebase, so individual
# non-golint items from the default exclusion list will gradually be addded
# to the exclude-rules below
exclude-use-default: false

exclude-rules:
# Exclude duplicate code and function length and complexity checking in test
# files (due to common repeats and long functions in test code)
- path: _(test|gen)\.go
linters:
- cyclop
- dupl
- gocognit
- funlen
- lll
- linters:
- paralleltest # false positive: https://github.com/kunwardeep/paralleltest/issues/8.
text: "does not use range value in test Run"
- linters:
- forbidigo
text: 'use of `os\.(SyscallError|Signal|Interrupt)` forbidden'

linters-settings:
nolintlint:
# Disable to ensure that nolint directives don't have a leading space. Default is true.
allow-leading-space: false
exhaustive:
default-signifies-exhaustive: true
govet:
check-shadowing: true
cyclop:
max-complexity: 25
maligned:
suggest-new: true
dupl:
threshold: 150
goconst:
min-len: 10
min-occurrences: 4
funlen:
lines: 80
statements: 60
forbidigo:
forbid:
- '^(fmt\\.Print(|f|ln)|print|println)$'
# Forbid everything in os, except os.Signal and os.SyscalError
- '^os\.(.*)$(# Using anything except Signal and SyscallError from the os package is forbidden )?'
# Forbid everything in syscall except the uppercase constants
- '^syscall\.[^A-Z_]+$(# Using anything except constants from the syscall package is forbidden )?'
- '^logrus\.Logger$'

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
- cyclop
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gocheckcompilerdirectives
- gochecknoglobals
- gocognit
- goconst
- gocritic
- gofmt
- gofumpt
- goimports
- gomoddirectives
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- revive
- reassign
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
fast: false
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Package xk6ssh registers the xk6-ssh javascript extension
package xk6ssh

import "go.k6.io/k6/js/modules"
import (
"github.com/spf13/afero"
"go.k6.io/k6/js/modules"
)

func init() {
modules.Register("k6/x/ssh", &K6SSH{})
modules.Register("k6/x/ssh", &K6SSH{fs: afero.NewOsFs()})
}
29 changes: 17 additions & 12 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ import (
"os"
"path"

"github.com/spf13/afero"

"golang.org/x/crypto/ssh"
)

type RSAKey struct {
file string
password string
}

// SSH is the main export of k6 docker extension
// K6SSH is the main export of the k6 extension
type K6SSH struct {
Session *ssh.Session
Client *ssh.Client
Config *ssh.ClientConfig
Out *bytes.Buffer
Stdin io.WriteCloser
fs afero.Fs
}

// ConnectionOptions provides configuration for the SSH session
type ConnectionOptions struct {
RsaKey string
Host string
Expand All @@ -40,7 +39,7 @@ func (k6ssh *K6SSH) rsaKeyAuthMethod(options ConnectionOptions) (ssh.AuthMethod,
pk = k6ssh.defaultKeyPath()
}

key, err := os.ReadFile(pk)
key, err := afero.ReadFile(k6ssh.fs, pk)
if err != nil {
return nil, err
}
Expand All @@ -52,6 +51,7 @@ func (k6ssh *K6SSH) rsaKeyAuthMethod(options ConnectionOptions) (ssh.AuthMethod,
return ssh.PublicKeys(signer), nil
}

// Connect starts and SSH session with the provided options
func (k6ssh *K6SSH) Connect(options ConnectionOptions) error {
var authMethod ssh.AuthMethod
var err error
Expand All @@ -65,9 +65,10 @@ func (k6ssh *K6SSH) Connect(options ConnectionOptions) error {
}

k6ssh.Config = &ssh.ClientConfig{
Config: ssh.Config{},
User: options.Username,
Auth: []ssh.AuthMethod{authMethod},
Config: ssh.Config{},
User: options.Username,
Auth: []ssh.AuthMethod{authMethod},
// #nosec G106
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
ClientVersion: "",
Timeout: 0,
Expand All @@ -82,20 +83,24 @@ func (k6ssh *K6SSH) Connect(options ConnectionOptions) error {
return nil
}

func (conn *K6SSH) defaultKeyPath() string {
func (k6ssh *K6SSH) defaultKeyPath() string {
//nolint: forbidigo
home := os.Getenv("HOME")
if len(home) > 0 {
return path.Join(home, ".ssh/id_rsa")
}
return ""
}

// Run executes a remote command over SSH
func (k6ssh *K6SSH) Run(command string) (string, error) {
session, err := k6ssh.Client.NewSession()
if err != nil {
return "", err
}
defer session.Close()
defer func() {
_ = session.Close()
}()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
err = session.Run(command)
Expand Down

0 comments on commit 7b32941

Please sign in to comment.