Skip to content

Commit

Permalink
Make openqa-mon testable again
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Manzini <[email protected]>
  • Loading branch information
ilmanzo committed Apr 22, 2024
1 parent 973cc3c commit 13617fc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/openqa-mon/openqa-mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ func SetStatus() {
}
}

func parseProgramArguments() error {
args, err := expandArguments(os.Args[1:])
func parseProgramArguments(cliargs []string) error {
args, err := expandArguments(cliargs[1:])
if err != nil {
return err
}
Expand Down Expand Up @@ -690,7 +690,7 @@ func main() {
os.Exit(1)
}

if err := parseProgramArguments(); err != nil {
if err := parseProgramArguments(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
Expand Down
83 changes: 70 additions & 13 deletions cmd/openqa-mon/openqa-mon_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
package main

import "testing"
import (
"reflect"
"testing"
)

func TestAddJobs(t *testing.T) {
args_t := []string{"https://openqa.opensuse.org", "--jobs", "5701879,5701880"}
remotes_t := make([]Remote, 0)
j := []int{1, 2, 3}
r1 := Remote{"http://openqa.opensuse.org", j}
remotes_t = append(remotes_t, r1)
parseArgs(args_t, &remotes_t)
actual_len := len(remotes_t[0].Jobs)
expected_jobs := 3
if (actual_len != expected_jobs) {
t.Error("Expected", expected_jobs, "got ", actual_len)
}
func TestAddRemoteFromCLI(t *testing.T) {
remotes = make([]Remote, 0)
testargs := []string{"openqa-mon", "https://openqa.opensuse.org/5701879", "https://openqa.opensuse.org/5701880"}
err := parseProgramArguments(testargs)
if err != nil {
t.Error("Error parsing command line:", err)
}
if len(remotes) != 2 {
t.Error("Expected 2 remotes, got ", len(remotes))
}
}

func TestAdd2JobsToSameRemote(t *testing.T) {
remotes = make([]Remote, 0)
testargs := []string{"openqa-mon", "https://openqa.opensuse.org/t5701879+1"}
err := parseProgramArguments(testargs)
if err != nil {
t.Error("Error parsing command line:", err)
}
expected := []int64{5701879, 5701880}
if !reflect.DeepEqual(remotes[0].Jobs, expected) {
t.Error("Expected,", expected, "jobs, got", remotes[0].Jobs)
}
}

func TestRemotesMultipleJobs(t *testing.T) {
remotes = make([]Remote, 0)
testargs := []string{"openqa-mon", "https://openqa.suse.de/t100..150", "https://openqa.opensuse.org/t19999+1"}
err := parseProgramArguments(testargs)
if err != nil {
t.Error("Error parsing command line:", err)
}
if len(remotes) != 2 {
t.Error("Expected 2 remotes, got ", len(remotes))
}
totalJobs := len(remotes[0].Jobs) + len(remotes[1].Jobs)
if totalJobs != 53 {
t.Error("Expected 53 jobs, got", totalJobs)
}
}

func TestShouldGiveError(t *testing.T) {
tests := []struct {
input []string
expectedmsg string
}{
{
[]string{"openqa-mon", "--jobs", "foo,bar,baz"},
"jobs need to be defined after a remote instance",
},
{
[]string{"openqa-mon", "https://openqa.opensuse.org", "--jobs", "tfoobar"},
"illegal job identifier: tfoobar",
},
{
[]string{"openqa-mon", "https://openqa.opensuse.org", "--jobs", "-31415"},
"missing job IDs",
},
}
for _, tc := range tests {
remotes = make([]Remote, 0)
err := parseProgramArguments(tc.input)
if err.Error() != tc.expectedmsg {
t.Error("Expected", tc.expectedmsg, "got:", err)
}
}
}

0 comments on commit 13617fc

Please sign in to comment.