-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests to improve coverage
- Loading branch information
Showing
7 changed files
with
251 additions
and
0 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,37 @@ | ||
package k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestUnidleReplicas(t *testing.T) { | ||
var testCases = map[string]struct { | ||
input string | ||
expect int | ||
}{ | ||
"simple": {input: "4", expect: 4}, | ||
"high edge": {input: "16", expect: 16}, | ||
"low edge": {input: "1", expect: 1}, | ||
"zero": {input: "0", expect: 1}, | ||
"too high": {input: "17", expect: 16}, | ||
"way too high": {input: "17000000", expect: 16}, | ||
"overflow too high": {input: "9223372036854775808", expect: 1}, | ||
"too low": {input: "-1", expect: 1}, | ||
"way too low": {input: "-17000000", expect: 1}, | ||
"overflow too low": {input: "-9223372036854775808", expect: 1}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
deploy := appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{idleAnnotation: tc.input}, | ||
}, | ||
} | ||
assert.Equal(tt, tc.expect, unidleReplicas(deploy), name) | ||
}) | ||
} | ||
} |
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,46 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestLinewiseCopy(t *testing.T) { | ||
var testCases = map[string]struct { | ||
input string | ||
expect []string | ||
prefix string | ||
}{ | ||
"logs": { | ||
input: "foo\nbar\nbaz\n", | ||
expect: []string{"test: foo", "test: bar", "test: baz"}, | ||
prefix: "test:", | ||
}, | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
out := make(chan string, 1) | ||
in := io.NopCloser(strings.NewReader(tc.input)) | ||
go linewiseCopy(ctx, tc.prefix, out, in) | ||
timer := time.NewTimer(500 * time.Millisecond) | ||
var lines []string | ||
loop: | ||
for { | ||
select { | ||
case <-timer.C: | ||
break loop | ||
case line := <-out: | ||
lines = append(lines, line) | ||
} | ||
} | ||
assert.Equal(tt, tc.expect, lines, name) | ||
}) | ||
} | ||
} |
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,41 @@ | ||
package k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestIntFromLabel(t *testing.T) { | ||
labels := map[string]string{ | ||
"foo": "1", | ||
"bar": "hello", | ||
"baz": "true", | ||
"negative": "-1", | ||
"max": "9223372036854775807", | ||
"overflow": "9223372036854775808", | ||
} | ||
var testCases = map[string]struct { | ||
target string | ||
expect int | ||
expectErr bool | ||
}{ | ||
"foo": {target: "foo", expect: 1}, | ||
"bar": {target: "bar", expectErr: true}, | ||
"baz": {target: "baz", expectErr: true}, | ||
"negative": {target: "negative", expect: -1}, | ||
"max": {target: "max", expect: 9223372036854775807}, | ||
"overflow": {target: "overflow", expectErr: true}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
result, err := intFromLabel(labels, tc.target) | ||
if tc.expectErr { | ||
assert.Error(tt, err, name) | ||
} else { | ||
assert.NoError(tt, err, name) | ||
assert.Equal(tt, tc.expect, result, name) | ||
} | ||
}) | ||
} | ||
} |
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,37 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestSpinAfter(t *testing.T) { | ||
wait := 500 * time.Millisecond | ||
var testCases = map[string]struct { | ||
connectTime time.Duration | ||
expectSpinner bool | ||
}{ | ||
"spinner": {connectTime: 600 * time.Millisecond, expectSpinner: true}, | ||
"no spinner": {connectTime: 400 * time.Millisecond, expectSpinner: false}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
var buf strings.Builder | ||
// start the spinner with a given connect time | ||
ctx, cancel := context.WithTimeout(context.Background(), tc.connectTime) | ||
wg := spinAfter(ctx, &buf, wait) | ||
wg.Wait() | ||
cancel() | ||
// check if the builder has spinner animations | ||
if tc.expectSpinner { | ||
assert.NotZero(tt, buf.Len(), name) | ||
} else { | ||
assert.Zero(tt, buf.Len(), name) | ||
} | ||
}) | ||
} | ||
} |
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,39 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
"github.com/gliderlabs/ssh" | ||
"k8s.io/client-go/tools/remotecommand" | ||
) | ||
|
||
func TestTermSizeQueue(t *testing.T) { | ||
var testCases = map[string]struct { | ||
input ssh.Window | ||
expect remotecommand.TerminalSize | ||
}{ | ||
"term size change": { | ||
input: ssh.Window{ | ||
Width: 100, | ||
Height: 200, | ||
}, | ||
expect: remotecommand.TerminalSize{ | ||
Width: 100, | ||
Height: 200, | ||
}, | ||
}, | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
in := make(chan ssh.Window, 1) | ||
tsq := newTermSizeQueue(ctx, in) | ||
in <- tc.input | ||
output := tsq.Next() | ||
assert.Equal(tt, tc.expect, *output, name) | ||
}) | ||
} | ||
} |
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,27 @@ | ||
package k8s_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
"github.com/uselagoon/ssh-portal/internal/k8s" | ||
) | ||
|
||
func TestValidateLabelValues(t *testing.T) { | ||
var testCases = map[string]struct { | ||
input string | ||
expectError bool | ||
}{ | ||
"valid": {input: "foo", expectError: false}, | ||
"invalid": {input: "naïve", expectError: true}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
if tc.expectError { | ||
assert.Error(tt, k8s.ValidateLabelValue(tc.input), name) | ||
} else { | ||
assert.NoError(tt, k8s.ValidateLabelValue(tc.input), name) | ||
} | ||
}) | ||
} | ||
} |
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 @@ | ||
package sshserver | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestDisableSHA1Kex(t *testing.T) { | ||
var testCases = map[string]struct { | ||
input string | ||
expect bool | ||
}{ | ||
"no sha1": {input: "diffie-hellman-group14-sha1", expect: false}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
conf := disableSHA1Kex(nil) | ||
assert.Equal(tt, tc.expect, | ||
slices.Contains(conf.Config.KeyExchanges, tc.input), name) | ||
}) | ||
} | ||
} |